RASPBERRY PI | LED PROJECT
SECTION 1 | CONTROL AN LED
Learn how to connect and control a single LED using a breadboard and a microcontroller (e.g., Arduino, Raspberry Pi).
Step-by-step Guide:
Step-by-step Guide:
- Gather Components:
- 1 LED (any color)
- 1 resistor (calculated previously, e.g., 220Ω)
- Breadboard
- Jumper wires
- Microcontroller (e.g., Arduino)
- Wiring Instructions:
- Insert the LED into the breadboard (longer leg = positive/anode, shorter leg = negative/cathode).
- Connect the resistor to the LED's shorter leg (negative).
- Use a jumper wire to connect the other end of the resistor to the breadboard's GND pin.
- Connect another jumper wire from the LED’s positive leg to pin 13 on the microcontroller.
3: Programming the Microcontroller:
import RPi.GPIO as GPIO
import time
# Set up GPIO
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(12, GPIO.OUT) # Set pin 12 as an output
# Blink the LED
try:
while True:
GPIO.output(13, GPIO.HIGH) # Turn LED on
time.sleep(1) # Wait 1 second
GPIO.output(13, GPIO.LOW) # Turn LED off
time.sleep(1) # Wait 1 second
except KeyboardInterrupt:
GPIO.cleanup() # Reset GPIO settings
import time
# Set up GPIO
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(12, GPIO.OUT) # Set pin 12 as an output
# Blink the LED
try:
while True:
GPIO.output(13, GPIO.HIGH) # Turn LED on
time.sleep(1) # Wait 1 second
GPIO.output(13, GPIO.LOW) # Turn LED off
time.sleep(1) # Wait 1 second
except KeyboardInterrupt:
GPIO.cleanup() # Reset GPIO settings
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // Turn LED on
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn LED off
delay(1000); // Wait for 1 second
}
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // Turn LED on
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn LED off
delay(1000); // Wait for 1 second
}
SECTION 2 | CONTROL MULTIPLE LEDs
Learn how to connect and control multiple LEDs in sequence or pattern.
Step-by-step Guide:
Step-by-step Guide:
- Gather Components:
- 3 LEDs (different colors, if available)
- 3 resistors (one for each LED)
- Breadboard
- Jumper wires
- Raspberry Pi
- Wiring Instructions:
- Connect the 3 LEDs to separate rows on the breadboard.
- Attach resistors to the cathode (negative leg) of each LED, connecting them to a GND pin.
- Connect the anodes (positive legs) to GPIO pins 7, 11, and 13 (physical pins 7, 11, and 13) using jumper wires.
3. Programming with Python:
- Use this Python script to create a blinking pattern
import RPi.GPIO as GPIO
import time
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
leds = [7, 11, 13] # Define LED pins
for pin in leds:
GPIO.setup(pin, GPIO.OUT)
# Blink the LEDs in sequence
try:
while True:
for pin in leds:
GPIO.output(pin, GPIO.HIGH) # Turn LED on
time.sleep(0.5) # Wait 0.5 seconds
GPIO.output(pin, GPIO.LOW) # Turn LED off
except KeyboardInterrupt:
GPIO.cleanup()
import time
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
leds = [7, 11, 13] # Define LED pins
for pin in leds:
GPIO.setup(pin, GPIO.OUT)
# Blink the LEDs in sequence
try:
while True:
for pin in leds:
GPIO.output(pin, GPIO.HIGH) # Turn LED on
time.sleep(0.5) # Wait 0.5 seconds
GPIO.output(pin, GPIO.LOW) # Turn LED off
except KeyboardInterrupt:
GPIO.cleanup()
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
digitalWrite(8, HIGH);
delay(500);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
}
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
digitalWrite(8, HIGH);
delay(500);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
}
SECTION 3 | FASTEST TO THE BUTTON GAME
Build an interactive game where players compete to press a button faster, lighting up an LED for the winner.
Step-by-step Guide:
Step-by-step Guide:
- Gather Components:
- 3 LEDs (e.g., red and green)
- 2 buttons
- 3 resistors for LEDs, 2 pull-down resistors for buttons (10kΩ)
- Breadboard
- Jumper wires
- Microcontroller
- Wiring Instructions:
- Connect each button to a digital pin (e.g., 18 and 22) and ground through pull-down resistors.
- Connect one LED to pin 7 (red) and the other to pin 11 (green), and 13 each with a resistor (dependning on the LED you are using 220Ω) to ground.
- Programming the Microcontroller:
- Use this code to create the game
import RPi.GPIO as GPIO
import time
import random
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
button1 = 18
button2 = 22
led1 = 7
led2 = 11
start_led = 13 # Third LED to signal the game start
GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(start_led, GPIO.OUT)
print("Game Ready! Press Ctrl+C to exit.")
try:
while True:
input("Press Enter to start a new round...")
# Random delay before start signal
delay = random.uniform(2, 5)
print(f"Get ready... (waiting {round(delay, 1)}s)")
time.sleep(delay)
# Turn on start LED
GPIO.output(start_led, GPIO.HIGH)
duration = random.randint(1, 10)
print(f"Start LED ON for {duration} seconds!")
time.sleep(duration)
GPIO.output(start_led, GPIO.LOW)
print("GO! Press your button!")
# Wait for a button press after the start LED goes OFF
start_time = time.time()
winner = None
while True:
if GPIO.input(button1) == GPIO.HIGH:
winner = "Player 1"
GPIO.output(led1, GPIO.HIGH)
break
elif GPIO.input(button2) == GPIO.HIGH:
winner = "Player 2"
GPIO.output(led2, GPIO.HIGH)
break
end_time = time.time()
reaction_time = round(end_time - start_time, 3)
print(f"{winner} wins! Reaction time: {reaction_time} seconds")
# Keep LED on for a second, then turn both off
time.sleep(1)
GPIO.output(led1, GPIO.LOW)
GPIO.output(led2, GPIO.LOW)
except KeyboardInterrupt:
print("\nGame exited.")
GPIO.cleanup()
import time
import random
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
button1 = 18
button2 = 22
led1 = 7
led2 = 11
start_led = 13 # Third LED to signal the game start
GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(start_led, GPIO.OUT)
print("Game Ready! Press Ctrl+C to exit.")
try:
while True:
input("Press Enter to start a new round...")
# Random delay before start signal
delay = random.uniform(2, 5)
print(f"Get ready... (waiting {round(delay, 1)}s)")
time.sleep(delay)
# Turn on start LED
GPIO.output(start_led, GPIO.HIGH)
duration = random.randint(1, 10)
print(f"Start LED ON for {duration} seconds!")
time.sleep(duration)
GPIO.output(start_led, GPIO.LOW)
print("GO! Press your button!")
# Wait for a button press after the start LED goes OFF
start_time = time.time()
winner = None
while True:
if GPIO.input(button1) == GPIO.HIGH:
winner = "Player 1"
GPIO.output(led1, GPIO.HIGH)
break
elif GPIO.input(button2) == GPIO.HIGH:
winner = "Player 2"
GPIO.output(led2, GPIO.HIGH)
break
end_time = time.time()
reaction_time = round(end_time - start_time, 3)
print(f"{winner} wins! Reaction time: {reaction_time} seconds")
# Keep LED on for a second, then turn both off
time.sleep(1)
GPIO.output(led1, GPIO.LOW)
GPIO.output(led2, GPIO.LOW)
except KeyboardInterrupt:
print("\nGame exited.")
GPIO.cleanup()
import RPi.GPIO as GPIO
import time
import random
import threading
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
button1 = 18
button2 = 22
led1 = 7
led2 = 11
start_led = 13 # LED to signal start
GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(start_led, GPIO.OUT)
# Shared flag for early press
early_press_detected = False
early_loser = None
# Function to monitor early presses
def detect_early_press():
global early_press_detected, early_loser
while not early_press_detected:
if GPIO.input(button1) == GPIO.HIGH:
early_press_detected = True
early_loser = "Player 1"
break
elif GPIO.input(button2) == GPIO.HIGH:
early_press_detected = True
early_loser = "Player 2"
break
time.sleep(0.01)
print("Game Ready! Press Ctrl+C to exit.")
try:
while True:
input("\nPress Enter to start a new round...")
# Reset flags
early_press_detected = False
early_loser = None
# Start early press detection in a separate thread
monitor_thread = threading.Thread(target=detect_early_press)
monitor_thread.start()
# Random countdown before start LED turns off
delay = random.uniform(2, 5)
print(f"Get ready... (waiting {round(delay, 2)}s)")
GPIO.output(start_led, GPIO.HIGH)
time.sleep(delay)
GPIO.output(start_led, GPIO.LOW)
if early_press_detected:
print(f"{early_loser} pressed too early and LOSES this round!")
# Wait for thread to finish cleanly
monitor_thread.join()
continue # Skip to next round
print("GO! Press your button!")
# Wait for a valid button press
winner = None
while not winner:
if GPIO.input(button1) == GPIO.HIGH:
winner = "Player 1"
GPIO.output(led1, GPIO.HIGH)
elif GPIO.input(button2) == GPIO.HIGH:
winner = "Player 2"
GPIO.output(led2, GPIO.HIGH)
print(f"{winner} wins!")
time.sleep(1)
GPIO.output(led1, GPIO.LOW)
GPIO.output(led2, GPIO.LOW)
except KeyboardInterrupt:
print("\nGame exited.")
GPIO.cleanup()
import time
import random
import threading
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
button1 = 18
button2 = 22
led1 = 7
led2 = 11
start_led = 13 # LED to signal start
GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(start_led, GPIO.OUT)
# Shared flag for early press
early_press_detected = False
early_loser = None
# Function to monitor early presses
def detect_early_press():
global early_press_detected, early_loser
while not early_press_detected:
if GPIO.input(button1) == GPIO.HIGH:
early_press_detected = True
early_loser = "Player 1"
break
elif GPIO.input(button2) == GPIO.HIGH:
early_press_detected = True
early_loser = "Player 2"
break
time.sleep(0.01)
print("Game Ready! Press Ctrl+C to exit.")
try:
while True:
input("\nPress Enter to start a new round...")
# Reset flags
early_press_detected = False
early_loser = None
# Start early press detection in a separate thread
monitor_thread = threading.Thread(target=detect_early_press)
monitor_thread.start()
# Random countdown before start LED turns off
delay = random.uniform(2, 5)
print(f"Get ready... (waiting {round(delay, 2)}s)")
GPIO.output(start_led, GPIO.HIGH)
time.sleep(delay)
GPIO.output(start_led, GPIO.LOW)
if early_press_detected:
print(f"{early_loser} pressed too early and LOSES this round!")
# Wait for thread to finish cleanly
monitor_thread.join()
continue # Skip to next round
print("GO! Press your button!")
# Wait for a valid button press
winner = None
while not winner:
if GPIO.input(button1) == GPIO.HIGH:
winner = "Player 1"
GPIO.output(led1, GPIO.HIGH)
elif GPIO.input(button2) == GPIO.HIGH:
winner = "Player 2"
GPIO.output(led2, GPIO.HIGH)
print(f"{winner} wins!")
time.sleep(1)
GPIO.output(led1, GPIO.LOW)
GPIO.output(led2, GPIO.LOW)
except KeyboardInterrupt:
print("\nGame exited.")
GPIO.cleanup()