When press button, python script runs automatically on Raspberry Pi

18,770

Solution 1

You will always need some program to monitor your input, whether it be from a keyboard, mouse, or a button wired to GPIO. In the case of the keyboard and mouse the OS provides this for you. So to trigger programs from a GPIO pin you will need to write a script much like this:

import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(18)
    if input_state == False:
        subprocess.call(something)
        # block until finished (depending on application)

Here's a button circuit (from this tutorial)

button circuit

Solution 2

A more efficient alternative to polling is to use interrupts:

#!/usr/bin/env python2.7  
# script by Alex Eames http://RasPi.tv/  
# http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio  
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  

(from http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio)

Share:
18,770
mayaah
Author by

mayaah

Just trying to learn some things.

Updated on June 06, 2022

Comments

  • mayaah
    mayaah almost 2 years

    Can't find tutorial online.

    When I press a button, I want some python script to run. I don't want to run the python script first on the Raspberry Pi's terminal and then wait for the button to be pressed like some tutorials mention. I also want the whole script to run after I press the button, not that I have to press the button for the whole duration of the script to run.

    Basically, I want the script to run without having to have a HDMI monitor or mouse connected to Raspberry Pi or a GUI thing. Just the press of a button.

    Also if anyone has diagrams on how to set up the button with the GPIO and code that would be really helpful.

    How do I do this?? I can't find anything on it and it seems so simple.