Electronic – arduino – How to stop the flicker of LEDs

arduinoledrgb

Recently I started messing around with controlling RGB leds with the Arduino for a project I'm building, so I built the following schematic:

schematic

simulate this circuit – Schematic created using CircuitLab

Essentially what the code does in the Arduino is that I have a 3D array as follows:

int paterns [][4][4] = {{{255,0,255,255},{255,0,51,255},{255,255,51,255},{255,255,0,51}}};

where the first dimension contains arrays that contain 4 sub-arrays.
The second layer array contains 4 int values, the first is intensity (brightness) of the led, and the other three are Red, Green, Blue respectively. This way, when I want to color the 4 LEDs, I access the main array and Iget the 2D array from it, which contains brightness, red, green and blue values for each of the 4 leds.

Then, these values are writen using the analogWrite function to each pin respectively. Brightness is essentially controlling the transistor as a variable resistor.

Now the problem is that after creating the new circuit (LEDs are connected with solid core copper wires spaced ~9cm away from each other as seen in the diagram), the LEDs appear to flicker a lot in certain colors. For instance, when the following is pulled from the array:

{{255,255,255,0},{255,153,153,0},{255,51,51,255},{255,0,255,0}}

the yellow-ish LEDs flash a lot, whereas the blue and green are perfectly stable, and as far as I can tell the red diode is the one causing the noticable flicker. Having said that, I disconnected the red channel, and although the colors are altered, they only flickered very very little. In fact it was so little that you must pay close attention to the diode to notice it.

Here is the full code:

int oneGround = 11, twoGround = 13, threeGround = 12,fourGround = 10;
int red = 9, green = 8, blue = 7;
int dela = 100;

int paterns [][4][4] = 
{{{255,255,102,102},{255,153,51,0},{255,255,255,0},{255,204,255,0}},
{{255,0,255,51},{255,0,255,204},{255,51,255,255},{255,0,204,204}},
{{255,255,255,255},{255,255,0,0},{255,204,0,204},{255,153,0,153}},
{{255,102,0,102},{255,102,102,0},{255,0,153,102},{255,0,255,51}},
{{255,0,153,51},{255,10,231,84},{255,10,231,84},{255,66,120,84}},
{{255,66,120,84},{255,66,41,255},{255,255,143,17},{255,30,186,237}},
{{255,153,0,204},{255,0,0,255},{255,204,51,0},{255,0,102,102}},
{{255,153,255,51},{255,153,153,0},{255,153,153,0},{255,153,153,0}},
{{255,153,153,0},{255,0,204,204},{255,0,204,204},{255,0,204,204}}};

void setup() {
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(oneGround, OUTPUT);
  pinMode(twoGround, OUTPUT);
  pinMode(threeGround, OUTPUT);
  pinMode(fourGround, OUTPUT);
}

int c = 0, d1 = 0;

void loop() {
  lightUpValues(oneGround, paterns[d1][0][0], paterns[d1][0][1], paterns[d1][0][2], paterns[d1][0][3]);
  lightUpValues(twoGround, paterns[d1][1][0], paterns[d1][1][1], paterns[d1][1][2], paterns[d1][1][3]);
  lightUpValues(threeGround, paterns[d1][2][0], paterns[d1][2][1], paterns[d1][2][2], paterns[d1][2][3]);
  lightUpValues(fourGround, paterns[d1][3][0], paterns[d1][3][1], paterns[d1][3][2], paterns[d1][3][3]);
  if (c == 2000) {
    d1++;
    c = 0;
  } else c++;
  if (d1 == 5) {
    d1 = 0;
  }
}

void lightUpValues(int groundPin,int brightness, int r , int g, int b) {
  analogWrite(groundPin, brightness);
  analogWrite(red, r);
  analogWrite(green, g);
  analogWrite(blue, b);
  delayMicroseconds(dela);
  digitalWrite(groundPin, LOW);
}

So, how can I stop the flicker of the LEDs?

Notes:

  1. I haven't yet learned C, all I know comes from Java. Sorry if my code harms your eyes!

  2. I'm also a newbie in electronics, I only know a few basics and thats it, I really want to learn more though! 🙂

  3. The hardcoded color array is created by a program I made, it is not written bit-by-bit…

Thank you for your time!

Best Answer

You have several options:

  1. Increse the PWM/matrix update/strobe frequency beyond the limit for human vision.

  2. Run it at 100 % duty cycle.

  3. Low-pass filter it.

  4. Run it at constant current with a different driver.