# 8×8 LED MATRIX – BEATING HEART TUTORIAL

In our previous entry, we showed you how to build your own 8×8 LED Matrix, using just a few parts readily available parts and some solder. Now that the matrix is completed, we are going to go ahead and make it do some really cool things!

For this project, we will use a code we adapted from arduino-er‘s blog. We thought this animation code was great! However, due to the different configuration of matrix pins and cathode/anode arrangement, it needed to be adapted to suit our needs.

To get started, all you need is an 8×8 LED Matrix and an arduino compatible device.

(If you’re using an “off the shelf” 8×8 LED Matrix, you may need to adapt the code and use some resistors. )

// 2-dimensional array of row pin numbers:
const int row[8] = {
19, 18, 17, 16, 15, 14, 2, 3
};// 2-dimensional array of column pin numbers:
const int col[8] = {
4, 5, 6, 7, 8, 9, 10, 11
};// 2-dimensional array of pixels:
int pixels[8][8];int count = 1000;char str[] = “FABCDEDCBA”;
int strLen = sizeof(str);
int ptrChar = 0;typedef bool charMapType[8][8];const charMapType charBlank = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};const charMapType heart0 = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};const charMapType heart1 = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};const charMapType heart2 = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0}
};const charMapType heart3 = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};const charMapType heart4 = {
{0, 1, 1, 0, 0, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};const charMapType *charMap[6] = {&heart0, &heart1, &heart2, &heart3, &heart4, &charBlank};

void setup() {
// initialize the I/O pins as outputs
// iterate over the pins:
for (int thisPin = 0; thisPin < 8; thisPin++) {
// initialize the output pins:
pinMode(col[thisPin], OUTPUT);
pinMode(row[thisPin], OUTPUT);
// take the row pins (i.e. the cathodes) high to ensure that
// the LEDS are off:
digitalWrite(row[thisPin], HIGH);
}

//setupScreen();
setupChar();

}

void loop() {

// draw the screen:
refreshScreen();

if(count– == 0){
count = 1000;
setupChar();
}

}

void setupChar(){
char c = str[ptrChar];
int offset = c – ‘A’;

const charMapType *cMap = charMap[offset];
//charMapType *cMap = &charDummy;

for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
bool v = (*cMap)[x][y];

if(v){
pixels[x][y] = LOW;
}else{
pixels[x][y] = HIGH;
}
}
}

ptrChar++;
if(ptrChar>=strLen-1){
ptrChar = 0;
}

}

void refreshScreen() {
// iterate over the cols (anodes):
for (int thisCol = 0; thisCol < 8; thisCol++) {
// take the col pin (anode) high:
digitalWrite(col[thisCol], HIGH);
// iterate over the rows (cathodes):
for (int thisRow = 0; thisRow < 8; thisRow++) {
// get the state of the current pixel;
int thisPixel = pixels[thisRow][thisCol];
// when the col is HIGH and the row is LOW,
// the LED where they meet turns on:
digitalWrite(row[thisRow], thisPixel);
// turn the pixel off:
if (thisPixel == LOW) {
digitalWrite(row[thisRow], HIGH);
}
}
// take the col pin low to turn off the whole row:
digitalWrite(col[thisCol], LOW);
}
}

If you have any trouble copy/pasting this code, we have it available in .txt form as well – Arduino 8×8 Beating Heart LED Matrix Code

We opted to use a 9v battery, DC Barrel Jack Adapter, and 9 Volt Battery Clip Pigtail so we could run the LEDs without the OSEPP board being connected to the computer.

Key things to remember:

1. Our matrix is a common-row cathode arrangement, so we must match our LOW signal arduino “row pins” to the corresponding pins on our matrix. Likewise, we must match the arduino’s HIGH “column pins” up with our matrix’s column pins. (we have this set in the first part of the code, however you can reprogram it if desired)
2. OSEPP Uno & Arduino UNO pins “ANALOG IN” also serve as additional digital pins 14-19… so A0 = “14” & A5 = “19”