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

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


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

Execution : sudo python

Exit the programme : ctrl+c

Leave a Reply

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