Author Options:

PWM fade Answered

I am using micro controller in a project that has a set of functions (API) that can be used for a plethora of things.. (just up to you to program it that way) LOL

It has a PWM control... which i am trying to use to FADE in (and out eventually) a high powered LED (LUX III to be specific).. the problem I having is that at a certain point (say at 50-60%) of full brightness I see a VISUAL jump in brightness..

Im trying to get it to be smooth the whole way through to 100%

more or less the code is like:


which I then wait 10 mSecs and loop it again...

I have tried to put in some conditional checking to check to see if the current level of PWM is at a certain threshold..and then only loop it again after a longer delay (say 300mSecs) or something..

I tried to search.. ut Im not sure on how to get a CONSISTANTLY smooth fade...ot smooth to say 50-60%..then a visual jump in brightness and then smooth to 100%..

any ideas or links on the subject are appreciated.




Could you measure the average voltage coming out of the pwm pin some how ie true rms voltmeter or capacitor and regular voltmeter. This will tell you if the voltage coming out of the the pwm is changing in a linear fashion. If you are generating a smoothly ramping voltage then you will have to investigate the LED characteristics. LEDs like all diodes have a "knee voltage" that is they start to conduct dramatically after a certain voltage. Check LED characteristic curves. If you combine this with possible non linearities in human vision I think it is almost certain that you will need some kind of look up table solution. Store a range of empirically determined values in an array and use a loop to feed them to the pwm statement.

ok..curves is what Im looking for... but Im not sure if the chip API Im using allows or has support for arrays to loop through.. I will have to look at the datasheet...and/or contact the creator as well to find out.. how can I go about getting the correct values I need to put intot he array to loop through? trial and error? thanks


9 years ago

bump.... anybody? All these members here..'someone' has to have some advice? LOL

You may be more successful controlling to PWM yourself with outputs and delays. What else is the micro controller doing? If the program runs in a large loop regularly, you could add in an extra function to control PWM.

Im not sure Im following.. (sorry new to all this)...

the micro controller has specific PWM pins..... and the API has set functions to set the PWM.. I can fade in & out... however when I get to a certain point in fading in a high powered LED, such as a LUXIII or a P4 LED.. I see a visual JUMP in brightness as a certain point..

when asking around... I was told I need to code a 'curve' type stepping to make things more 'smooth' during my fade in/out..

this is the chip Im using:


and here is a link to the documentation for the API: (page 22)



Looking at the pin-out image of the ORION, the PWM pins are multi-purpose, each able to be an output. If you are unable to do what you want with the PWM function of the chip, don't use it. Write your own function to handle the on/off time of the output. With this you cold either pre-define the on/off times within a lookup table, or find an equation which will define the on/off time. The second will be harder to implement, but it will be much smaller.

AGain.. I guess Im not following.. (sorry).. how can I 'not' use the API that the chip works with? Wasnt it created/falshed with that HEX to only work with the API that is in the documentation? here is a SMALL portion of the code I am using.. (this portion only does the PWM fade in/out)... the rest of the code handles other stuff (blinking accent leds..and sound control..plus accelerometer control...etc) example code: //code removed due to formatting restrictions of post/forum this 'does' fade in & out... however.. at around (say) 50% or so.. I see a JUMP in brightness... instead of smooth, gradual fade in to full brightness... make sense? Im asking for a generic solution on how people gradually fade in their high powered LEDs... (doesnt have to be specific to this ORION chip)... but Im sure others have had to use a 'curve' type stepping routine in their code before... no? maybe I was led down the wrong path? the chip works fine..and will do what it is programmed to do I assume.. Thanks

Generic PWM Solution

for(mark=0; mark++; mark == 100){ //Start for loop
space = 100 - mark; //Calculate the off time
out(0) = 1; //Turn on the output(0)
wait(mark); //Stay on for mark-time
out(0) = 0; //Turn off the output(0)
wait(space); //Stay off for the space-time
} //End for loop

This will increase from off to on at a rate of one brightness level per 0.1seconds. To make it change from on to off, invert the for statement.

i.e. for(mark = 100; mark--; mark == 0){


9 years ago

I found that most of the high levels of LED PWM look about the same to me, a 50% duty cycle looks a lot brighter than "half brightness". I made a more continuous-looking PWM on my arduino by using a square-root function (ie rather than fading between 0-255 increasing by 1 each time, I used 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 255). This appears more linear to human eyes but a bit jumpier because there are only 17 values. I'm not quite sure I get your problem- while fading you say it appears to suddenly get brighter at one point? Try slowing down the fade so that it takes 5-10 seconds and watch to see if it still happens. If so, check your code to make sure it's not skipping any values, a linear PWM shouldn't do this.


Reply 9 years ago

While I havent visited this problem in a while (this post was not replied to/seen for a while).. last I rememeber.. when I traced/output the values of the PWM increments..that were 'not' skipping anything..

I 'do' believe this to be a 'human eye' thing..where I see a visual jump in brightness at a certain PWM value..and not much more visual increase in brightness

example of code (linked to as site wont alow posting the code)


I'm not familiar with your APL, but could this be a integer range / rollover error?

There's no conditional to check if the PWM variable =< 0, so an integer would just roll over. I.E., if byte A=0, A-1 yields a value of 255.