16. Chapter 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.
16.1. Project 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.
16.1.1. Component List
ESP32-S3 WROOM x1
|
GPIO Extension Board x1
|
||
Breadboard x1
|
|||
74HC595 x2
|
Resistor 220Ω x8
|
8*8 LEDMatrix x1
|
|
Jumper M/M |
|||
16.1.2. Component knowledge
16.1.2.1. 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.
The LED matrix that we use in this project is a common anode LED matrix.
Here is how a common anode LED matrix works. First, choose 16 ports on ESP32-S3 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.
16.1.3. 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 |
|
16.1.4. Code
The following code will make LEDMatrix display a smiling face, and then display scrolling character “0-F”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “Micropython_Codes”. Select “HC595.py”, right click your mouse to select “Upload to /”, wait for “HC595.py” to be uploaded to ESP32-S3 and double click “LED_Matrix.py”.
16.1.4.1. 16.1_LED_Matrix
Click “Run current script”, and the LED Matrix display a smiling face, and then display characters “0 to F” scrolling in a loop on the LED Matrix.
The following is the program code:
1import time
2from my74HC595 import Chip74HC595
3
4smilingFace=[0x1C, 0x22, 0x51, 0x45, 0x45, 0x51, 0x22, 0x14]#^_^#
5numdata = [
6 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # " "
7 0x00, 0x00, 0x3E, 0x41, 0x41, 0x3E, 0x00, 0x00, # "0"
8 0x00, 0x00, 0x21, 0x7F, 0x01, 0x00, 0x00, 0x00, # "1"
9 0x00, 0x00, 0x23, 0x45, 0x49, 0x31, 0x00, 0x00, # "2"
10 0x00, 0x00, 0x22, 0x49, 0x49, 0x36, 0x00, 0x00, # "3"
11 0x00, 0x00, 0x0E, 0x32, 0x7F, 0x02, 0x00, 0x00, # "4"
12 0x00, 0x00, 0x79, 0x49, 0x49, 0x46, 0x00, 0x00, # "5"
13 0x00, 0x00, 0x3E, 0x49, 0x49, 0x26, 0x00, 0x00, # "6"
14 0x00, 0x00, 0x60, 0x47, 0x48, 0x70, 0x00, 0x00, # "7"
15 0x00, 0x00, 0x36, 0x49, 0x49, 0x36, 0x00, 0x00, # "8"
16 0x00, 0x00, 0x32, 0x49, 0x49, 0x3E, 0x00, 0x00, # "9"
17 0x00, 0x00, 0x3F, 0x44, 0x44, 0x3F, 0x00, 0x00, # "A"
18 0x00, 0x00, 0x7F, 0x49, 0x49, 0x36, 0x00, 0x00, # "B"
19 0x00, 0x00, 0x3E, 0x41, 0x41, 0x22, 0x00, 0x00, # "C"
20 0x00, 0x00, 0x7F, 0x41, 0x41, 0x3E, 0x00, 0x00, # "D"
21 0x00, 0x00, 0x7F, 0x49, 0x49, 0x41, 0x00, 0x00, # "E"
22 0x00, 0x00, 0x7F, 0x48, 0x48, 0x40, 0x00, 0x00, # "F"
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 # " "
24]
25
26'''
27Pin(38) - 74HC595(1).ds,
28Pin(39) - 74HC595(1).st_cp,
29Pin(40) - 74HC595(1).sh_cp,
30Pin(41) - 74HC595(1).oe
31
3274HC595(1).q7' - 74HC595(2).ds,
33Pin(39) - 74HC595(2).st_cp,
34Pin(40) - 74HC595(2).sh_cp,
35Pin(41) - 74HC595(2).oe
36'''
37chip = Chip74HC595(38,39,40,41)
38try:
39 while True:
40 for j in range(100):
41 cols=0x01
42 for i in range(8):
43 chip.disable()
44 chip.shiftOut(1,smilingFace[i])
45 chip.shiftOut(1,~cols)
46 time.sleep_us(500)
47 cols<<=1
48 chip.enable()
49 for i in range(136):
50 for k in range(5):
51 cols=0x01
52 for j in range(i,8+i):
53 chip.disable()
54 chip.shiftOut(1,numdata[j])
55 chip.shiftOut(0,~cols)
56 cols<<=1
57 chip.enable()
58 time.sleep_us(500)
59except:
60 pass
Import time and my 74HC595 modules.
1import time
2from my74HC595 import Chip74HC595
Use a nesting of two for loops to display a smiling face.
1for j in range(100):
2 cols=0x01
3 for i in range(8):
4 chip.disable()
5 chip.shiftOut(1,smilingFace[i])
6 chip.shiftOut(1,~cols)
7 time.sleep_us(500)
8 cols<<=1
9 chip.enable()
Use a nesting of two for loops to display “0”-“F”.
1for i in range(136):
2 for k in range(5):
3 cols=0x01
4 for j in range(i,8+i):
5 chip.disable()
6 chip.shiftOut(1,numdata[j])
7 chip.shiftOut(0,~cols)
8 cols<<=1
9 chip.enable()
10 time.sleep_us(500)
The amount of pins of ESP32-S3 is limited, so we need to find ways to save pins. If we use ESP32S3’s GPIO to control the LEDMatrix instead of 74HC595, we need 16 pins to drive LED matrix. In this example, we use two 74HC595 chips to drive the LED matrix, requiring only three pins, so that we could save the rest of 13 pins.
16.1.4.2. Reference
- Class HC595
Before each use of HC595, please make sure HC595.py has been uploaded to “/” of ESP32S3, and then add the statement “ import HC595 ” to the top of the python file.
Chip74HC595(): The object to control LEDMatrix.
chip=Chip74HC595() or chip=Chip74HC595(15,2,4,5)
set_bit_data(data): Write data to 74HC595.
clear(): Clear the latch data of 74HC595.








