Gadgetboard application: Rainbow Colors

With the advent of blue LEDs, we can generate almost any visible color. It also gives us a good excuse to play with the gadgetboard's PWM channels. Eventually, I'd like to build these into a computer case mod; then I'll be able to illuminate the case with any color.

PWM is a simple, power-efficient way of applying partial power to a device. The Wikipedia article gives a good overview. In the gadgetboard, setting PWM values between 0 and 255 tells an output channel to be "on" for that many 255ths of its cycle.

For these pictures, I connected my oscilloscope to output 1 and the ground hook to the left of it. (Measuring outputs 5-8 is trickier since the N-channel MOSFETs only switch things to ground. I would have had to hook up a resistor from the output to an adjacent power terminal so that a positive voltage would show up when the MOSFET was off.) The middle picture shows the result of giving the command "p 1 128"; the output is on 128/255=50% of the time. Incidentally, since each on-off cycle takes 2 horizontal divisions, and the bottom of the display tells us that each division is 2ms, we can see that the period of the signal is 4ms, and thus that the frequency of the PWM signal is 1/0.004 = 250Hz. This frequency is set by a divider from the CPU clock, and can be changed in software.

The left hand image shows the result of "p 1 32"; here the output is only on 32/255=12% of the time. Note that the on-off cycle still takes 4ms; the Atmel uses the same PWM frequency regardless of duty cycle. The right hand image shows the result of "p 1 234", or 88% duty cycle.

The LEDs shown are high brightness devices packaged in sockets, designed to run on 12Vdc. I soldered leads to them and tinned the other ends so they don't fray when I put them in the screw terminals. Here's the digikey product page. Since the units have built-in limiting resistors, I don't have to worry about adding them to keep the LEDs from drinking too much current and burning out. I just connect each one to its output and the adjacent power or ground pin, making sure to observe the polarity of the LED.

Digikey doesn't have a good selection of devices with red, green and blue LEDs in a single, 4-wire package, but sites like superbrightleds.com seem to have a much better selection.

This description only shows the project's progress through the second prototype stage. I just connected red, green and blue LEDs to ports 1, 2 and 7 (three of the four PWM channels). Here's the light controlling script I've been using to try out different configurations. It's not very polished; I just modify the script, commenting out various lines of code when I want to switch between cycling through colors and displaying random sequences. When I'm ready to finish the project, I'll convert the script to C and load it into the microcontroller for its normal operation disconnected from a PC.

There are two bits of fancy math that I hacked together for this project, neither of which are totally necessary. The first is that I wanted to deal in terms of hue, saturation and value when thinking about color. I used the formulas from the linked article to convert HSV values to RGB.

The second is an attempt to compensate for the fact that humans have nonlinear response to color intensity. So, in other words, the difference between 95% and 96% PWM duty cycle is much less noticeable than the difference betweeen 1% and 2%. That stands to reason, since 2% means the light is on for twice as long as 1%, whereas 96% is less than 1% more than 95%. If we gradually ramp up from 1% to 100%, then, you'll notice the light quickly get bright, then appear to stay essentially the same brightness for a long time. So I experimented with a few exponential curves to give the appearance of uniform increase in brightness, since I couldn't find any standard way of doing so; please email if you find such a formula.

Here are some videos of different ways of cycling through the color space. I wrapped aluminum foil around the LEDs to combine the light into a single color.
Random colors:

Cycling through hue, sturation and value at different rates:

Cycling through hue and saturation with value of 1 (never gets dim):