2

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)

Steven Lu
  • 1,030
  • 4
  • 14
  • 24

7 Answers7

5

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.

Olin Lathrop
  • 310,974
  • 36
  • 428
  • 915
  • 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. – Steven Lu Jun 18 '11 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. – Olin Lathrop Jun 18 '11 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. – Olin Lathrop Jun 18 '11 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? – Steven Lu Jun 19 '11 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. – Olin Lathrop Jun 19 '11 at 12:10
  • (continued) With a LED supply of at least 5V, the LED current will be reasonably fixed at 314mA in this example. This is independent of the LED power supply voltage. Higher voltages will still allow the same LED current, but the voltage on the transistor will go up. As this goes higher, the transistor will dissipate more power, but the LED current will continue to be regulated regardless of the supply voltage, as long as it's 5V or more. Too high and the transistor voltage or power dissipation spec is exceed and it will go poof. – Olin Lathrop Jun 19 '11 at 12:13
  • So there is no negative feedback and I am calibrating the current based on the transistor's gain? If I have one transistor with a gain of 50 and the next one is 100, then the second one will be letting twice as much current through even when setting everything up in the same way? – Steven Lu Jun 19 '11 at 12:41
  • No, the emitter resistor is the negative feedback path. The emitter current is 320mA in the example. As long as the gain is "large", most of this will come from the collector and the circuit is largely independent of the gain. At a gain of 50 we got 314mA collector current. At infinite gain it would only be 320mA. That's the feedback of the voltage accross the emitter resistor at work. – Olin Lathrop Jun 19 '11 at 16:24
  • This is confusing me, though, because it seems like what you're telling me is that I do not need an op-amp. So, please correct me if I'm wrong: The emitter voltage is determined only by the base voltage. Doesn't matter what collector voltage is. Then, since emitter voltage is known the current thru the resistor is known. The transistor only lets through enough current from the collector (and from the base, these two determined by the gain) to maintain the emitter voltage and I do not have to worry about the emitter voltage deviating from base current minus 0.6v. – Steven Lu Jun 22 '11 at 20:54
  • i guess i can see why this works. The use of the op amp just releases the slight load that would otherwise be on the microcontroller. And forces the emitter voltage to equal mcu pin voltage. Neither of these is necessary if I have high enough gain on my transistor. – Steven Lu Jun 22 '11 at 20:56
  • @Steven: Yes, from your last two comments it seems you understand. – Olin Lathrop Jun 22 '11 at 21:32
3

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

enter image description here

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.

stevenvh
  • 145,145
  • 21
  • 455
  • 667
  • 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. – Steven Lu Jun 12 '11 at 21:09
  • @Steven - I've edited my answer (a bit long for this comment) – stevenvh Jun 13 '11 at 06: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. – stevenvh Jun 13 '11 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? http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=BD681S-ND Also, what about the opamp? I'm not really sure which kind to look for and a place like digikey has lots of options. – Steven Lu Jun 14 '11 at 00: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? – Steven Lu Jun 14 '11 at 03:06
  • Could I perhaps use another opamp to linearly scale the PWM output to a lower range, so as to allow me to use a smaller resistor for R_sense? If I have Vin in the range of 0 to 1V and use Rsense = 0.2 I can get up to 5Amps, and the 5 amps would dissipate only one watt at the resistor rather than 5 watts if I used a 1ohm and 5 volts Vin. Does any of this make sense? – Steven Lu Jun 14 '11 at 03:45
  • Also the R1 in the diagram... is it necessary? – Steven Lu Jun 14 '11 at 04:40
  • So I got my first shipment of LED's and have been playing with them on my current controlled power supply. They actually put out a great deal of light even at very small currents so it seems like controlling their brightness by varying the voltage is probably a better way for me to obtain smooth intensity transitions. Since I can easily construct a maximum current limiter using the LM317 I do believe my problem has been simplified. – Steven Lu Jun 16 '11 at 03:36
  • Alright I think i understand this circuit now, stevenvh. These negative feedback opamp circuits are very convenient, and not much current flows through them at all since they maintain an equilibrium state. I definitely know what to do with my circuit now :) – Steven Lu Jun 18 '11 at 12:14
  • I can't find any packages that have multiple power transistors in one chip. I am gonna take a guess... too much power dissipation for that to be practical? – Steven Lu Jun 18 '11 at 15:06
  • @Steven - R1 is not necessary. Decreasing \$R_{SENSE}\$ and lower the control voltage is a very Good Idea. (Sorry for the late reply, but I try to follow a number of threads, and sometimes lose sight of one, esp. if the latest comments are hidden) – stevenvh Jun 19 '11 at 08:54
  • So it seems to me like this circuit here wouldn't work too well if Vcc can't be significantly higher than Vin. Because the emitter voltage is Vin and Vcc must be higher by whatever the load voltage drop is, plus the C-E drop. – Steven Lu Jun 22 '11 at 20:44
2

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.

stevenvh
  • 145,145
  • 21
  • 455
  • 667
  • 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? – Steven Lu Jun 12 '11 at 07:35
  • 1
    @Steven: expensive? It's 1.08 euro at Mouser! – Federico Russo Jun 12 '11 at 08: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. – Steven Lu Jun 12 '11 at 20:35
  • Actually theyre only like $1.50 at digikey. Must have been another site that I checked on. – Steven Lu Jun 12 '11 at 20:42
  • TL4242 is a tiny chip!! I'll get around to trying it out... eventually... – Steven Lu Jun 18 '11 at 15:03
1

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.

dhsieh2
  • 206
  • 1
  • 5
  • 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: http://www.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. – Steven Lu Jun 18 '11 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. – Steven Lu Jun 22 '11 at 21:07
1

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.

stevenvh
  • 145,145
  • 21
  • 455
  • 667
  • 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 :) – Steven Lu Jun 18 '11 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. – stevenvh Jun 19 '11 at 08: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) – Steven Lu Jun 19 '11 at 12:02
0

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.

Joe
  • 3,609
  • 20
  • 23
  • How? Isn't the LM317 a voltage regulator? – Steven Lu Jun 12 '11 at 04: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. – Steven Lu Jun 12 '11 at 06: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. – stevenvh Jun 12 '11 at 06: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. – Steven Lu Jun 12 '11 at 06:42
  • @Steven - you're right that the LM317's current is difficult to control by a microcontroller. See my answer below for an alternative. – stevenvh Jun 12 '11 at 06:51
  • @Joe to "sample voltage across the shunt" this implies I would need to use an op-amp and transistors and such in order to accomplish the task? In this case I suppose I should just skip the lm317 altogether. – Steven Lu Jun 12 '11 at 08:21
  • I don't think the LM317 is the right choice for this. Just describing how it might be possible. If you have the current limiting resistor to ground, then you can use the micro's ADC to sample the voltage with no additional components. The LM317 seems like an odd choice for this. – Joe Jun 12 '11 at 15:37
0

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.

Majenko
  • 55,955
  • 9
  • 105
  • 187
  • Digital potmeters have rather high resistance values (10 - 100k\$\Omega\$). For 200 - 300mA you'd need less than 10 \$\Omega\$ – stevenvh Jun 13 '11 at 09: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. – Majenko Jun 13 '11 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. – Steven Lu Jun 18 '11 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. – stevenvh Jun 20 '11 at 07:30