Big banging PWM?

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
I designed and built a solar light controller. Back when I designed the controller I did not have the forethought of using one one my 4 PWM pins as the control pin for the light.

Now I want to at least add PWM to conserve battery life. I really don't want to redesign and re-fabricate the whole board. It is working great as is.

So I figured I could big bang the PWM for the current pin. I figure I don't need any kind of accuracy, I probably don't need to worry about a frequency. I just need to toggle the light off and on. Is this a good assumption?

The question is how to do the PWM.

One way I thought was to have a bit map.

1010101010101010

would give me 50% duty cycle.

In my main loop I would simply look at each bit one at a time, and either turn the light off or on.

So would
1001001001001001

give me a 37.5 duty cycle?

1000100010001000 = 25% duty cycle

and so on?
 

THE_RB

Joined Feb 11, 2008
5,438
I've done it with a fast TMR0 interrupt;

(every TMR0 interrupt);
if(x==0) LED on
if(x==PWM) LED off
x++
if(x==100) x=0

x will count from 0-99, giving you 100 levels of PWM brightness. The PWM frequency will be int frequency / 100, so for example a 20MHz PIC the TMR0 runs at 5MHz, int occurs every 256 ticks and PWM takes 100 ints, so;
PWM freq = 5MHz /256 /100 = 195 Hz.
 

ErnieM

Joined Apr 24, 2011
8,415
I've done it with a fast TMR0 interrupt;

(every TMR0 interrupt);
if(x==0) LED on
if(x==PWM) LED off
x++
if(x==100) x=0

x will count from 0-99, giving you 100 levels of PWM brightness. The PWM frequency will be int frequency / 100, so for example a 20MHz PIC the TMR0 runs at 5MHz, int occurs every 256 ticks and PWM takes 100 ints, so;
PWM freq = 5MHz /256 /100 = 195 Hz.

There's a glitch in the output for PWM = 0.

This would work to give NO output for a PWM = 0
over the range 0 <= PWM <= PWM_MAX

Rich (BB code):
#define PWM_MAX  99
unsigned char x, PWM;

(every TMR0 interrupt):
LATXbits.LED = (x < PWM);  // semi-pseudo code where the target port bit is defined
if(++x > PWM_MAX) x = 0;
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Thanks to the both of you. If I understand correctly basically I just set the output to 0 every X times.

So I am turning the light off for a very brief period of time more and more frequently as I increase PWM_MAX?

I assume this will achieve my intent of conserving battery power?


What about my idea of using a bitmap? I would think that more evenly spreads out when the light is off. Or am I over thinking it?

The methods you two posted is very simple. Is it that it occurs so fast that spreading the off time out really is not going to make a difference when it comes to your eye?

From a quick test with a single LED, that seems to be the case.
 

ErnieM

Joined Apr 24, 2011
8,415
Well, one of the products I developed was basically a LED light dimmer. It ran the LED at a constant current under a 10 bit PWM control where the on time went from about 10/1023 to 1023/1023. The rate was around 200Hz, and no matter how fast I shook the thing I could not perceive the blinking, and normally I am very sensitive to it. I startle- jump all the time when my eyes scan the road and I think someone's LED taillight are changing.

Do note that by adjusting the on time like this I was able to do a precise control of the perceived brightness, as the final product was to get a LED to mimic the same intensity as a voltage-controlled (dimable) incandescent light bulb it was intended to replace.

So while you may save some power by switching it, you will also loose brightness.

TANSTAFL
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Yes I realize I will alter the brightness. What I was thinking is having a "power saver" mode. As the battery gets low it goes into power saver mode.
 

John P

Joined Oct 14, 2008
2,061
If conserving battery power is important, you should use an inductor instead of the resistor in series with the LED, with a few extra components (at least a diode) to create a switching power supply. That way you'd turn more of the energy into light during each cycle, so your PWM duty cycle could be lower for a given perceived level of brightness.
 

ErnieM

Joined Apr 24, 2011
8,415
The ultimate power savings is to pulse the LED on for a brief amount of time, then wait several seconds. You will be amazed at how quick you can turn a LED on and still see it from across the room, in normal lighting no less.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
If conserving battery power is important, you should use an inductor instead of the resistor in series with the LED, with a few extra components (at least a diode) to create a switching power supply. That way you'd turn more of the energy into light during each cycle, so your PWM duty cycle could be lower for a given perceived level of brightness.

Already covered. I have a buck puck driving the LEDs.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
The ultimate power savings is to pulse the LED on for a brief amount of time, then wait several seconds. You will be amazed at how quick you can turn a LED on and still see it from across the room, in normal lighting no less.
Not sure I understand. Are you saying to turn it off just every few seconds? Or pulse it, leave it on full for several seconds then pulse it again?
 

ErnieM

Joined Apr 24, 2011
8,415
Leave it OFF for several seconds, then turn it ON for just a fraction of a second.

Exactly how long you leave it off is a subjective decision based on how long you think someone will glance at it to make sure it is on.

I wish I had written it down (maybe I did but that was 4 jobs ago and I left behind my lab books) but it was maybe 1 or 10 milliseconds was all that was needed to see it as plainly as being on 1/10 of a second.

We were investigating a battery powered device and wanted to put in a low battery indicator light, and did not want to waste what little juice was left there.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Leave it OFF for several seconds, then turn it ON for just a fraction of a second.
Sorry still confused. If I leave it on for just a fraction of a second, and the turn it off for several seconds, how will the viewer see the light?
 

John P

Joined Oct 14, 2008
2,061
It's not totally nutty. My smoke detectors do this--to show that they're alive, they blink a light every few seconds. But there it's so infrequent that you pretty much have to be watching to see the light, and I think what they're doing isn't replacing what an LED that's always on would do for you, but they're letting you check the unit if you're willing to look at it.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
It's not totally nutty. My smoke detectors do this--to show that they're alive, they blink a light every few seconds. But there it's so infrequent that you pretty much have to be watching to see the light, and I think what they're doing isn't replacing what an LED that's always on would do for you, but they're letting you check the unit if you're willing to look at it.
Oh OK I see. Sort of like a status indicator.

No this is for a garden spot light and needs to be on on the time (at night). It lights the entry sign to our community.

One thing I considered was to have a light sensor so it would light when the headlights of a car hit it but not sure how reliable that would be since I can't have sensors on both sides of the road.

Another is a motion sensor but I am guessing by the time the car came into range of the sensor, the sign would only be visible for a brief period until the car moved on.
 
Top