Raspberry Pi‎ > ‎SANKI Device‎ > ‎

Button


Button Interrupts 

Hardware

 
 
 


Pin Connect

 Raspberry Pi Button 
PIN 23  PIN A 
 GND GND 




Python

$ sudo nano botton-interrrupt.py
  

#!/usr/bin/env python2.7 import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # GPIO 23 set up as input. It is pulled up to stop false signals GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) print "Make sure you have a button connected so that when pressed" print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n" raw_input("Press Enter when ready\n>") print "Waiting for falling edge on port 23" # now the program will do nothing until the signal on port 23 # starts to fall towards zero. This is why we used the pullup # to keep the signal high and prevent a false interrupt print "During this waiting time, your computer is not" print "wasting resources by polling for a button press.\n" print "Press your button when ready to initiate a falling edge interrupt." try: GPIO.wait_for_edge(23, GPIO.FALLING) print "\nFalling edge detected. Now your program can continue with" print "whatever was waiting for a button press." except KeyboardInterrupt: GPIO.cleanup() # clean up GPIO on CTRL+C exit GPIO.cleanup() # clean up GPIO on normal exit


Test and Result


Test :

$ sudo python botton-interrrupt.py



Button - Run OS Task


Python

Press button to run program and print out result.

  • Install Fortune
  • $ sudo apt-get install fortune
  • Test 
  • fortune -s science

$ sudo nano bottonOS.py
  

#!/usr/bin/env python
from time import sleep
import os 
import RPi.GPIO as GPIO

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

while True:

if ( GPIO.input(23) == False ):
print("Printing")
os.system("fortune -s science | python printer_device.py")
print("Printed")



sleep(1)
$ sudo nano printer_device.py
  

import printer, textwrap, sys 

p=printer.ThermalPrinter(serialport="/dev/ttyAMA0") 
unwrapped_text = sys.stdin.read() 
wrapped_text = textwrap.fill(unwrapped_text, 32
p.print_text(wrapped_text)
p.linefeed() 
p.linefeed() 
p.linefeed()

Modify the /etc/rc.local file to run this program anytime.

sudo nano /etc/rc.local 


Add this before exit0 :

cd /home/pi/printer/ && python gpio_test.py &

Test Press Button

$ sudo python checkbutton.py

#**************************** Checkbutton.py
import RPi.GPIO as GPIO
from time import sleep     # this lets us have a time delay (see line 12)
GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering
GPIO.setup(25, GPIO.IN)    # set GPIO 25 as input

def checkbutton()
   try:
        # this will carry on until you hit CTRL+C
        if GPIO.input(25): # if port 25 == 1
            print "Port 25 is 1/GPIO.HIGH/True - button pressed"
        else:
            print "Port 25 is 0/GPIO.LOW/False - button not pressed"
        sleep(0.1)         # wait 0.1 seconds

   except KeyboardInterrupt:
       GPIO.cleanup()         # clean up after yourself

while True: 
checkbutton()

Test Press Button 2


  1. #!/usr/bin/env python
  2.  
  3. from time import sleep
  4. import os
  5. import RPi.GPIO as GPIO
  6.  
  7. GPIO.setmode(GPIO.BCM)
  8. GPIO.setup(23, GPIO.IN)
  9. GPIO.setup(24, GPIO.IN)
  10. GPIO.setup(25, GPIO.IN)
  11.  
  12. while True:
  13. if ( GPIO.input(23) == False ):
  14. os.system('mpg321 binary-language-moisture-evaporators.mp3 &')
  15. if ( GPIO.input(24) == False ):
  16. os.system('mpg321 power-converters.mp3 &')
  17. if ( GPIO.input(25)== False ):
  18. os.system('mpg321 vader.mp3 &')
  19. sleep(0.1);

Reference

  1. http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
  2. http://geekgurldiaries.blogspot.hk/2012_12_01_archive.html