Thursday, September 13, 2012

Arduino & Tri-Colored LEDs

I'm currently working on an unnamed project which requires the integration of tri-color LEDs (RGB). These are merely four-pin LEDs with a common anode or cathode and an input pin for red, green, and blue. With varying currents, a multitude of colors can be produced. So I created a C++ (arduino) library for handling such LEDs. Since most of my knowledge of programming is in C, learning about classes in C++ is a significant accomplishment for me.

LED Choice & Wiring

The RGB LED I had was a common cathode, so I tied the cathode to ground and the red, green, and blue wires of the LED to three of the PWM outputs. I imagine if you were to use a common anode RGB LED, you would still tie the three color-wires to PWMs, but the anode would be tied to +5V and a low PWM value would be brighter than a high PWM value on the common anode LED.

I had to take care to choose resistors that respected the LEDs' current limits. For the red wire, it was 50mA, and for the green and blue, 30mA. The circuit below did well for me, though I may have used resistors slightly higher than those depicted. I certainly wouldn't go with lower resistance.
Figure 1: Wiring up a common cathode RGB LED
Be sure to check your LED's spec-sheet for the proper current limits.

Example Code & triColorLEDs Library

If you want to see a the library in use, check out this short video below:


Its a little hard to see from the video, but it is cycling through 7 colors: Red, Green, Blue, YellowTurquoise, Purple, and White. This was tested on the Arduino Duemilanove with 328P, as well as the Arduino Pro Mini (16MHz, 3.3v). I can't guarantee it works with every Arduino out there.

The best way to find out, though, is to download the triColorLEDs Library for yourself. The example that comes with it is actually different than the video above. Once you download the zip file, extract it to C:\Program Files\Arduino\arduino-1.0.1\libraries and use away.

Next

Well, several weeks later after I started this post (and saved it as a draft), I started reading up on different types (byte vs. int vs. long), and I finally cleared up a little of the fuzziness my Mechanical Engineer brain encountered with these different types. In the future, I'll be condensing the library a little bit so it takes up less space. When that happens, I'll let you know.

No comments:

Post a Comment

Please keep your comments respectful and in the spirit of constructive criticism.