Learning how to use the GPIO pins on your Raspberry Pi opens up a whole world of possibilities. The basic principles learned in beginner projects pave the way for useful knowledge in DIY electronics and programming.
This tutorial will show you two ways to add a button to your Raspberry Pi project. The button will be used to control the LED. Written instructions are available below the video.
You will need
First, make sure you have the following components:
- 1 x Raspberry Pi (Anyone will do, model 3B is used in this tutorial)
- 1 x button
- 1 x LED
- 1 x 220 ohm resistor (higher values are ok, your LED will just be dimmer)
- 1 x breadboard
- connect wires
Once assembled, you should have components that look something like this:
You will also need an SD card with the Raspbian operating system installed. The fastest way to do this is to use the NOOBS (New Software Out of the Box) image. Instructions on how to do this are available in this video:
You’ll be using the Pi’s GPIO pins to create the circuit, and if you’re unfamiliar with them, our Raspberry Pi GPIO pins guide will help. The circuit here is almost the same as our previous Raspberry Pi LED project, with the addition of the button you’ll be using today.
Set up your schema according to this schema:
- Contacts 5 V and GND connected to the board’s power bus.
- Pin 12 (GPIO 18) connected to the positive branch of the LED.
- one branch resistor connects to the negative leg of the LED, and the other leg connects to the breadboard’s ground rail.
- Pin 16 (GPIO 23) attached to one side of the button and the other side to the breadboard ground rail.
Once that’s set up, here’s what it should look like:
Check if the schematic is correct and then turn on your Raspberry Pi.
Method 1: RPi.GPIO Library
After the Pi boots up, go to the menu and select » Programming» > «Thonny Python IDE» . A new Python script will open. If you’re completely new to Python, it’s a great language for beginners, and there are many great places to learn more about Python after you’re done with this tutorial!
Start by importing the RPi.GPIO library and setting the board mode.
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD)
Now declare variables for LED and button numbers.
ledPin = 12 buttonPin = 16
Please note that since we have board mode set to BOARD, we use pin numbers, not GPIO numbers. If this confuses you, the Raspberry Pi pinout diagram will help you figure it out.
It’s time to set up the GPIO pins. Set LED pin to pin and button pin to input with pull-up resistor
GPIO.setup(ledPin, GPIO.OUT) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
The text after GPIO.IN refers to internal pull-up resistor Raspberry Pi. You must enable this to get a clean read from the button. Since the button goes to the ground pin, we need a pull-up resistor to keep the input pin HIGH until you push it.
Before we continue, let’s take a look at pull-up and pull-down resistors.
Interval: Pull Up/Pull Down Resistors
When you configure a GPIO pin for input, it reads that pin to determine its state. In this circuit, you need to read if the output is HIGH or LOW, to turn on the LED when the button is pressed. This would be simple if these were the only states a pin could have, but unfortunately there is a third state: FLOATING .
The floating pin has a value between high and low, resulting in unpredictable entry actions. The solving problems are solved by pull-up/pull-down resistors.
The image above is a simplified diagram of a button and a Raspberry Pi. The GPIO pin is connected to ground via a button. An internal pull-up resistor connects the GPIO pin to the Pi’s internal power supply. This current flows and the pin rises safely to HIGH.
When you press the button, the GPIO pin connects directly to the ground pin and the button shows low.
Pull-down resistors are provided to connect the switch to the power pin. This time, an internal resistor pins the GPIO pin to ground, holding it LOW until you press the button.
The theory of Pull-up and Pull-down resistors is confusing at first glance, but it is important knowledge when working with microcontrollers. For now, if you don’t quite get it, don’t worry!
Let’s continue where we left off.
Next, set up the program loop:
while True: buttonState = GPIO.input(buttonPin) if buttonState == False: GPIO.output(ledPin, GPIO.HIGH) else: GPIO.output(ledPin, GPIO.LOW)
Cycle while True continuously executes the code inside it until we terminate the program. Every time it loops it updates buttonState reading input from buttonPin . As long as the button is not pressed, it remains HIGH .
Once the button is pressed, buttonState becomes LOW . It starts if statement because the False coincides with LOW and the LED turns on. Operator else disables the LED when buttonPin is not set to False.
Save and run your script
Save the script by clicking » File» > «Save As» and choosing a filename. You can start the sketch by pressing the green button playback on the Thonny toolbar.
Now press the button and your LED should light up! Press the red button Stop at any time to stop the program
If you’re having trouble, carefully check the code and schema for errors and try again.
Method 2: GPIO null library
The RPi.GPIO library is fantastic, but there is a new kid on the block. The GPIO null library was created by Raspberry Pi community manager Ben Nuttall with the goal of making code easier and more readable and writeable.