Intro: Demystifying 4 Pin RGB LEDS (Radio Shack 276-0028)
RGB LEDs are fairly useful in projects allowing for a wide range of color from a single unit. However they can be a pain in the neck to work with and they too often come with little or no documentation. I searched for some time to find a common resource on them and this Instructable is an attempt to pool the fruits of that search into one place.
But first, a word about diodes. What is a diode? It is the electronic equivalent of a one-way valve. They have a flow direction, and if inserted backwards do not work. They prevent the voltage from flowing through, except in the direction of their flow. The first diodes were used as rectifiers in decoding radio signal modulation. For more on the history of the Diode, your welcome to check them out on Wikipedia http://en.wikipedia.org/wiki/Diode
Whats that have to do with LEDs you ask? Well LEDs are by nature diodes. It is in their name -- Light Emitting Diode. This means that if you cross connect the Anode (+) and the Cathode (-), nothing happens and your circuit fails. Usually resulting in nothing more than an open circuit as voltage cannot flow. For more on the history of the Light Emitting Diode, or LED, you can go here on Wikipedia http://en.wikipedia.org/wiki/Light_emitting_diode
Four pin RGD LEDs are a little different. They share a common Anode, but three separate Cathodes, one for Red, one for Green, and one for Blue. If your not careful you can pass current through conflicting parts and cause a spectacular failure complete with a snap, a pop, a whiff of ozone, and plastic shrapnel flying all over your workspace.
This is usually caused by the omission of a current limiting resistor to the circuit (yes, I'm guilt of this). Incidentally, this sort of failure is not covered by Radio Shack's warranty.
The LED in this project was purchased at a local Radio Shack and has the following part number, 276-0028
Step 1: The Pinout
One of the most annoying part of these little gems is the lack of a pinout diagram that was useful.
While looking at the photo, you will notice that the four pins are three different lengths. Also there is a flat side of the housing on the left side. With the flat side to the left, as in the photo, the pins are: RED Cathode (medium length pin), Common Anode (longest pin), , BLUE Cathode (shortest length pin on the inside), and GREEN Cathode (shortest length pin on the outside). This is not how I would have designed the unit as I think it would be easier to keep up with it if were Red-Green-Blue, but that's logic for you.
A quick look at the datasheet screen-printed on the package indicates that the different Cathodes use different current levels. 2.0V to 2.6V max at 50ma for the Red, and 3.5V to 4.0V at 30ma for the Green and Blue. Its important to note this when calculating what resistor(s) are necessary for your project.
Step 2: Placement on a Breadboard
With four pins close together you need to make sure to place each pin in a different column on your breadboard. This will require slightly bending the legs to accomplish. You might want to use a precision screwdriver or toothpick or similar object to assist you in placeing the pins in the desired socket.
If your hooves are as large as mine, you may find it useful to use lateral jumpers to open up the area your working with.
Step 3: Resistence Is Not Futile, It Is Required!
One of the most common newbie mistakes with LEDs is simply hooking them up like a common lightbulb. I've seen people using button-cell batteries for this purpose to test LEDs and while it might work for a little while for that purpose its not a good thing, really.
LEDs do not tolerate variable voltage well. That means that if you apply too much voltage, they pop. That means if you apply too little voltage they don't work at all. The 'Goldilocks' zone is far more narrow than with a incandescent light bulb, and unlike the old trusty filament bulbs, you cannot control the intensity of the output with voltage, they either light up, or not. Let me say that again, YOU CANNOT CONTROL THE INTENSITY OF THE OUTPUT WITH VOLTAGE. In order to control intensity you must use a micro-controller capable of Pulse-Wave Modulation, or PWM. For this I am using an Arduino UNO, more on that in the next step.
You must have a resistor in the circuit to make sure everything works right. Now from the way I learned about these things when dinosaurs walked the earth and Microsoft was still on version 3.0 of Windows (running under 4.01 of MS-DOS no less), you would place the resistor between the voltage source and the LED. The LED Calculator for single LEDs shows this method, http://led.linear1.org/1led.wiz Its exceptionally useful, bookmark it... No really.
Wisdom being what it is you will occasionally find references to placing the resistor AFTER the LED. I have done both under test conditions and can attest that it too works. In fact the LED series parallel array wizard http://led.linear1.org/led.wiz shows this alternative method. If someone cares to enlighten me on why this works when it seems to violate the need for a resistor in the first place, I'll be happy to add their knowledge here.
Step 4: Must!... Retain!... Control!
Ok, don't go, I'll nix the Shatner voice...
After you have figured out what resistor(s) you need, you need a way to control how much light each of the three elements produce. I'm using an Arduino Uno for this, and a fairly simple wiring setup. I route Pin3 to the Green Cathode, pin6 to the Blue Cathode, pin9 to the Red Cathode, and pin11 to the Common Anode. There is a single 220 ohm resistor (which is overkill, but it was what I had at hand). Even though you can see two LEDs, they are wired in parallel so it would work just as well as with one.
I can pass a value between 0 and 255 to the analogWrite() function and it will fade the LED element to the desired level. If that scheme seems familiar, it should be as web-colors are expressed in decimal notation that way. The only major difference is that the scales are reversed. Passing a value of '0' to the LED turns it on at full intensity, while a value of '255' effectively turns it off completely. Due to the limits of PWM some combinations appear to flash or fluctuate, but plenty of combinations do work. Also since the elements are not matched for current drawn if the RED element will overpower the other two at any even setting. These limitations of my simple setup can be fixed by the use of a more complex design, such as the one by mpilchfamily in their instructable 6-button-RGB-controller
I found a schematic and sample code from fritzing.org. I hesitate to include this as I had difficulty getting it to work as desired. It does work however. The writer of that article also seems to mix up his terms when labeling the Anode and Cathodes, but shows the polarity correctly.
As you can see above, I don't pull the power from the far side of the Arduino, but from another digital pin (pin 11), so a single line of code needed to be added to the sample code to get it to work. Just add. "digitalWrite(11,HIGH);" to the top of the void loop() section and your off to the races. (I've included my sourcecode in the final step)
I should take a moment to point out a few things that had me going in circles. First and most off, there doesn't appear to be circuit leading to ground, this is in fact not true, it only appears that way. The 4 pin LED is controlled by stepping the PWM voltage to ground. The Arduino board itself becomes the path to ground. So you apply power to the Cathode and ground out (or not) the Anodes in order to get a certain amount of colored light, that's why the higher the number the dimmer the LED element.
Personally I wasn't happy with just random colors, and found the blending effect not at all what I was looking for so I started tweaking by reducing the range of the randomized number and adding base value modifiers so that I always had a certain level of one of the three elements going. By watching the serial monitor you can get an idea of the values needed for each color combination. This gives you a place to start looking for what you need for your project.
Step 5: Final Thoughts
I've included my source code and you are certainly welcome to use it in your own experiments.
mhermark made it!