19. Chapter 74HC595 and 7-segment display

In this chapter, we will learn a new component: 7-segment display.

19.1. Project 7-segment display

In this project, we will use the 74HC595 chip and a 7-segment digital tube display to display the numbers 0 to 9.

19.1.1. Component list

Microbit x1

Chapter03_00

Expansion board x1

Chapter03_01

Breakboard x1

Chapter03_02

USB cable x1

Chapter03_03

1-digit 7-segment-display x1

Chapter19_00

F/M x5 M/M x12

Chapter14_00

74HC595 x1

Chapter18_01

Resistor 220Ω x8

Chapter18_02

19.1.2. Component knowledge

19.1.2.1. 1-digit 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. There are two kinds of 1-digit 7-segment display: Common Anode and Common Cathode. The one we use is that have a Common Anode(+) and individual Cathodes. Its internal structure and pin designation diagram is shown below:

../../../_images/Chapter19_01.png

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 7,6,4,2,1 and 9, and turn OFF LED segments 10 and 5.

../../../_images/Chapter19_02.png

If we use a byte to show the state of the LEDs that connected to pin 5, 10, 9, 1, 2, 4, 6, 7, we can use 0 to represent the state of on and 1 for off. Then the number 0 can be expressed as a binary number 11000000, namely hex 0xc0.

The numbers and letters that can be display are shown below:

Number/Letter

Binary number

Hexadecimal number

Decimal number

0

11000000

0xc0

192

1

11111001

0xf9

249

2

10100100

0xa4

164

3

10110000

0xb0

176

4

10011001

0x99

153

5

10010010

0x92

146

6

10000010

0x82

130

7

11111000

0xf8

248

8

10000000

0x80

128

9

10010000

0x90

144

A

10001000

0x88

136

b

10000011

0x83

131

C

11000110

0xc6

198

d

10100001

0xa1

161

E

10000110

0x86

134

F

10001110

0x8e

142

19.1.3. Circuit

Schematic diagram

Chapter19_03

Hardware connection

Chapter19_04

19.1.4. Block code

Open MakeCode first. Import the .hex file. The path is as below:

( How to import project )

File type

Path

File name

HEX file

../Projects/BlockCode/19.1_SevenSegmentDisplay

SevenSegmentDisplay.hex

After importing successfully, the code is shown as below:

../../../_images/Chapter19_05.png

After checking the connection of the circuit and verifying it correct, the code is downloaded into micro:bit. You can see that the 7-segment display shows 0, 1… 9 in turn.

Set 74HC595 data pin as P0, launch pin as P1, and clock pin as P2.

../../../_images/Chapter19_06.png

In the for loop, the digital tube displays the numbers 0 to 9 in turn, and change the number every 500ms.

../../../_images/Chapter19_07.png

19.1.4.1. Reference

Block

Function

Chapter19_08

It belongs to Freenove Extension Block.

It is used to control digital tube to display number and

character 0-F by using 74HC595.

19.1.5. Python code

Open the .py file with Mu. Code, the path is as below:

File type

Path

File name

Python file

../Projects/PythonCode/19.1_SevenSegmentDisplay

SevenSegmentDisplay.py

After loading successfully, the code is shown as below:

../../../_images/Chapter19_09.png

After checking the connection of the circuit and verifying it correct, the code is downloaded into micro:bit. You can see that the 7-segment display shows 0, 1… 9 in turn.

The following is the program code:

 1from microbit import *
 2number =[0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90]
 3LSBFIRST=1
 4MSBFIRST=2
 5#define the pins connect to 74HC595
 6dataPin=pin0 #DS Pin of 74HC595(Pin14)
 7latchPin=pin1 #ST_CP Pin of 74HC595(Pin12)
 8clockPin=pin2 #SH_CP Pin of 74HC595(Pin11)
 9def shiftOut(value,dPin,cPin,order):
10    for i in range (8):
11        cPin.write_digital(0)
12        if order==MSBFIRST:
13            flag=value<<i & 0x80
14            if flag==0x80:
15                dPin.write_digital(1)
16            else:
17                dPin.write_digital(0)
18        else:
19            flag=value>>i & 0x01
20            if flag==0x01:
21                dPin.write_digital(1)
22            else:
23                dPin.write_digital(0)
24        cPin.write_digital(1)
25while True:
26    for Num in number:
27        latchPin.write_digital(0)
28        shiftOut(Num,dataPin,clockPin,MSBFIRST)
29        latchPin.write_digital(1)
30        sleep(500)

Define variable number to store numbers 0, 1, 2…9 in Hexadecimal.

Define pins P0, P1, P2 for 74HC595.

1number =[0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90]
2LSBFIRST=1
3MSBFIRST=2
4#define the pins connect to 74HC595
5dataPin=pin0 #DS Pin of 74HC595(Pin14)
6latchPin=pin1 #ST_CP Pin of 74HC595(Pin12)
7clockPin=pin2 #SH_CP Pin of 74HC595(Pin11)

Custom shiftOut () function is used for writing data to 74HC595 serially.

 1def shiftOut(value,dPin,cPin,order):
 2    for i in range (8):
 3        cPin.write_digital(0)
 4        if order==MSBFIRST:
 5            flag=value<<i & 0x80
 6            if flag==0x80:
 7                dPin.write_digital(1)
 8            else:
 9                dPin.write_digital(0)
10        else:
11            flag=value>>i & 0x01
12            if flag==0x01:
13                dPin.write_digital(1)
14            else:
15                dPin.write_digital(0)
16        cPin.write_digital(1)

Call the shiftOut() function, and write the hexadecimal number stored in the number variable to 74HC595 serially, then turn ON the LEDs through parallel output of Q0 ~ Q7.

1while True:
2    for Num in number:
3        latchPin.write_digital(0)
4        shiftOut(Num,dataPin,clockPin,MSBFIRST)
5        latchPin.write_digital(1)
6        sleep(500)