Introduction: RGB LED Strip Circuit With Arduino
This Instructable covers the assembly of a circuit capable of PWM-ing (pulse width modulating) a 9-12v RGB LED strip and programming an Arduino to cycle through a range of colors. I will discuss how to mount a transistor to a heatsink & assemble the circuit but won't get into soldering as some RGB LED strips come with leads (and there is no shortage of great tutorials out there).
You will need:
- A Microcontroller
- Breadboard or PCB
- RGB LED strip
- Battery (9-12v)
- 3 NPN transistors (I'm using TIP 120s)
- LM7805 Voltage regulator (optional, but recommended)
- (4) 4-40 screws (length, up to you)
- (4) 4-40 nuts
- (4) nylon washers
- 22g hookup wire
Update 2015: A lot of people seem to be interested in digitally-addressable LEDs (opposed to LEDs in "series", in other words the ones that blink together vs. individually controllable LEDs), so I added an additional section for these as well (circuit and code). Adafruit produces one variety called NeoPixels and they have an excellent guide.
Step 1: Brief Explanation of the Electronics
If you are new to Arduino and are wondering why more than a few LEDs or other components like motors won't activate when functions are called in the code, its because each output has a current limit of 40mA. In other words, a component cannot draw more than 40mA of current from each channel. To refer to the water analogy of electronics, the "pump pressure" is 5V, and the amount of water is the number of electrons (measured in Amps, or in our case, a much smaller amount - milliamps, mA). To accommodate a load that requires more current than 40mA at 5V, we will use our microcontroller to control a transistor, which will provide a component with power from an external source (the battery).
Without getting too technical, its worth knowing that individual strips are made up of 3 LEDs in series which can be cut with clippers at any junction. If you want to cut the strip at any point, just be sure to leave connection points on each halve. To understand how the RGB LED strip can be powered with 9-12V, you need to know the difference between circuits in series vs. parallel (this page has a simple explanation with great illustrations, and there is a popular Instructable that covers wiring LEDs in series & parallel). Basically, when active components are connected in series, their supply voltages are added together. For example, since an average RGB LED requires 3.3 V and 60mA (at full brightness; each color channel draws 20mA, so R-G-B all on at same time is 20 x 3 = 60mA), each strip of 3 RGB LEDs will require approximately 9.9V (the strip I'm using from Jameco can be powered between 9-12V. Be sure to look at your product's datasheet to prevent frying your components. Not all RGB LED strip is powered in the 9-12V range, such as Adafruit's digitally addressable RGB LED strip). One more thing, these strips are "common anode," meaning the LEDs share a positive terminal (read about anode vs. cathode).
Perhaps the greatest take-away is the power limitation of the Arduino. The next section which shows how to use a transistor can be applied to all sorts of other components (ex. motors, solenoids, servos) that require more than 40mA at 5V.
Note: The same concept applies to NeoPixels, but they require a lower voltage (5V). If the power source is lets say a 9 or 12V battery or charger, the power must be regulated.
Note2: The water analogy turns out to be a pretty poor way to visualize what actually occurs at the electron level. William Beaty explains how transistors actually work.
Step 2: Mounting Components to a Heat Sink
A heat sink's function is to dissipate heat so components don't fry, melt connections/become un-soldered, or become too hot to touch. This is really important if you're using a 7805 for voltage regulation - the 7805 will output a steady 5V as a power source to the Arduino* and dissipate a bit of heat. This is good practice even though the Arduino has a built-in voltage regulator. I've also found the SMD voltage regulator on the ProMini has a slightly lower range than stated on the datasheet (Max Input 12V, I've fried a few at that voltage).
This fabrication is pretty straight-forward. Apply some heat sink compound to the transistor (first image in this series) and then put the nylon washer on screw on then through the transistor (second). The compound increases thermal conductivity, which aids in the dissipation process. Lastly, fasten the nut to the back of the heat sink.
If you're using the classic 12V LED strip, add a heat sink to each transistor and the 7805.
If you're using digitally-addressable LEDs like the Neo Pixel, you do not need transistors, only a heat sink for the 7805 (which will power the strip and the Arduino).
Step 3: Assemble the Circuit
Now that the transistors and 7805 are mounted to heat sinks, let's assemble the circuit. I think its worth noting that you'll save yourself time and frustration if you use good breadboarding techniques such as color-coding wire connections, and using heat shrink tubing to prevent shorts (see image of battery). Its considerably easier to find errors too when your wire connections are shorter and color-coated. I think the only way to learn this is the hard way, but I figured its worth mentioning.
The first schematic is for an Arduino full-development board (Uno, Pro Mini, etc), but is essentially the same if you want to use an ATTiny (for this component, you just need to hook it up to the right pins). If you are breadboarding the circuit, be sure to look up the datasheets and pinouts for your exact components. I have provided a simple illustration depicting the pinouts for the components I'm using (Tip 120, 7805). If you are unfamiliar with reading schematics, here is a pretty good tutorial and a symbol guide.
Important Note: You can fry your Arduino if it is powered by the battery via V-in and the USB cable at the same time. Unplug the battery before you upload programs. Also, if you want to power your Arduino via USB during development and still leave battery plugged in (to power the strip), simply remove the connection between the output of 7805 to V-in, but be sure that you still have ground connected, otherwise it won't work.
The deeper I get into Arduino, the more I find myself programming chips directly using an ISP (In-System Programmer, I use a shield with HexTool for convenience but there are cheaper ways of doing this such as using the Arduino directly), and learning AVR programming. I bring this up because there seems to be a bit of confusion out there regarding the ATTiny85, as to whether it has two or more PWM outputs. The answer seems to be yes if you know what you're doing, but I have not seen working code that enables more than two PWM channels for this chip.
Step 4: Write (or Hack) a Program and Upload to Your Arduino
So now that you have a functional circuit, its time to upload some code. If this is your first Arduino program, it may be helpful to example a digitalWrite blinking program before moving on to fading in order to understand the structure of the language.
Its taken me quite a while to realize that there's no reason to reinvent the wheel, and with the current obsession with all things blinky, I've also realized that there's no reason for me to write a program to demonstrate the range of colors for an RGB LED. Clay Shirky has published a pretty convenient program, which is a good starting point for controlling your RBG LED strip before making it interactive with some kind of input.
Ps. If its not working, be sure you have common ground between transistors, battery, and Arduino!