PWM and fade effect


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1

    Default PWM and fade effect

    Hi all,

    I have a question regarding PWM.
    I'm using PWM to fade in/out a LED on a 12F675 with the code below.

    Code:
    For steps=255 TO 1 STEP -1
    pwm white,steps,2
    Next
    I'm looking for a linear result but is not working.
    When the led is almost off the speed increases ( which is obvious since the pic is using less bits to count ).

    My question is, is there a workarround to minimize this effect and make the fade out/in more linear ?

    Thanks

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ruijc View Post
    Hi all,

    I have a question regarding PWM.
    I'm using PWM to fade in/out a LED on a 12F675 with the code below.

    Code:
    For steps=255 TO 1 STEP -1
    pwm white,steps,2
    Next
    I'm looking for a linear result but is not working.
    When the led is almost off the speed increases ( which is obvious since the pic is using less bits to count ).

    My question is, is there a workarround to minimize this effect and make the fade out/in more linear ?

    Thanks
    I think the code is producing a linear change in PWM duty cycle. What you're perceiving as being non-linearity is probably a result of the fact that LED (and most lamps in general) brightness do not change linearly with a change in voltage.

    You should actually be trying to create a logarithmic rate of change in order to produce a linear change of brightness

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ruijc View Post
    Hi all,

    I have a question regarding PWM.
    I'm using PWM to fade in/out a LED on a 12F675 with the code below.

    Code:
    For steps=255 TO 1 STEP -1
    pwm white,steps,2
    Next
    I'm looking for a linear result but is not working.
    When the led is almost off the speed increases ( which is obvious since the pic is using less bits to count ).

    My question is, is there a workarround to minimize this effect and make the fade out/in more linear ?

    Thanks
    Have your PWM command inside a TIMER overflow.

    Like,
    Code:
    For steps=255 TO 1 STEP -1
    TMR1IF = 0
    WHILE TMR1IF = 0  
     pwm white,steps,2
    WEND
    Next
    Thus, you get the same time of speed for each PWM step.

    ------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4


    Did you find this post helpful? Yes | No

    Default

    As I understand it, your eye is a logarithmic device. So brightness changes should also be logarithmic, not linear, to be perceived correctly.
    Tim Barr

  5. #5


    Did you find this post helpful? Yes | No

    Default duty cycle

    I successfully changed apparent LED brightness just using a linear on/off duty cycle change in a loop. I believe your eye isn't registering "actual brightness" but "average brightness" (a function of time) instead, so I don't believe it's logarithmic. I would guess that the actual brightness of the LED is the same regardless of duty cycle, but the eye is doing the averaging (this could be confirmed with a scope and a phototransistor).

    Actually I'm really curious about this now.

    picster

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by picster View Post
    I successfully changed apparent LED brightness just using a linear on/off duty cycle change in a loop. I believe your eye isn't registering "actual brightness" but "average brightness" (a function of time) instead, so I don't believe it's logarithmic. I would guess that the actual brightness of the LED is the same regardless of duty cycle, but the eye is doing the averaging (this could be confirmed with a scope and a phototransistor).

    Actually I'm really curious about this now.

    picster

    Yes it's curious ;-)

    The eye is averaging the output brightness from the led, while at the same time it is also working on the principle of persitence of vision, POV, and the two effects combined mean there will be little hope of seeing a true linear change. It may be achievable in the long run but as soon as the observer changes their head position, or look at the led from a different angle, the flicker will be more noticeable and then the effect will not be accurate, or linear, again.

    I build display devices which regularly use crossfading effects between digits and wether I use filament bulbs, neon nixie tubes or leds or VFD's, a linear voltage change always produces a non-linear change in brightness. See Youtube video as an example.

    Another thing to consider too, is that as your eye sees a light source which has an increasing brightness it reacts by closing down the iris and therefore it 'spoils' the effect you thought you were going to see, but we all experience this effect 'similarly' and are accustomed to it.

    The code posted in the first message should run 'linearly' no matter what the PWM duty cycle is set at, because even with a small on time the PWM routine should be padding the rest of the time period with a respective amount of offtime to produce a fixed PWM period

    Chris

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink

    Hi,

    May be you should add the PWM output, without any filtering it is totally "random" ( short way of saying it ...) as a signal ... so ...

    is it really the correct way to achieve a decent "linear" fading ... ???

    ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 00:55
  2. 16f876a Pwm
    By Hainkm in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th December 2007, 17:33
  3. LED fade without PWM command
    By Nick in forum mel PIC BASIC
    Replies: 7
    Last Post: - 29th June 2005, 20:56
  4. Help with PWM and duty and integers
    By JDM160 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th March 2005, 06:03
  5. Fade out an LED using PWM?
    By RossW in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 6th August 2004, 19:59

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts