Turning the Power of the Raspberry Pi into Interactive Devices with Buttons

A step-by-step guide on how to connect buttons to your Raspberry Pi and program them for various functions using Python. This tutorial will teach you everything from connecting a button to your Raspbe …


Updated September 20, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


A step-by-step guide on how to connect buttons to your Raspberry Pi and program them for various functions using Python. This tutorial will teach you everything from connecting a button to your Raspberry Pi to writing code that makes the button functional.

Connecting a button to your Raspberry Pi is an essential first step in building interactive devices with it. Buttons allow users to control the behavior of the device by triggering specific actions when pressed, and can be used for many different purposes such as activating functions or controlling loops in your program. In this article, we’ll show you how to connect a button to your Raspberry Pi using GPIO pins (General-Purpose Input/Output), and then walk through a simple example of how to write Python code that makes the button functional.

Part 1: Connecting the Button

Before we start programming, let’s first connect the button to your Raspberry Pi. Here are the steps you need to follow:

  1. Prepare your materials: You will need a male-to-male jumper wire, a button (we recommend using a momentary push button), and a breadboard (optional but recommended).
  2. Power off your Raspberry Pi to avoid any accidental short circuits.
  3. Locate the GPIO pins on your Raspberry Pi. They are located on the side of the board, labeled with numbers and letters (e.g., GPIO 17).
  4. Connect one end of the jumper wire to pin 17 (or any other available GPIO pin) and the other end to the button’s positive terminal (usually marked with a ‘+').
  5. Connect the button’s negative terminal (marked with a ‘-') to the ground pin on your Raspberry Pi (labeled as GND).
  6. If you are using a breadboard, connect the jumper wire and button to the appropriate rows. Ensure that the ground wire is connected to the negative side of the board.
  7. Turn on your Raspberry Pi.

Part 2: Writing Python Code for Your Button

Now that the button is connected to your Raspberry Pi, let’s write some code to make it functional. We will use the RPi.GPIO library in Python for this purpose. Here are the steps you need to follow:

  1. Install the RPi.GPIO library by running sudo apt-get install python3-rpi.gpio (for Python 3) or sudo apt-get install python-rpi.gpio (for Python 2).
  2. Open a text editor and create a new Python file, e.g., button_example.py.
  3. Import the RPi.GPIO library by adding the following line to your code: import RPi.GPIO as GPIO
  4. Set up the GPIO pins by adding the following lines:
   GPIO.setmode(GPIO.BCM) # use the Broadcom pin numbering scheme
   GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set up pin 17 as an input with a pull-up resistor
  1. Create a function that will be called when the button is pressed. For example:
   def button_pressed(channel):
       print("Button Pressed!")
  1. Add a listener to the button pin, specifying the function you just created: GPIO.add_event_detect(17, GPIO.FALLING, callback=button_pressed, bouncetime=300)
    • The GPIO.FALLING parameter means that the function will be called when the button is pressed (i.e., when the input voltage drops from high to low).
    • The bouncetime parameter specifies a delay in milliseconds to avoid detecting multiple presses due to electrical noise.
  2. Finally, add an infinite loop to keep your program running:
   try:
       while True:
           time.sleep(0.1) # wait 0.1 seconds before checking for button presses again
   except KeyboardInterrupt: # Ctrl+C will exit the program
       GPIO.cleanup() # clean up the GPIO pins on exit
  1. Save and run your Python file with sudo python3 button_example.py (or python button_example.py if you’re using Python 2).
  2. Whenever you press the button, the message “Button Pressed!” should be printed in your terminal.

You can now modify the button_pressed() function to perform any action you want when the button is pressed. For example, you could toggle an LED on and off or trigger a motor to move. The possibilities are endless!