# How can I stop the flicker of LEDs?

Recently I started messing around with controlling RGB leds with the Arduino for a project I'm building, so I built the following 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...

• This maybe an ugly method to filter LEDs but try an LC filter after the LEDs at the cathode. en.m.wikipedia.org/wiki/LC_circuit May 12, 2016 at 10:06
• Also why are your transistors the wrong way around? May 12, 2016 at 10:07
• Seems your schematic may have a bug. Should the transistors actually be PNP types? (Since they are connected to the LED cathodes.)
– Nedd
May 12, 2016 at 10:10
• Oh wow im sorry @Nedd they are indeed in the wrong way round on the schematic Ill fix it ASAP! Thanks! May 12, 2016 at 10:24
• Argh! That's a rather obfuscated schematic. Particularly around the LEDs, draw the high voltages at top and the low voltages at bottom. I had to decipher your schematic instead of just see the circuit. May 12, 2016 at 11:02

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.

• Thanks for your response! The first two options sound very nice, but how can i do it? How can i increase the frequency? Also wouldnt 100% duty cycle alter the colors of the leds? May 12, 2016 at 10:51
• standard PWM frequency is 490Hz if I'm not mistaken - shouldn't flicker because of that.
– RJR
May 12, 2016 at 10:58
• It depends on the vision from human to human (some see consumer-speed-DLP projector flicker, some don't) but mostley the angle from the center of your vision. 490 Hz should do the job. 100 % duty cycle will leave you without any dimming or more than 7 colors of RGB in total, but it would be 100 % flicker free. May 12, 2016 at 11:05
• @RJR Hmm, So how can i adjust this frequency??? And i dont really mind if some colors are excluded (unless they are pure Red Green or Blue. May 12, 2016 at 11:07
• changing the frequency is possible but not without some caveats: softsolder.com/2009/02/21/changing-the-arduino-pwm-frequency
– RJR
May 12, 2016 at 11:24

I see some problems that might create flicker:

1. You're using PWM on both the anode and the cathode of the LEDs, which makes them interfere with each other. The brightness value you're using doesn't cause the transistor to act like a variable resistor; it turns it on and off really fast, keeping it on for approximately $\frac{100 \times brightness}{255} \%$ of the time overall. So the LED will only be on when both PWM cycles happen to be on at the same time.

2. You're using PWM while multiplexing. I'm sure there's a way to do this that avoids flicker, but the multiplexing process is basically putting each LED into a 25% duty cycle already before the PWM from the analogWrite command is applied, again creating two interacting cycles.

3. This probably isn't having much of an effect, but I notice you're turning on the ground pin for each LED before setting its RGB values, which means you'll get the tiniest flash of the previous LED's color values when the current LED comes on. You should set the RGB values before turning on the ground pin.