RGB LED Strip Circuit With Arduino

301K22445

Intro: 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.

Have fun!

Ps. If its not working, be sure you have common ground between transistors, battery, and Arduino!

33 Comments

will it work, if I use 2n2222a transistors?

depends how long your strip is. 2n222a is NPN like the Tip120 but only is rated for max current of 500mA (Tip120 is > 1A).

dear author: where are the transistors? just cannot see them. waiting for your answer/

In the first picture, the transistors are visible on the left side of the breadboard and they are screwed into the heat syncs. In the second photo, each component is clearly labeled, hover over each item for a description.

thank you. to be checked.
(looking around in this page for the "send" button. hope to guess right)

hi there. had to change email for technical reasons. can i ask basic questions? millions of them but i will restrain myself.

first, i got this strip of leds, simple, non-addressable. at home i found out it has only two wires, while in your instructable the strip has 4. what is that? could i still atach it to arduino and program it, like all the strip on, off, blink, at my pace? the arduino says 5v, and the strip says 12v. could it work together? ok, enough for now. (i am an old programmer, but know nothing in eletronics).

Your LEDs are probably one color (one channel). The LEDs shown here are RGB (one wire per color and one for ground).

Regarding power, the Arduino requires 5V and the LED string needs 12v. If you want to use one power source, use a 12V power supply to power LEDs directly and use a voltage regulator (7805) before providing power to the Arduino.

Step 3 of this instructable shows a diagram and explains this in greater detail. Hope this helps.

I'm trying to build this circuit, but I am not good with schematics. Can some one tell me which holes to stick the wires in the breadboard. I tried using the pictures but I couldn't see some of them.

Thanks for the tutorial. I was thinking of using some digipots to do this. I thought transistors pretty much acted like relays. Does this mean they can step up voltage through c-e proportionally to b?

Never mind. I read on PWM. Awesome stuff! and awesome tutorial too. Thanks.

You have 12v going into the collector of your transistor and 5v from the arduino going into the base. On common transistors, this won't open the transistor to saturation and will impede full current flow. Do the Tip120's not care about this?

FYI, if you're going to use a BJT, you need a current limiting resistor between the microcontroller and the base of the transistor. Without it you risk damaging the microcontroller.

Can this circuit in combination with the correct program be used to activate the leds in sequence ? or would there need to be a time element along with this circuit ? If so any suggestions ?

No, you would need shift registers in between each LED. Alternatively, you can check out Adafruit's NeoPixel strips (awesome).

haha thank you. I ended up stumbling upon those after researching for a bit. Definitely the way I'm going to go

Could you upload your ATTiny85 code somewhere, currently I'm working on this but all I need right now is the tripple pwm :/

I have read that it is possible, though I have not been able to get it working. Would love to see some working code that allows for 3 PWM channels with the 85.

More Comments