User avatar
raspberrypiguy1
Posts: 379
Joined: Sun Sep 02, 2012 7:01 pm

TUTORIAL: How to get a button working with the Pi!

Sat Feb 09, 2013 12:56 pm

Dear forumers!

This is just a quick patch of code for one of my older tutorials! Feel free to watch it all here:

http://www.youtube.com/watch?feature=en ... GdtAA&NR=1

And here is the code I use in the program!

Code: Select all

import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)

while True:
    input_value = gpio.input(17)
    if input_value == False:
        print('The button has been pressed...')
        while input_value == False:
            input_value = gpio.input(17)
Be careful you copy the correct indentation, otherwise Python will throw up some nasty errors!

The Raspberry Pi Guy
Last edited by raspberrypiguy1 on Sun Sep 15, 2013 9:41 am, edited 1 time in total.
Matt, The Raspberry Pi Guy YouTube channel, author of Learn Robotics with Raspberry Pi, available now: http://mybook.to/raspirobots, Computer Science & Electronics Undergraduate at The University of Edinburgh

User avatar
rew
Posts: 435
Joined: Fri Aug 26, 2011 3:25 pm

Re: TUTORIAL: How to get a button working with the Pi!

Sun Feb 10, 2013 9:42 am

Hi,

On my system where I trigger a snapshot, I configure the pin with a pullup. This saves having to mount the pullup externally.
(I happen to use a different pin).

Code: Select all

GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Oh. And in my script I have a "time.sleep (0.1)" in both cases where the switch is stable. Otherwise the CPU will be polling as fast as it can all the time, wasting power and crowding out other programs that might need to do something.
Check out our raspberry pi addons: https://www.bitwizard.nl/shop/

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: TUTORIAL: How to get a button working with the Pi!

Sun Feb 10, 2013 9:52 am

Here's mine...

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
prev_input = 0
while True:
  input = GPIO.input(17)
  if ((not prev_input) and input):
    os.system("sudo shutdown -h now")
  prev_input = input
  time.sleep(0.05)
Uses basically no appreciable CPU, and catches the button press regardless of length, and only catches it once.

User avatar
raspberrypiguy1
Posts: 379
Joined: Sun Sep 02, 2012 7:01 pm

Re: TUTORIAL: How to get a button working with the Pi!

Sun Feb 10, 2013 10:28 am

Ahhh! Great! Looks good! Might even update my tutorial! Maybe!

The Raspberry Pi Guy
Matt, The Raspberry Pi Guy YouTube channel, author of Learn Robotics with Raspberry Pi, available now: http://mybook.to/raspirobots, Computer Science & Electronics Undergraduate at The University of Edinburgh

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: TUTORIAL: How to get a button working with the Pi!

Sun Feb 10, 2013 8:38 pm

Don't credit me for it though. It's cobbled from code lifted here, there, and everywhere.

:oops:

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: TUTORIAL: How to get a button working with the Pi!

Sun Feb 10, 2013 9:22 pm

tonyhughes wrote:Here's mine...

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
prev_input = 0
while True:
  input = GPIO.input(17)
  if ((not prev_input) and input):
    os.system("sudo shutdown -h now")
  prev_input = input
  time.sleep(0.05)
Uses basically no appreciable CPU, and catches the button press regardless of length, and only catches it once.
I've taken the liberty to simplify your code while preserving the functionality:

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
while True:
    if GPIO.input(17):
        os.system("sudo shutdown -h now")
    prev_input = input
    time.sleep(0.05)
pythonhosted.org/RPIO


User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: TUTORIAL: How to get a button working with the Pi!

Mon Feb 11, 2013 6:28 pm

metachris wrote:

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
while True:
    if GPIO.input(17):
        os.system("sudo shutdown -h now")
    prev_input = input
    time.sleep(0.05)
After reading over it again I saw I missed the 'prev_input=...' line. Also I'd advise to add the pulldown resistor at GPIO.setup:

Code: Select all

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
while True:
    if GPIO.input(17):
        os.system("sudo shutdown -h now")
    time.sleep(0.05)
You could also do the same with interrupts like this (using my GPIO2 module):

Code: Select all

import GPIO2

def do_shutdown(*args):
    os.system("sudo shutdown -h now")

GPIO2.add_interrupt_callback(17, do_shutdown, edge='rising')
GPIO2.wait_for_interrupts()
pythonhosted.org/RPIO

RussoNC
Posts: 48
Joined: Fri Sep 07, 2012 6:53 am
Location: New Caledonia
Contact: Website

Re: TUTORIAL: How to get a button working with the Pi!

Mon Feb 11, 2013 9:36 pm

Thank you !! This is EXACTLY that I was looking for, i'm new in this the raspberry pi's GPIO so it's a little confusing, so thank you so much for your tutorials, continue like this! :D
Whant to see MySQL, nginx in action on the Raspberry pi ? Check my site: www.pirepository.com

Rosenkrantz
Posts: 11
Joined: Thu Sep 17, 2015 2:42 pm

Re: TUTORIAL: How to get a button working with the Pi!

Fri Jan 29, 2016 11:25 am

Hello does any of you guys know how to use a button and an led. When the button is pressed the led will light until you press the button again?

Hope for help

gordon77
Posts: 4934
Joined: Sun Aug 05, 2012 3:12 pm

Re: TUTORIAL: How to get a button working with the Pi!

Fri Jan 29, 2016 12:09 pm

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.OUT) # led, pin 18 to gnd (with approx 300 ohm resistor)
GPIO.output(18,GPIO.LOW)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button, pin 12 to gnd (with approx 300 ohm resistor)
led = 0

while True:
   if GPIO.input(12) == False and led == 0:
      GPIO.output(18, GPIO.HIGH)
      led = 1
      time.sleep(.5) # debounce
   elif GPIO.input(12) == False and led == 1:
      GPIO.output(18, GPIO.LOW)
      led = 0
      time.sleep(.5) # debounce

paulv
Posts: 562
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: TUTORIAL: How to get a button working with the Pi!

Fri Jan 29, 2016 12:29 pm

You may not realize this, but several of the solutions presented here actually rely on switch or button bounce and glitches.
The ultimate test for your routine would be to use a potmeter (10..50K) between the 3V3 and ground, and feed the wiper with a 10K resistor (to avoid loading) to the GPIO input. Something like this :
Hysteresis Measurement.png
Hysteresis Measurement.png (9.16 KiB) Viewed 30149 times
If you are surprised and want to know why, have a look here :viewtopic.php?uid=52264&f=28&t=134394&start=0

Success!
Last edited by paulv on Fri Jan 29, 2016 8:40 pm, edited 1 time in total.

gordon77
Posts: 4934
Joined: Sun Aug 05, 2012 3:12 pm

Re: TUTORIAL: How to get a button working with the Pi!

Fri Jan 29, 2016 1:21 pm

It works for me whatever it relies on. :D

I know there are problems with edge detection, so I don't use that.

Gordon

gurudattakr123
Posts: 2
Joined: Wed Mar 15, 2017 4:07 pm

Re: TUTORIAL: How to get a button working with the Pi!

Fri Mar 17, 2017 5:03 pm

I am using 3 buttons for three different functions. For example, when i press 1st button it must display 1st button, for 2nd it must display 2nd etc. Help me with this. I am new to python.

Code: Select all

from gpiozero import Button
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
button1=17
button2=27
button3=22
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(button3,GPIO.IN,pull_up_down=GPIO.PUD_UP)
print('press a button')
while(1):
      if GPIO.input(17)==0:
           print('button1 pressed')
      elif GPIO.input(27)==0:
           print('button2 pressed')
      else :
           print('button3 pressed')
.

Thank you

Frankie Yip
Posts: 1
Joined: Mon Mar 21, 2016 5:11 am

Re: TUTORIAL: How to get a button working with the Pi!

Sun Jun 11, 2017 10:54 am

gurudattakr123 wrote:I am using 3 buttons for three different functions. For example, when i press 1st button it must display 1st button, for 2nd it must display 2nd etc. Help me with this. I am new to python.

Code: Select all

from gpiozero import Button
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
button1=17
button2=27
button3=22
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(button3,GPIO.IN,pull_up_down=GPIO.PUD_UP)
print('press a button')
while(1):
      if GPIO.input(17)==0:
           print('button1 pressed')
      elif GPIO.input(27)==0:
           print('button2 pressed')
      else :
           print('button3 pressed')
.

Thank you
Hi gurudattakr123
May I know is your above code workable. Cause I have download and unable to run the code.
Thanks

Return to “Automation, sensing and robotics”