Go Down

Topic: Need more control over PWM for led fade. (Read 10581 times) previous topic - next topic

mactsk

Hi, i am doing a 3 RGB led lamp, the lamp fades up in a random color when the light in the room goes out, then shifts to a second random color, and after a prior of time it fades to off.
All the 3 leds shows the same color.
This is all working well, no problems  :D

The problem is that in the lowest range of the fade out, form (i guess) 20 to 0, the steps are clearly visible and disturbing, in particular the "brightness 1" is too bright, so when it switches to 0 the step in the dark is highly noticeable.
Part of the problem is that i use a HSB color library i found that uses a number from 0 to 99 for brightness, so i have less steps than direct PWM control, however 100 steps could be good.

Sure i can add a resistor on the Leds to have a dimmer "brightness 1", but this would also cut off on the maximum brightness.
Someone have an idea of how can i fix this?

Tnx

johnwasser

You could put in a 100-byte mapping table to get the taper you like.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

mactsk

with a mapping table (if i'm correct) i could change the linearity of the first 20-30 steps, that may be an improvement, but the biggest problem is the on/off effect of the steps 0/1, because 1 is too bright...

Grumpy_Mike

It looks like the 8 bit resoloution on the PWM is a bit low for you. The only soloution is to go for more bits of resoloution on the PWM and that will involve using an external chip to provide it. The TLC5940 is a popular choice, that shives you 12 bit resoloution.

fungus


It looks like the 8 bit resoloution on the PWM is a bit low for you. The only soloution is to go for more bits of resoloution on the PWM and that will involve using an external chip to provide it. The TLC5940 is a popular choice, that shives you 12 bit resoloution.


Even 12 bits isn't really enough. 16 bits is noticeably better. You can do it in software.
Advanced Arduino

Grumpy_Mike

Quote
You can do it in software.

What! 16 bit PWM in software? That is like 65k refreshes in 20 mS.

mactsk

Is it possible to do 16bit PWM in software?
I planned to make the final project on a 328 with internal 8mhz clock, maybe i should get back to the external clock XD
The minimum duty cycle of 12/16 bit PWM is less than 8bit PWM?

TLC5940 seems interesting, it seems to be also a 16bit resolution version, but the package is not trough hole  :(

Magicj


TLC5940 seems interesting, it seems to be also a 16bit resolution version, but the package is not trough hole  :(


The chips I have are through hole versions ??

You might also want to consider the PCA9685 - Adafruit make a nice board, albeit a bit expensive http://www.adafruit.com/products/815

fungus


Is it possible to do 16bit PWM in software?


Of course!


TLC5940 seems interesting, it seems to be also a 16bit resolution version, but the package is not trough hole  :(


TI does make a 16 bit PWM chip. I don't remember the number but it's not a TLC5940.
Advanced Arduino

Paul__B


What! 16 bit PWM in software? That is like 65k refreshes in 20 mS.


Well, no, it isn't, because you would change the timer value on the fly rather than polling for it.

Actually, the point is that you only need this resolution for the dimmest part of the cycle, so what you actually do, is to use PWM for 1 mS, and then turn the LED off for 19 - or adjust these proportions as necessary.

johnwasser

If you only need 1 or 2 channels of 16-bit PWM you can use Timer1.  At 16 MHz your PWM can go at 244 Hz.  I guess at 8 MHz it would be 122 Hz which should still be plenty.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

mactsk

I need 3 PWM's since is a RGB lamp.
Is there a sample code or library to use higher frequency PWM?
it can be done on every pin?

johnwasser


Is there a sample code or library to use higher frequency PWM?
It can be done on every pin?


No, it can only be done on on pins associated with an Output Compare Register.  Each of the three Timers has two OCR's so that is why the Arduino UNO only has six PWM pins.  And only one of those timers is a 16-bit timer so four of those outputs are limited to 8-bit PWM which you say is inadequate.  You might want to switch to an Arduino that has a processor more 16-bit PWM outputs.  The Arduino Mega should almost certainly do the job.

The Arduino Leonardo chip (ATmega32u4) has two 16-bit timers and four  PWM outputs with up to 16-bit resolution.  It is possible that some of those outputs are not connected to Arduino pins or share a pin with something you need but it might be worth a look. It also has six PWM channels capable of 11-bit resolution so that might be enough.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

enanthate


Part of the problem is that i use a HSB color library i found that uses a number from 0 to 99 for brightness, so i have less steps than direct PWM control, however 100 steps could be good.


There are some good suggestions here, but have you tried changing this to 0-255 first to get smaller steps? How about not dimming to 0 at all, but stopping at 1 instead?

Just my thoughts, a temporary solution if you'ld like.

Grumpy_Mike



What! 16 bit PWM in software? That is like 65k refreshes in 20 mS.


Well, no, it isn't, because you would change the timer value on the fly rather than polling for it.

Actually, the point is that you only need this resolution for the dimmest part of the cycle, so what you actually do, is to use PWM for 1 mS, and then turn the LED off for 19 - or adjust these proportions as necessary.


That is my point. You can not have true 16 bit software PWM only a fudge.

Go Up