2

I'm wondering if it's possible to have my momentary button control a relay in Python whilst I'm holding the button down?

Currently, I'm able to control the relay with a small python script, switching it on for a duration of 5 secondes:

relay.py:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)

GPIO.output(4, GPIO.LOW)
time.sleep(5)
GPIO.output(4, GPIO.HIGH)
GPIO.cleanup()

print "Done!"

And able to track when the momentary button is being hold down:

button.py:

import os
from time import sleep
import RPi.GPIO as GPIO

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

while True:
        if (GPIO.input(25) == False):
                print "Button being pushed!"
        sleep(0.025)

Yet, I'm unable to combine the two. Any help would be really appreciated!

1
  • 1
    It seems like a straightforward task but you need to understand a little about programming and Python. Have you tried any of the on-line Python tutorials?
    – joan
    May 26, 2016 at 10:44

2 Answers 2

2

I'd suggest giving the new gpiozero library a try. It's intended to simplify GPIO programming and sits atop the RPi.GPIO library you're currently using. Rather than concentrating on pins, it's focus is on representing the devices attached to pins which tends to make reasoning about the program a bit easier. Here's several different ways of connecting the operation of your relay with your button using gpiozero. First a straight-forward declarative method:

from gpiozero import Button, OutputDevice

relay = OutputDevice(4)
btn = Button(25)

while True:
    if btn.is_pressed:
        relay.on()
    else:
        relay.off()

Next, a method using callbacks:

from gpiozero import Button, OutputDevice
from signal import pause

relay = OutputDevice(4)
btn = Button(25)

btn.when_pressed = relay.on
btn.when_released = relay.off
pause()

Finally, a declarative method:

from gpiozero import Button, OutputDevice
from signal import pause

relay = OutputDevice(4)
btn = Button(25)

relay.source = btn.values
pause()

This last one effectively says "set the state of the relay to the state of the button" (various functions are available in gpiozero.tools to do things like inverting state, combining states of multiple devices, etc.)

I'd strongly recommend reading the Recipes chapter in the docs which we're always adding new stuff too.

1
  • This code doesn't seem to work. Atleast the OutputDevice bit. Relay goes on as soon as I initialize OutputDevice and ignores calls to on() or off(). The relay goes off only when you close the device :-(
    – sumitkm
    Jul 24, 2017 at 22:19
1

One solution is to use interrupts with some debounce time , as expressed on this tutorial , if you want to switch on the relay button on button press , the code should look like this

#!/usr/bin/env python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4, GPIO.OUT, )
# GPIO 25 set up as an input, pulled down, connected to 3V3 on button press
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(25, GPIO.RISING, callback=my_callback, bouncetime=500)

def my_callback(channel):

    print "falling edge detected on 25"
    GPIO.output(4,0) #Turn on Relay
    time.sleep(5)    #wait 5 seconds
    GPIO.output(4,1) #Turn off relay

try:
    while 1:
        time.sleep(1)

except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

you can increase the debounce time for blocking new interrupts for a certain period of time , debounce time is explained on this tutorial

I would recomend to read all 3 parts of the tutorial , and make a code yourself , for a better understanding of how it works , hope this helps

EDIT , if you want the relay to be on while you are pressing the button , you can substitute this code into the callback

GPIO.remove_event_detect(25)
while GPIO.input(25):
    GPIO.output(4,0) #Turn on Relay
GPIO.output(4,0) #Turn on Relay
GPIO.add_event_detect(25, GPIO.RISING, callback=my_callback, bouncetime=500)

please note that this code assumes bcm pin numbering , and gpio 25 is interrupted by a 3v3 signal (from the raspberry 3v3 pin where you connect your pushbutton to activate it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.