Kimovitzh
Posts: 8
Joined: Sun Dec 27, 2015 11:02 am
Location: Denmark

GPIO as trigger.

Mon Dec 28, 2015 8:44 am

Hi.

So i've got this op and running:

Code: Select all

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, 1)
And instead of it just turning on, i want it to just give a puls so it can trigger an input on my already existing homeautomation system. So just on and then off again.

Any thoughts?

Regards.
Kim.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: GPIO as trigger.

Mon Dec 28, 2015 9:31 am

At the most basic

Code: Select all

switch on
sleep
switch off
How long a pulse do you need? How accurate does it need to be?

I think the pigpio module has a trigger function that may also suit your needs: http://abyz.co.uk/rpi/pigpio/python.html#gpio_trigger
Last edited by elParaguayo on Mon Dec 28, 2015 9:34 am, edited 1 time in total.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: GPIO as trigger.

Mon Dec 28, 2015 9:33 am

you could do something like this:

Code: Select all

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, 1)
sleep(0.1)
GPIO.output(16,0)
GPIO.cleanup()
After you used the GPIO pins and your program is done, you need to run "GPIO.cleanup()" so you don't get error messages. ;)

Laurens

Kimovitzh
Posts: 8
Joined: Sun Dec 27, 2015 11:02 am
Location: Denmark

Re: GPIO as trigger.

Mon Dec 28, 2015 10:02 am

elParaguayo wrote:At the most basic

Code: Select all

switch on
sleep
switch off
How long a pulse do you need? How accurate does it need to be?

I think the pigpio module has a trigger function that may also suit your needs: http://abyz.co.uk/rpi/pigpio/python.html#gpio_trigger
I think the time on this is ok, but if i want to set up the time, how do i do then?

Kimovitzh
Posts: 8
Joined: Sun Dec 27, 2015 11:02 am
Location: Denmark

Re: GPIO as trigger.

Mon Dec 28, 2015 10:03 am

Laurens-wuyts wrote:you could do something like this:

Code: Select all

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, 1)
sleep(0.1)
GPIO.output(16,0)
GPIO.cleanup()
After you used the GPIO pins and your program is done, you need to run "GPIO.cleanup()" so you don't get error messages. ;)

Laurens
Fantastic, tanks a lot, and thanks for the heads up on the GPIO.cleanup() :)

Kimovitzh
Posts: 8
Joined: Sun Dec 27, 2015 11:02 am
Location: Denmark

Re: GPIO as trigger.

Mon Jan 11, 2016 6:57 am

Laurens-wuyts wrote:you could do something like this:

Code: Select all

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, 1)
sleep(0.1)
GPIO.output(16,0)
GPIO.cleanup()
After you used the GPIO pins and your program is done, you need to run "GPIO.cleanup()" so you don't get error messages. ;)

Laurens
Hi again, is there a way to do the same code, but instead of starting with a 1 and ending with a 0, start with 0 and end with 1?

I tried the obvious by just editing the script and replace 1 with 0 and visa versa. But I doesn't work.

Any ideas? :)

User avatar
rpdom
Posts: 17274
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: GPIO as trigger.

Mon Jan 11, 2016 7:18 am

Kimovitzh wrote:Hi again, is there a way to do the same code, but instead of starting with a 1 and ending with a 0, start with 0 and end with 1?

I tried the obvious by just editing the script and replace 1 with 0 and visa versa. But I doesn't work.

Any ideas? :)
That's because of the GPIO.cleanup. What that does is to set the GPIOs used by your program back to the default levels and modes.

So, your program sets pin 16 to output, then sets it to a "1" (high) state. Then you run GPIO.cleanup which puts it back to "0" again. Take out the cleanup if you want to leave the pin in the state you set it to.

Return to “Python”