## Arduino Program to Turn on Diagonal LEDs of an 8*8 LED Matrix

by realfinetime  |  in LED Matrix at  11:51

We have already seen the circuit to connect 8*8 LED matrix to arduino through 8 bit shift register 74595 in previous blog. Next is a simple program to turn on diagonal LEDs (1, 10, 19, 28, 37, 46, 55, 64) only and turn off all other LEDs of an 8*8 LED matrix. LED matrix will look like as shown in the following image.

```int latchPin = 12;  //Pin connected to ST_CP of 1st 74595
int clockPin = 13;  //Pin connected to SH_CP of 1st 74595
int dataPin = 11;   //Pin connected to DS of 1st 74595

int latchPin2 = 6;  //Pin connected to ST_CP of 2nd 74595
int clockPin2 = 7;  //Pin connected to SH_CP of 2nd 74595
int dataPin2 = 5;   //Pin connected to DS of 2nd 74595

int i=0,j=0;
int anode_decimal={1, 2, 4, 8, 16, 32, 64, 128};
int cathode_decimal={254, 253, 251, 247, 239, 223, 191, 127};

void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
}

void loop() {
for(j=0;j<8;j++)
{
/************To increase the ON time of LEDs five times more than
OFF time to increase the brightness of LEDs*************/
for(i=0;i<5;i++)
{
/*************************  TURN ON DIAGONAL LEDs ONLY  ***************************/

// take the latchPin low so the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, anode_decimal[j]);
// shift out the bits:
digitalWrite(latchPin, HIGH);

// take the latchPin low so the LEDs don't change while you're sending in bits:
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, cathode_decimal[j]);
// shift out the bits:
digitalWrite(latchPin2, HIGH);
}

/**************************  TURN OFF ALL LEDs  ***************************/

/*** Send LOW to all Anode pins (16, 15, 11, 3, 10, 5, 6, 13) of LED matrix ***/

// take the latchPin low so the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
//Send 0 0 0 0 0 0 0 0 (1) to Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595
shiftOut(dataPin, clockPin, MSBFIRST, 0);
// shift out the bits:
digitalWrite(latchPin, HIGH);

/*** Send HIGH to all cathode pins (4, 7, 2, 8, 12, 1, 14 and 9) of LED matrix ***/

// take the latchPin low so the LEDs don't change while you're sending in bits:
digitalWrite(latchPin2, LOW);
//Send 1 1 1 1 1 1 1 1 (255) to Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595
shiftOut(dataPin2, clockPin2, MSBFIRST, 255);
// shift out the bits:
digitalWrite(latchPin2, HIGH);
}
}
```

If uploading is successful, diagonal LEDs will turn on and all the other LEDs will turn off as shown in the image given above. We have defined two arrays in program. One is 'anode_decimal' and the other is 'cathode_decimal'. 'anode_decimal' array has numbers that should be shifted out to the anode pins of 8*8 LED matrix to turn on diagonal LEDs only. 'cathode_decimal' array also has numbers that should be shifted out to the cathode pins of 8*8 LED matrix to turn on diagonal LEDs only.

When j=0

anode_decimal[j]=1.
cathode_decimal[j]=254.

When anode_decimal[j] (1) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 0 0 0 0 1. When cathode_decimal[j] (254) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 1 1 1 1 0. This will turn on the 1st LED. This process continues five times ('i' loop) to increase the on time five times more than the off time. This will increase the brightness of LEDs.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.

When j=1

anode_decimal[j]=2.
cathode_decimal[j]=253.

When anode_decimal[j] (2) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 0 0 0 1 0. When cathode_decimal[j] (253) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 1 1 1 0 1. This will turn on the 10th LED. This process continues five times ('i' loop) to increase the on time five times more than the off time. This will increase the brightness of LEDs.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=2

anode_decimal[j]=4.
cathode_decimal[j]=251.

When anode_decimal[j] (4) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 0 0 1 0 0. When cathode_decimal[j] (251) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 1 1 0 1 1. This will turn on the 19th LED. This process continues five times ('i' loop) to increase the on time five times more than the off time. This will increase the brightness of LEDs.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=3

anode_decimal[j]=8.
cathode_decimal[j]=247.

When anode_decimal[j] (8) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 0 1 0 0 0. When cathode_decimal[j] (247) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 1 0 1 1 1. This will turn on the 28th LED. This process continues five times ('i' loop) to increase the on time five times more than the off time. This will increase the brightness of LEDs.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=4

anode_decimal[j]=16.
cathode_decimal[j]=239.

When anode_decimal[j] (16) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 1 0 0 0 0. When cathode_decimal[j] (239) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 0 1 1 1 1. This will turn on the 37th LED. This process continues five times ('i' loop) to increase the on time five times more than the off time. This will increase the brightness of LEDs.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=5

anode_decimal[j]=32.
cathode_decimal[j]=223.

When anode_decimal[j] (32) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 1 0 0 0 0 0. When cathode_decimal[j] (223) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 0 1 1 1 1 1. This will turn on the 46th LED. This process continues five times ('i' loop) to increase the on time five times more than the off time. This will increase the brightness of LEDs.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=6

anode_decimal[j]=64.
cathode_decimal[j]=191.

When anode_decimal[j] (64) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 1 0 0 0 0 0 0. When cathode_decimal[j] (191) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 0 1 1 1 1 1 1. This will turn on the 55th LED. This process continues five times ('i' loop) to increase the on time five times more than the off time. This will increase the brightness of LEDs.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=7

anode_decimal[j]=128.
cathode_decimal[j]=127.

When anode_decimal[j] (128) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 1 0 0 0 0 0 0 0. When cathode_decimal[j] (127) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 0 1 1 1 1 1 1 1. This will turn on the 64th LED. This process continues five times ('i' loop) to increase the on time five times more than the off time. This will increase the brightness of LEDs.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
Only one LED glows at a time. Each LED glows at each iteration of 'j' loop. Since iteration is very fast, due to persistence of vision, our eyes will feel that all LEDs are glowing together. Hence eye will feel that all diagonal LEDs are glowing together.

1. Hi friend!
How could I adapt this code to use with the MAX7219 matrix module?
thanks

2. Como escribir palabras como "hola"

## IMPORTANT NOTICE

All the circuits, published in this blog is only after testing and getting proper results in my private lab. When you try these circuits, you should check the supply voltage, polarity of components, presence of childrens nearby and shorts in the circuits. This website will not be responsible for any harm happened to you or your components caused by your carelessness.