15. Chapter 74HC595 & 7-Segment Display
In this chapter, we will introduce the 7-Segment Display.
15.1. Project 7-Segment Display.
We will use 74HC595 to control 7-segment display and make it display hexadecimal character “0-F”.
15.1.1. Component List
ESP32-S3 WROOM x1
|
GPIO Extension Board x1
|
||
Breadboard x1
|
|||
74HC595 x1
|
Resistor 220Ω x8
|
7-segment display x1
|
|
Jumper M/M |
|||
15.1.2. Component knowledge
15.1.2.1. 7-segment display
A 7-segment display is a digital electronic display device. There is a figure “8” and a decimal point represented, which consists of 8 LEDs. The LEDs have a common anode and individual cathodes. Its internal structure and pin designation diagram is shown below:
As we can see in the above circuit diagram, we can control the state of each LED separately. Also, by combining LEDs with different states of ON and OFF, we can display different characters (Numbers and Letters). For example, to display a “0”: we need to turn ON LED segments A, B, C, D, E and F, and turn OFF LED segments G and DP.
In this project, we will use a 7-Segment Display with a common anode. Therefore, when there is an input low level to a LED segment the LED will turn ON. Defining segment “A” as the lowest level and segment “DP” as the highest level, from high to low would look like this: “DP”, “G”, “F”, “E”, “D”, “C”, “B”, “A”. Character “0” corresponds to the code: 1100 0000b=0xc0.
For detailed code values, please refer to the following table (common anode).
CHAR |
DP |
G |
F |
E |
D |
C |
B |
A |
Hex |
ASCII |
|---|---|---|---|---|---|---|---|---|---|---|
0 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
0xc0 |
1100 0000 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
0xf9 |
1111 1001 |
2 |
1 |
0 |
1 |
0 |
0 |
1 |
0 |
0 |
0xa4 |
1010 0100 |
3 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
0xb0 |
1011 0000 |
4 |
1 |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
0x99 |
1001 1001 |
5 |
1 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
0x92 |
1001 0010 |
6 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0x82 |
1000 0010 |
7 |
1 |
1 |
1 |
1 |
1 |
0 |
0 |
0 |
0xf8 |
1111 1000 |
8 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0x80 |
1000 0000 |
9 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
0x90 |
1001 0000 |
A |
1 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0x88 |
1000 1000 |
B |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
0x83 |
1000 0011 |
C |
1 |
1 |
0 |
0 |
0 |
1 |
1 |
0 |
0xc6 |
1100 0110 |
D |
1 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0xa1 |
1010 0001 |
E |
1 |
0 |
0 |
0 |
0 |
1 |
1 |
0 |
0x86 |
1000 0110 |
F |
1 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
0x8e |
1000 1110 |
15.1.3. Circuit
Schematic diagram |
|---|
|
Hardware connection. If you need any support, please contact us via: support@freenove.com |
|
15.1.4. Code
In this section, the 74HC595 is used in the same way as in the previous section, but with different values transferred. We can learn how to master the digital display by sending the code value of “0” - “F”.
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32_S3/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “74HC595_and_7_segment_display”.
Select “my74HC595.py”, right click your mouse to select “Upload to /”, wait for “my74HC595.py” to be uploaded to ESP32-S3 and then double click“74HC595_and_7_segment_display.py”.
15.1.4.1. 74HC595_and_7_segment_display
Click “Run current script”, and you’ll see a 1-bit, 7-segment display displaying 0-f in a loop.
The following is the program code:
1import time
2from my74HC595 import Chip74HC595
3
4lists =[0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,
5 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e]
6
7chip = Chip74HC595(12,13,14)
8try:
9 while True:
10 for count in range(16):
11 chip.shiftOut(0,lists[count])
12 time.sleep_ms(500)
13except:
14 pass
Import time and my74HC595 modules.
1import time
2from my74HC595 import Chip74HC595
Put the encoding “ 0 “ - “ F “ into the list.
1lists =[0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,
2 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e]
Define an object, whose pins applys default configuration, to drive 74HC595.
1chip = Chip74HC595(12,13,14)
Send data of digital tube to 74HC595 chip.
1chip.shiftOut(0,lists[count])
15.2. Project 4-Digit 7-Segment Display
Now, let’s try to control more digit 7-segment display
15.2.1. Component List
ESP32-S3 WROOM x1
|
GPIO Extension Board x1
|
||
Breadboard x1
|
|||
74HC595 x1
|
Resistor 220Ω x8
|
7-segment display x1
|
|
Jumper M/M |
|||
15.2.2. Component knowledge
15.2.2.1. 4 Digit 7-Segment Display
A 4 Digit 7-segment display integrates four 7-segment displays into one module, therefore it can display more characters. All of the LEDs contained have a common anode and individual cathodes. Its internal structure and pin designation diagram is shown below:
The internal electronic circuit is shown below, and all 8 LED cathode pins of each 7-segment display are connected together.
Display method of 4 digit 7-segment display is similar to 1 digit 7-segment display. The difference between them is that the 4-digit displays each Digit is visible in turn, one by one and not together. We need to first send high level to the common end of the first digit display, and send low level to the remaining three common ends, and then send content to 8 LED cathode pins of the first Digit Display. At this time, the first 7-segment display will show visible content and the remaining three will be OFF.
Similarly, the second, third and fourth 7-segment displays will show visible content in turn by scanning the display. Although the four number characters are displayed in turn separately, this process is so fast that it is imperceptible to the naked eye. This is due to the principle of optical afterglow effect and the vision persistence effect in human sight. This is how we can see all 4 number characters at the same time. However, if each number character is displayed for a longer period, you will be able to see that the number characters are displayed separately.
15.2.3. Circuit
Schematic diagram |
|---|
|
Hardware connection. If you need any support, please contact us via: support@freenove.com |
|
15.2.4. Code
In this code, we use the 74HC595 IC Chip to control the 4-Digit 7-Segment Display, and use the dynamic scanning method to show the changing number characters.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “4_Digit_7_Segment_Display”. Select “my74HC595.py”, right click your mouse to select “Upload to /”, wait for “my74HC595.py” to be uploaded to ESP32-S3 and double click “4_Digit_7_Segment_Display.py”.
15.2.4.1. 4_Digit_7_Segment_Display
Click “Run current script”, and the Nixie tube display as shown in the image below.
The following is the program code:
1import time
2from my74HC595 import Chip74HC595
3from machine import Pin
4
5comPin=[47,35,36,21]
6num =[0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,
7 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e]
8
9def led_display():
10 for i in range(0,4):
11 chns=Pin(comPin[i],Pin.OUT)
12 chns.value(1)
13 chip.shiftOut(0,num[i])
14 time.sleep_ms(1)
15 chns.value(0)
16 chip.shiftOut(0,0xff)
17
18
19#Pin(38)-74hc595.ds, Pin(39)-74hc595.st_cp, Pin(40)-74hc595.sh_cp
20chip = Chip74HC595(38,39,40)
21try:
22 while True:
23 led_display()
24except:
25 pass
Import time, my74HC595 and Pin modules.
1import time
2from my74HC595 import Chip74HC595
3from machine import Pin
Define common anode pins for digital tubes and request a list to put character encodings in it.
1comPin=[47,35,36,21]
2num =[0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,
3 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e]
Request an object to drive 74HC595 and associate pins with it.
1chip = Chip74HC595(38,39,40)
Make the digital tube display “0123”.
1def led_display():
2 for i in range(0,4):
3 chns=Pin(comPin[i],Pin.OUT)
4 chns.value(1)
5 chip.shiftOut(0,num[i])
6 time.sleep_ms(1)
7 chns.value(0)
8 chip.shiftOut(0,0xff)









