Go Down

Topic: Non Linear Fading (Read 2604 times) previous topic - next topic


Hey Guys, It's been a while.
I just have a quick question to draw on the vast collective experience of the forums.

I want to do PWM fading (which already works fine) but i want a non linear fade that ramps up and goes faster towards the end (cubic, logarithmic, exponential, who cares). I found something that i thought would help; the tween library
Although, it seems to be a little... much, and outdated.

All that aside, if anyone just has suggestions on a nice, clean and simple way to accomplish such a thing with my own homebrew code, please share your ideas!

Thanks for reading.



This is a very similar problem to the ramp up / ramp down for stepping motors. Basically instead of making the time to the next change a fixed interval make it one you calculate from a fixed value plus (or minus) the number of steps you have made in the fade, times a constant.
Can you hack that or do you want more of a pointer?


I think i get what you're saying.

currently i have something like:

for(int i = 0; i < 255; i++){
  analogWrite(pin, i);

and i should change it to:

for(int i = 0; i < 255; ){
  analogWrite(pin, i);
  i = (i + i + 1) * 0.5;

what do you think?


Yes that's one way, changing the increment in the value written to the PWM pin. If you actually ran that code it would be too fast so you would have to put a delay in it. Therefore instead of changing the increment you could change the delay. Or you could do both if you could get your head round it.  ;)


Feb 02, 2009, 01:09 pm Last Edit: Feb 02, 2009, 01:10 pm by x1dude Reason: 1
Ahh, you were talking about the delay.
I wrote that code just for that post to be as simple as possible, so i sort of forgot the delay.
But now i totally see where you're coming from, and changing the delay actually sounds superior since the PWM still steps through each level.

Speaking of levels, is there any way to get more PWM resolution? say 1024 levels instead of 255? You would need some kind of external chip to do it for you, a DAC of course. Any suggestions for that? (3 channels would be nice)

Anyway, Thankyou greatly Grumpy Mike, you've helped me out yet again! You just do such a great job here...


is there any way to get more PWM resolution?

No that's built into the chip. The only way would be to use an external PWM driver like the TLC9450. That gives you 4096 levels, however I would be surprised if you noticed the difference when talking about an LED. The eye can only distinguish about 80 levels of brightness in an image.


Yeah, i know there is a point of diminishing returns, but at level 1 PWM the led is actually brighter than i want it to be, so i was looking for a way to make that level... less. Hence the external chip.

I did a quick search on that TLC9450 on farnell but couldnt pull anything up. Anythign in particular i should be searching for? I've never bought a DAC before.

One more thing (sorry) i know there is a map function on the arduino, but how can i map one range to another (wait for it) non linearly?


TLC9450 on farnell but couldnt pull anything up.

Sorry I meant a TLC5940

i was looking for a way to make that level... less

Use a bigger resistor in line with the LED.

how can i map one range to another (wait for it) non linearly?



Thanks yet again Grumpy Mike.

That Fscale looks like exactly what i was after!

Unfortunatly, i cant just use a resistor since im actually controlling a MOSFET which does the heavy switching for a few Amps of leds.
Maybe i could use a resistor and capacitor to the signal in of the MOSFET to reduce the lowest level or rather, smooth its transition. (it is a visible step from about 0-5 on the PWM range) or would the MOSFET not like that?


Do you not have any current limiting resistors at all?

Don't do that with a FET please.

The other alternative is to reduce the voltage your FET is switching, that reduces the current.


Are you talking about a current limiting resistor from arduino to the signal pin of the MOSFET? If so, why? I thought the input resistance was huge.
If you mean on the output, I'm actually running some LED strips which are already set up to take 12V raw, so the MOSFET just switches the power supply directly to the strips. Is that all ok?

Also, am i right in saying that the MOSFET is either on or off? if i put 1V on the input pin (they are logic level MOSFET's by the way) they will be off, right?


I mean the output.

set up to take 12V raw,

So if you feed them with 10V they will be dimmer.

Also, am i right in saying that the MOSFET is either on or off?

Not quite, a FET has a threshold voltage but there is also a liner region that can half turn a FET on. Even with so called logic FETs. If you operate the FET in this mode it will have a large current down it with a largish voltage across it and you will exceed the power dissipation of the FET.

Go Up