Uncategorized

relay always on problem

Interrupts with Python on the Raspberry Pi and RPi.GPIO does not work properly

Ask QuestionAsked 4 years, 9 months agoActive 2 years, 2 months agoViewed 478 timeshttps://tpc.googlesyndication.com/safeframe/1-0-37/html/container.htmlReport this ad0

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

add a commentstart a bounty

1 Answer

ActiveOldestVotes0

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.

  1. init GPIO pin.
  2. 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.

example coil data

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.

100mS loop
10mS loop, showing 5mS delay

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.

  1. Disconnect gpio from In1.
  2. Connect Gnd to In1, relay activates, led turns on.
  3. Disconnect Gnd from In1, which now “floats”, relay deactivates, led turns off.
  4. 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.

photocoupler driven relay
relay test results
enter image description here

shareeditdeleteflagedited May 8 ’18 at 1:35answered May 7 ’18 at 0:37tlfong0116522 silver badges66 bronze badges

  • 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    

add a comment

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.