Raspberry_Pi_Read_Digital_Inputs_with_Python_Buttons_and_Other_Peripherals
Raspberry_Pi_Read_Digital_Inputs_with_Python_Buttons_and_Other_Peripherals
In this guide, you’ll learn how to set the Raspberry Pi GPIOs as digital inputs and
how to read their state using a Python program. As an example, we’ll read the
state of a pushbutton (pressed or not pressed), but the example can be applied to
any other peripherals that output digital signals. We’ll use the gpiozero interface.
Table of Contents
Throughout this tutorial, we’ll cover the following main topics:
Prerequisites
Before continuing with this tutorial, check the following prerequisites.
1. Get familiar with the Raspberry Pi board—if you’re not familiar with the
Raspberry Pi, you can read our Raspberry Pi Getting Started Guide here.
2. You must know how to run and create Python files on your Raspberry Pi.
We like to program our Raspberry Pi via SSH using an extension on VS
Code. We have a detailed tutorial about that subject: Programming
Raspberry Pi Remotely using VS Code (Remote-SSH).
This means they can be used to both read and send information, allowing your Pi
to interact
with the outside world.
Most models of Raspberry Pi boards have a double row of 40 GPIO pins. The
layout of the pins is usually the same for most Raspberry Pi models.
In this tutorial, we’ll take a look at how we can set the Raspberry Pi GPIOs as
outputs to control an LED or any other actuator that can be controlled with high
(3V3) and low (0V) signals.
For example, GPIO 25 corresponds to pin 22 (see the picture below). Throughout
this tutorial, we’ll refer to GPIO pins by their GPIO numbering (Broadcom
numbering).
To learn more about the Raspberry Pi GPIOs, check the following guide:
Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all
the parts for your projects at the best price!
Reading Raspberry Pi Digital Inputs using gpiozero
(Pushbutton)
The gpiozero library provides a collection of interfaces for everyday components
like LEDs, buttons, potentiometers, sensors, and much more.
To read digital inputs, the gpiozero library provides the Button interface,
designed especially for pushbuttons, and the DigitalInputDevice for generic
digital inputs. Both interfaces work in a similar way, but use functions with different
names.
Create a new python file on your Raspberry Pi called pushbutton_led.py and copy
the following code.
led = LED(14)
button = Button(4)
button.when_pressed = led.on
button.when_released = led.off
pause()
Importing Libraries
First, you import the LED component from the gpiozero library to control the
GPIO that the LED is connected to and the Button component to interact with the
pushbutton. Then, you also need to import the pause() function from the
signal module to keep your program running so that it can detect events.
Next, you create an LED object called led that refers to GPIO 14 , which is the
GPIO that the LED is connected to. Change the number if you’re using another
GPIO.
led = LED(14)
When you create and use this LED object, your program knows that GPIO 14 is
an output that can be set to HIGH or LOW. After this declaration, you can use led
to refer to your GPIO 14 . You can use this LED object to control other
components than LEDs, as long as they can be controlled with HIGH and LOW
signals.
Declaring the pushbutton is also simple. You just need to create an instance of the
Button class. Pass as an argument the GPIO the pushbutton is connected to, in
our case, it’s GPIO 4 .
button = Button(4)
pull_up : the default value is True > the GPIO will be pulled high by
default, you need to connect the other pin of the pushbutton to GND as we
did in the circuit. If you want the button to work on the other way around, set
this flag to False and wire the other side of the pushbutton to 3.3V.
hold_time : the length of time in seconds that we must wait after the
button has been pressed to be considered that the button was held (
when_held handler )
Button Events
You can use the when_pressed and when_released handlers to detect when
the button was pressed or released and associate a function to run when each
event is detected.
when_pressed
In the following line, when the button is pressed( when_pressed ), the LED turns
on.
button.when_pressed = led.on
when_released
button.when_released = led.off
Instead of turning an LED on and off you can associate any other function that you
need to run when those button events are detected.
In the end, we call the pause() function. It keeps the program running even after
all the code has run through to detect events—in this case, it’s continuously
checking the pushbutton state.
pause()
button.when_pressed = led.toggle
pause()
In summary…
1) To read the state of a pushbutton, you can use the Button interface of the
gpiozero
library. You need to import it first like this:
button = Button(GPIO_NUMBER_OF_YOUR_CHOICE)
button.when_pressed = your_function
button.when_released = your_function
Demonstration
Save your python file. Then run it on your Raspberry Pi. Run the following
command on the directory of your project file (use the name of your file):
python pushbutton_led.py
The LED connected to GPIO 14 should light up when you press the pushbutton.
The LED will turn off when you release the pushbutton.
led = LED(14)
button = Button(4)
while True:
if button.is_pressed:
led.on()
else:
led.off()
pull_up : the default value is True > the GPIO will be pulled high by
default.
Here’s an example that turns on an LED, when the GPIO reads a HIGH signal:
led = LED(14)
input = DigitalInputDevice(4)
while True:
if input.value:
led.on()
else:
led.off()
If you want to use events instead, take a look at the following code. It works
similarly to the pushbutton example we’ve seen previously.
led = LED(14)
input = DigitalInputDevice (4)
print(input.value)
input.when_activated = led.on
input.when_deactivated =led.off
pause()
Wrapping Up
In this tutorial, you learned how to set the Raspberry Pi GPIOs as digital inputs
and how to read their state. The gpiozero library comes with a Button class
with useful functions especially for pushbuttons. There is also a
DigitalInputDevice class to use with generic input devices.
We hope you found this tutorial useful. If you’re a beginner to the Raspberry Pi,
you can get started with the following tutorials:
You can check all our Raspberry Pi projects on the following link:
Build Web Server projects with the ESP32 and ESP8266 boards to control outputs
and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server
communication protocols DOWNLOAD »
Recommended Resources
SUBSCRIBE
4 thoughts on “Raspberry Pi: Read Digital Inputs with
Python (Buttons and Other Peripherals)”
Toni
June 16, 2023 at 1:18 pm
Apart from that, it would be great to have the same thing for ESPs and the
pico as well. Speakig of: The pico seems to be a real gem, when it comes
to interrupts, timers, etc.
Reply
Sara Santos
June 21, 2023 at 10:29 am
Hi Toni.
Thanks for your suggestions.
I’ll definitely create more tutorials about the Pico and regular Raspberry
Pi soon.
Regards,
Sara
Reply
Eric Carter
July 4, 2023 at 1:04 pm
Nice tutorial. One minor issue is that if the on-wire interface is enabled via
raspi-config that defaults to GPIO4 so the code will not run. A safer bet
might be to use GPIO18
Reply
Sara Santos
July 4, 2023 at 4:37 pm
Reply
Leave a Comment
Name *
Email *
Website
Post Comment
About Support Terms and Conditions Privacy Policy Refunds Complaints’ Book