RASPBERRY PI GETTING STARTED SERIES

Read A Button With GPIOZERO

A momentary button is an input device. It is essentially a sensor with only two possible states: “pressed” and “not pressed”. In your circuit, you have connected the output of the button to GPIO14. Your Python program will check the voltage at GPIO14 to determine if the button is pressed or not.

You now know how to “do” output. It’s time to learn “input”.

A momentary button is an input device. It is essentially a sensor with only two possible states: “pressed” and “not pressed”. In your circuit, you have connected the output of the button to GPIO14. Your Python program will check the voltage at GPIO14 to determine if the button is pressed or not.

Unlike with the LED example, this time we’ll go straight to write a regular Python program. This is because we want to get Python to read the state of GPIO14 many times per second, so that as soon as we press the button, Python can print a message on the screen to let us know if has detected the press.

We can also do this on the CLI, but it will be a clunky implementation which I prefer to avoid.

Use Vim to create a new Python program:

$ vim button_gpiozero.py

Copy this code into the Vim buffer:

from gpiozero import Button
import time
button = Button(14,None,True)
while True:
    if button.is_pressed:
        print("Button is pressed")
    else:
        print("Button is not pressed")
    time.sleep(0.1)

Save the buffer to disk, and quit Vim (“:wq”).

There’s a few new elements in this code, so lets take a moment to learn.

In the first line, you are importing the Button module, another member of the gpiozero library.

In the second line you import the “time” module, so that you can insert a tiny delay later in your program.

This is important because without this delay, you program will sample the button GPIO as fast as your Raspberry Pi can, leaving little time for anything else. We only want to read the state of a button, not to totally dominate the CPU.

Line three is a little challenging because it contains three parameters. You can find detailed information about these parameters in the gpiozero documentation for the Button object. The first one is the GPIO number. Since the button is connected to GPIO14, you’ll enter “14” in the first parameter.

The second parameter controls the type of pull-up/down resistor we are using. The Raspberry Pi can use internal pull-up resistors resistor, but in our circuit we have provided an external pull-down resistor. This resistor ensures that voltage at GPIO14 is equal to GND when the button is not pressed.

For an article on pull-up/down resistors, please have a look at this article.

Because of the presence of the external pull-down, we use “None” as the value of the second parameter.

In the third parameter, we include the active state value. Because when the button is pressed the voltage at GPIO14 is “HIGH”, and when the button is not pressed the voltage is “LOW”, we use the “True” value for this parameter. If the voltages were reversed (i.e. pressed buttons created a “LOW” voltage, and not pressed button created “HIGH”), then for the third parameter we would write “False”.

You have saved the configured Button object in the “button” variable, and then get into an infinite loop.

In the look, your program simply reads the state of the button and if it is pressed, it prints “Button is pressed” to the console. If it isn’t pressed, it prints “Button is not pressed”.

Run the program like this:

$ python3 button_gpiozero.py

Now press the button as see how the message on the console changes accordingly. It should look like this:

A button was just pressed.

It works! 

Ready for some serious learning?

Start right now with Raspberry Pi Full Stack - Raspbian

This is our most popular Raspberry Pi course & eBook.


This course is a hands-on project designed to teach you how to build an Internet-of-Things application based on the world’s most popular embedded computer.


You will learn how to build this application from the ground up, and gain experience and knowledge with technologies such as...


  • The Linux operating system and the command line, 
  • The Python programming language,
  • The Raspberry Pi General Purpose Input Output pins (GPIOs), 
  • The Nginx web server,
  • The Flask Python web application microframework,
  • JQuery and CSS for creating user interfaces,
  • How to deal with timezones, 
  • How to create charts with Plotly and Google Charts, 
  • How to do datalogging with Google Sheet, 
  • How to create applets with IFTTT,
  • How to secure your application with SSL.
  • We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time

    Image
    {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}