PDA

View Full Version : PWM and fade effect



ruijc
- 29th June 2009, 19:28
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.



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

Chris Barron
- 7th July 2009, 15:27
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.



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

sayzer
- 8th July 2009, 07:45
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.



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,


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.

------------

falingtrea
- 8th July 2009, 18:03
As I understand it, your eye is a logarithmic device. So brightness changes should also be logarithmic, not linear, to be perceived correctly.

picster
- 8th July 2009, 20:12
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

Chris Barron
- 9th July 2009, 00:05
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 http://www.youtube.com/watch?v=ek8S4r7bBUU 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

Acetronics2
- 9th July 2009, 09:54
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

duncan303
- 9th July 2009, 11:57
47uf and a divider/zener?

or is that not the spirit of the thread :D


___

sougata
- 10th July 2009, 10:58
Hi,

Darrel once made a multi output soft PWM routine which is interrupt based and works as intended from a PWM signal. Search the forum....
I did a software PWM for a UPS but it is primarily in assembly.
The concept is pretty simple.

Create a timer interrupt.
On every tick increment a counter with it.
Match with your PWM value sets or resets the output.
Counter overflow, sets or resets the output.


Would run in the background

Or better though select a chip with hardware PWM.

From the PBP manual


f you want continuous PWM output and the PICmicro MCU has PWM hardware, HPWM may be used instead of PWM.

Pin is made an output just prior to pulse generation and reverts to an input after generation stops. The PWM output on a pin looks like so much garbage, not a beautiful series of square waves. A filter of some sort is necessary to turn the signal into something useful. An RC circuit can be used as a simple D/A converter:


It is better to handle the LEDs by current since the brightness (lux) is a function of the current through the chip. It is pretty easy to kill LUMILEDS with overvoltage causing current overshoots.

Good Luck