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.

Dirk
  • 3,372
  • 3
  • 16
  • 25
ela kay
  • 19
  • 3
  • 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 '20 at 08: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": https://raspberrypi.stackexchange.com/questions/95862/relay-status-leds-are-blinking-but-the-relay-coil-is-not-activating/95881#95881 (b) "Rpi GPIO 5V Relay Problem": https://raspberrypi.stackexchange.com/questions/99988/rpi-gpio-controlling-5v-relay-problem/100021#100021. – tlfong01 Mar 28 '20 at 08:48
  • 1
    Post a photo of your wiring. – CoderMike Mar 28 '20 at 09:37
  • You haven't specified what module you are using, but it is probably one of the sub-standard devices "designed" for Arduino (like dozens of others). See https://raspberrypi.stackexchange.com/a/100014/8697 No fiddling with code will make it work. – Milliways Mar 28 '20 at 09:41
  • 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 '20 at 09:41
  • I am glad to hear that you have solved the problem yourself. Have a great weekend. Cheers. – tlfong01 Mar 28 '20 at 12:39
  • 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 '20 at 13:42

1 Answers1

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.

ela kay
  • 19
  • 3
  • 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 '20 at 08:02
  • References: (1) "KY019 5V Relay Module Rpi GPIO Signal Current and Relay Swtich Current": https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158#p1323061 (2) "Songle/Tongling 5V Relay Switch Activate/Deactivate Current": https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&sid=afe85828d05507bb6a7edb0bb104a6d9&start=25#p1324588 (3) "Songle/Tongling 5V Relay Switch Activate/Deactivate Current": https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&sid=afe85828d05507bb6a7edb0bb104a6d9&start=25#p1326822 – tlfong01 Mar 29 '20 at 08:02