Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 2 of 47 · Inputs · Beginner

Button controls an LED

Read a pushbutton and let it switch an LED. This is the base of every operator panel and every machine start button.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Button controls an LED: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Green LEDLED
ButtonPush button

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1ColouredGreen LED - anode +Pin 11 (GPIO17)
2BlackGreen LED - cathode -Pin 6 (GND)
3ColouredButton - SignalPin 13 (GPIO27)
4BlackButton - GNDPin 9 (GND)

Python code (gpiozero)

from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(27)

button.when_pressed = led.on        # run led.on when pressed
button.when_released = led.off      # run led.off when released

print("Press and hold the button on the screen")
pause()                             # keep the program alive

The same program with RPi.GPIO

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)   # button to GND

try:
    while True:
        pressed = (GPIO.input(27) == 0)     # pulled up: 0 means pressed
        GPIO.output(17, pressed)
        time.sleep(0.02)
finally:
    GPIO.cleanup()

How the code works

You will learn

  • Input: reading a button
  • Events: run a function when something happens
  • Pull-up resistors

Try this

  • Make the button toggle the LED on each press: use led.toggle instead of led.on.
  • Print "pressed" and "released" with your own functions.
  • Add a second LED that is on only while the button is NOT pressed.

On a real Pi

  • Wire the button between GPIO27 and GND. The internal pull-up keeps the pin high until you press.
  • Real buttons bounce for a few milliseconds. gpiozero filters this for you.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, green1Robu.in Amazon.in ThinkRobotics
5330 ohm resistor, 1/4 W
One per LED
1Robu.in Amazon.in ThinkRobotics
6Tactile push button (12 mm, with cap)1Robu.in Amazon.in ThinkRobotics
7Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
8Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 1. Blink an LEDAll projects3. Traffic light with a walk button →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.