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.
- 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(12, GPIO.HIGH) # Turn LED on
time.sleep(1) # Wait 1 second
GPIO.output(12, 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(12, GPIO.HIGH) # Turn LED on
time.sleep(1) # Wait 1 second
GPIO.output(12, 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 12, 16, and 18 (physical pins 12, 16, and 18) using jumper wires.
- 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 = [12, 16, 18] # 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 = [12, 16, 18] # 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:
- 2 LEDs (e.g., red and green)
- 2 buttons
- 2 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., 2 and 3) and ground through pull-down resistors.
- Connect one LED to pin 8 (red) and the other to pin 9 (green), each with a resistor to ground.
- Programming the Microcontroller:
- Use this code to create the game
import RPi.GPIO as GPIO
import time
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
button1 = 18
button2 = 22
led1 = 12
led2 = 16
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)
# Fastest button press game
try:
while True:
if GPIO.input(button1) == GPIO.HIGH:
GPIO.output(led1, GPIO.HIGH)
time.sleep(1)
GPIO.output(led1, GPIO.LOW)
elif GPIO.input(button2) == GPIO.HIGH:
GPIO.output(led2, GPIO.HIGH)
time.sleep(1)
GPIO.output(led2, GPIO.LOW)
except KeyboardInterrupt:
GPIO.cleanup()
import time
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
button1 = 18
button2 = 22
led1 = 12
led2 = 16
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)
# Fastest button press game
try:
while True:
if GPIO.input(button1) == GPIO.HIGH:
GPIO.output(led1, GPIO.HIGH)
time.sleep(1)
GPIO.output(led1, GPIO.LOW)
elif GPIO.input(button2) == GPIO.HIGH:
GPIO.output(led2, GPIO.HIGH)
time.sleep(1)
GPIO.output(led2, GPIO.LOW)
except KeyboardInterrupt:
GPIO.cleanup()
int button1 = 2;
int button2 = 3;
int led1 = 8;
int led2 = 9;
void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
if (digitalRead(button1) == HIGH) {
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
} else if (digitalRead(button2) == HIGH) {
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led2, LOW);
}
}
int button2 = 3;
int led1 = 8;
int led2 = 9;
void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
if (digitalRead(button1) == HIGH) {
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
} else if (digitalRead(button2) == HIGH) {
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led2, LOW);
}
}