EasyManuals Logo

SunFounder ESP32 User Manual

Default Icon
771 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #329 background imageLoading...
Page #329 background image
SunFounder ESP32 Starter Kit
(continued from previous page)
rclk = machine.Pin(27, machine.Pin.OUT) # STcp
srclk = machine.Pin(26, machine.Pin.OUT) # SHcp
# Define the hc595_shift function to shift data into the 74HC595 shift register
def hc595_shift(dat):
# Set the RCLK pin to low
rclk.off()
# Iterate through each bit (from 7 to 0)
for bit in range(7, -1, -1):
# Extract the current bit from the input data
value = 1 & (dat >> bit)
# Set the SRCLK pin to low
srclk.off()
# Set the value of the SDI pin
sdi.value(value)
# Clock the current bit into the shift register by setting the SRCLK pin to high
srclk.on()
# Latch the data into the storage register by setting the RCLK pin to high
rclk.on()
num = 0
# Shift data into the 74HC595 to create a moving LED pattern
for i in range(16):
if i < 8:
num = (num << 1) + 1 # Shift left and set the least significant bit to 1
elif i >= 8:
num = (num & 0b01111111) << 1 # Mask the most significant bit and shift left
hc595_shift(num) # Shift the current value into the 74HC595
print("{:0>8b}".format(num)) # Print the current value in binary format
time.sleep_ms(200) # Wait 200 milliseconds before shifting the next value
During script execution, you will see the LED light up one by one, and then turn off in the original order.
How it works?
This code is used to control an 8-bit shift register (74595), and output different binary values to the shift register, with
each value displayed on an LED for a certain period of time.
1. The code imports the machine and time modules, where the machine module is used to control hardware I/O,
and the time module is used for implementing time delays and other functions.
import machine
import time
2. Three output ports are initialized using the machine.Pin() function, corresponding to the data port (SDI),
storage clock port (RCLK), and shift register clock port (SRCLK) of the shift register.
3.10. 2.4 Microchip - 74HC595 323

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the SunFounder ESP32 and is the answer not in the manual?

SunFounder ESP32 Specifications

General IconGeneral
BrandSunFounder
ModelESP32
CategoryController
LanguageEnglish