COMPUTER SCIENCE CAFÉ
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
Picture
RASPBERRY PI | LED PROJECT
Picture
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:
  1. Gather Components:
    • 1 LED (any color)
    • 1 resistor (calculated previously, e.g., 220Ω)
    • Breadboard
    • Jumper wires
    • Microcontroller (e.g., Arduino)
  2. 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.​
Picture
3: Programming the Microcontroller:
  • RASPBERRY PI
  • ARDUINO
<
>
​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
​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
}
SECTION 2 | CONTROL MULTIPLE LEDs
Learn how to connect and control multiple LEDs in sequence or pattern.
Step-by-step Guide:
  1. Gather Components:
    • 3 LEDs (different colors, if available)
    • 3 resistors (one for each LED)
    • Breadboard
    • Jumper wires
    • Raspberry Pi
  2. 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.
Picture
3. Programming with Python:
  • Use this Python script to create a blinking pattern
  • RASPBERRY PI
  • ARDUINO
<
>
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()
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);
}

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:
  1. 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
  2. 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.
  3. Programming the Microcontroller:
    • Use this code to create the game
  • RASPBERRY PI
  • WITH MULTI THREADING
<
>
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 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()

Picture
SUGGESTIONS
We would love to hear from you
SUBSCRIBE 
To enjoy more benefits
We hope you find this site useful. If you notice any errors or would like to contribute material then please contact us.