RGB LED PWM Matrix flickers

Hey All,

I have been experimenting with with some some RGB leds in a matrix circuit. I want to use pulse width modulation to control the brightness of each colour but I am getting a lot of flickering in the light. I have a loop to test the circuit. If I don't use PWM, all the leds light up as expected and there is no flickering:

for (int c = 0; c < columns; c++){
for(int r = 0; r < rows; r++) {
//turn an LED on
digitalWrite(row_pins[r],LOW);
digitalWrite(col_pins

,HIGH);
          delayMicroseconds(100);
     
          //turn the LED off
          digitalWrite(col_pins[c],LOW);
          digitalWrite(row_pins[r],HIGH);               
      }
  }

But if I try to use PWM to achieve the same effect I get a lot of flickering in the leds:

for (int c = 0; c < columns; c++){
      for(int r = 0; r < rows; r++) {
          //turn an LED on
          digitalWrite(row_pins[r],LOW);
          analogWrite(col_pins[c],255);
          delayMicroseconds(100);
     
          //turn the LED off
          analogWrite(col_pins[c],0);
          digitalWrite(row_pins[r],HIGH);               
      }
  }

Does anyone know how to solve this so that I can use a PWM pulse to turn the columns lines on without flickering leds?

Thanks!

You are getting strobeing between the refresh rate of the matrix and the output frequency of the PWM.

You could try altering the frequency of both or better still synchronise the refresh of the matrix to the PWM frequency.

Probably the simplest method though is to apply some smoothing capacitors to the PWM outputs to give a DC output rather than a PWM. This has the disadvantage that some of the lower intensities won't work as the average voltage will be below the turn on voltage of the LED.

Avoid Arduino's PWM and use a longer on-delay (1-2mS). if your timing can afford it. The eye needs a fairly long time to register lights.

Be careful too that most of the time your LEDs are reverse-biased, and most lists their absolute max as 5-volts!

By RGB I take it you mean discrete LEDs each with a different color? And each on a unique r-c?

Grumpy, i'll try the capacitor solution. How would I synchronize the refresh of the matrix to the PWM frequency? Can I alter the PWM frequency without changing the duty cycle and vise versa.
I am using RGB LEDS, they are each 3 LEDS in one package with a common cathod. I need the PWM for colour mixing.

The built-in PWM runs at about 500Hz, so you'll need 2mS to see a complete cycle.

I built a 6 x 5 LED matrix and I was getting good results with delays of a minimum of 500uS between transitions. I was only using red LEDs but it should work for RGB as well.

I'm using a 6x6 matrix and I get flickering if my delay is any longer than 1000 microseconds. Also, if I use delayMicroseconds(1000) there is no flickering but if I use delay(1) they flicker like crazy.... aren't those two instructions supposed to do the ssame thing?

You're seeing the effects of interrupts - which delayMicroseconds() turns off.

I generally use my own PWM - by a delayMicroseconds(500) (or less if flickering is too noticeable) after setting each individual LED, before turning it off. Then I always have it nested inside a loop which repeats the code for all LEDs for 100000 uSecs.