Pritee Badgujar Internet of Things (IoT)
Practical - 2
Aim: Displaying different LED patterns with Raspberry Pi.
Objective:
Connect electronic components safely to the
Raspberry Pi's GPIO pins.
Write Python scripts to control the state of the LEDs
(on/off).
Implement various LED patterns, such as simple blinking,
chasing, etc,
Gain foundational knowledge for more complex IoT (Internet
of Things) and automation projects.
Materials Required:
Raspberry Pi (e.g., Raspberry Pi 3 B+)
Jumper Wires
LEDs (Light Emitting Diodes) board
Step 1: Hardware Setup (Wiring)
S.No Rsp Pi GPIO Rsp Pi PIN Board Pin
number number Name
1 GPIO 2 PIN 3 D0 (1)
2 GPIO 3 PIN 5 D1 (2)
3 GPIO 4 PIN 7 D2 (3)
4 GPIO 17 PIN 11 D3 (4)
Chalk and Duster
Pritee Badgujar Internet of Things (IoT)
5 GPIO 27 PIN 13 D4 (5)
6 GPIO 22 PIN 15 D5 (6)
7 GPIO 10 PIN 19 D6 (7)
8 GPIO 9 PIN 21 D7 (8)
9 GND PIN 6 GND (0)
Step 2: Create Python file “Practical2.py” and
write the following code:
import RPi.GPIO as GPIO
import time
# Define GPIO pins connected to D0–D7
led_pins = [2, 3, 4, 17, 27, 22, 10, 9]
GPIO.setmode(GPIO.BCM)
# Setup all pins as output
for pin in led_pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
# Basic patterns
def all_on():
for pin in led_pins:
Chalk and Duster
Pritee Badgujar Internet of Things (IoT)
GPIO.output(pin, GPIO.HIGH)
def all_off():
for pin in led_pins:
GPIO.output(pin, GPIO.LOW)
def blink_all(times=3, delay=0.5):
for _ in range(times):
all_on()
time.sleep(delay)
all_off()
time.sleep(delay)
def chasing(delay=0.1):
for pin in led_pins:
GPIO.output(pin, GPIO.HIGH)
time.sleep(delay)
GPIO.output(pin, GPIO.LOW)
def ping_pong(delay=0.1):
for pin in led_pins + led_pins[::-1]:
GPIO.output(pin, GPIO.HIGH)
time.sleep(delay)
GPIO.output(pin, GPIO.LOW)
# Run test patterns
try:
while True:
blink_all()
Chalk and Duster
Pritee Badgujar Internet of Things (IoT)
chasing()
ping_pong()
except KeyboardInterrupt:
print("Exiting...")
finally:
all_off()
GPIO.cleanup()
run the file.
Chalk and Duster