Raspberry Pi Push button interface

shape
shape
shape
shape
shape
shape
shape
shape
blog-details
December, 2019

RPi Digital Input

Digital Input that means; feed digital data to uC or uP, In our case RPi take the digital I/P  by help of GPIO pin(General purpose Input Output pin) for example pushbutton, keypad,  

In Digital language we have two things that is ‘0’ n ‘1’; ‘0’ means GND(LOW) and ‘1’ means 5v(HIGH) on the other hand we can say ‘0’ means OFF and ‘1’ means ON

So those devices/sensors output like either 0 (GND/LOW) or 1 (5v/HIGH) we can say these are Digital Input devices; Don’t be confused Sensor’s output means it is input for uC(Micro controller)/uP(Micro Processor).

Find some digital input device below

 

INSTALL DEPENDENCY:

  • Check any Python package installed in RPi
  • Open terminal paste below code
python

python3

  • If python, python3 and GPIO library of python is Not installed copy the below code and paste in terminal.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python
sudo apt-get install pyhton3
sudo apt-get install python-rpi.gpio python3-rpi.gpio

REQUIRED COMPONENTS

  1. Raspberry Pi (setup)- 1pic
  2. Momentary Push button – 1pic
  3. Resistor 10K, 220E – 1pic, 1pic
  4. LED (3.5mm) -1pic
  5. Breadboard- 1pic
  6. Jumper wire – as per requirement

CIRCUIT CONNECTION:

Pull-Up / Active Low:

  • Above graph show the behavior of GPIO3 i.e Pin5 of RPi
  • According to Pull-up concept initially (Normal Condition/ not pushed) Pin5 is in HIGH state when button is pressed Pin5 goes LOW
  • As per pull-up concept Pin5 get activate signal at LOW Pulse; so, on the other hand we can say this is Pull-Up/ Active LOW method

CODE:

CODE FOR PUSH BUTTON (PULL-UP)

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)#SET PIN 5 to pull up

while 1 :#infinite Loop
    buttonstate = GPIO.input(5) # digital data read from pin 5
    if buttonstate == 0 :
        print("PUSH BUTTON PRESSED")
        GPIO.output(3,True)
    if buttonstate == 1 :
        print("PUsh button release!!!")
        GPIO.output(3,False)

 

CODE DESCRIPTION

import RPi.GPIO as GPIO
import time
RPi.GPIO means General Purpose Input Output Python library for Raspberry pi import this library as a GPIO
import time library for some time delay.

GPIO.setwarnings(False) – for not show runtime warnings

GPIO.setmode(GPIO.BOARD) if u use GPIO.BOARD mode that means we declare pin as board configuration
GPIO.setup(3, GPIO.OUT)//Declare pin3 as output pin
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)#SET PIN 5 input pin which is connected in pull up method.

buttonstate = GPIO.input(5) # digital data read from pin 5

if buttonstate == 0 :
print(“PUSH BUTTON PRESSED”)
GPIO.output(3,True)
if buttonstate == 1 :
print(“PUsh button release!!!”)
GPIO.output(3,False)
As discussed above according to pull up methode normally pin5 in HIGH/1 state and when button pressed it goes LOW/0 (i.e buttonstate == 0) that time led goes on which connected to pin 3

 

 

NOTE:

  • Save above code in .py 
    • E.g.:  pushbutton .py

OUTPUT:

.

CODE FOR PUSH_BUTTON PUSH COUNT

#Tested code by Robogenesis
import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)#SET PIN4 to pull up resistor

lastbuttonstate = 0
buttonstate = 0
buttoncount = 0
y=0
while 1 :
    buttonstate = GPIO.input(5)
    if lastbuttonstate != buttonstate :
        if buttonstate == False :
            y=y+1
            buttoncount = buttoncount+1
            print (buttoncount)
    lastbuttonstate = buttonstate
    if y == 1 :
        GPIO.output(3,True)
    elif y == 2 :
        GPIO.output(3,False)
        y=0

 

NOTE:

  • Save above code in .py 
    • E.g.:  pushbutton_count .py

OUTPUT:

 

 

 

Comments

client

John Smith

Lorem ipsum, dolor sit amet consectetur adipisicing elit. Et minus, saepe porro a voluptatem, quidem aut libero consequatur unde molestiae quae impedit accusantium dolor est corporis! Dolores ut dignissimos doloribus?

Jan 19, 2018 - 9:10AM Reply
client

Doe John

Lorem ipsum, dolor sit amet consectetur adipisicing elit. Et minus, saepe porro a voluptatem, quidem aut libero consequatur unde molestiae quae impedit accusantium dolor est corporis! Dolores ut dignissimos doloribus?

Jan 19, 2018 - 9:10AM Reply
client

Steven Doe

Lorem ipsum, dolor sit amet consectetur adipisicing elit. Et minus, saepe porro a voluptatem, quidem aut libero consequatur unde molestiae quae impedit accusantium dolor est corporis! Dolores ut dignissimos doloribus?

Jan 19, 2018 - 9:10AM Reply
client

John Cina

Lorem ipsum, dolor sit amet consectetur adipisicing elit. Et minus, saepe porro a voluptatem, quidem aut libero consequatur unde molestiae quae impedit accusantium dolor est corporis! Dolores ut dignissimos doloribus?

Jan 19, 2018 - 9:10AM Reply

Leave a Reply