Dimming an LED

Would like some help in dimming an LED.

I need an understanding on how to get the PWM (pulse width modulation) concept to work on the April board. In doing some research I found the article below about Arduino boards. How can I accomplish the same on an April board?

In the electric imp API Reference there is a PWM reference: hardware.pin9.configure(PWM_OUT_STEPS, 0.020, 0.0, 37); // PWM period ~= 0.02s, duty cycle 0.0, 37 steps from 0.0 to 1.0 but again how do I go about implementing this?

Notice on the Arduino board there are three digital pins (9-11) which are labeled PWM. Devices (like LEDs) connected to these pins can employ continuous pulse width modulation using only the analogWrite() command like so:

/*

  • A simple PWM example
    */

int pin = 11; // LED connected to PWM pin 11
int pulsewidth = 127; // Any value between 0 and 255

void setup() {
// None required for analogWrite!
}

void loop() {
analogWrite(pin, pulsewidth);
}
The example above should cause the connected LED to glow at about 50% intensity (255/2 = 127ish). Play around with the value of the pulsewidth variable and note the changes in LED brightness.

Link to full article:

http://principialabs.com/arduino-pulse-width-modulation/

On an imp any pin can do PWM. You should generally be PWMing an LED at 200Hz minimum, so configure it like this:

// Set up 200Hz PWM, duty cycle zero (ie will stay low)
hardware.pin1.configure(PWM_OUT, 1.0/200, 0);

// Set a duty cycle of 73%
hardware.pin1.write(0.73);

…and that’s it. If your LED is connected between 3v3 and pin1 (with a current limiting resistor) then LOW is on and HIGH is off, so 73% there would be 27% of max brightness (1-0.73=0.27).

@Hugo:
From what I can tell, hardware.pin1.write(j); will set the duty cycle of the PWM signal even if j is greater than 1; that is, the following are all equivalent, and will all set the PWM signal to a 73% duty cycle:
hardware.pin1.write(0.73);
hardware.pin1.write(1.73);
hardware.pin1.write(89.73);

Can you confirm that this is true?

On a related note, I have noticed that if the value of j is a negative number, the duty cycle appears to be set to 100%. Any comments on this?

That sounds like a bug. Anything above 1 should be treated as 1 - and anything below 0 should be treated as 0.

I’ve just fixed that, it’ll be out in a future release :slight_smile:

git push? :wink:

Peter

$ git push
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 497 bytes, done.
Total 5 (delta 4), reused 0 (delta 0)
To ei.git
1c66885…17a414e master -> master

:slight_smile:

How would you go about dimming an LED light bulb?

If it’s a low voltage one, and you’re using a FET or transistor to drive it, then you’d do it just as above.

If it’s high voltage, you need a triac dimmer circuit. The silly thing is that though that’s great for incandescent, CCFLs and LEDs have to go through a lot of hoops to translate that into a suitable signal for dimming the LED!