Timers and Flickering LEDs!
Recently back to work on a 12V RGB LED colour controller that I haven’t worked on in a while as a break from study (actually, read as avoidance of doing any study for a day). After finally finding the current version of the code (that was an ordeal and a half – had the newer copy in a different folder). I had gotten basic colour selection, brightness control and a seven colour strobe sequence working. Now it was time to do the rest of the colours and get the ‘colour wheel’ / fade working. Easy. Well, I did a complete re-write of the code, and changed to colour mapping from hard coded RGB to using HSI, which I will talk about in another post. In a nutshell, it let me separate the colour I wanted to set from the brightness or intensity, and as a result, I can easy just set the colour, and change the brightness – and it will re-calculate the new colour codes automatically.
Also made the colour wheel exceptionally easy. So, moving from the small 9 led test strip to the full length 3m ceiling mounted strip and the moment of truth. Switched it on, and chose white. Grea…. aw… what the flicker? It’s pulsing… and quite noticably. So, is there a glitch in the code, or fluctuation of the power supply meaning I really do need to put some filter caps on it. Try some of the other colours… they’re all fine… just white, but some combinations show the flicker also.
Back to google, and a general search for ‘rgb led arduino flicker’ and ‘rgb led arduino pwm flicker’. I suspected the issue was related to the particular pwm duty cycle, so that’s why I looked for PWM related results as well. I saw a few posts which suggested the original posters problems were power related, but then I saw two posts by a chap who has done some great articles on Arduino stuff in the past. And he was having the same problem. He said he though some of his trouble was power supply related, but when he tested the output of the PWM control signals for the LEDs, he noticed that they weren’t what they should have been… and one channel was going in and out of phase… and visibly so! Great… so I wasn’t my imagination. And in his follow-up post, he had the solution… just one line of code in the setup function, and the PWM signal to the three pins in use would be in perfect sync! Eureka! The miraculous line of code is as follows:
Do I hear you ask “What does it do?” Excellent question. In a nutshell, it changes the Timer 1 setting from Phase Correct PWM to Fast PWM, which is what Timer 0 is set to. Although Phase Correct PWM may be the better quality / higher accuracy PWM mode, due to the fact that we are both using RGB leds that are connected to pins controlled by Timer 0 (5 & 6) & Timer 1 (9 & 10), they need to be set to the same mode. Otherwise, the difference in operation will result in a visible flicker. For a much clearer and detailed explanation, please check out Jean-Claude’s postings which I linked above.
After adding just this one line of code, I can happily report that the colour controller is now running without a single flicker. Now I just have to fine tune the colours and add the final touches!