Chapter 15 74HC595 & LED Bar Graph

We have used LED bar graph to make a flowing water light, in which 10 GPIO ports of ESP32 is occupied. More GPIO ports mean that more peripherals can be connected to ESP32, so GPIO resource is very precious. Can we make flowing water light with less GPIO? In this chapter, we will learn a component, 74HC595, which can achieve the target.

Project 15.1 Flowing Water Light

Now let’s learn how to use the 74HC595 IC chip to make a flowing water light using less GPIO.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

74HC595

Chapter15_00

Resistor 220Ω x8

Chapter01_04

Jumper M/M x15

Chapter01_05

LED Bar Graph x1

Chapter15_01

Circuit

The circuit of this project is similar to the one in the last chapter. The only difference is that the photoresistor is replaced by the thermistor.

Schematic diagram

Chapter15_03

Hardware connection

If you need any support, please contact us via: support@freenove.com

Chapter15_04

Code

In this project, we will make a flowing water light with a 74HC595 chip to learn about its functions.

Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “15.1_Flowing_Water_Light”. Select “my74HC595.py”, right click your mouse to select “Upload to /”, wait for “my74HC595.py”to be uploaded to ESP32-WROVER and then double click “Flowing_Water_Light.py”.

15.1_Flowing_Water_Light

../../../_images/Chapter15_10.png

Click”Run current script” and you will see that Bar Graph LED starts with the flowing water pattern flashing from left to right and then back from right to left. If it displays nothing, maybe the LED Bar is connected upside down, please unplug it and then re-plug it reversely.

If you have any concerns, please contact us via: support@freenove.com

The following is the program code:

 1import time
 2from my74HC595 import Chip74HC595
 3
 4chip = Chip74HC595(14,12,13,5)
 5# ESP32-14: 74HC595-DS(14)
 6# ESP32-12: 74HC595-STCP(12)
 7# ESP32-13: 74HC595-SHCP(11)
 8# ESP32-5 : 74HC595-OE(5)
 9
10while True:
11    x=0x01
12    for count in range(8):
13        chip.shiftOut(1,x)
14        x=x<<1;
15        time.sleep_ms(300)
16    x=0x01
17    for count in range(8):
18        chip.shiftOut(0,x)
19        x=x<<1
20        time.sleep_ms(300)

Import time and my74HC595 modules.

1import time
2from my74HC595 import Chip74HC595

Assign pins for ESP32-WROVER to connect to 74HC595.

1chip = Chip74HC595(14,12,13,5)

The first for loop makes LED Bar display separately from left to right while the second for loop make it display separately from right to left.

 1x=0x01
 2for count in range(8):
 3    chip.shiftOut(1,x)
 4    x=x<<1;
 5    time.sleep_ms(300)
 6x=0x01
 7for count in range(8):
 8    chip.shiftOut(0,x)
 9    x=x<<1
10    time.sleep_ms(300)

Reference

Class Chip74HC595

Before each use of the object Chip74HC595, make sure my74HC595.py has been uploaded to “/” of ESP32, and then add the statement “from my74HC595 import Chip74HC595” to the top of the python file.

Chip74HC595():An object. By default, 74HC595’s DS pin is connected to Pin(14) of ESP32, ST_CP pin is connected to ESP32’s Pin(12) and OE pin is connected to ESP’s Pin(5). If you need to modify the pins, just do the following operations.

chip=Chip74HC595() or chip=Chip74HC595(14,12,13,5)

shiftOut(direction, data): Write data to 74HC595.

direction: 1/0. “1” presents that high-order byte will be sent first while “0” presents that low-order byte will be sent first.

data: The content that is sent, which is one-byte data.

clear(): Clear the latch data of 74HC595.