Iot Manual
Iot Manual
To control an array of 8 LEDs using a Raspberry Pi, possibly with a shift register like the
74HC595 (often referred to as "row chip"). This setup allows you to control multiple LEDs
with fewer GPIO pins by using serial communication to shift data into the register. Here’s a
basic guide to wiring and coding this:
Components Needed:
Wiring Guide:
1. 74HC595 to Raspberry Pi:
Python Code:
def shift_out(value):
"""Shift out a byte to the 74HC595 shift register."""
# Iterate over each bit in the byte
for i in range(8):
# Set data_pin high if the corresponding bit is 1
GPIO.output(data_pin, value & (1 << (7 - i)))
# Pulse the clock to shift the bit
GPIO.output(clock_pin, GPIO.HIGH)
GPIO.output(clock_pin, GPIO.LOW)
def update_leds(value):
"""Update LEDs by shifting out a value and latching it."""
GPIO.output(latch_pin, GPIO.LOW) # Latch low to begin shifting data
shift_out(value) # Shift out the bits
GPIO.output(latch_pin, GPIO.HIGH) # Latch high to store the value
try:
while True:
for i in range(256): # Loop through values from 0-255 (8-bit range)
update_leds(i)
time.sleep(0.1)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
Explanation:
shift_out(value): Sends out 8 bits to the shift register, which controls which LEDs are turned
on.
update_leds(value): Sends data to the 74HC595 and then toggles the latch pin to display the
value.
This setup will cause the LEDs to display a binary count (0-255), lighting up different
combinations of LEDs.
Make sure to adjust the GPIO pin numbers if you connect the shift register to different
Raspberry Pi pins.