hharrysidh
Posts: 2
Joined: Sat Jan 06, 2018 5:58 pm

Python GPIO add_event_detect

Sat Jan 06, 2018 6:14 pm

Hello Everyone,

I am new to python pi development. But I have pretty good understand what is happing and happened to turn on LED from the program and from the button.

What I am trying to do
But I am trying to achieve turn on the LED from the button in a specific way. I want to Turn on LED when the button is press without use LOOP and without using wait for wait_for_edge event to occur.

HOW I DID IT
In the Following program, you can see I achieved turn ON LED for 5 sec by Adding the add_event_detect and than wait for that event to happen.
I can also do that by LOOP. I also tried without wait_for_edge and loop DIDN't worked.

HOW I WOULD LIKE IT
I will like to achieve this by only setting up GPIO add_event_detect and callback method without LOOP or wait for edge method and exist the program. Then the program will run in the background.

Code: Select all

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

btn_input = 4;
LED_output = 17;

# GPIO btn_input set up as input.
GPIO.setup(btn_input, GPIO.IN)
GPIO.setup(LED_output, GPIO.OUT)

# handle the button event
def buttonEventHandler (pin):
    # turn LED on/off
    GPIO.output(LED_output,True)
    time.sleep(5)
    GPIO.output(LED_output,False)

GPIO.add_event_detect(btn_input, GPIO.RISING, callback=buttonEventHandler)  
try:  
    GPIO.wait_for_edge(btn_input, GPIO.FALLING)  
except:
    GPIO.cleanup()      


Thanks in advance

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Python GPIO add_event_detect

Sat Jan 06, 2018 8:54 pm

You need to have 2 separate event handlers. ! for rising/turning on and a second for falling turning off.

The following code will do what you want forever till control-C is pressed. You will add your program where the "while True: pass" statement is atm

Code: Select all

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

btn_input = 4;
LED_output = 17;

# GPIO btn_input set up as input.
GPIO.setup(btn_input, GPIO.IN)
GPIO.setup(LED_output, GPIO.OUT)

# handle the button event
def buttonEventHandler_rising (pin):
    # turn LED on
    GPIO.output(LED_output,True)
    
def	def buttonEventHandler_falling (pin):
    # turn LED off
    GPIO.output(LED_output,False)


	
GPIO.add_event_detect(btn_input, GPIO.RISING, callback=buttonEventHandler_rising) 
GPIO.add_event_detect(btn_input, GPIO.FALLING, callback=buttonEventHandler_falling)
 
try:  
    while True : pass  
except:
    GPIO.cleanup()      


hharrysidh
Posts: 2
Joined: Sat Jan 06, 2018 5:58 pm

Re: Python GPIO add_event_detect

Sun Jan 07, 2018 6:31 pm

Thanks for quick reply

Two things:

First one:
How can I run this script in the background? So, I don't use two terminal windows.
I don't want to run this in one terminal window and rest of the things in other terminal windows. e.g for example if I can convert this to service or something

if yes how to start, stop and see if it's running

Second:
Is it possible to run this script without LOOP? Like if I see events for pin and that's it and no LOOP or event wait are required.

Btarlton
Posts: 1
Joined: Wed Jun 27, 2018 1:50 am

Re: Python GPIO add_event_detect

Wed Jun 27, 2018 1:57 am

To run it in the back ground simply add an '&' after the python command to run it:

Code: Select all

python myapp.py &
See this link for more info:
https://kb.iu.edu/d/afnz

If you want it to run as soon as the PI boots up, add this command to your /etc/rc.local to have it kick off the program automatically once the pi is up. (I'm assuming you are running Raspbian or the like).

To kill the program you'd have to know the Process id and kill it. Killing the process is covered in the link above as well.

Good luck!

jeffrey.davis
Posts: 1
Joined: Wed May 08, 2019 5:06 pm

Re: Python GPIO add_event_detect

Wed May 08, 2019 5:10 pm

Hello,
I have been looking around for some information on what (PIN) means in the following line:

def buttonEventHandler (pin): # handle the button event

i see the same line od code sometimes with nothing in the parentheses. I am having trouble understanding what PIN means in there. Might anybody be able to help me out?

thanks,

jeff

Code: Select all

#!/usr/bin/env python

import time
import RPi.GPIO as GPIO


# handle the button event
def buttonEventHandler (pin):
    print "handling button event"

    # turn the green LED on
    GPIO.output(25,True)

    time.sleep(1)

    # turn the green LED off
    GPIO.output(25,False)



# main function
def main():

    # tell the GPIO module that we want to use 
    # the chip's pin numbering scheme
    GPIO.setmode(GPIO.BCM)

    # setup pin 23 as an input
    # and set up pins 24 and 25 as outputs
    GPIO.setup(23,GPIO.IN)
    GPIO.setup(24,GPIO.OUT)
    GPIO.setup(25,GPIO.OUT)

    # tell the GPIO library to look out for an 
    # event on pin 23 and deal with it by calling 
    # the buttonEventHandler function
    GPIO.add_event_detect(23,GPIO.FALLING)
    GPIO.add_event_callback(23,buttonEventHandler,100)

    # turn off both LEDs
    GPIO.output(25,False)
    GPIO.output(24,True)

    # make the red LED flash
    while True:
        GPIO.output(24,True)
        time.sleep(1)
        GPIO.output(24,False)
        time.sleep(1)


    GPIO.cleanup()



if __name__=="__main__":
    main()

tqhien
Posts: 79
Joined: Thu Feb 02, 2012 10:07 am

Re: Python GPIO add_event_detect

Fri May 10, 2019 3:35 pm

The "pin" parameter is simply the GPIO that called the callback function.

You could define the same callback to multiple pins and print(pin) inside the function to view which one was triggered.

Hien.

byroncar
Posts: 7
Joined: Tue Apr 30, 2019 6:33 pm
Location: Norway

Python GPIO add_event_detect

Tue May 14, 2019 1:07 am

Good morning,

try to use SysGPIO. It is mauch easier and better to work with yout GPIO.

best regards

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 324
Joined: Fri Jan 06, 2012 7:46 pm

Re: Python GPIO add_event_detect

Thu May 16, 2019 7:10 pm

If you're new to this, you might want to have a look at the gpiozero library: https://gpiozero.readthedocs.io/en/stable/recipes.html. It makes Python on the Pi so much simpler.

It sounds like you're looking for something like this: https://gpiozero.readthedocs.io/en/stab ... rolled-led

Andyroo

Re: Python GPIO add_event_detect

Thu May 16, 2019 7:32 pm

There is also a very good book to help with GPIO Zero and basic electronics available as a free PDF here though it can be purchased as a printed copy to help the Foundation.

Return to “Python”