3x3 LED matrix test

Hello everyone,

I plan to make an 8x8 led matrix to run sth. like snake etc. on it.

To test muliplexing I soldered a small 3x3 led matrix.

Now my issue: I wrote a code that works perfectly fine displaying a non-moving picture using “delay(10)” to light the leds every 13ms:

int anode[3] = {14, 15, 16};
int cathode[3] = {5, 6, 7};
byte led[3][3];

void setup(){
  pinMode(anode[0], OUTPUT);
  pinMode(anode[1], OUTPUT);
  pinMode(anode[2], OUTPUT);
  pinMode(cathode[0], OUTPUT);
  pinMode(cathode[1], OUTPUT);
  pinMode(cathode[2], OUTPUT);
  led[0][0] = 1;
  led[0][1] = 0;
  led[0][2] = 1;
  led[1][0] = 0;
  led[1][1] = 1;
  led[1][2] = 0;
  led[2][0] = 1;
  led[2][1] = 0;
  led[2][2] = 1;
}


void loop(){
    
  for(int i=0;i<3;i++){    
    
    digitalWrite(anode[0], LOW);
    digitalWrite(anode[1], LOW);
    digitalWrite(anode[2], LOW);
    digitalWrite(cathode[0], LOW);
    digitalWrite(cathode[1], LOW);
    digitalWrite(cathode[2], LOW);  
    
    digitalWrite(anode[i], HIGH);    
    
    for(int k=0;k<3;k++){
      digitalWrite(cathode[k], led[i][k]);
    }
    
    delay(1);
    
  }
  
  digitalWrite(anode[0], LOW);
  digitalWrite(anode[1], LOW);
  digitalWrite(anode[2], LOW);
  digitalWrite(cathode[0], LOW);
  digitalWrite(cathode[1], LOW);
  digitalWrite(cathode[2], LOW); 
    
  delay(10);

}

But with this I can’t for instance use delay() to move sth. with a certain speed across the screen…
Therefore I wrote the following code using “Timer1.initialize(13000)” and “Timer1.attachInterrupt(multiplex)” from “TimerOne.h”.

#include "TimerOne.h"

int anode[3] = {14, 15, 16};
int cathode[3] = {5, 6, 7};
byte led[3][3];

void setup(){
  pinMode(anode[0], OUTPUT);
  pinMode(anode[1], OUTPUT);
  pinMode(anode[2], OUTPUT);
  pinMode(cathode[0], OUTPUT);
  pinMode(cathode[1], OUTPUT);
  pinMode(cathode[2], OUTPUT);
  led[0][0] = 1;
  led[0][1] = 0;
  led[0][2] = 1;
  led[1][0] = 0;
  led[1][1] = 1;
  led[1][2] = 0;
  led[2][0] = 1;
  led[2][1] = 0;
  led[2][2] = 1;  
  Timer1.initialize(13000);
  Timer1.attachInterrupt(multiplex);
}

void loop(){
   
}


void multiplex(){  
    
  for(int i=0;i<3;i++){    
    
    digitalWrite(anode[0], LOW);
    digitalWrite(anode[1], LOW);
    digitalWrite(anode[2], LOW);
    digitalWrite(cathode[0], LOW);
    digitalWrite(cathode[1], LOW);
    digitalWrite(cathode[2], LOW);
    
    digitalWrite(anode[i], HIGH);    
    
    for(int k=0;k<3;k++){
      digitalWrite(cathode[k], led[i][k]);
    }
    
    delay(1);
    
  }
  
  digitalWrite(anode[0], LOW);
  digitalWrite(anode[1], LOW);
  digitalWrite(anode[2], LOW);
  digitalWrite(cathode[0], LOW);
  digitalWrite(cathode[1], LOW);
  digitalWrite(cathode[2], LOW);
  
}

This works fine and I’m able to display a moving picture now … but for some reason (only) the middle anode-row flickers as if the 13000µs to call the “multiplex” function were too long…

Have you got any idea why this happens? I mean in both cases the screen is updated every 13ms + the hardware is the same (http://s1.directupload.net/images/130717/5g8ysth6.png - I’ve made it quite strange since I still have to order transistors and proper resistors for a higher pulsed current).

And one more question about multiplexing in general: Do you usually light up the LEDs in each row for only a few microseconds and therefor call the function that updates the screen like every 100-1000µs ?

Help would be appreciated :slight_smile:

regards
BruteForce

Try shortening the hold time of each layer, to 500uS-1mS range. That will result in 8mS frame rate for the entire cube, 125Hz refresh rate, flicker free.

Also go to direct port manipulation for IO changes, digitalWrite is way slower by comparison.

Thanks, it works fine now.