The fundamentals of driving a plain LED

To successfully drive an LED, you need to arrange for a controlled amount of current to flow through the LED and switch it off if necessary.

Sometimes most of the work is done for you. For example, you can really easily find non-intelligent LED strips that are packaged such that “all” you need to do is put unregulated 12V or 24V through them, switching them on or off as necessary. Sometimes you are talking to intelligent LEDs and you don’t even need to do that.

A basic LED circuit

The easiest way to control the current flowing through an LED is to just throw a resistor in. And the easiest way to switch them on and off is to just leave it wired on all of the time.

I’m going to dance between the switching part and the controlling current part of the story as we go. You’ll probably want to understand some of the stuff in the LED tour in order for all of this to make complete sense.

Resistors and why a lot of folks resist using them

When you are talking about 20 mA LEDs used as an indicator, a resistor works pretty well to get it roughly bright enough.

The problem is that for a given set of LEDs, the Vf will vary. Ergo, if you put a bunch of the same LED in a row with a bunch of resistors, the brightness will be a little different between each one.

With a little bit of electronics, you can make a constant-current LED driver, as if it’s a magical resistor that sets itself to whatever resistance is needed to get you 20 mA flowing through.

However, this doesn’t solve all of your problems. You are still converting energy to heat. Not only does this destroy the overall energy efficiency of the LED, it also makes the existing problem of heat dissipation from a high-power LED array even worse.

Switching and a teaser on PWM

You can sometimes get away with running a LED with a resistor to control the current off of a GPIO pin. It’s generally at least OK for an indicator LED or two. The problem is that a lot of the time there’s a maximum amount of power you can run through the package, plus a maximum power per LED.

And sometimes, you’ve got something like the ESP8266 which is specced for 2 mA per pin max, which is barely ampere current for anything.

Either way, you don’t want to melt your microcontroller.

You might think you can avoid this by using a switch, but if you look at switches, you find that there’s a lot of the small cheap switches can’t handle much power either.

Finally, if you want to dim an LED, generally the best answer is to PWM it, where you flip it on and off at a high enough frequency that you can’t see it.

I’m skipping over the details of this for this section, but just know that it’s not just about switching the LED on and off, it’s also about switching it on fast enough that you can’t see it flicker.

After all, people object to you flashing them.

Switching LEDs on and off with various transistors

This is mostly useful if you want to drive a non-intelligent LED strip like the popular 12V or 24V strips you can find. Those come with current control built-in for ease of wiring so you just need to switch them on and off with a transistor.

If you want to drive things like a high-powered discrete LED module where it doesn’t come with something to control the current, generally the driver circuit is going to have a control line so a separate transistor to switch it on and off isn’t necessary.

I have a whole section on transistors that goes into more detail, but here I’m just going to talk about how to use transistors to as a low-side driver for LED strips.

BJT’s

schematic of a BJT led driver

Can handle at least 20 mA, frequently up to 500 mA, sometimes way above that. A 2N2222 is fine here.

You need to calculate a set of bias resistors to some degree of accuracy based on the current flowing through the device. Each transistor has a gain ratio for how many amps of drive current can flow through it based on a given number of amps of control current.

“Wastes” energy to drive the transistor because they are current-based devices.

I guess there’s one kinda useful case, which is if you are using a low-voltage microcontroller at something like 1.8v, it’s hard to find a MOSFET that will switch low enough, but it might only need 2-3 mA of power to switch and your microcontroller might be able to switch this.

Alternatively, these are sturdier than MOSFETs frequently so if you are doing this on perfboard or a breadboard, these can be a reasonable pick.

You can get a bit better performance by driving one transistor by a second one, called a “Darlington” transistor, which you can get bundled into a single package, a la the TIP120.

schematic of a Darlington led driver

However, there are much better options for driving big loads.

MOSFETs

If you want one singularly useful part for driving electronic loads, the modern MOSFET is that part. They get used so often, it’s like we’ve all got a FETish for them.

It looks like this:

schematic

None of the values are super-critical. It’s nice to throw in a pull-up resistor, somewhere between 10k and 100k so that it starts up in a reasonable state. It’s helpful to pull in a gate resistor, maybe 100 ohms. You probably want to find a MOSFET that’s a logic-level MOSFET or use a gate driver.

There’s a lot of 12V and 24V LED strips out there and they are best driven with an N-channel MOSFET like this. Even a SOT-23 tiny little MOSFET can drive 2-3 amps, which is a lot more than a BJT would drive.

Switching a paralel set of devices

By parallel, I mean “I’ve got some pins on a microcontroller and I want to magnify the amount of power they can control”.

Obviously, everything I talked about in the previous section is also covered here. You can always wire up a row of 8 transistors, it’s just that this can be really really annoying to get all placed and wired and soldered.

74-series buffers (e.g. 74HC125, 74HC241)

These are only really useful for driving more digital loads. If you connect a 20 mA LED to each output pin, you’ll probably melt the part, so these are quite marginal for driving LEDs.

Darlington drivers (e.g. ULN2003/2803) and ULN2003/2803 styled MOSFET driver chips

schematic

This particular part is really handy if you’ve got some loads you want to run off of an IO port. There’s even a snubber diode already there if you want to drive a small motor or a solenoid or a relay. And there’s already resistors there as well.

Basically, this is what you thought the 74-series buffers were going to do for you. And these are generally around 500 mA per pin, plus you can bridge several pins in parallel to switch more current.

The standard ULN2003A or ULN2803A chip is not especially compatible with 3.3v outputs.

I did discover, however, that there are some parts that are available, even as through-hole parts, that not only are 3.3v compatible but also use MOSFETs instead of darlington transistors.

Regulating power for one-device-per-LED a.k.a. “Linear LED drivers”

Okay, now we’re switching back from switching the LED to controlling the power again.

There’s a bunch of different kinds of part in this space.

schematic

There’s some two-terminal devices that act as smart resistors, except that they generally need to be at a much higher voltage, like 2-3v higher. This means that you probably can’t use them off of a GPIO but you can use them for something like a power LED.

schematic

There’s some more complicated devices that have more terminals and are usually a bit bigger, frequently with thermally-enhanced packages, that can handle several watts of power. And in this case, there’s usually some sort of enable pin that will let you control the LED directly through the device instead of requiring you to have a MOSFET or something to turn the LED on and off.

One nice attribute of a lot of these drivers is that they can PWM a lot faster than a switching power supply. And they are a lot simpler.

Serial-to-parallel latches

By serial-to-parallel, I mean “I’ve got two pins on a microcontroller and I want to run a bunch of stuff off of it”

Pretty much any modern microcontroller has plenty of power to twiddle the lines necessary to make this work. In fact, it can do it so fast that you can get PWM with a pretty good refresh rate and bitdepth.

And it’s fairly easy to route around a large circuit because you can chain them. And, if you compare using one of these to a Darlington driver or something like that, you start to realize that for a bunch of LEDS, this is actually a lot less wiring.

74HC165

A basic shift register. Problem is that you can see the signals shift when it writes. This may or may not actually present a problem, depending on how fast you shift the signals. Also, you can generally only source maybe 70 mA total, 20 mA per pin (check the datasheet) so you can melt it.

This is probably not worth messing with unless that’s all you happen to have handy. Or you want to save a pin.

74HC595

schematic

A serial-to-parallel latch. Picture it as a 74xx165 with a latch on the parallel outputs, so that if you are shifting new data, it won’t give you ‘snow’. Same problem as the 74HC165 with melting, however.

On the micro-controller side, you end up with three pins. One pin is the serial data, the second pin is the serial clock, and the third pin is the pin that transfers the data from the shift register portion to the latch.

There’s more of these

There’s a bunch more devices like this with various features. For example, there’s devices like TPIC6C596 that’s like a 74HC595 made sweet sweet love to a ULN2803A and had little cute little semiconductor babies that drive a lot more power but otherwise work like the 74HC595.

I2C/SPI/Serially controlled linear LED drivers

These are intelligent controllers for LEDs. Generally, they are going to incorporate a constant-current driver and an interface of some sort that you need to find or write a library for.

But this usually comes with a lot of extra features. You can PWM an LED that’s connected to a 74HC595 shift register, but it’s potentially a lot of effort and CPU. Whereas this way, you can just set the PWM value. And it might even have self-diagnostic systems to let you know if the LEDs are disconnected, etc.

Switching power supply LED drivers

This is a very large subject and I’m going to skip over it for now, but a switching power supply avoids turning energy into heat if it’s stepping down the voltage… plus it can actually step up the voltage.

There’s a bunch of devices and they are basically a standard switching power supply, except that instead of being constant-voltage, they are constant-current, which means that you can directly drive an LED off of them.

A linear driver is going to tend to be able to PWM faster but these are much more power efficient.

There’s a whole bunch of step-up LED drivers that are designed to run one or more chains of relatively small LEDs for things like LCD backlights. Generally, they aren’t designed to handle that many amps of power, so you can’t run 30+W 36V LEDs off of a 5V power supply.

There’s a whole bunch of step-down LED drivers that are designed to run generally one or more really large LEDs. One sweet spot is low-voltage, generally 5-6 volts, and then you start to see much higher voltage drivers that are optimized for the 12-40V case for things like a 12V car system or running the popular COB modules that are designed for 30-32V or so and over an amp of power. So if you want beefy LEDs, you want a step-down power supply.

There’s not many devices that are good at driving a parallel set of LEDs, say so you could drive a RGBW LED without having four parallel circuits. Most of them are offline drivers meant for LED lightbulbs off of 120/240V power supplies.

(Yeah, I was confused when I saw “Offline” being used as a term for switching power supplies and it took me for a while that they meant something like “off of a wall plug”)

While you can get intelligent LEDs really easily and you can get modularized DC/DC converters fairly easily, it’s hard to find LED switching power supplies pre-made for you, so using most of these ends up with you sourcing the parts and fabbing a PCB.

Where to go from here?

  • Transistors
  • Switching power supplies

Posted:

Updated: