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 | KEY PRESS CONTROL
Picture
In this guide, you'll learn how to connect three LEDs to a Raspberry Pi and write Python code to control them using keyboard input. This project is useful for understanding GPIO control and event-driven programming in Python.
SECTION 1 | MATERIAL NEEDED
  • Raspberry Pi (any model with GPIO pins)
  • 3 LEDs (red, green, blue)
  • 3 Resistors (330Ω recommended)
  • Breadboard and jumper wires
SECTION 2 | THE CONNECTIONS
Each LED has two terminals:
  1. Anode (+) connects to a Raspberry Pi GPIO pin
  2. Cathode (-) connects to GND through a 330Ω resistor

​Pin Connections
Red LED - GPIO 7 (Pin 7)
Green LED -GPIO 11 (Pin 11)
Blue LED - GPIO 13 (Pin 13)
SECTION 3 | THE CODE
Once wired, we need a Python script to read the state of the toggle switch. The script below detects when the switch is turned ON or OFF and prints a message.

In this project Tkinter is needed to capture keyboard input events in a simple GUI environment, allowing real-time interaction with the Raspberry Pi's GPIO controls.  Rather than like in the terminal where you would have to press enter after a keypress to action the command Tkinter provideds a platform to detect your keypress in real time.
  • RASPBERRY PI
  • GUI INTERFACE WITH TKINTER
<
>
import RPi.GPIO as GPIO
import time
import sys
import tkinter as tk


GPIO.setmode(GPIO.BOARD)

# Set up LED pins as outputs
GPIO.setup(7, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

def key_input(event):
    key_press = event.char.lower()
    
    if key_press == "r":
        GPIO.output(7, True)
        print("Red LED ON")
    elif key_press == "g":
        GPIO.output(11, True)
        print("Green LED ON")
    elif key_press == "b":
        GPIO.output(13, True)
        print("Blue LED ON")
    elif key_press == "x":  # Turn off all LEDs
        GPIO.output(7, False)
        GPIO.output(11, False)
        GPIO.output(13, False)
        print("All LEDs OFF")

command = tk.Tk()
command.bind("<KeyPress>", key_input)
command.mainloop()

    
  • Press keys to control LEDs:
    • Press 'r' → Red LED turns on
    • Press 'g' → Green LED turns on
    • Press 'b' → Blue LED turns on
    • Press 'x' → All LEDs turn off
SECTION 2 | TROUBLE SHOOTING
1. LEDs not turning on
  • Check wiring connections and GPIO pin assignments.
  • Ensure the correct resistor values are used to limit current.
  • Confirm that the Raspberry Pi is providing power correctly.
2. Unresponsive Key Presses
  • Ensure the Tkinter window is active and in focus when pressing keys.
  • Verify that the script runs without errors.
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.