# So You Want to Use PWM, Eh?

Pulse-width modulation.  It probably sounds a little confusing if you’re new to electronics.  Kindof a word mashup, really.  What do pulses, width, and modulation have to do with each other anyway?  I remember first learning about PWM during my freshman year of college at RPI.  I was in a pilot course called “Foundations of Engineering” under the excellent instruction of Professor Kevin Craig (whom I later worked for).  I remember thinking later, “Hey, this PWM stuff is pretty clever!”  So let’s take a look at PWM and see what we can learn.  (If you’re already familiar with the basics of PWM, skip down a few paragraphs for more advanced topics and experiments!)

Say you’ve got a light-emitting diode (LED) and a battery.  If you connect the two directly, the LED should produce a lot of light (assuming the voltage of the battery isn’t too high for the LED).  But what if you wanted to reduce the amount of light that LED produces?  Well, you could add a resistor in series with the LED to reduce the amount of current supplied by the battery.  However, this won’t allow for easily adjustable brightness and may waste a bit of energy.  That loss may not matter for a single LED, but what if you’re driving several high-power LEDs or light bulbs?  This is where pulse-width modulation comes into play.

Imagine you could connect and disconnect the LED and battery multiple times per second, causing the LED to flash or pulse (see graph above).  If this ON-OFF cycle is fast enough, you won’t even notice the blinking.  In fact, the LED will appear to be continuously lit, but reduced in brightness.  In addition, its brightness will be proportional to the ratio of the on and off times.  In other words, if the LED is connected for 30% of a pulse cycle, it will appear to be producing about 30% of its full brightness continuously, even though it’s actually turning completely on and off.  So to adjust the brightness of the LED, all we need to do is adjust, or modulate, that ON-OFF ratio, also known as the pulse width – hence the name!  The ratio between the on and off time is also commonly called the duty cycle.

Now in case you’re imagining yourself frantically flipping switches on and off, or tapping wires against battery terminals, you can stop.  Just put a transistor in series with your LED!  It can act as a switch which can be controlled by a microcontroller or some type of oscillator circuit (see links below).

So what’s PWM good for, anyways?  Well, dimming LEDs and other lights is just one of a number of applications (example).  You’ll also find PWM used in motor controllers.  You can make a very simple DC speed control using a PWM generator and a single transistor (examples – notice the extra diodes in use here to prevent damaging inductive spikes).  In addition, PWM is very important for some types of power supplies; specifically the aptly-named “switched-mode” PSUs.  This technique can also be used to create a digital to analog converter (DAC) by low-pass filtering the square wave.  Finally, pulse-width modulation is sometimes used as a means of digital communication.  For example, to command the position of a hobby servo.

Now you may be wondering why I’m writing about PWM all of a sudden.  Well, there’s actually a point to all of this background information.  By now, you’ve probably seen a car or two with these new-fangled LED tail lights.  They’re pretty easy to spot since you can typically make out the individual LEDs within the whole tail light assembly:

But have you ever noticed that on some cars (e.g. Cadillacs), these lights tend to flicker?  You may not see it if you’re looking straight ahead, but if you quickly move your eyes from left to right, you may catch a glimpse of the flicker created by a low-frequency PWM controller.  Now, call me strange, but I find this really annoying and distracting.  Maybe I just have fast eyes or something, but I hate flicker.  Back in the days of CRT monitors I could usually tell the difference between 60Hz and 70Hz refresh rates.  But in the case of these tail lights, it sounds like there’s danger for people with photosensitive epilepsy.  According to the Epilepsy Foundation, flashing lights in the 5 to 30Hz range can trigger seizures.  Obviously, having a seizure while driving would not be a good thing for anyone.

By the way, if you’re ever trying to determine the frequency of a blinking light, just snap a couple pictures while moving your camera (or the light).  The one catch is that you need to be able to specify a known shutter speed.  Then you just have to count the blinks and divide by the shutter speed (in seconds) to find frequency.  Here’s an example:

This method can also give you a pretty good indication of duty cycle – in this case it looks to be about 60%.  Here’s a second shot I took while on the road one night.  You can tell the streetlights are running on 60Hz AC (although they’re not LEDs so they never go completely dark during a cycle), while the green stoplight is likely getting DC:

I’m thinking this long-exposure shot might also pass as modern art in some circles.

## The Advanced Stuff

So what’s the deal with these awful low-frequency PWM tail lights?  Well, one reason you might choose a lower frequency is to save on energy lost during switching.  Both LEDs and the transistors used to drive them have parasitic capacitance.  In other words, they store a very very small amount of energy (think nanojoules) each time you turn them on.  This energy is consumed in addition to the steady-state power drawn by the LED to provide illumination.  Furthermore, this stored energy is rapidly dissipated (and thus not recovered) each time the device turns off.  Now if you’re turning an LED on and off fifty times per second, it’s probably no big deal.  But what if you wanted to eliminate any possibility of flicker by driving the frequency up into the kilohertz range?  Would this introduce substantial power loss?  I was curious, so  setup a simple experiment to find out.

The heart of this test circuit is fairly simple – two bright red LEDs (Model OVLBR4C7) along with 92Ω current-limiting resistors controlled by a BS170 MOSFET.  To measure the power consumed by this circuit, I’ve taken a non-traditional approach.  Because I was worried that the cheap ammeters I have available would be thrown off by varying PWM frequencies, I decided to measure power consumption based on the discharge time of a supercapacitor.  And who doesn’t love supercaps, anyways?

The theory is pretty simple.  The energy stored in a capacitor is equal to ½*C*V² (Joules).  So all I had to do was charge up the cap, measure its voltage, let the circuit discharge it over a fixed period of time, then measure the final cap voltage.  For my 2.5F capacitor (from NessCap), I chose ~60 seconds as my discharge period.  Here’s a screenshot of the voltage logging application I used to collect my test data:

The white line in the graph above plots the capacitor voltage during discharge.  The red line indicates the voltage measured across a phototransistor (L14C1).  This was used to quantify the amount of light produced by the LEDs at each test point.  To get a better measurement I covered the LEDs and phototransistor with an opaque plastic cup, then covered the whole setup with a shoebox and turned off the lights.  I was trying to see if, for some reason, the intensity of the LEDs was non-linear with respect to duty cycle or was affected by PWM frequency.  Unfortunately this data turned out to be rather boring, but I’ve still included it in my summary spreadsheet which you can download below.

Now before I go on, you’re probably wondering what sort of data acquisition hardware I’m using.  Well I doubt you’ve heard of it as it hasn’t yet been commercially released.  Right now it’s being called the RPI IOboard.  It’s a pretty impressive piece of hardware with dual 12-bit, 1.5MSPS ADCs, dual 14-bit, 1.4MSPS DACs, and a host of digital I/O all powered by a 400Mhz Blackfin processor.  For the past few years it’s been developed at RPI and tested at a number of schools across the country.  However since the project’s lead professor, Don Millard, left RPI last year, I’m not exactly sure what will become of the board.  The screenshot you see above is actually one of several executable VIs I developed as examples for use with the board.  Further information on the hardware can be found here.

So back to the experiment at hand.  For my first round of testing, I utilized the IOBoard to generate varying PWM signals for the MOSFET.  Thus, the current required to drive the BS170 was not included in my first measurements.  I varied both frequency and duty cycle for three pairs of LEDs: white (C513A-WSN), red (OVLBG4C7), and green (OVLBR4C7).

TABLE 1: Data for power consumption tests without gate-drive losses:

 Frequency/Duty Cycle (WHITE LED) 30% 60% 90% 50 Hz 36.15 mW 62.08 mW 84.89 mW 300 Hz 36.26 mW 63.50 mW 85.12 mW 10 kHz 38.75 mW 64.25 mW 86.14 mW 100 kHz 38.52 mW 62.80 mW 86.59 mW
 Frequency/Duty Cycle (RED LED) 30% 60% 90% 50 Hz 54.70 mW 93.82 mW 123.75 mW 300 Hz 57.76 mW 93.81 mW 125.35 mW 10 kHz 56.99 mW 94.00 mW 126.08 mW 100 kHz 56.61 mW 95.11 mW 125.47 mW
 Frequency/Duty Cycle (GREEN LED) 30% 60% 90% 50 Hz 41.49 mW 71.29 mW 91.65 mW 300 Hz 41.93 mW 70.29 mW 91.69 mW 10 kHz 41.90 mW 69.96 mW 93.36 mW 100 kHz 42.57 mW 69.71 mW 93.58 mW

So if you look through the data above, you’ll notice that there is, on average, a slight positive correlation between power consumption and frequency.  In other words, the higher the switching frequency, the greater the power consumption.  This is just what we would expect.  Again, this data does not include losses due to transistor gate capacitance, only losses due to the LEDs’ capacitance and the MOSFET’s output capacitance.

For my next test, I wanted to see what losses might be incurred in driving the MOSFET’s gate.  Thus, I called on my trusted 8-bit AVR microcontroller (ATMega644P).  I wrote a very simple program (which may be downloaded below) to produce a varying PWM output from one of the MCU’s timer/counter outputs.  I then measured the power consumption of the entire circuit, AVR included.  For this test I only used a 60% duty cycle:

TABLE 2: Data for the ATMega644 driving a BS170 and two green LEDs:

 Test Frequency Total Average Power (mW) Calculated Switching Losses (mW) 50 Hz 91.741 0.000 300 Hz 92.708 0.000 10 kHz 92.622 0.016 100 kHz 92.978 0.157 1 Mhz 95.789 1.568

TABLE 3: Data for the ATMega644 driving a FDP8860 and two green LEDs:

 Test Frequency Total Average Power (mW) Calculated Switching Losses (mW) 50 Hz 93.475 0.004 300 Hz 95.809 0.021 10 kHz 98.238 0.710 100 kHz 114.526 6.848 1 Mhz 161.657 60.914

TABLE 4: Data for the ATMega644 directly driving two green LEDs:

 Test Frequency Total Average Power (mW) Calculated Switching Losses (mW) 50 Hz 69.278 0.000 300 Hz 67.926 0.000 10 kHz 68.778 0.015 100 kHz 68.534 0.147 1 Mhz 70.708 1.467

## Discussion of Results

In Tables 2-4, we’re starting to see a much clearer positive correlation between frequency and power consumption.  For these tests I also added a fifth data point not gathered with the IOBoard: a frequency of 1Mhz.  This should in theory increase our maximum losses by 10x.  The results seem to support with this prediction.

The tables above also include a rudimentary calculation for switching losses based on capacitances.  I measured the capacitance of my green LEDs to be about 120pF (this value was not mentioned in the datasheet).  The gate capacitance of the BS170 is given in its datasheet as 24pF.  Finally, the input capacitance of the FDP8860 (a much beefier power MOSFET) is typically listed as 9200pF.  To determine switching losses I again applied the formula for a capacitor’s stored energy (½*C*V²).  At each switching interval, the parasitic capacitances in the circuit store and then dissipate this much energy.  So to determine how much power is lost, we simply multiply this lost energy by the switching frequency (since 1 watt = 1 joule/sec).  It appears that these calculated figures match the measurements fairly well.  Isn’t it nice when math agrees with reality?  Gives me a fuzzy feeling, that.

Now we can essentially think of the 50Hz test point as a baseline with zero switching loss.  For the data in Table 4, the 50Hz power consumption is about 69.3mW.  The calculation predicts that at 1Mhz, we’ll lose 1.5mW to parasitic capacitance for a total consumption of 69.3 + 1.5 = 70.8mW.  This isn’t that far from our measured 70.7mW.

It’s also interesting to note the substantially higher losses incurred when using the FDP8860.  This is largely due to its (relatively) enormous input capacitance of 9200pF.  This is nearly 400x the capacitance of the tiny BS170.  That’s the price you pay for the ability to sustain larger currents without overheating.  For more information on power MOSFETs have a look at this IRF document called “Power MOSFET Basics.”

## Summary

Well after all that, I’m going to say that whoever manufactures these tail lights can’t really use efficiency as an excuse for choosing a low switching frequency.  Unless they need huge FETs to drive huge currents, switching losses really aren’t so much of an issue.  I’m guessing that somehow it was just cheaper to go with a low frequency.  I’m pretty sure the components themselves aren’t any cheaper, but perhaps the assembly was less expensive.  It may be that some automakers already had a low-frequency module in place to drive old incandescent bulbs and then when LEDs came along they just kept using that same module.  Anybody out there care to comment on this?

So my advice to those making LED dimmers: pick a frequency of about 300-500Hz to eliminate flicker while keeping switching loss low.  Then find yourself a sufficiently large transistor with low capacitance and low on-resistance.  And if you’re working on motor controls or power supplies, things get a lot more interesting, but as a start, try a frequency in the 20+ kHz range to avoid audible whine.  Good luck!

• For further reading on LED losses, try this NI article: Light Emitting Diodes.
• For more accurate MOSFET swithing loss formulae, try this MAXIM article.
• Test code for the ATMega644P is available here.
• A complete spreadsheet containing all data can be downloaded here.

Update (9/22/2010): In the comments below, Jas Strong pointed out that in my switching loss calculations, I’d also neglected the power lost in the MOSFET during turn-on.  Jas is absolutely correct about that; I should have mentioned this previously.  Essentially, while the gate capacitance of the MOSFET is charging, the resistance between drain and source will pass from very high to very low resistance as the conduction channel is formed.  This time period, although short, includes a region of, shall we say, “moderate” resistance which briefly dissipates additional power.

Now, in the case of my two-LED test setup, I neglected the effects of resistive switching loss because they’re quite small.  Let’s take a quick look at the numbers.  First, we need to know how long it takes Vgs to reach the threshold voltage.  For simplicity, I’m going to assume that my AVR drives the gate with a constant current of 40mA (the maximum an AVR will provide per I/O pin).  Our worst-case turn-on time will occur with the FDP8860, which has a gate capacitance of 9200pF and a typical threshold voltage of 1.6V.  Using the formula ic = C*(dv/dt), I find dv/dt = 4,347,826 which means we reach Vth in 1.6/4,347,826 = 368ns.  At a switching frequency of 1Mhz, this represents about 37% of a switching cycle.  However, we need to double this since we lose power durning turn-on and turn-off.  Thus, we’re losing energy in the MOSFET’s resistance over 74% of a single cycle at 1Mhz.  That sounds like a lot, but just how much energy is actually lost?

To determine this loss, I’m going to make a big assumption and say that the MOSFET ramps linearly from 20kΩ down to 0Ω during turn-on.  I’m also going to assume the voltage of the diode is constant at 3V and the power supply is constant at 4.2V.  Remembering that I have 92Ω resistors in series with the LEDs, the instantaneous power dissipation in the FET becomes 2*Rmos*[(4.2-3)/(92+Rmos)]^2 (based on the fact that I have two LEDs and using the formula P = RI^2 and ohms law, I = V/R).  Now I need to integrate to determine an average power dissipation over this interval.  If my math is correct (feel free to check me), I get a loss of 0.632mW.  Since this occurs during 74% of a cycle, the total loss at 1Mhz will be about 0.468mW.  Not too serious in my opinion.

Now of course, the power required by my two-LED setup is piddly in comparison with that drawn by a couple brake lights.  Once you start sinking more current into your LEDs, this resistive switching loss, as well as the on-resistance of your MOSFET, is going to start to make a bigger difference.  So thanks very much Jas for pointing this out!

 Frequency Duty Cycle Start Cap Voltage Start Phototransistor Voltage 50 0.3 4.248407 1.464428967 300 0.3 4.246836767 1.4911225 10000 0.3 4.2389857 1.4911225 100000 0.3 4.243696367 1.538228733
This entry was posted in Hobbies and tagged , , , , , . Bookmark the permalink.

### 19 Responses to So You Want to Use PWM, Eh?

1. Michael Hoskins says:

From what I understand, LED tail light assemblies (especially Cadillac’s) are from the SnapLED family, which is a self-contained product.

In their product announcement, they mention the difficulties of using resistors to drive the lamps and having switched to a PWM solution. What those difficulties are, I have no idea, but this probably saved them a dollar or two per car.

The newer versions of these do seem to have a higher duty cycle, which they claim “eliminates the flicker typically associated with PWM.”

2. Connor says:

Great article. I’ve often wondered if this is an artefact of designing PWM systems for A/C power. I can’t see any logic to it, but it seems too coincidental that both are fixed around the 50Hz number. In any case, it makes no sense to me that car brake lights are modulated in the first place; they light at a constant brightness when in use.

• Keenan says:

It needs to be a constant current source. Otherwise they’d need lots of ballast resistors and it’d waste a lot of power and generate quite a bit of heat. Using a PWM regulated current source is likely cheaper and definitely much more efficient. Also allows them to use the same LED bulbs for the indicators and the brakes.

I think a frequency higher than 300Hz might be required though, since whenever I notice these taillights, it’s due to POV effects, not the flicker itself, kinda how it shows in the long exposure shots in the article. Looks really strange.

As for the green traffic signal, I suspect you can’t see the 60Hz flicker because the tungsten filament has too much thermal mass and acts as a low-pass filter. Though many of these are LEDs now (which this one could be too if it’s using a high PWM frequency). The street lamps are (usually mercury) arc lamps so their light output is much more tightly coupled to current flow than the heated filament in an incandescent.

• Mike says:

Cool, yea. I was thinking that was an LED traffic light on DC, but you might be right – AC with tungsten makes sense too. I don’t think I’ve ever specifically tried to identify a tungsten light with a camera, but I have done a lot of work with photodiodes. You can definitely detect the pulsation of an incandescent bulb by the voltage oscillation of a photodiode circuit. It is small in amplitude and large in DC offset, but it’s definitely detectable. And, interestingly, it occurs at 120Hz instead of 60Hz. This is because incandescent bulbs emit peak light during both the high and low peaks of their AC input – i.e. negative voltage light is the same as positive voltage light. Thus, the electronic frequency is doubled in light output.

3. Aaron says:

Thanks for this

Also I’m glad you also see the delay on taillights and such, i though I was a freak when I would turn my head and see something ‘weird’ about the taillights of some cars

Also happens with some projectors for me, if I twitch my eyes back and forth quickly I can see the RGB spaced out.

4. Mike says:

@Michael – Yea, I wonder what those difficulties were… I find it amusing that they claim flicker is “typically” associated with PWM. Not in my experience.

@Connor – Thanks! You might be right about that AC stuff. I think the reason for modulation is that you do need two different brightnesses at night – a low level and a high level to indicate braking. I understand that older tail light bulbs actually had two filaments for this purpose – one bright filament and one dim.

@Aaron – My pleasure, I’m glad to hear I’m not alone either. I’ve noticed that projector effect as well; it’s a problem with DLP called the “Rainbow Effect”: http://en.wikipedia.org/wiki/Digital_Light_Processing#The_color_wheel_.22rainbow_effect.22

5. Toolman says:

I thought the PWM was to make braking more noticable; our eyes are more sensitive to changes (delta) in light, so while scanning ahead, these stick out much more!

• Mike says:

Yea, that could be one reason for the flicker. If that’s the case, can I tie my tail lights to my stereo so they pulse with the music?

6. Newbie Four says:

I believe car manufacturers use PWM supplies with very low switching frequencies because they are easier to filter out in the audio system. I seem to remember several cars having noise issues in their radios when early daytime driving lights used a higher switching frequency that was easily audible over the radio.

• Mike says:

Ah, now that’s a good point. Spark plus also caused noise in some older radios. Still, this seems like am easy problem to solve – why not just put the PWM circuitry right inside the tail lights? This would reduce the chance of noise reaching the radio. You may need a few extra filter caps on the supply lines as well, but that’s no big issue. But of course, this solution may have just been a few bucks too expensive…

• Jez says:

In reality though they could just shield it all because the ignition system is shielded to stop it interfeering DVB-TV and radios and it’s also a whole lot more than just the taillights that’re usually under PWM control these days in my experience. I’ve seen sidelights, interior / dashlights and even dipped beams under PWM control. The inertior ones are usually the most obvious for low resolution / low drive frequency PWM, you can see the obvious steps in brightness as they dim.

As for why they use PWM on tailights etc in cars, its probably explained when you think that supply voltage to the cars power system will vary from ~11v to ~14v depending on whether the engine is running or not so a regulated supply would be sort of important for anything that is massively voltage sensitive…an incandescent lamp would probably not think much of being rated to 12V and run between 11V and 14V but a 12V LED being overdriven by 2-2.5V usually doesn’t end well or at the very least drastically reduces its life.

7. Dave says:

I’m very happy to know that I’m not alone being distracted by those car LED lights. I can’t understand why they use such low speed flickering, it’s very, very annoying.

8. Jas Strong says:

You neglected another source of loss from high switching frequencies- the dissipation of the transistor itself.

Transistors are very efficient when they are in saturation- either turned hard on (very low Rds, but high current) or turned all the way off (very high Rds, but tiny current). During the process of turning on, the gate capacitance causes the transistor to pass through the linear region between the two, and during this period the transistor will dissipate significant power. Same thing happens while it’s turning off.

This is why fast commutation is important to high power applications like electric vehicles, and hence the rise of exotic transistor topologies like IGBTs.

• Mike says:

Thanks Jas, that’s a good point! I’ve added a brief update to the end of my post to address this problem. In the case of my test setup, the additional losses aren’t really significant. However, if I were drawing more current with my LEDs, this certainly could become a problem. Thanks for pointing this out.

9. Martin says:

Nice article, and good work on explaining the details of PWM with respect to LED dimming. I’d like to point out two items which you may find interesting.

First, a professor who does a lot of work with LED lighting and associated power electronics once explained to me that lower switching frequencies (~sub 1kHz) put a lot of stress on the bonding wire(s) leading to the LED substrate. The idea is that low switching frequencies lead to a lot of thermal expansion/contraction of the wires, whereas at higher frequencies the thermal mass of the wire is high enough to preclude rapid changes in temperature and thus length. He pointed out that this could lead to premature failure of LEDs. It seems to me that this would only be an issue with high-power LEDs.

Second, while we’re talking about thermal mass, the LED junction has a certain thermal mass as well. If I remember correctly, the efficiency of an LED decreases with temperature. Therefore, it would be safe to assume that a cooler junction would lead to higher efficiency. Therefore, a high switching frequency (such as 1MHz) would probably result in slightly higher LED efficiency since the temperature-efficiency correlation is non-linear. I wonder if it would be possible to test this…

Anyway, thanks again for your article.

• Mike says:

Thanks Martin, this is really interesting. I’d never even thought about what might be happening to the wires within an LED. What you say makes sense though, particularly when dealing with high currents. Good point about efficiency vs temperature as well; now you’ve got me reminiscing about my undergraduate microelectronics class…