The Studio of Eric Valosin

Tuesday, April 22, 2014

Arduino and LED's for Complete and Total Morons like Me

I've been working on a project using an Arduino to control RGB LED's with PWM and discovered there's a WHOLE TON of stuff people just assume you know. It took many hours to get the basic understanding to even know what to google. So here you go.


First some clarification -

PWM, or Pulse Width Modulation means controlling the R, G, and B components of the LED values from 0-255 through digital output pins that tell it how frequently to turn on, giving the effect of dimming, fading, or being at partial brightness.)

Choosing your LED. I went with common Anode as opposed to common Cathode, so that's what this is written for. With common Anode, the long pin ("common") needs to be connected to the +Voltage, and the R,G,and B pins need to be connected to a resistor (to protect them) and then to the digital output pins of the Arduino. Then the LED lights up when pulled HIGH, which essentially means the PWM color scale goes from 0(fully on) to 255(fully off). This is counterintuitive, and you can easily fix your program by writing your color value "r" as "255-r"

Common Cathode are just the opposite - Common goes to Ground, not +5V, and it turns on when pulled LOW; PWM = 0(fully off) to 255(fully on).


First look at the LED's data sheet for two things - the Forward Voltage (Vf) and the Forward Current (If).

Forward Voltage is the amount the applied voltage drops when it passes through the LED. You'll need this to calculate how much resistance you need. For the sake of argument we'll say it's 3, but check your data sheet.

Forward Current is the amount of current (in mA) that each diode draws (remember RGB LEDs are actually 3 diodes in one, and each needs to be accounted for separately)


If you don't want to blow out the LED, you need resistors connected to the R,G,and B pins. To figure this out, you first need to know how much voltage you plan on supplying. The typical Arduino supplies 5V, so we'll go with that, but this can be adjusted accordingly if, say, you're hooking it up to a 9V battery, etc.

1) Finding your Resistor with Ohm's Law - when solved for Resistance, Ohm's Law says the Resistance (R) you need in order to get the proper current at a given voltage is equal to the Voltage (V) divided by the Current (I).
R = V/I

Remember that you're supplying 5V, but there is a drop due to the LED's Forward Voltage. So, you must subtract the forward voltage from the total voltage
V = VDD - Vf
in this case V = 5 - 3
V = 2

So, R = 2/I
I is in this case 20mA (or .02A)
so R = 2/.02
R = 100 ohms (shown as Ω)

So you need to connect your LED's terminal to a Resistor of at least 100 Ω so that when it draws the 20mA of current it doesn't blow itself up. Always round up if you land on a value that's not a common resistor value.

2) JUST in case, you should also make sure the resistor is the right Wattage (usually they come in 1/4W, 1/6W, 1/8W, etc), using the following formula:

Power (P) = Current(I) Squared times Resistance (R) [pardon my lack of superscript and subscript... blogger and I are fighting right now]
we now know the resistance is 100, so in this case
P = .02squared x 100
P = .04

Since 1/8W (.125) is well above .04, you should be fine with pretty much whatever you choose, but just pick one that's higher than the value of P so that you don't overheat the resistor.

Series or Parallel?
Single color LEDs can be wired in series, meaning the anode of one is connected directly to the cathode of the next and so on, and they can all share one resistor. However this is not possible with RGB LEDs since they share a common pin between the three colors. They must be wired in parallel, meaning each LED is connected directly to the power source and ground, rather than to each other. Every pin therefore needs its own resistor or else you'll end up with inconsistent color and insufficient protection.

- LED's wired in Series share the same amount of current but multiply the amount of voltage per LED.
- LEDs in Parallel are the opposite, they all share the same Voltage but the current is multiplied per LED.

SO, if you have 5 LEDs in series, each wanting 3V and 20mA, you'll need to provide 15V (3V x 5 LEDs) and 20mA.
If you have 5 LEDs in parallel, each wanting 3V and 20mA, you'll need to provide 3V and 100mA (20mA x 5 LEDs)


We're not commonly told this, but the Arduino pins can only take so much power. I'm using the Arduino Uno, so if your model differs, this is stuff you'll want to google. In general, try googling things like "Arduino max voltage" or "arduino [insert name of pin here] max current"

Check here for starters - You'll definitely want to google these limitations for yourself. EVERY PIN ON THE ARDUINO has its limitations. Don't just take my word for it. My bad memory or misread data sheet, or incomplete information here is not worth your fried arduino pin.

It naturally supplies 5V, but can handle up to about 12V using the power barrel plug thingy (a technical term). Don't exceed that unless you like the smell of burning electronics. That power can be converted to 5V with the +5V pin, or sent straight through the Vin pin. Each of these pins can handle only so much current running through them

+5V pin = max 450mA - 600mA current depending on power source
Vin pin = max 1A current

Then you need to know how much the output pins you're using can take. These are the ones you really want to pay attention to:

Digital I/O pin = max 40mA current
total max current from all I/O pins combined = 200mA

This means that any given output pin can actually only support 2 LEDs in parallel at full brightness without risking damage! ...whoops.

MOSFETs for More Power...
Clearly there is an way you can still hook up more LEDs, but you'll want to look into an external power supply if the voltage or current demands get too big for the V pins, as well as something to isolate the output pins from the massive current being drawn by the LEDs

I recommend n-channel MOSFET transistors (see here for a great reference article by Adafruit), which can handle up to 16A and essentially acts like a switch that passes the higher current straight from a power supply to the LED to ground without ever touching the Arduino, whenever it's "gate" is triggered by a smaller current sent from the Arduino I/O pin. Here's a great MOSFET tutorial

The MOSFET has a "gate" "source" and "drain." I won't go into why here, but suffice it to say that to drive a common anode LED with a MOSFET you should connect the following:

Gate --  to the digital I/O pin that sends color/PWM signal
Source -- to the LED leg that corresponds with the arduino output pin
Drain -- to ground 

Then you can connect each leg of the LED to it's own MOSFET (using 3 MOSFETS, one for each color - you don't need one for each LED, all of the red LED terminals can be connected to the red MOSFET and so on) and the common Anode to the high-current power source!

For added protection, you should have a low value resistor between the gate and the pin (lots of argument on exactly how low since the current is not consistent, but I've seen between 100 and 330Ω recommended), as well as high value (pull-down) resistor connecting Gate to Drain, essentially to prevent things from going the wrong way inside (I've seen 1k, 10k, and 100k recommended).

A PWM side note: I still don't quite understand why, but the n-channel MOSFET inverts the voltage applied to it. So, remember before that we had to write our "r" value as "255-r" to get it to cooperate with common anode? well now it gets inverted back, so you can leave it as good ol' "r" (as if you were coding for a common cathode LED. Don't change the wiring though...)

I think that about covers it, and I hope this is helpful! Good luck to you as you try to light stuff up without blowing it up!

No comments:

Post a Comment