Go Down

Topic: Due + PCA9685 LED flicker!! What's going on here!! (Read 610 times) previous topic - next topic

Tevian

Ok I spent many hours building an Aquarium LED controller for a friend. I've moved from a Nano to the Due. After many hour moving the code over and getting the Due to work with a Nextion touchscreen I thought I had it working. Now this weird problem shows it self!!!

Ok this controller is running a PCA9685 on I2C. The PWM channels ramp up from a starting time and ramp down after an off time. The ramp up worked great on both Nano and Due. However I was only testing the first channel thinking that when I expand to the other channels it would work fine. NOT AT ALL!!

I get MAD flicker as the PWM output is at its peak. The flicker is "cascading". I mean the 4th channel output has flicker from about 3300-4096 as the PWM is set. The 3rd channel has flicker from about 3800-4096. the 2nd has flicker at about 4000-4096. And the 1st channel has no flicker at all and works fine. These numbers are an estimate and I'm judging it by my eye. How ever the 4th channel defiantly flickers at around 3300 but only when send a new command to set a PWM.

So I started a simple sketch with the basic PWM library and a loop that ramps the PWM up and down. This basic code does indeed make the same flicker!!
Code: [Select]

#include <Wire.h>
#include "PCA9685.h"

PCA9685 pwmController;     

uint16_t brightness;
uint16_t fadeAmount = 5;

void setup()
{
    Wire.begin();                       
    Wire.setClock(400000);
   
    pwmController.resetDevices();       // Software resets all PCA9685 devices on Wire line
    pwmController.init(B000000);        // Address pins A5-A0 set to B000000
    pwmController.setPWMFrequency(100); // Default is 200Hz, supports 24Hz to 1526Hz             
   
    for (int i = 0; i <= 3; i++)
      {
        pwmController.setChannelPWM(i, 0);
      }
    delay(1000);
   
}
void loop()
{
 
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 4096)
    {
    fadeAmount = -fadeAmount;
    }
 
    for (int i = 0; i <= 3; i++)
      {
        pwmController.setChannelPWM(i, brightness);
      }
 
  delay(5);
}


I've tried changing the pwmController.setPWMFrequency to different values. The flicker is less visible at higher frequencies but is still there no matter the value. I've tried changing Wire.setClock to different values. The flicker is less visible at 1mhz and more visible at 100khz. I need to operate below 400khz because I also have an RTC.

Here is a short vid from my phone. You can see the LEDs flicker as the ramping crests the top values and it starts with channel 3 then 2 then 1 and 0 is not effected. 
Please discount the shutter effect!!

https://drive.google.com/open?id=189iaAK4lGDkSMqWnVSp2lbf2Wn2R03Ko

These are test LEDs running straight from the PWM pin on the PC9685. 



Any help with save my sanity  :o !!  Thx in advance!

ard_newbie



uint16_t brightness;
uint16_t fadeAmount = 5;

.............................

if (brightness <= 0 || brightness >= 4096)
    {
    fadeAmount = -fadeAmount;
    }

.............................

There is at least an issue with your variable declaration. An uint16_t is always >= 0 !

Klaus_K

Maybe this answers your question

https://community.nxp.com/message/919888


Additionally two points that are not your issue but might affect you later.

Your brightness value is not initialized. Its likely set to zero by the compiler now, but you cannot be sure.

The next part of the code looks cleverly short, but could create an issue later. If the brightness value is ever set beyond the 4096 + fadeAmount, the brightness will not get back into a valid range. Lets say it set to 5000. 5000 -5 +5 -5 +5 The same is true at the other end, but because you use unsigned it cannot go below 0.

if (brightness <= 0 || brightness >= 4096)
{
 fadeAmount = -fadeAmount;
}

Tevian

ard_newbie  ya I made it quick by copying some code from the "Fade" example from the Arduino IDE. I just needed something that would ramp the PWM value quick to see if the flickering was my code or something else. My full sketch is not done yet and is quite long. Still kinda learning to code.

Thx Klaus_K. I'll read that in a few. As stated this is not code that I'm incorporating into my project but thanks for the info.

ard_newbie

For PWM, example sketches you might consider the pwm library from antodom:

https://github.com/antodom/pwm_lib

Tevian

Wow thank you guys. Thank you Klaus_k that helped. It was indeed the phase balancing feature. I also found a discussion here.

https://electronics.stackexchange.com/questions/216707/flickering-on-pca9685-pwm-controller-with-3w-rgb-led-and-picobuck

They suggested an addition to the PCA9685 command from

Code: [Select]

PCA9685 pwmController;

To this
Code: [Select]

PCA9685 pwmController(Wire, PCA9685_PhaseBalancer_None);


Works now!!! Man I lost hours trying to figure if it was in my code. I should have stopped freaking
and starting searching. Thx again guys.

Go Up