RaspberryPi

Setting up Raspberry Pi and Button Inputs

Note: This article is a blurb. Help expand it.

Tags

Tutorial

In this tutorial you will be shown how to setup the Raspberry Pi (RPi) button input/ output using GPIO

The Linux version in this tutorial is Occidentalis by Adafruit. If you have not set up Occidentalis, take a look at this tutorial

Hardware

You will require 1 button to be connected to the Raspberry PI GPIO pin. The button must be connected via a pull-up or pull-down resistor. The button in this tutorial is connected to pin 18 (see pinout).

Software

  1. Run sudo python
  2. create file button.py using sudo nano /home/pi/button.py:
   import RPi.GPIO as GPIO
   import time

   GPIO.setmode(GPIO.BCM)
   GPIO.setup(18,GPIO.IN)

   while True:
      time.sleep(0.1)
      if (GPIO.input(18)):
         print("Button Pressed") 
  1. Run code while in the same folder with sudo python /home/pi/button.py

Place into startup script:

  1. edit /etc/rc.local via sudo nano /etc/rc.local
  2. Add startup things here above exit 0.
  3. WARNING: Add a & after the command to run process in the background
  4. Add python /home/pi/button.py &