I have 2 sets of RGB Leds, each set is driven by 3 OUTPUT analog pins of the arduino, as illustrated below: enter image description here

The code is supposed to put on HIGH the 6 analog pins at the same time so that the Leds light up all at the same time, but when I run it, there's a latency between the 2 groups of Leds. Here's the code:

const int r[2] = {3, 9};
const int g[2] = {5, 10};
const int b[2] = {6, 11};

const int red[3] = {255, 0, 0};
const int green[3] = {0, 255, 0};
const int blue[3] = {0, 0, 255};

int* colors[3] = {red, blue, green};

void setup() {
    for(int i=0;  i<2; i++){
        pinMode(r[i], OUTPUT);
        pinMode(g[i], OUTPUT);
        pinMode(b[i], OUTPUT);

void setColor(int r, int g, int b, int color[]){
    int R = 255 - color[0];
    int G = 255 - color[1];
    int B = 255 - color[2];
    analogWrite(r, R);
    analogWrite(g, G);
    analogWrite(b, B);

void loop() {
    for(int c=0; c<3; c++){
      setColor(r[0], g[0], b[0], colors[c]);
      setColor(r[1], g[1], b[1], colors[3-c-1]);

I should point out that the values of the resistors are in my circuit are different than in the diagram.

Is there something wrong in the wiring or in the code that prevents the Leds from wiring all at the same time?

EDIT: I tried the code one more time on the arduino and all Leds lit up at the same time. That happened in the past, but sometimes there's just a latency that I can neither explain nor fix...

  • How should 3 groups of each 3 ints fit into an array of type int and size 3? you need either a 2 dimensional array or a bigger type (like long, which has 4 bytes instead of the 2 bytes of int) – chrisl Dec 17 '20 at 20:56
  • You can't use red, blue and green as ints in colors[], they are int*s, and you can't call setColor() with 5 arguments. Does this even compile for you? – ocrdu Dec 17 '20 at 21:22
  • 1
    @it worked event with that... I edited the code to correct the problem and checked that the code compiled on the arduino IDE. – S.E.K. Dec 17 '20 at 23:44
  • 1
    How big is the latency? Maybe you reflect if the delay(1000) is at the right place. analogWrite() will take some time to execute, but it should be only a few milliseconds and I doubt that one would notice. If he latter is the case, you should try direct port manipulation – Sim Son Dec 18 '20 at 5:47
  • How much latency are we talking about? Seconds or just fractions of a second? If your code and your hardware are correct, the time between the lighting of the two LEDs should be imperceptible. – StarCat Dec 18 '20 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.