How to add buttons on the Raspberry Pi machine

Learning to use GPIO batteries on Raspberry Pi will open your eyes to a potential world. The basic principles gained through beginner projects will be useful for both tinkering with DIY electronics and programming.

Learning to use GPIO batteries on Raspberry Pi will open your eyes to a potential world. The basic principles obtained through beginner projects will be useful for tinkering with DIY electronic devices and programming skills as well.

This tutorial shows you two ways to add a button to your Raspberry Pi. This button will be used to control the LED. The written instructions are available below the video.

How to add buttons on the Raspberry Pi machine

  1. Things to prepare
  2. Set up electrical circuits
  3. Method 1: RPi library.GPIO
    1. Set button
    2. Disconnect: Pull-up / pull-down resistors
    3. Program loop
    4. Save and run the script
  4. Method 2: Library GPIO Zero

Things to prepare

To get started, make sure you have the following components:

  1. 1 Raspberry Pi (Any model. However, today's tutorial will use model 3B)
  2. 1 push button
  3. 1 LED
  4. 1 220 Ohm resistor (the higher the better, your LED will only dim)
  5. 1 Breadboard
  6. Conductor

After preparing all the necessary tools, gather everything as shown below:

Picture 1 of How to add buttons on the Raspberry Pi machine

You also need an SD card with the Raspbian operating system installed. The fastest way to do this is with the NOOBS (New Out Of the Box Software) image. Instructions on how to do this are available in the following video:

Set up electrical circuits

You will use the GPIO pin of Pi to create the circuit. Set up your circuit according to this diagram:

Picture 2 of How to add buttons on the Raspberry Pi machine

  1. The 5v and GND pins connect to the breadboard's power line.
  2. Pin 12 (GPIO 18) connected to the anode of the LED.
  3. One foot of the resistor is attached to the cathode of the LED and the other is attached to the ground of the breadboard.
  4. Pin 16 (GPIO 23) is attached to one side of the button, the other pin is attached to the ground of the breadboard.

After setup is complete, everything will look like this:

Picture 3 of How to add buttons on the Raspberry Pi machine

Check your circuit to make sure everything is correct, and then continue with the next step on your Raspberry Pi.

Method 1: RPi library.GPIO

When Pi has started, go to the menu and select Programming> Thonny Python IDE . A new Python script will open. If you're completely unaware of Python, then it's a great language for beginners and there are many great places to learn more about Python, after you complete all the necessary steps with this tutorial!

Picture 4 of How to add buttons on the Raspberry Pi machine

Start by entering the RPi.GPIO library and setting up the board mode.

 import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) 

Now declare the variables for the button pin numbers and LED lights.

 ledPin = 12 buttonPin = 16 

Note that because you have set the board mode to BOARD, you are using the pin number instead of the GPIO number. If that confuses you, the Raspberry Pi pinout chart can help you better understand everything.

Picture 5 of How to add buttons on the Raspberry Pi machine

Set button

It's time to set up GPIO batteries. Setting the LED pin is the output and the button pin is the input along with a pull-up resistor (hanging resistor).

 GPIO.setup(ledPin, GPIO.OUT) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 

The following paragraph GPIO.IN refers to the pull-up resistor inside of the Raspberry Pi. You need to activate this resistor to receive signals from the button. Since the button is grounded, we need a pull-up resistor to hold the input pin in the HIGH position until you press it.

Before continuing, look at the pull-up and pull-down resistors.

Disconnect: Pull-up / pull-down resistors

When you configure a GPIO pin to an input, it will read that pin to determine the status of the battery. In this circuit, you need to know the pin is in HIGH or LOW position to activate the LED when pressing the button. This will be simple if it's just the states that a battery might have, but unfortunately, there's a third state of FLOATING.

A battery FLOATING has a value that lies between HIGH and LOW, causing the input to work without being unpredictable. Pull-up / pull-down resistors will solve this problem.

Picture 6 of How to add buttons on the Raspberry Pi machine

The figure above is a simple diagram with a button and a Raspberry Pi. The GPIO pin lands through the button. Pull-up resistors in attaching GPIO batteries to the internal Pi power supply. This current and battery are pulled up HIGH safely.

When you press the button, the GPIO pin connects directly to the grounded pin and the read position button at this time is LOW.

Picture 7 of How to add buttons on the Raspberry Pi machine

The pull-down resistor is when the switch is connected to the battery. This time, the internal resistor is attached to the grounded GPIO battery, holding it LOW until you press the button.

The theory of pull-up resistance and pull-down seems quite confusing, but this is an important knowledge needed when working with microcontrollers. Now, if you haven't fully understood them, don't worry!

Please continue with the next steps.

Program loop

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) 

The loop while True constantly runs code inside it until we finish the program. At each loop, it updates buttonState by reading input from buttonPin. When the button is not pressed, it is still HIGH.

When the button is pressed, buttonState becomes LOW. This activates the if statement, because False is like LOW and the LED is on. Another command will turn off the LED whenever the buttonPin button is not in False status.

Save and run the script

Save your script by clicking File> Save As and selecting the file name. You can run the sketch by clicking the green Play button on the Thonny toolbar.

Picture 8 of How to add buttons on the Raspberry Pi machine

Now press the button, and your LED will light up! Press the red Stop button at any time to stop the program.

Picture 9 of How to add buttons on the Raspberry Pi machine

If you get stuck, check the code and set up your circuit thoroughly to find the error and try again.

Method 2: Library GPIO Zero

RPi.GPIO library is great, but we still have another option. The library GPIO Zero was created by the Raspberry Pi community manager, Ben Nuttall with the intention of making the code simpler, easier to read and easier to write.

To test this new library, open a new Thonny file and enter the library.

 from gpiozero import LED, Button from signal import pause 

You will notice that you did not enter the entire library. Since you only use LEDs and buttons, you only require those modules in the script. We also imported Pause from the signal library (this is a Python library for event management).

Setting the battery is much easier with GPIO Zero:

 led = LED(18) button = Button(23) 

Because the GPIO Zero library has modules for LEDs and buttons, you don't need to set the input and output as before. You will notice that although the batteries do not change, the numbers here are different from the numbers above. That's because GPIO Zero only uses GPIO pin numbers (also known as Broadcom numbers or BCM).

The rest of the script has only three lines:

 button.when_pressed = led.on button.when_released = led.off pause() 

pause () here simply prevents the script from escaping when it runs to the end. Events involving two buttons are activated whenever the button is pressed and released. Save and run your script, then you will see the same result as before!

In two ways to add a Raspberry Pi button, the GPIO Zero method seems easiest. But RPi library.GPIO is still worth it for most beginners with Raspberry Pi projects to learn how to use. As simple as this project, knowledge about RPi.GPIO can still be used at some point.

Using GPIO batteries is a great way to learn and invent your own device, but it's a bit far from everything you can do with Pi. Refer to the article: 10 great projects that combine Raspberry Pi and Windows 10 IoT Core for more ideas.

Good luck!

See more:

  1. 10 great Arduino projects for beginners
  2. 20 great applications from micro-Raspberry Pi computers
  3. 5 great apps that make your Raspberry Pi really useful
Update 26 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile