A smile and scrolling digits on an 8x8 matrix (74HC595 rows, pins 2 to 9 columns), on an Arduino Uno
A common-anode 8x8 LED matrix: a 74HC595 (DS 11, ST_CP 12, SH_CP 13) drives its rows through 220 ohm resistors and pins 2 to 9 its columns, scanned a column at a time to draw a smiling face, then scroll 0 to 9 and A to F. The whole build, an Arduino Uno and 10 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Advanced About 35 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Multiplexing: drawing a picture one column at a time
- Adding output pins with a 74HC595 shift register
- Sending a byte with shiftOut and latching it
- Storing a bitmap as hex numbers, one per column
- Why a common-anode matrix needs LOW to turn a column on
- Difficulty: Advanced
- Time: About 35 minutes
- Board: Arduino Uno R3
- Parts: 12
How it works
An 8x8 LED matrix has 64 LEDs but only 16 pins. This one is common anode. A 74HC595 shift register drives the eight rows (anodes) through 220 ohm resistors, and Uno pins 2 to 9 drive the eight columns (cathodes). An LED lights when its row is HIGH and its column is LOW.
The sketch lights one column at a time, very fast, for 1 ms each. Your eye blends the 8 columns into one steady picture.
First it shows a smiley face for 500 passes. Then it scrolls the digits 0 to 9 and the letters A to F across the matrix, shifting the picture one column at a time.
Parts list
12 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 1 × LED matrix, bare 8x8 (1088AS)
- 8 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| 74HC595 shift register pin GND | Arduino Uno R3 pin GND |
| 74HC595 shift register pin OE | Arduino Uno R3 pin GND |
| 74HC595 shift register pin SHCP | Arduino Uno R3 pin 13 |
| 74HC595 shift register pin STCP | Arduino Uno R3 pin 12 |
| 74HC595 shift register pin DS | Arduino Uno R3 pin 11 |
| LED matrix, bare 8x8 (1088AS) pin C8 | Arduino Uno R3 pin 9 |
| LED matrix, bare 8x8 (1088AS) pin C7 | Arduino Uno R3 pin 8 |
| LED matrix, bare 8x8 (1088AS) pin C6 | Arduino Uno R3 pin 7 |
| LED matrix, bare 8x8 (1088AS) pin C5 | Arduino Uno R3 pin 6 |
| LED matrix, bare 8x8 (1088AS) pin C4 | Arduino Uno R3 pin 5 |
| LED matrix, bare 8x8 (1088AS) pin C3 | Arduino Uno R3 pin 4 |
| LED matrix, bare 8x8 (1088AS) pin C2 | Arduino Uno R3 pin 3 |
| LED matrix, bare 8x8 (1088AS) pin C1 | Arduino Uno R3 pin 2 |
| 74HC595 shift register pin MR | Arduino Uno R3 pin 5V |
| 74HC595 shift register pin VCC | Arduino Uno R3 pin 5V |
| Resistor, 220 Ω (2) pin 1 | 74HC595 shift register pin Q1 |
| Resistor, 220 Ω (3) pin 1 | 74HC595 shift register pin Q2 |
| Resistor, 220 Ω (4) pin 1 | 74HC595 shift register pin Q3 |
| Resistor, 220 Ω (5) pin 1 | 74HC595 shift register pin Q4 |
| Resistor, 220 Ω (6) pin 1 | 74HC595 shift register pin Q5 |
| Resistor, 220 Ω (7) pin 1 | 74HC595 shift register pin Q6 |
| Resistor, 220 Ω (8) pin 1 | 74HC595 shift register pin Q7 |
| Resistor, 220 Ω (1) pin 1 | 74HC595 shift register pin Q0 |
| Resistor, 220 Ω (5) pin 2 | LED matrix, bare 8x8 (1088AS) pin R5 |
| Resistor, 220 Ω (7) pin 2 | LED matrix, bare 8x8 (1088AS) pin R7 |
| Resistor, 220 Ω (8) pin 2 | LED matrix, bare 8x8 (1088AS) pin R8 |
| Resistor, 220 Ω (6) pin 2 | LED matrix, bare 8x8 (1088AS) pin R6 |
| Resistor, 220 Ω (3) pin 2 | LED matrix, bare 8x8 (1088AS) pin R3 |
| Resistor, 220 Ω (2) pin 2 | LED matrix, bare 8x8 (1088AS) pin R2 |
| Resistor, 220 Ω (4) pin 2 | LED matrix, bare 8x8 (1088AS) pin R4 |
| Resistor, 220 Ω (1) pin 2 | LED matrix, bare 8x8 (1088AS) pin R1 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
/*
Sketch 15.2.1
LED Matrix
modified 2016/5/13
by http://www.freenove.com
Wiring, as the lesson's: a common-anode 8x8 matrix. The 74HC595's Q0..Q7
drive the eight rows (the anodes), Q0 the top row, each through 220 ohm;
pins 2 to 9 drive the eight columns (the cathodes), column 1 on pin 2.
74HC595 DS -> 11, ST_CP -> 12, SH_CP -> 13.
The two declarations below are what the Arduino IDE writes in for itself
ahead of setup(); the rest is the lesson's sketch as it is.
*/
void matrixRowsVal(int value);
void matrixColsVal(byte value);
int latchPin = 12; // Pin connected to ST_CP of 74HC595 (Pin12)
int clockPin = 13; // Pin connected to SH_CP of 74HC595 (Pin11)
int dataPin = 11; // Pin connected to DS of 74HC595 (Pin14)
int LEDPin[] = {2, 3, 4, 5, 6, 7, 8, 9}; // column pin (cathode) of LED Matrix
// Define the pattern data for a smiling face
const int smilingFace[] = {
0x1C, 0x22, 0x51, 0x45, 0x45, 0x51, 0x22, 0x1C
};
// Define the data of numbers and letters, and save them in flash area
const int data[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // " "
0x00, 0x00, 0x21, 0x7F, 0x01, 0x00, 0x00, 0x00, // "1"
0x00, 0x00, 0x23, 0x45, 0x49, 0x31, 0x00, 0x00, // "2"
0x00, 0x00, 0x22, 0x49, 0x49, 0x36, 0x00, 0x00, // "3"
0x00, 0x00, 0x0E, 0x32, 0x7F, 0x02, 0x00, 0x00, // "4"
0x00, 0x00, 0x79, 0x49, 0x49, 0x46, 0x00, 0x00, // "5"
0x00, 0x00, 0x3E, 0x49, 0x49, 0x26, 0x00, 0x00, // "6"
0x00, 0x00, 0x60, 0x47, 0x48, 0x70, 0x00, 0x00, // "7"
0x00, 0x00, 0x36, 0x49, 0x49, 0x36, 0x00, 0x00, // "8"
0x00, 0x00, 0x32, 0x49, 0x49, 0x3E, 0x00, 0x00, // "9"
0x00, 0x00, 0x3E, 0x41, 0x41, 0x3E, 0x00, 0x00, // "0"
0x00, 0x00, 0x3F, 0x44, 0x44, 0x3F, 0x00, 0x00, // "A"
0x00, 0x00, 0x7F, 0x49, 0x49, 0x36, 0x00, 0x00, // "B"
0x00, 0x00, 0x3E, 0x41, 0x41, 0x22, 0x00, 0x00, // "C"
0x00, 0x00, 0x7F, 0x41, 0x41, 0x3E, 0x00, 0x00, // "D"
0x00, 0x00, 0x7F, 0x49, 0x49, 0x41, 0x00, 0x00, // "E"
0x00, 0x00, 0x7F, 0x48, 0x48, 0x40, 0x00, 0x00 // "F"
};
void setup() {
// set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
for (int i = 0; i < 8; i++) {
pinMode(LEDPin[i], OUTPUT);
}
}
void loop() {
// Define a one-byte variable (8 bits) which is used to represent the selected state of 8 column.
int cols;
// Display the static smiling pattern
for (int j = 0; j < 500; j++ ) { // repeat 500 times
cols = 0x01; // Assign 0x01(binary 00000001) to the variable, which represents the first column is selected.
for (int i = 0; i < 8; i++) { // display 8 column data by scaning
matrixColsVal(cols); // select this column
matrixRowsVal(smilingFace[i]);// display the data in this column
delay(1); // display them for a period of time
matrixRowsVal(0x00); // clear the data of this column
cols <<= 1; // shift"cols" 1 bit left to select the next column
}
}
// Display the dynamic patterns of numbers and letters
for (int i = 0; i < 128; i++) { // "space,0-9,A-F"16 letters ,each letter hold 8 columns, total 136 columns. Firstly, display space ,then we need shift 128 times(136-8)
for (int k = 0; k < 10; k++) { // repeat image of each frame 10 times.
cols = 0x01; // Assign binary 00000001. Means the first column is selected.
for (int j = i; j < 8 + i; j++) { // display image of each frame
matrixColsVal(cols); // select this column
matrixRowsVal(pgm_read_word_near(data + j));// display the data in this column
delay(1); // display them for a period of time
matrixRowsVal(0x00); // close the data of this column
cols <<= 1; // shift"cols" 1 bit left to select the next column
}
}
}
}
void matrixRowsVal(int value) {
// make latchPin output low level
digitalWrite(latchPin, LOW);
// Send serial data to 74HC595
shiftOut(dataPin, clockPin, LSBFIRST, value);
// make latchPin output high level, then 74HC595 will update the data to parallel output
digitalWrite(latchPin, HIGH);
}
void matrixColsVal(byte value) {
byte cols = 0x01;
// Output the column data to the corresponding port.
for (int i = 0; i < 8; i++) {
digitalWrite(LEDPin[i], ((value & cols) == cols) ? LOW : HIGH);
cols <<= 1;
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
matrixRowsVal sketch.ino
This sends one byte to the 74HC595. It pulls latchPin LOW, shifts the byte in with shiftOut, and pulls the latch HIGH so all eight outputs update at the same time. Each bit of the byte is one row of the current column.
void matrixRowsVal(int value) {
// make latchPin output low level
digitalWrite(latchPin, LOW);
// Send serial data to 74HC595
shiftOut(dataPin, clockPin, LSBFIRST, value);
// make latchPin output high level, then 74HC595 will update the data to parallel output
digitalWrite(latchPin, HIGH);
}matrixColsVal sketch.ino
This picks which column is on. It takes a byte with one bit set and checks each bit in turn. A set bit writes LOW to that column pin, which turns the column on. All other columns get HIGH, so they stay off.
void matrixColsVal(byte value) {
byte cols = 0x01;
// Output the column data to the corresponding port.
for (int i = 0; i < 8; i++) {
digitalWrite(LEDPin[i], ((value & cols) == cols) ? LOW : HIGH);
cols <<= 1;
}
}loop sketch.ino
For each column it selects the column, sends that column's row data, waits 1 ms, and then clears the rows. Doing this for all 8 columns is one frame. The smiley repeats 500 times. The scroll then moves the window along the data table by one column per step, drawing each step 10 times.
void loop() {
// Define a one-byte variable (8 bits) which is used to represent the selected state of 8 column.
int cols;
// Display the static smiling pattern
for (int j = 0; j < 500; j++ ) { // repeat 500 times
cols = 0x01; // Assign 0x01(binary 00000001) to the variable, which represents the first column is selected.
for (int i = 0; i < 8; i++) { // display 8 column data by scaning
matrixColsVal(cols); // select this column
matrixRowsVal(smilingFace[i]);// display the data in this column
delay(1); // display them for a period of time
matrixRowsVal(0x00); // clear the data of this column
cols <<= 1; // shift"cols" 1 bit left to select the next column
}
}
// Display the dynamic patterns of numbers and letters
for (int i = 0; i < 128; i++) { // "space,0-9,A-F"16 letters ,each letter hold 8 columns, total 136 columns. Firstly, display space ,then we need shift 128 times(136-8)
for (int k = 0; k < 10; k++) { // repeat image of each frame 10 times.
cols = 0x01; // Assign binary 00000001. Means the first column is selected.
for (int j = i; j < 8 + i; j++) { // display image of each frame
matrixColsVal(cols); // select this column
matrixRowsVal(pgm_read_word_near(data + j));// display the data in this column
delay(1); // display them for a period of time
matrixRowsVal(0x00); // close the data of this column
cols <<= 1; // shift"cols" 1 bit left to select the next column
}
}
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The picture is flipped, upside down or scrambled This project
Check that Q0 to Q7 go to rows 1 to 8 in order, and that pins 2 to 9 go to columns 1 to 8 in order. - Nothing lights at all This project
The 74HC595's OE pin must go to ground and its MR pin to 5 V. If OE is not tied low, the outputs stay off. - The image flickers This project
Keep the 1 ms delay in each column. A longer delay makes the scan visibly slow. - Outputs show garbage, or the register never clears. 74HC595 shift register
Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte. - The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely. - Several LEDs light at once or the wrong ones appear. LED matrix
Scan one row at a time, fast, and turn off the previous row first. Check the datasheet pin map for rows and columns, since the 1088AS pin order is not intuitive. - LEDs are dim or pins are overloaded. LED matrix
Use a current limiting resistor on each column, around 220 ohms. A single pin cannot drive a whole row of LEDs on, so use a driver chip such as a 74HC595 or transistors. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.