# Controlling high output LED's with a microcontroller

I'd like to build a circuit to precisely control the current through a couple of (LED and laser) diodes. I would like to minimize the number of components if possible. Power consumption is not a primary concern, so linear regulators and dissipating resistors are fine.

I am looking at a LM317-based circuit and this will serve fine for a constant or potentiometer-tuned current, but I want my current to be controllable by a microcontroller. How might I achieve this?

If it is possible to do this without an additional op-amp, BJT, or FET, please explain.... Actually, please explain no matter what. :)

Looking to drive LED's individually (or in series) at up to not much more than 1A. (Vf < 4V)

• What is the current range? Jun 12, 2011 at 4:17
• Now i'm preferring a solution that can drive 3A Jun 14, 2011 at 0:24

Okay, so the TL4242 is too expensive. No problem, you can still make a current source using more common components.

The current is defined by the input voltage as $I_{LOAD} = \frac{V_{IN}}{R_{SENSE}}$. Choose $R_{SENSE}$ in function of the microcontroller's $V_{DD}$. If you supply $V_{IN}$ with the PWM signal the current will switch between $I_{LOAD}$ and zero.

edit
I hadn't looked at the price for the TL4242, but Federico is right: the price is very reasonable. You would spend more if you want to PWM control the LM317.

edit: driving 2A
One thing to keep in mind when selecting components is that the transistor replaces your series resistor which usually controls the LED current.
If the transistor were to be purely a switch you would want it to be as efficient as possible, i.e. dissipate very little power. In that case a MOSFET would be the best option, there are lots of them which have $R_{DS(ON)}$ of less than $100m\Omega$.
Now that the switching element is a current limiting device this is not that important. You can use a BJT here. Power transistors for 2A $I_{C}$ don't have high $H_{FE}$, which means that the opamp would need to supply a rather high current into the base, and this may be beyond the opamp's capabilities. A Darlington transistor like the TIP110 is the solution.

• Suppose I want to drive up to 1.5 or 2A using this method. What components for the op-amp and the transistor would you suggest? For example it seems like a 2N2222 can only provide 800mA. Jun 12, 2011 at 21:09
• @Steven - I've edited my answer (a bit long for this comment) Jun 13, 2011 at 6:59
• @Steven - BTW, the 2n2222 may allow for 800 mA, but it's only 500 mW. That's because it's a switching transistor, which is not meant to work in the active region like we're doing here. Jun 13, 2011 at 11:53
• @stevenvh Thanks. I just realized I might want to drive some CREE XM-L LED's which take up to 3A. Will this 4A Darlington work? search.digikey.com/scripts/DkSearch/… Also, what about the opamp? I'm not really sure which kind to look for and a place like digikey has lots of options. Jun 14, 2011 at 0:11
• Also it seems all the current will pass thru R_sense. R_sense will probably be a bit over 1 ohm, at 3 amps that's 3 watts being dissipated. Is there not a way to make this a bit more efficient? Jun 14, 2011 at 3:06

PWM is the way to modulate the output, but something else is needed too since you want the current to be precise. Set it up so that when the microcontroller pin is high, you get the maximum current with good precision, then go down from there using PWM.

At 300mA you want to keep the LED current off the nicely regulated supply. Whatever you have driving the regulated supply is probably a little higher voltage and not so well regulated. A nice trick is to drive the LED from a NPN in controlled current sink mode. This means drive the base directly from the microcontroller output pin, the emitter goes to ground via a resistor, and the LED is connected between a positive supply and the collector. If the microcontroller power supply is well regulated, then the voltage accross the resistor will be reasonably fixed and sets the current the transistor will draw when on.

For example, if the micro is running from a nice 3.3V supply, then the emitter voltage will be about 2.6V. 2.6V / 300mA = 8.7 Ohms. You will have to experiment a little to get the exact current you want since the exact B-E drop is hard to guess, but this will be a good starting point. Actually I'd pick the nearest standard resistor size down, like 8.2 Ohms, then calibrate the rest in the micro. You should get a little more than 300mA with 8.2 Ohms, but whatever you do get should be pretty repeatable. It will also be quite independent of the unregulated voltage the LED is connected to, as long as it's enough to run the LED. Let's say you measure 320mA when the micro output pin is high. You then run the PWM from 0 to 94.8% to get your 0-300mA full scale.

For most purposes, figuring out this scale factor once in the lab and hard coding it will be good enough.

• Hi @Olin. I'm re-parsing your answer and I think what you are saying is a little different from what others have posted. My understanding of what happens with the transistor is a little shaky. You describe connecting the microcontroller output pin to the base, and putting the 8.2Ohm resistor between emitter and ground. This would cause those 300mA to flow from the microcontroller through the B-E junction into ground, would it not? Won't that kick the transistor into full-on saturation and it won't help me regulate current to the LED. Not to mention the microcontroller can't handle 300mA. Jun 18, 2011 at 10:19
• You got the connection right, but that's not what will happen. Remember that the emitter current is the base current plus collector current, and that the collector current is the base current times the transistor gain. Therefore most of the current comes thru the collector. Since the voltage accross the emitter resistor will be reasonably fixed, this collector current and therefore the LED current will also be fixed. Jun 18, 2011 at 22:52
• Continued to work around character limit in comments. Let's say the transistor has a gain of 50 and the emitter current is 320mA. That means the base current will be 6.3mA and the collector (also the LED) current 314mA. Jun 18, 2011 at 22:54
• Okay, but doesn't this also assume that the LED power supply is 3.3V also? Suppose I connect +100V to the Collector, and my +3.3V MCU pin to the Base, and an 8Ohm resistor from Emitter to ground. Wouldn't asserting the pin HIGH cause the transistor to let through around 12 amps and fry everything? Jun 19, 2011 at 12:00
• It assumes the LED power supply is several volts higher than the microcontroller supply. Let's say the micro holds the base at 3.3V to turn on the LED. From the example before, the micro will be supplying 6.3mA, and the transistor will allow up to 314mA thru its collector if given enough voltage. Let's say the LED needs 2.1V (typical for green) and the transistor saturation voltage is 200mV. That means the LED power supply needs to be at least the emitter voltage plus 2.3V, or about 4.9V. A 5V supply would just work. Jun 19, 2011 at 12:10

PWM is the typical way to control the brightness of LEDs. The LM317 is probably too slow for this, but the TI TL4242 may be made to order: it's an adjustable constant current source, PWM controlled and able to deliver up to 500 mA.

• awesome, but it's a bit expensive. I'd like to get a solution using something basic like the LM317 if possible. It shouldn't be hard to smooth out the PWM with a lowpass filter. It just seems like I should be able to construct a voltage controlled current source with the LM317. But HOW? Jun 12, 2011 at 7:35
• @Steven: expensive? It's 1.08 euro at Mouser! Jun 12, 2011 at 8:18
• @Federico Nice, they're a lot more at digikey. At one dollar, this may be more cost effective than an op-amp and transistor circuit. This will definitely be a great option for LED's which I intend to drive under 500mA. Jun 12, 2011 at 20:35
• Actually theyre only like \$1.50 at digikey. Must have been another site that I checked on. Jun 12, 2011 at 20:42
• TL4242 is a tiny chip!! I'll get around to trying it out... eventually... Jun 18, 2011 at 15:03

I'm not sure what your ultimate goal is, but you could eliminate the need for a current source altogether? Again, this depends on what you are trying to achieve here and what hardware you are dealing with. But, if your microcontroller has hardware PWM (maybe software PWM could work too), what I had in mind was just to have a MOSFET and a resistor per diode you want to drive.

The microcontroller would drive the gates of the MOSFETs (couldn't post an image). Have the source of the MOSFET tied to ground and the drain to the other end of the circuit. Using PWM, you can control the average current without much wasted power. This is very similar to what @Olin Lathrop suggested. Like he said, you should choose a resistor that would yield the maximum current you want with good precision.

• You're definitely right! I realized this too and last night I built a circuit which uses three Darlington power transistors for an RGB LED unit, controlled with PWM from an arduino. Here's a video of it: youtube.com/watch?v=wCpl6USt7mo I did spend some time making measurements and picking appropriate resistors out of my set but it definitely works well. One downside is that there is a dependence on the Vf of the LED's for the calculation of resistances necessary. I'm now trying to look into methods to get a current drive that is independent of that. Jun 18, 2011 at 10:00
• If this isnt too tall of an order, could somebody characterize the usage of mosfets in one or two sentences? Specifically as it might be used for a switching role. Is it that I can control a current via a voltage difference? Whenever I read about fet's i can't understand all the terms. Jun 22, 2011 at 21:07

I changed my mind. I suggested this voltage-controlled current source, but thinking about it I realize it has its problems. The PWM signal swings the opamp's input between GND and V+, to switch the current source on and off.
This is rather taxing for the opamp, and you'll have to find a fast one (high Gain-BandWidth product) to cope with it. And it looks like rail-to-rail and fast don't go together easily. That's why I want to get rid of the opamp handling the PWM signal.
The idea is as follows: use a fixed input voltage for the opamp so that you also have a fixed current through your LEDs. Now use the PWM signal to shut off the driving darlington transistor. You can do this by placing an NPN transistor between the darlington's base and ground, and driving it with the PWM signal. Don't forget the base transistor.
When the PWM signal is low this transistor doesn't conduct, the opamp drives the darlington, and the LEDs see the programmed current. When the PWM signal is high, the transistor shuts down the darlington and there's no current through the LEDs. You will have to place a series resistor on the opamp's output, otherwise the transistor will shortcut it. Calculate the resistor's value such that, given the darlington's $H_{FE}$, you get the required collector current.

• Your original circuit makes a lot of sense to me though, and I especially like it because it uses a minimal number of vital parts: a transistor, an opamp, and one current handling resistor. The rail to rail opamps aren't too expensive and also come in multiples (i want to drive multiple LEDs at once) so I'll try that solution out to see if it gets the job done. I like to go hands-on before digging into hypothetical calculations, its more fun that way :) Jun 18, 2011 at 17:47
• @Steven - That's OK. Just try to understand why I wanted to change it and keep that in mind, in case it should switch too slowly. Jun 19, 2011 at 8:57
• Yep, if I can't get the opamp circuit to work too good I may try more things with transistors. mosfets also look like something I should learn at some point. However for the time being i can get by with resistors and a power BJT (though these darlingtons get quite hot, theyve got quite a voltage drop) Jun 19, 2011 at 12:02

You should be able to use PWM output with a low pass filter (resistor in series with PWM pin and capacitor parallel with end of resistor and ground.) Use this to drive the ADJ pin on the LM317.

To get constant current, you would need to sample voltage across the shunt and use that to set current.

• How? Isn't the LM317 a voltage regulator? Jun 12, 2011 at 4:02
• where is the shunt? Could you describe in a little bit of detail how I would modify the circuit shown in my link to get adjustability? The part involving PWM and a lowpass filter I understand completely. Jun 12, 2011 at 6:12
• @Steven - the LM317 is a voltage regulator, but it does so by keeping a constant voltage between the output and the Adj input. That means that a resistor between these pin will cause a defined current to flow. Jun 12, 2011 at 6:40
• @stevenvh I understand that. But I can't modify the value of a resistor with a microcontroller, now can I? ;) I'd like somebody to help me come up with a circuit which has a convenient place to hook up a 0-5V analog V_set, and have it produce a current I_out proportional to V_set, powered from V_in. Jun 12, 2011 at 6:42
• @Steven - you're right that the LM317's current is difficult to control by a microcontroller. See my answer below for an alternative. Jun 12, 2011 at 6:51

If you're wanting to use the LM317 you could simply replace one (or even both) of the adjustment resistors with a digital potentiometer. These are little chips that emulate a pot but are linked to an MCU through such things as SPI or I²C for the MCU to control the resistance.

Microchip do some, and they do free samples.

• Digital potmeters have rather high resistance values (10 - 100k$\Omega$). For 200 - 300mA you'd need less than 10 $\Omega$ Jun 13, 2011 at 9:18
• Analogue Devices provide 1KΩ digital pots, and as I happily use resistances in that range to regulate an LM317T I can see no reason not to use them. With the 317T the lower your resistances the higher your accuracy. Yes, there is a limit to how high you can go before it starts to fail, but resistances in the 1-2K range are fine. Jun 13, 2011 at 16:51
• Digital potentiometers are a very cool idea. Although I think in my use case the digipot would be in series with the load and I doubt they design these to handle lots of current. Jun 18, 2011 at 15:02
• @Steven - That's right, they can't handle much current. And even then, Matt wasn't convinced, but even a 1k$\Omega$ pot with 256 taps has a step of 4$\Omega$. You probably want more control than that. Jun 20, 2011 at 7:30