I have a piece of Python code that I use to control a relay. I am using SunFounder 2 Channel 5V Relay Shield Module for Arduino.
Please find below a Section of code in terms of how we initialise relevant GPIO and how I activate them when an Interrupt occurs.
def initGPIO (): GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.OUT, initial=GPIO.LOW)
: : :
def onLight(delay): GPIO.output(23,True) time.sleep(delay) GPIO.output(23,False)
The Issue is that Once onLight is called, the relay is always in ACTIVATE mode. this is despite the fact that I try to output 0V on GPIO 23 after a delay. Please Let us know where the Issue ispythonraspberry-pishareedit follow flagedited May 7 ’18 at 15:26Alex P.24.9k1616 gold badges9494 silver badges147147 bronze badgesasked Oct 19 ’15 at 20:44user26823693111 silver badge77 bronze badges
- The code runs only once or is in a loop ? Did you try to put a delay also after switching to
False? – secarica Nov 22 ’15 at 9:49
- @secarica: I made it work – user2682369 Nov 23 ’15 at 21:46
I read your program segment and found that it has nothing to do with interrupt. Perhaps it is the word “interrupt” in the title that scares people away.
Your code can be described by the 2 step pseudo code below.
- init GPIO pin.
- set pin high, delay, set pin low.
You did not mention the delay time. If you are using a loop with less than 10ms, or you are actually using interrupt, then it might make things complicated.
I would suggest you to remove the interrupt part, and make any loop longer than 10ms, which is the coil’s max operation time, as shown below.
I used code segment to make a loop of 100ms and 10ms loop time. I found that the coil has a delay of about 5ms. I wonder if your interrupt function is too fast to miss it.
I am using HCT125 to up shift gpio pin signal from 3V3 logic to 5V0 logic, because I worry that 3v3 signal is not strong enough (a possible cause of your circuit not working), as described below.
I have written the following test program which works with or without HCT125.
# *** relay01.02 tlfong01 2018mayhkt1627 *** #!/usr/bin/python3 # Description # Function = loop turning relay on/off # Hardware = rpi3B # Logical level 3V3 to 5V0 up shifter = HCT125 # Software = stretch 2018Apr, python3.6.4 # Relay = phtotcoupler 807C # Coil = Songle SRD-05VDC-SL-C # Note: this program works with or without the up shifter HCT125 import RPi.GPIO as GPIO from time import sleep # *** Config *** relayPin = 20 # *** Setup cleanup *** def setGpioMode(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) return def cleanupGpio(): GPIO.cleanup() return def setupDigiOutPin(digiOutPin): GPIO.setup(digiOutPin, GPIO.OUT) GPIO.output(digiOutPin, GPIO.LOW) return # *** Set high/low/toggle digiOutPin *** def setDigiOutPinHigh(digiOutPin): GPIO.output(digiOutPin, GPIO.HIGH) return def setDigiOutPinLow(digiOutPin): GPIO.output(digiOutPin, GPIO.LOW) return def toggleDigiOutPin(digiOutPin, highSeconds, lowSeconds, toggleCount): for i in range(toggleCount): setDigiOutPinHigh(digiOutPin) sleep(highSeconds) setDigiOutPinLow(digiOutPin) sleep(lowSeconds) return # *** Init/Cleanup/Test *** def init(): setGpioMode() setupDigiOutPin(relayPin) return def cleanup(): cleanupGpio() return def test(): print('Begin turning on/off relay every 100mS') init() toggleDigiOutPin(digiOutPin = relayPin, highSeconds = 0.1, lowSeconds = 0.1, toggleCount = 10000) print('End of test') cleanup() return def main(): test() return # *** Main *** if __name__ == '__main__': main() # *** End ***
The relay mentioned in your question, SunFounder 2 Channel DC 5V Relay Module with Optocoupler Low Level Trigger Expansion Board is actually Low Activate.
To verify my guess, you can do the following.
- Disconnect gpio from In1.
- Connect Gnd to In1, relay activates, led turns on.
- Disconnect Gnd from In1, which now “floats”, relay deactivates, led turns off.
- Connect 5V0 to In1, same as 3. above.
One possible cause is that your gpio logical high signal, usually 2.4V ~ 2.9V, is not high enough.
In1 might need 3.0V+ to deactivate.
One fix is to step up the GPIO signal.
- please make sure to remove referral ids from your product links in your future posts – Alex P. May 7 ’18 at 15:32
- Thanks for your reminder. I have never heard of the term “referral id”. At first it thought it was “ad referral”. I googled and found “id referral” which means a number identifying a personal who recommends a product. Anyway, I have already removed all the internet links. – tlfong01 May 8 ’18 at 1:40