Big banging PWM?

Discussion in 'Embedded Systems and Microcontrollers' started by spinnaker, Oct 16, 2012.

  1. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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?
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
  3. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605

    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

    Code ( (Unknown Language)):
    1. #define PWM_MAX  99
    2. unsigned char x, PWM;
    3.  
    4. (every TMR0 interrupt):
    5. LATXbits.LED = (x < PWM);  // semi-pseudo code where the target port bit is defined
    6. if(++x > PWM_MAX) x = 0;
     
  5. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.
     
  6. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    After all this time I just realized I have "Big Banging" in the title. :).
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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
     
  8. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.
     
  9. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    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.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  11. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    988

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

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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?
     
  13. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  14. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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?
     
  15. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    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.
     
  16. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.
     
Loading...