NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » PWM with multiple leds

September 22, 2009
by mcai8sh4
mcai8sh4's Avatar

As some of you may know, I've made a binary clock - displays the time in a binary format by lighting up LEDs. It's been a while since I've altered anything on this, but I feel the need to change.

Over the past couple of nights, I've been trawling through the ATMega datasheet, trying to decipher it's somewhat terse ramblings to reveal it's hidden treasure (well, I just want understand how to set up PWM), and I've managed to figure some of it out.

I can now alter the brightness of LEDs on 4 (out of six) PWM channels - PB1, PB2, PB3, PD3 (I'll get the other two channels sorted eventually).

Whilst I realize this is no great achievement (and quite unimpressive to watch) - I'm proud as hell. This is the first time I've managed to understand anything useful from the datasheet.

But I digress...

I want to now try my new found knowledge in a project - which brings be back to my clock.

One of the things that concerns me is the power consumption of having those LEDs lit all the time. So my thought is thus : If I use PWM to dim the LEDs, will it increase the battery life (50% duty cycle = nominally 50% increase in battery life?) or will it make no real difference?

If it will save the battery then my thinking is PWM oscillates a pin high and low very fast... on my clock, the led's all share a common ground (the blue rail) so when the PWM is low it will act like a ground - lighting my LED.

This led to another thought - am I gonna break something if all my leds are connected to one PWM pin. Can the chip handle sinking that much?

The most LEDs on at any one time would be 8 : example 11:31 = 1011 11111

Ideas/suggestions/insults/jokes welcomed.

Thanks, Steve

September 22, 2009
by rusirius
rusirius's Avatar

Steve, I'm not an expert by any means when it comes to electronics, so take this with a grain of salt, or at least wait for someone else to back it up.. ;)

 With most things (i.e. motors, incandescent light bulbs, etc) you have what's known as an "inrush current"... Basically, when turning something on, the current will spike to really abnormally high levels, and then settle in to normal once it's running...  A motor for example is caused by the back EMF, a light bulb by the resistance in the filiment...

 I'm not sure if that same principle applies to LED's or not...  I'm sure it would be on a much smaller scale if so... Still... Here's my thoughts...

 Between any inrush that might exist, combined with what I can only guess is an increase of current required to operate the PWM on the microcontroller, I'm guessing that the end result would be a small gain if any...

 In fact, a much simplier way (I know, sometimes simple isn't "fun") is simply to use a current limiting resistor in series with the LED...  Technically, even if you don't have any resistors currently in series, you actually do... Thanks to Humberto for explaining this to me recently, but the MCU actually has about 30 ohms of resistance on the I/O lines...  If you're using both sides for the LED's then that's 60 Ohms...  That's generally in a decent range for an LED... It's overdriving it a bit, but not terribly so...

 You can calculate the required resistance for and LED using R = (Vs - Vf) / If.  Given a 5v supply and an average of 2V drop across a standard LED, you could calculate the correct resistance for a "normal" 20mA current... Which would be 3 / .02, or 150 Ohms...  Subtract the roughly 60 you have from the MCU and you have 90, which means you would want a 100Ohm resistor in series...

 Now if you wanted to cut the drive in half, you simply do the same thing...  Let's say you now want to shoot for 10mA...  So 3 / .01 = 300 Ohm, subtract the 60 "built in" and you get 240... Thus a 270Ohm resistor... (or 220, but I just have a habit of erring on the side of "less current"...)

 Doing this definately should decrease the amount of drain... You'll certainly be pulling less current from the battery, which means more battery life...

 You can also do a neat trick...  I think... It works in my head anyway...  If you calculate for small current... Let's say 10mA...  So you set up an I/O with a 270Ohm resistor...  Following that you place a diode...  Then you set up another PIN with another 270 Ohm resistor and another diode...  These two diodes you tie together and then go to your LED, which then goes to ground...  In theory (at least in my head) this should mean that when EITHER pin is driven high, you should get 300 Ohms, or 10mA driving the LED... If you drive BOTH pins high, the resistors should act as though in parallel bringing the total resistance down to 150 Ohms, and thus driving the LED at 20mA...  So now you wire in a cad sensor and drive both with it's "light" and only one when it's "dark".. So now when it's dark and you don't need much light to see it, you'll be saving energy.. and not blinding yourself all at the same time...

Actually I just thought of something... I believe the diodes are going to have a voltage drop across them too, so that would have to be factored in to the calculations...

OR... As I recall reading about the AVR, You can "float" a pin when not in use.. That means it won't matter if it see's the 5v from the other pin, it won't sink or source any current... In that case you can eliminate the diodes altogether and won't have to mess with that.. ;) There.. problem solved... ;)

Post a Reply

Please log in to post a reply.

Did you know that you can adjust most wire strippers to make it easier to strip insulation faster? Learn more...