Chapter 16 74HC595 & LED Matrix
Thus far we have learned how to use the 74HC595 IC chip to control the LED bar graph and the 7-segment display. We will now use 74HC595 IC chips to control a LED matrix.
Project 16.3 LED Matrix
In this project, we will use two 74HC595 IC chips to control a monochrome (one color) (8X8) LED matrix to make it display both simple graphics and characters.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
||
Breadboard x1
|
|||
74HC595 x2
|
Resistor 220Ω x8
|
Jumper M/M
|
8*8 LEDMatrix x1
|
Component knowledge
LED Matrix
A LED matrix is a rectangular display module that consists of a uniform grid of LEDs. The following is an 8X8 monochrome (one color) LED matrix containing 64 LEDs (8 rows by 8 columns).
In order to facilitate the operation and reduce the number of ports required to drive this component, the positive poles of the LEDs in each row and negative poles of the LEDs in each column are respectively connected together inside the LED matrix module, which is called a common anode. There is another arrangement type. Negative poles of the LEDs in each row and the positive poles of the LEDs in each column are respectively connected together, which is called a common cathode.
Here is how a common anode LED matrix works. First, choose 16 ports on ESP32 board to connect to the 16 ports of LED matrix. Configure one port in columns for low level, which makes that column the selected port. Then configure the eight port in the row to display content in the selected column. Add a delay value and then select the next column that outputs the corresponding content. This kind of operation by column is called scan. If you want to display the following image of a smiling face, you can display it in 8 columns, and each column is represented by one byte.
Column |
Binary |
Hexadecimal |
|---|---|---|
1 |
0001 1100 |
0x1c |
2 |
0010 0010 |
0x22 |
3 |
0101 0001 |
0x51 |
4 |
0100 0101 |
0x45 |
5 |
0100 0101 |
0x45 |
6 |
0101 0001 |
0x51 |
7 |
0010 0010 |
0x22 |
8 |
0001 1100 |
0x1c |
To begin, display the first column, then turn off the first column and display the second column. (and so on) …. turn off the seventh column and display the 8th column, and then start the process over from the first column again like the control of LED bar graph project. The whole process will be repeated rapidly in a loop. Due to the principle of optical afterglow effect and the vision persistence effect in human sight, we will see a picture of a smiling face directly rather than individual columns of LEDs turned ON one column at a time (although in fact this is the reality we cannot perceive).
Then, to save the number of GPIO, we use a 74HC595. When the first column is turned ON, set the lights that need to be displayed in the first column to “1”, otherwise to “0”, as shown in the above example, where the value of the first column is 0x1c. This value is sent to 74HC595 to control the display of the first column of the LED matrix. Following the above idea, turn OFF the display of the first column, then turn ON the second column, and then send the value of the second column to 74HC595 …… Until each column is displayed, the LED matrix is displayed again from the first column.
Circuit
In circuit of this project, the power pin of the 74HC595 IC chip is connected to 3.3V. It can also be connected to 5V to make LED matrix brighter.
Schematic diagram |
|---|
|
Hardware connection |
If you need any support, please contact us via: support@freenove.com
|
Sketch
The following code will make LED matrix display a smiling face, and then display scrolling character “0-F”.
Download the code to ESP32-WROVER, and the LED matrix display a smiling face, and then display characters “0 to F” scrolling in a loop on the LED matrix.
Sketch_16.3_LED_Matrix
The following is the program code:
1/**********************************************************************
2 Filename : LED Matrix Display
3 Description : Use 2 74HC595 to drive the LED Matrix display
4 Auther : www.freenove.com
5 Modification: 2024/06/19
6**********************************************************************/
7int latchPin = 2; // Pin connected to ST_CP of 74HC595(Pin12)
8int clockPin = 4; // Pin connected to SH_CP of 74HC595(Pin11)
9int dataPin = 15; // Pin connected to DS of 74HC595(Pin14)
10
11// Define the pattern data for a smiling face
12const int smilingFace[] = { //"^ⅴ^"
13 0x1C, 0x22, 0x51, 0x45, 0x45, 0x51, 0x22, 0x14
14};
15// Define the data of numbers and letters, and save them in flash area
16const int data[] PROGMEM = {
17 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // " "
18 0x00, 0x00, 0x21, 0x7F, 0x01, 0x00, 0x00, 0x00, // "1"
19 0x00, 0x00, 0x23, 0x45, 0x49, 0x31, 0x00, 0x00, // "2"
20 0x00, 0x00, 0x22, 0x49, 0x49, 0x36, 0x00, 0x00, // "3"
21 0x00, 0x00, 0x0E, 0x32, 0x7F, 0x02, 0x00, 0x00, // "4"
22 0x00, 0x00, 0x79, 0x49, 0x49, 0x46, 0x00, 0x00, // "5"
23 0x00, 0x00, 0x3E, 0x49, 0x49, 0x26, 0x00, 0x00, // "6"
24 0x00, 0x00, 0x60, 0x47, 0x48, 0x70, 0x00, 0x00, // "7"
25 0x00, 0x00, 0x36, 0x49, 0x49, 0x36, 0x00, 0x00, // "8"
26 0x00, 0x00, 0x32, 0x49, 0x49, 0x3E, 0x00, 0x00, // "9"
27 0x00, 0x00, 0x3E, 0x41, 0x41, 0x3E, 0x00, 0x00, // "0"
28 0x00, 0x00, 0x3F, 0x44, 0x44, 0x3F, 0x00, 0x00, // "A"
29 0x00, 0x00, 0x7F, 0x49, 0x49, 0x36, 0x00, 0x00, // "B"
30 0x00, 0x00, 0x3E, 0x41, 0x41, 0x22, 0x00, 0x00, // "C"
31 0x00, 0x00, 0x7F, 0x41, 0x41, 0x3E, 0x00, 0x00, // "D"
32 0x00, 0x00, 0x7F, 0x49, 0x49, 0x41, 0x00, 0x00, // "E"
33 0x00, 0x00, 0x7F, 0x48, 0x48, 0x40, 0x00, 0x00 // "F"
34};
35
36void setup() {
37 // set pins to output
38 pinMode(latchPin, OUTPUT);
39 pinMode(clockPin, OUTPUT);
40 pinMode(dataPin, OUTPUT);
41}
42
43void loop() {
44 // Define a one-byte variable (8 bits) which is used to represent the selected state of 8 column.
45 int cols;
46 // Display the static smiling pattern
47 for (int j = 0; j < 500; j++ ) { // repeat 500 times
48 cols = 0x01;
49 for (int i = 0; i < 8; i++) { // display 8 column data by scaning
50 matrixRowsVal(smilingFace[i]);// display the data in this column
51 matrixColsVal(~cols); // select this column
52 delay(1); // display them for a period of time
53 matrixRowsVal(0x00); // clear the data of this column
54 cols <<= 1; // shift"cols" 1 bit left to select the next column
55 }
56 }
57 // Display the dynamic patterns of numbers and letters
58 for (int i = 0; i < 128; i++) {
59 for (int k = 0; k < 10; k++) { // repeat image of each frame 10 times.
60 cols = 0x01; // Assign binary 00000001. Means the first column is selected.
61 for (int j = i; j < 8 + i; j++) { // display image of each frame
62 matrixRowsVal(pgm_read_word_near(data + j));// display the data in this column
63 matrixColsVal(~cols); // select this column
64 delay(1); // display them for a period of time
65 matrixRowsVal(0x00); // close the data of this column
66 cols <<= 1; // shift"cols" 1 bit left to select the next column
67 }
68 }
69 }
70}
71
72void matrixRowsVal(int value) {
73 // make latchPin output low level
74 digitalWrite(latchPin, LOW);
75 // Send serial data to 74HC595
76 shiftOut(dataPin, clockPin, LSBFIRST, value);
77 // make latchPin output high level, then 74HC595 will update the data to parallel output
78 digitalWrite(latchPin, HIGH);
79}
80
81void matrixColsVal(int value) {
82 // make latchPin output low level
83 digitalWrite(latchPin, LOW);
84 // Send serial data to 74HC595
85 shiftOut(dataPin, clockPin, MSBFIRST, value);
86 // make latchPin output high level, then 74HC595 will update the data to parallel output
87 digitalWrite(latchPin, HIGH);
88}
The first “for” loop in the “while” loop is used to display a static smile. Displaying column information from left to right, one column at a time with a total of 8 columns. This repeats 500 times to ensure sufficient display time.
1int cols;
2// Display the static smiling pattern
3for (int j = 0; j < 500; j++ ) { // repeat 500 times
4 cols = 0x01;
5 for (int i = 0; i < 8; i++) { // display 8 column data by scaning
6 matrixRowsVal(smilingFace[i]);// display the data in this column
7 matrixColsVal(~cols); // select this column
8 delay(1); // display them for a period of time
9 matrixRowsVal(0x00); // clear the data of this column
10 cols <<= 1; // shift"cols" 1 bit left to select the next column
11 }
12}
The second “for” loop is used to display scrolling characters “0 to F”, for a total of 17* 8 = 144 columns. Displaying the 0-8 column, then the 1-9 column, then the 2-10 column…… and so on…128-136 column in consecutively to achieve the scrolling effect. The display of each frame is repeated a certain number of times and the more repetitions, the longer the single frame display will be and the slower the scrolling movement.
1// Display the dynamic patterns of numbers and letters
2for (int i = 0; i < 128; i++) {
3 for (int k = 0; k < 10; k++) { // repeat image of each frame 10 times.
4 cols = 0x01; // Assign binary 00000001. Means the first column is selected.
5 for (int j = i; j < 8 + i; j++) { // display image of each frame
6 matrixRowsVal(pgm_read_word_near(data + j));// display the data in this column
7 matrixColsVal(~cols); // select this column
8 delay(1); // display them for a period of time
9 matrixRowsVal(0x00); // close the data of this column
10 cols <<= 1; // shift"cols" 1 bit left to select the next column
11 }
12 }
13}
The amount of pins of ESP32 is limited, so you need to find ways to save pins. If you use ESP32’s GPIO to control the lattice without using 74HC595, you need 16 pins for the use of LED matrix. In this example, we use two 74HC595 to drive the LED matrix, requiring only three pins, so that we could save the rest of 13 pins.







