0
# import GPIO and datetime
import RPi.GPIO as GPIO
import datetime

# set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(37,GPIO.OUT) #Relay 1
GPIO.setup(35,GPIO.OUT) #Relay 2
GPIO.setup(33,GPIO.OUT) #Relay 3
GPIO.setup(31,GPIO.OUT) #Relay 4
GPIO.setup(32,GPIO.OUT) #Relay 5
GPIO.setup(36,GPIO.OUT) #Relay 6
GPIO.setup(38,GPIO.OUT) #Relay 7
GPIO.setup(40,GPIO.OUT) #Relay 8

# Turn lights on and off based on the time
try:
    while True:
        now = datetime.datetime.now().time()
        if now.hour == 9 and now.minute == 5:
            print(now)
            GPIO.output(40,True)
        else:
            GPIO.output(40,False)

finally:
# cleanup the GPIO before finishing :)
    GPIO.cleanup()

I have a separate power supply for the relay with 5v to JD-vcc, GND to GND and vcc to 3.3v on raspberry pi.

print(now)gives me the current system time but nothing on the relay. all i want for now is to switch the relays on and of at specific times. I am new to this any help is appreciated.

7
  • Troubleshooting suggestions: (1) When you do "GPIO.cleanup()" at the very end of code, all GPIO pins you setup earlier as OUTPUT mode will automatically return to INPUT mode, which is the /init/default/reset at power on mode. This "Automatically-return-all-GPIO-pins-to-input-mode-before-exit" is to prevent the always messing up things newbie forgetting to switch off the nuclear reactor and melt down or blow up something. / to continue, ...
    – tlfong01
    Mar 28, 2020 at 8:37
  • (2) Your JD-Vcc relay might be of "Low level activate/trigger" type designed for Arduino and might not work with Rpi. You might like to give us the link to your relay, so everybody can check it out. The following two Q&A links give more details: (a) "Rpi cannot activate 5V Relay with optical isolator and JD-Vcc jumper": raspberrypi.stackexchange.com/questions/95862/… (b) "Rpi GPIO 5V Relay Problem": raspberrypi.stackexchange.com/questions/99988/….
    – tlfong01
    Mar 28, 2020 at 8:48
  • 1
    Post a photo of your wiring.
    – CoderMike
    Mar 28, 2020 at 9:37
  • 2
    @tlfong01 thank you for responding. my 5v power supply to the relay board had insufficient current. I switched that out and it solved my problem.
    – ela kay
    Mar 28, 2020 at 9:41
  • 1
    @elakay Please make your comment about the solution an answer and accept it after two days with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Mar 28, 2020 at 13:42

1 Answer 1

1

In my case the current from my power supply to the relay module was not sufficient at 1.0 amps. I swapped it out with one rated at 2.0 amps and that got my relays switching. Thank you all.

2
  • Thank you for your answer. Yes, usually a 5V relay switch such as Songle or TongLing has roughly 80mA activation/saturation current. So if ALL 8 relay channel switches are on, the total current might be of the order 1A. (The Rpi GPIO pint current to trigger the relay module (not the relay switch) is only less than 10mA. / to continue, ...
    – tlfong01
    Mar 29, 2020 at 8:02
  • References: (1) "KY019 5V Relay Module Rpi GPIO Signal Current and Relay Swtich Current": raspberrypi.org/forums/viewtopic.php?f=37&t=77158#p1323061 (2) "Songle/Tongling 5V Relay Switch Activate/Deactivate Current": raspberrypi.org/forums/… (3) "Songle/Tongling 5V Relay Switch Activate/Deactivate Current": raspberrypi.org/forums/…
    – tlfong01
    Mar 29, 2020 at 8:02

Your Answer

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

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