Tag Archives: tutorial

LabVIEW: More Tips and Tricks

Quick post today.  I recently stumbled across this excellent PDF by Nick Golas.  It contains an enormous quantity of time-saving tips for users of LabVIEW (originally hosted by the IEEE here).  What’s great about this particular work is that it’s illustrated with a ton of screenshots.  For instance, I did not know that you could use the mouse scroll wheel, along with the Ctrl key, to quickly flip through stacked structures:

Use Ctrl + Mouse Scroll Wheel to Browse Stacked Structures

I’ve also never noticed the “Retain Wire Values” toolbar button, which does just that: it retains the last values of any wires which have been executed.  You can then apply probes while the VI is stopped and view the last states of your program.

Seriously though, if you use LabVIEW, take a few minutes and check this out.

Pure Analog Servo Control

A Standard Hobby ServoHobby servos, such as the one pictured at right, are wonderfully useful little devices.  You’ll find them moving control surfaces on model planes, in steering linkages on RC cars, and even in the feeding mechanism of an automatic ping-pong ball launcher (one of my simpler college design projects).

Anytime you need something to rotate to a specific position, think of the hobby servo.  They’re fairly low cost, and come in a variety of torque sizes, from tens to hundreds of ounce-inches.

So let’s say you’ve bought yourself a servo from Tower Hobbies (or wherever).  How are you going to control it?  Well, you could purchase a radio and receiver, but if you’re not planning on building your servo into a vehicle of some sort, that’s really overkill (and expensive).  You could program a microcontroller to generate the control signals, but that could get complicated if you’ve never worked with MCUs before.  Instead, what I’d like to discuss today is a purely analog circuit for PWM servo control.

First, let me give you a little background information.  Hobby servos are typically connected by three wires: power (red), ground (black), and signal (yellow/white).   The power and ground lines are typically hooked directly to your battery or power supply.  The signal line, however, is used to command the servo to move to a specific angular position.  This signaling is normally accomplished via pulse-width modulation (PWM).  That is, a digital pulse is sent to the servo on a routine basis (e.g. at 100Hz, or 100 times per second).  The width or duration of this pulse determines the position of the servo’s horn. For instance, a pulse width of 1ms commands a fully clockwise rotation, a width of 2ms commands a fully counter-clockwise rotation, and a width of 1.5ms will center the horn.

Now the question is, how do we generate such a signal?  Why, we simply use the following pulse-width modulator circuit (adapted from Maxim Application Note 3201):

PWM Generator SchematicAlright, so maybe you’re thinking, “Dude, that’s a big circuit.”  Well yea, it sortof is.

But then again, those three op-amps could actually all be housed inside a single 14-pin DIP/SOIC package.  And beyond that, all you need are eight resistors, one capacitor, and one potentiometer (a variable resistor).  So while this may be physically more complex than just plopping down a microcontroller, there’s no software required.

So just how does this circuit create our PWM signal?  Well let’s start with the “Integrator” section.  This group of components (R1, C1, and U1) mathematically integrate or sum the voltage wired into the left terminal of R1 (line label #5).  Put simply, the capacitor C1 is summing up this input voltage over time.  To see how this happens, let’s start by analyzing the node between R1 and C1 (label #2).  Now assuming all of our op-amps are ideal (a fair assumption in most cases), no current will enter or leave their inverting (-) and non-inverting (+) terminals.  Since the current flowing through a series connection of electrical components (R1 and C1) must be equal, we can write the following:

Integrator Equation Derivation

The first half of this formula may look familiar; it’s ohm’s law (V/R = I).  However, we’ve defined the voltage across R1 as (V5 – 2.5).  Why?  This is because the voltage at the input terminals (+ and -) of our ideal op-amp must be equal since we have a negative feedback path (a connection from output to inverting (-) terminal) through the capacitor.  And we know that the voltage at the non-inverting (+) terminal of the op-amp must be 2.5V because of the voltage divider at node #1.  Thus, since C1 is providing a feedback path for the op-amp, we can safely assume that the inverting terminal is also at 2.5V.  The second half of this equation comes from the I-V relationship for capacitors, I = C*dv/dt.

So if we solve this equation for V3, with an initial capacitor voltage of zero, we get:

Integrator Equation DerivationIf we keep the voltage V5 constant, we’re left with just an equation for a straight line.  Basically, the output V3 starts at 2.5V, then ramps linearly up/down, depending on V5, as time (t) goes on.  If left unchecked, the output of U1 would eventually hit a supply limit (either 0V or 5V). However, the second half of the above circuit, labeled “Oscillator Comparator” ensures that this does not happen by switching V5 between 0V and 5V.

Let’s take a look at U2, the second op-amp pictured above.  You’ll notice there’s no feedback path between its output and its inverting (-) terminal.  So what we’ve got is a comparator.  That is, if the voltage on its non-inverting (+) terminal is greater than that on its inverting (-) terminal, the output of U2 will be roughly 5V (our positive supply voltage). Otherwise, the output will be roughly 0V.  I say “roughly” because this op-amp (TL072) can’t operate “rail-to-rail”, which means its output can’t quite reach our supply voltages.

In order to understand this comparator a little better, let’s take a look at the point at which it switches between its high (5V) and low (0V) output.  Since the inverting terminal of U2 is fixed at 2.5V by the voltage divider at node #1, this switching must take place when node #4 passes 2.5V.  Let’s determine the voltage V3 necessary for this to occur.  To begin, I’ll equate the currents through R2 and R3 (since again, no current flows into the + terminal):

Switching Point DerivationNow don’t be confused about where that 2.5V is coming from.  This is the switching voltage for U2.  We’re not saying that U2’s non-inverting (+) terminal is fixed at 2.5V.  It’s not, because we don’t have negative feedback.  This voltage will vary based on V3 and V5.  Anyway, solving for the switching-point voltage V3 we obtain the following:

Switching Point DerivationSo we’re going to have two switching points, based on the two possible values of V5.  When V5 is 5V, V3 will be decreasing linearly, and a switch will occur at V3 = 1.325V. However, when V5 is 0V, V3 will be increasing linearly, and switching will occur at V3 = 3.675V. So this is how the oscillation happens: V3 ramps linearly in one direction until it reaches a switching threshold, at which point the integration reverses and V3 ramps backwards.  So what does this give you?  A triangle wave, as seen in green in this PSpice simulation:

PSpice Simulation (Red = Threshold, Green = Triangle Wave Oscillation, Blue = Output)

Of course, we can’t just use a triangle wave to signal our servo.  What we need now is a third comparator to generate a PWM signal using this triangle wave and a variable threshold voltage (the red line pictured above).   This is where the components around U5 come into play.  Again, since U5 has no negative feedback path, it operates as a comparator.  Thus, its output can only be 5V or 0V (roughly).  So if we feed our triangle wave into its non-inverting (+) input, and a DC threshold voltage into its inverting (-) input, what we get at the output is a square wave (the blue line) whose pulse width is inversely proportional to our threshold (i.e. a higher threshold yields a shorter pulse).

The last trick here is that we can’t just hook up a potentiometer (pot) between power and ground.  That would give us a threshold voltage variable from 0V to 5V. What we actually need is a threshold voltage that varies from about 3.2V to 3.5V, for a pulse width ranging from 1-2ms (based on the form of triangle wave shown above).  Well in order to accomplish this, I’ve placed two additional resistors (Rx and Ry) in series with the potentiometer (Rpot).  In order to determine appropriate values for these resistors, I’ll start with two voltage divider formulae which are based on the two limits of the potentiometer:

Comparator Threshold Resistance DerivationSo when the pot’s screw is turned fully clockwise, the pot’s entire 10kΩ resistance will be placed between Rx and the inverting (-) input of U5.  This will produce our maximum threshold voltage, VH.   However, when the screw is turned fully counter-clockwise, the pot will act as a short between Rx and U5, yielding our lowest threshold voltage, VL.  If we now combine and solve these two formulae, we can determine values for Rx and Ry:

Comparator Threshold Resistance DerivationNote: To determine resistances for different potentiometer values, you’d just need to replace the 10k in the first set of equations with your updated value and re-solve.

Now of course, all we really need here is a means of controlling the threshold voltage at U5’s inverting (-) terminal.  Back when I was working on my automatic ping-pong ball launcher, I wanted to use my laptop and a DAQ card to control my servo.  The DAQ card I had available at the time didn’t allow me to generate precisely-timed digital signals.  However, it did provide several analog outputs, which I could have connected directly to U5 in order to control this circuit’s pulse width.  But I didn’t know about this circuit back then, so I actually just used a microcontroller programmed to generate the appropriate signals based on an ADC input connected to my DAQ hardware.

Finally, you may also be wondering, how can I calculate the frequency of this PWM signal?  (Or maybe you’re getting sick of all these equations?)  Well, given the above formulae, it’s actually quite simple to calculate.  We just need to set the integration formula equal to the switching voltage formula, like so:

Switching Frequency DerivationWe now solve for the time t, then multiply by four (since this equation gives you the time required by one quarter of a full cycle), and invert to find the frequency:

Switching Frequency DerivationAlright, enough of these crazy formulae.  Pictures of the final circuit?  Yes, please!

Protoboard Closeup

You’ll notice that Rx is actually a series combination of three resistors, while Ry is a series combination of two resistors.  This is because I didn’t have suitable values for Rx and Ry just lying around.  Oh well, it just makes things a little messier!  Here’s the full setup:

Full Protoboard SetupFinally, here’s a screenshot of the IOBoard oscilloscope VI I used to test out my PWM circuitry.  You’ll notice that, as I mentioned earlier, the comparator’s output doesn’t quite reach 0V and 5V because these op-amps (TL072) do not have rail-to-rail outputs:

IOBoard Scope - 2ms Pulse Width

One final note on the schematic above.   The resistance R10 should have been unnecessary. I initially included it because PSpice wouldn’t run my simulation with U5’s output floating.  However, after constructing this circuit I found it necessary for reliably servo operation.  I’m not entirely sure why this was the case; perhaps the voltage levels without R10 were slightly outside of the servo’s acceptable range?  The signal on the screen certainly didn’t appear much different with or without it.  Perhaps if I had a higher frequency/resolution scope I’d see something more telling…  Oh well, it may not be an issue with your servo.

Anyway, if you have any questions on this circuit or would like to make suggestions, feel free to leave a comment.  I’d love to hear about your experience.

Also, please help yourself to my PSpice files (from the Orcad 16.0 student demo).  These were used to create the schematic shown here as well to perform simulations.

PWM Generator Schematic 2

Here’s my final bill of materials (BOM):

  • 2x TL072 Operational Amplifier
  • 1x Hitec HS-81 Servo
  • 1x 4.7uF Capacitor (Can be electrolytic, despite the slight negative voltage)
  • 1x 10kΩ Potentiometer
  • 2x 20kΩ Resistor
  • 2x 1kΩ Resistor
  • 1x 470Ω
  • 1x 119kΩ Resistor
  • 1x 166kΩ Resistor

Update (11/2/2010): It was pointed out that the TL072 may require a minimum supply voltage of 7V, so 5V could be cutting it a little close here.  Now I’ve looked through the datasheet and don’t see a specific limit mentioned, but most of the graphs do only go down to ±3.5V (which I suppose you could interpret as 7V).  Regardless, the circuit works fine with a single 5V supply, although the outputs don’t go rail-to-rail (which is normal operation), as I mentioned earlier.  The real concern with most op-amps is their upper supply limit, as you don’t want to fry anything.  Also, you might be more interested in the TL074 for this project, which contains four op-amps in one package.  I didn’t happen to have a quad op-amp lying around when I built this circuit, hence the two duals.

V5

No no, you’ve got it backwards.

A lot of things in this world just aren’t easily reversible. And no, I’m not referring to the strict definition of thermodynamic reversible processes. What I mean is that many conversions (energy, chemical, etc.) and systems cannot be readily reversed. Your hairdryer likely can’t turn heat into electricity. You can’t very well make oranges out of orange juice. And I’m pretty sure your car won’t turn carbon dioxide back into gasoline.

Converting OJ Into Oranges: It Just Can't Be Done

Motors and Generators

Now of course, some processes can be reversed. For instance, many people know that DC electric motors can also be used as generators. Such motors work because of the forces generated through the interaction of two magnetic fields. One of these fields is brought about by the flow of current through coils of wire; the other is created by permanent magnets attached within the motor housing. See this HowStuffWorks article for more details. This same DC motor can also be used as an electrical generator.

NRC Steam Turbine Driven Electrical Generator
Generators work because of Faraday’s Law of Induction, which states that “The induced electromotive force (emf) in any closed circuit is equal to the time rate of change of the magnetic flux through the circuit.” In other words, a changing magnetic flux (e.g. a moving permanent magnet) will induce an electromotive force (a voltage) in a nearby electrical circuit (e.g. the motor’s windings). This Wikipedia entry on electrical generators is quite an interesting read and includes some history of generators such as the one pictured above.

Light-Emitting Diodes (LEDs)

Optek High-Intensity LEDsNow here’s something a bit more curious. I’m sure you’ve heard of or at least seen an LED. They’re everywhere: from alarm clocks to cell phones to outdoor lighting. As their name says, they emit light. But did you know that LEDs can also be used as light sensors? It’s true! While LEDs are optimized to emit light, they are, physically, not that different from photodiodes. Thus, instead of operating them in a forward bias, you can reverse bias them just as you would a photodiode. The following schematic comes from an Altera white paper and illustrates how the same the same LED can be used as both a sensor and an emitter:

Altera LED Emitter/Detector Design SchematicThis white paper also describes how, with two pins on a CPLD, microcontroller, or FPGA, the same LED can be used as both a sensor and an emitter without rewiring. This type of dual-purpose use could result in significant cost savings for devices produced on a large scale. In addition, it’s also possible to transmit and receive data using single LEDs. At RPI and other universities, research is proceeding on high-speed data transmission using ambient LED lighting – “Smart Lighting,” it’s called. In the future, when incandescent and fluorescent lights are replaced by high-intensity LEDs, your laptop might actually connect to networks via the lights in a room. They’ll be doing double duty: lighting the room and transmitting data via high-frequency modulation. Pretty cool, right?

The Speakers are Listening

A Typical SpeakerFinally, one of the most interesting and practical reversible technologies is the speaker. Just the ordinary, magnet-and-coil cone speaker. Did you know that a speaker can be used as a microphone? Interestingly, the reason this works is precisely the same reason that DC motors can be used as generators. Normally, speakers produce sound through vibrations created using coils and magnets. Just as with the motor discussed earlier, a magnetic field is created by passing current (the audio signal) through a coil of wire. This field will cause the coil to either be pulled towards or pushed away from the permanent magnet. This motion, when done very rapidly, results in sound. Check out this HowStuffWorks article for a brilliant animated illustration of this effect as well as further details on speaker operation.

Now, the great thing about the way in which speakers operate is that it’s entirely reversible. Instead of passing a current through the coil and causing it to move, we move the coil using sound and then amplify the resulting current. Again, this works because of Faraday’s Law of Induction (see above). But how does this work practically? Well, not too bad actually. Some people say that an average speaker may sound better than a cheap microphone. If you’d like to give it a try, just find an old speaker, then wire it into the microphone jack on your stereo, laptop, etc. To test, just speak into the speaker!

So You Want to Use PWM, Eh?

PWM Waveform Captured on an OscilloscopePulse-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.

PWM Graph - 30% Duty CycleImagine 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).

Hobby Servo (Commanded via PWM)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:

Ford LED Tail Light Upgrade - Ain't that a Fancy Photo?
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:

LED PWM Frequency Comparison

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:

Pulsing Streetlights

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.

Test Setup
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:

IOBoard Test Program
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.

Test Setup Closeup
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