Push buttons are an essential component in many Raspberry Pi projects and have many uses. 

In this guide, we will connect a momentary push button to a Raspberry Pi and program it using the GPIO Zero library. Upon button press, it will print out the words, "Button was pressed!"

After completing this guide, you will have gained a basic understanding of how to wire up and program push buttons with the Raspberry Pi.
    • Insert the Push Button so that it's legs are in Tie Points G36, G38, D36 and D38.

    • Connect the Push Button to GPIO with a Red Jumper between tie points B36 and A6.

    • Connect the Push Button to Ground with a Black Jumper between tie points B38 and A20.

    • Now that we have the Raspberry Pi connected to the push button, we will need to program it. To do so, we will install GPIO Zero, a Python library which builds upon existing GPIO libraries such as RPI.GPIO, rPIO, and pigpio. It will simplify the process by reducing boilerplate code. 

    • After opening a terminal window, enter the following: sudo apt-get update 

    • Then enter: sudo apt-get install python3-gpiozero

    • Click on the Raspberry Pi icon on the top left hand corner to access the main menu. 

    • Click on Programming > Python 3 (IDLE). 

    • Create a new file by clicking File > New File. 

    • Next, save the file by clicking File > Save

    • Name the file

  1. from gpiozero import Button
    btn = Button(4)
    while not btn.is_pressed:
    print("Button was pushed!")
    • First, we will import the Button class from gpiozero library: from gpiozero import Button

    • Then we will assign the variable btn to GPIO4: btn = Button(4)

    • A never-ending while loop is required here. So use the command: 
      while not btn.is_pressed:

    • When the button is pushed, the message "Button was pushed" will be printed: print("Button was pushed!")