Sunday, September 23, 2012

Dimming a 12V LED strip with a mosfet and PWM

Dimming a 12V LED strip with an N-channel power mosfet is pretty straightforward. In this experiment I'm using 5 meter of cool white LED strip running at about 800 mA giving 10 Watt.

the basic setup
 An Arduino Duemilanove (my trusty lucky white one) is used to provide pulse with modulation ( PWM ). Digital pin 9 and 10 (also known as OC1A and OC1B) are used. They both use the Timer2 facility of the atmega328p microcontroller which means I can have them output exactly the same PWM signal. Digital 9 is used to drive the mosfet and digital 10 is only used for displaying the unloaded PWM signal on the scope.

Basic PWM setup schematic

PWM on the scope
On the scope you can see the yellow PWM signal driving the mosfet and the blue one that is not connected to any load.

The following Arduino sketch does a simple PWM sweep in 10 steps per second:

 1 void setup()
 2 {
 3   pinMode( 9, OUTPUT);
 4   pinMode(10, OUTPUT);
 5 }
 7 byte b = 0;
 9 void loop()
10 {
11   analogWrite(9, b);
12   analogWrite(10, b);
13   delay(100);
14   ++b;
15 }

This gives a PWM signal as shown in the (crude) video below.

Not shown in the video is that this also nicely dims the LED strip.

This is actually good enough for simple dimming, but lets look at it more in detail. More specific lets look at the rise and fall of the PWM signal at the gate of the mosfet.

a detailed look at the voltage at the rising edge at the gate of the mosfet

Whats interesting to see is that it takes about 600ns in total to go from 0V to 5V at the mosfet gate.
The first sharp rise is from 0V across the threshold voltage of the mosfet to the Miller plateau voltage and takes about 100ns. At this point the current through the mosfet is already at the peak but the voltage across the mosfet from drain to source still has to drop from the 12V it starts at. This drop happens across the relatively flat part of the peak in the middle called the Miller plateau region. After this drop the Drain-Source voltage for this particular mosfet will be a bit above 150mV and the remaining 11.85V will be across the LED strip. Finally the voltage at the gate rises to 5V which will slightly lower the on-resistance of the mosfet and the final Drain-Source voltage should be about 100mV.
As mentioned all this takes about 600ns which is not very fast. Looking at the specification of the IRF540 mosfet a rise time of about 35ns should be possible.

I got quite some interesting info concerning mosfet gate drivers from this Texas Instruments paper.

The basic idea to get a faster transition is driving more current in the mosfet gate then the measly 50mA that the digital pin of the Arduino/atmega is capable of providing.

One solution suggested by the paper is to use a Bipolar totem-pole driver. The idea behind that is that by using the very fast operation and large gain of bipolar transistors the gate can be charged and discharged quickly.

adding the bipolar totem-pole mosfet driver 
When the PWM signal is high the NPN transistor is on and the PNP transistor is off. This allows a large current to flow from the 5V power supply and the tank capacitor C into the mosfet gate.

PWM high and NPN on
When the PWM signal is low the PNP transistor is on and the NPN transistor is off allowing for a quick sink of the charge on the mosfet gate into ground.

PWM low and PNP on

For the transistors a BC639 NPN and a BC640 PNP do the job. R is 3.3Ω. RB is 22Ω. RGATE is 4.7Ω. C is a big 1000μF electrolytic.

final setup
improved rise time
A rise time of 44ns using the bipolar totem-pole driver is not bad at all.

Lets take a look at the other side of the wave form.

fall ringing
There is some nasty ringing on the fall side of the pulse. This won't affect the dimming but it's interesting anyway to take a look what is happening.

First thing I checked is RGATE. Increasing the resistance off RGATE does lower the ringing but it also kills the just gained speed improvement.

Then I noticed something weird: if I unrolled part of the LED strip the peaks of the ringing got larger. I then replaced the LED strip by a single LED and surely the ringing is all but gone.

I assume the capacitance/inductance of the 5 meter LED strip and/or all its LEDs cause the ringing.

Adding a shunt resistor across the LED strip lowers the amplitude of the ringing but it requires quite a low ohm resistor which in turn gets very hot when there is 12V across it, and it wastes a bit of energy.

Measurement with the scope shows that the ringing is at about 3.5Mhz. A simple shunt capacitor of 100nF should nicely act as a low-pass filter. If we presume a parasitic resistance of 1Ω, using f = 1/(2*pi*R*C) gives about 1.5Mhz cut-off frequency for the filter.

schematic with CSHUNT added

with 100nF filter
That certainly looks nice. I still don't think it is more then aesthetics though..

 I'd love to hear comments on this! Either here or on G+.


K said...

The ringing is a classic problem caused by the drain-source capacitance of the transistor, in combination with inductance it is switching (this can be either leakage inductance of a transformer, an inductive load, or some other inductance - like a long coiled up wire). Basically, what you have is a LC-circuit, with charge starting in the C (in turn-on) or L (turn-off). Typically, this does no harm, unless the ringing brings the voltage in the neighborhood of the maximum drain-source voltage of your FET, which will reduce the lifetime of your FET (to seconds if your crossing it, or less obvious if you're just coming close).
The classic solution is the snubber, which in it simplest form is just a capacitor, shifting the frequency by making the C larger.
A resistor will dampen the oscillation faster, by increasing the R in the RLC-circuit, but will waste more energy. The classical paper on this subject is: Calculating optimum snubbers (

There's a lot of more sophisticated snubber topologies out there, mostly used for DC-DC convertors, but not a lot of literature. If you want to know more, you know where to find me ;)

There's also some stuff that will increase gate turn-off time, but this little comment box is a bit too small to explain :)


Unknown said...

Thanks for the info, K!

Unknown said...

You list the resistances as being R= 3.3 ohms, Rb= 22 ohms, and Rgate= 4.7 ohms. These seem awfully low...are they supposed to be Kilo-ohms rather than ohms?? The base current of 22 ohms seems like it would burnout the Arduino pin. Thanks for the writeup! It's exactly what I was looking for to setup some battery friendly dimmable LED lights in my camper van.


Unknown said...

Hey Bret,
I use those actual values yeah.

Indeed Rb could be lowered. The other values are actually needed like that to drive the mosfet really hard. The large capacitor can provide the needed current.

noogrub said...

Great post, thank you! I use the Texas Instrument MSP430s as controllers. They have a good paper you might want to read:

Cheery regards from Bloomington, IN, USA. - John

Unknown said...

For all the readers of this post: don't forget to look at the followup posts I made for this one where I further investigate.

RZ said...

NIce... What frequency do you use to drive the totem pole driver?

Unknown said...

I did not change the PWM setting used standard by Arduino for the timer2 PWM pins. It is almost 500 Hz.

Unknown said...

Thanks for the post. A bit of a noob question here. Where/how did you measure the mosfet output voltage to get a direct relationship between the gate PWM signal and output? When I attach my oscilloscope probes at the gate and drain I see an inverse PWM relationship.

Unknown said...

I just measure the output of the mosfet gate and pin 10. The mosfet gate is directly or indirectly driven from pin 9, and pin 10 is setup to output the exact same PWM signal as pin 9.

Omnimusha said...

what program you use to make the diagram?

Unknown said...

I made these schematics with Eagle CAD and the Gimp.

Unknown said...

Can I use the same circuit to drive 24V leds?

Unknown said...

@Kerem: sure, should work.

Liam D. Gray said...

Have you used a light meter to measure the amount of light emitted and how it responds to duty cycle? I've done so using a $3.95 log-scale analog light meter from Adafruit.

I implemented a similar circuit, with n-channel MOSFET driven by bipolar totem pole, inspired by yours. I have found highly non-monotonic changes in light level in response to linear changes in duty cycle. For instance as duty cycle goes up two steps, light level may go up by a factor of 10 and then back down again.

What I'm dimming is actually a battery powered 24-LED flashlight and I am guessing that the three AAA batteries have some capacitance, perhaps slowing the switching enough that rise and fall times are large relative to (1 / PWMfrequency). I am considering changing the PWM frequency (lowering it) and hoping this will help.

Unknown said...

Liam: if that is a case adding a big capacitance on your battery output should improve behavior. The bat can charge the cap slower yet the cap will provide current quickly to the circuit.