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:

Parts Needed for the Pi Button Tutorial

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:

Schema setup

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:

Fritzing Diagram for Pi Button Tutorial

  • 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:

Raspberry Pi is connected to a button and LED on a breadboard.

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!

Tony Python IDE

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.

My Pi Pinout

Button setup

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.

Pull-up Resistor Circuit Example

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 Resistor Circuit Example

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.

Program cycle

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.

Tonny Control Panel

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

Button GIF testing

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.

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

 from gpiozero import LED, Button from signal import pause 

You’ll notice that you haven’t imported the entire library. Since you are only using an LED and a button, you only need those modules in the script. We also import Pause from the signals library, which is a Python library for handling events.

Setting up pins is much easier with GPIO Zero:

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

Because the GPIO Zero library has LED and button modules, you don’t have to set up the inputs and outputs like before. You’ll notice that while the contacts haven’t changed, the numbers here are different from those above. This is because GPIO Zero only uses GPIO pin numbers (also known as Broadcom or BCM numbers).

The rest of the script consists of only three lines:

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

Here’s a call pause() just stops the script from exiting when it hits the bottom. The events of the two buttons are fired whenever the button is pressed and released. Save and run your script and you will see the same result as before!

Two Ways to Add a Button to Raspberry Pi

Of the two ways to configure the button, the GPIO Zero method seems to be the easiest. It’s still worth learning about the RPi.GPIO library, as it’s what most of the starter Raspberry Pi projects use. As simple as this project is, the knowledge can be used for a variety of purposes.

Using the GPIO pins is a great way to learn and invent your own devices, but it’s not all you can do with the Pi. Our Unofficial Raspberry Pi Guide Unofficial Guide Unofficial Guide filled with creative ideas and tutorials for you to try out for yourself! In another tutorial like this Wi-Fi connected button connect button

Похожие записи