0% found this document useful (0 votes)
21 views38 pages

Iot Manual

Uploaded by

sp6585486
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views38 pages

Iot Manual

Uploaded by

sp6585486
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Display array of 8 led using Raspberry Pi

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:

1. Raspberry Pi (any model)


2. 8 LEDs
3. 8 Resistors (e.g., 220Ω for current limiting)
4. 74HC595 Shift Register
5. Breadboard and Jumper Wires

Wiring Guide:
1. 74HC595 to Raspberry Pi:

74HC595 Raspberry Function RPI Physical Pin


VCC 5V 2
GND GND 6
DIN GPIO 17 11
CLK GPIO 27 13

Python Code:

import RPi.GPIO as GPIO


import time

# Define GPIO pins connected to the 74HC595 shift register


data_pin = 17 # DS pin
latch_pin = 22 # ST_CP pin
clock_pin = 27 # SH_CP pin

# Set up GPIO mode and pins


GPIO.setmode(GPIO.BCM)
GPIO.setup(data_pin, GPIO.OUT)
GPIO.setup(latch_pin, GPIO.OUT)
GPIO.setup(clock_pin, GPIO.OUT)

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.

You might also like