Connection & interaction with a push button

Prerequisites : Installation of Raspbian and Installation of Python

Schema :

Here I chose the GPIO 17, but you can choose the one that suits you.

Code :

sudo nano tstBtn.py

#button interaction :
import RPi.GPIO as GPIO
import time

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

input = GPIO.input(17)

prev_input = 0
while True:
  #take a reading
  input = GPIO.input(17)
  #if the last reading was low and this one high, print
  if ((not prev_input) and input):
    print("Button pressed")
  #update previous input
  prev_input = input
  #slight pause to debounce
  time.sleep(0.05)

Execution : sudo python tstBtn.py

Exit the programme : ctrl+c

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.