Hein (fub) wrote,
Hein
fub

  • Mood:

Pulse Width Modulation

A microcontroller is a digital component: there's a voltage on a pin, or not -- 1 or 0, nothing in between. On the PIC, this voltage is the same as the power-supply voltage, in this case, 5V. By connecting a pin to a resistor, you can make a current flow (I = V/R), which you can use to drive a LED. So, if you have an output of 5 volts and a resistor of 270 Ohm, you get roughly 18.5 mili-Ampere of current. Most LEDs can take about 20mA before they break and turn into DEDs (Darkness Emitting Diodes). The more current you feed a LED, the brighter it burns.

I bought some RGB-LEDs. These LEDs are actually three LEDs in one casing: a red one, a green one and a blue one. They all have their own input, but share a common output. By putting current on any of the three LEDs, you can make colors and combinations of colors.
Now, if you use digital logic, you can make 7 different colors (8, if you count 'off' as a color). Because you have 3 inputs that can have 2 values (0 or 1), you have 2^3 = 8 different colors. One of those is 0-0-0, which is 'black' or 'off'.

Now, 7 colors is kind of dull. I'm sure you know the feeling you had when you were a kid and looked in the box with crayons: it didn't contain the exact right color blue, or yellow -- so you had to make do with inferior coloring for your masterpiece of that day.
The graphics card in your computer uses 32-bits of color: each of the RGB-colors has 2^32 different values, so it can produce (2^32)^3 = 79228162514264337593543950336 different colors. That's probably more than is discernable by the human eye (though bakenius probably knows for sure). This means that pretty much any shade of color can be made with a graphics card -- if you know the magic number, you can always get the right crayon from the box!

That's a bit too ambitious for my colored lighting panels, though. But I do want more than 7 colors! So what's an electronics hobbyist to do?
The answer lies in Pulse Width Modulation. It's a fancy name for a relatively simple technique. Instead of 'always on' or 'always off', you switch the line from 'on' to 'off' and back again, so that it is only 1/16th of the time on, or 6/16th, or... you get the idea. If you do this fast enough, there is no discernable flicker, but the amount of current that flows through that line diminishes linearly. A period in which you can switch on or off is a Pulse. The Width of the Pulse varies, and you switch the lines from 0 to 1 (Modulation), hence the name. It's mostly used to slow down motors.

So an 8/16th PWM cycle gives half the current a full cycle gives. In our case, some 9.25 mA instead of the usual 18.5 mA. That's good, because the LED will burn less bright -- more values to make colors with! If we take a pulse width of 16, then each color will have 16 possible values, yielding 16^3 = 4096 possible colors. Certainly good enough for something that just has to cycle through a pre-determined set of colors, right?

But here comes the catch: LEDs don't work linear. A LED that is fed only half the current doesn't burn half as bright. Which means that you will have to experiment which values give off how much light -- which is kind of tedious. But it's even worse: every LED has it's own characteristic, so you will have to change your PWM-numbers for every new LED-type. And now comes the killer: the three LEDs in an RGB-LED all differ too.

I have the PWM-software written, but I will need to build a testing circuit for these particular types of RGB-LEDs. I'm not up to that today, it'll have to wait for some other time...

I hope you enjoyed our little lesson of today...
Subscribe

  • A token of my appreciation

    Postal workers from Baskingridge, NJ all the way to Nijmegen will curse the day the US Postal Service got the unholy idea of the flat rate envelope.…

  • MAME Schmups

    I've torrented the whole MAME ROM pack. Apparently that contains all the ROMs, but the EasyMame frontend seems to think that some ROMs are not…

  • Et in arcadia ego

    I've downloaded MAME plus a few ROMs. Like anyone my age, I used to hang out in arcades regularly -- when arcades still had games instead of endless…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 4 comments