Relay Problem

Viewed 10 times

So I am using a 16 channel relay that is connected to my Pi (https://www.amazon.com/dp/B0057OC66U/). I made a small program to turn all the relay LEDs on and then turn them off one by one with a 1 second interval. My problem is that the GPIO.cleanup() at the end of the program seems to be what turns the LEDs off and the GPIO.output(bcm,GPIO.LOW) seems to be ignored in the “tryblock”. Basically now all the program does is turn all the relay LEDs on in the for loop and then prints up to “SIXTEEN” and finally the GPIO.cleanup() is what turns all the LEDs off at once.

import RPi.GPIO as GPIO

import time


# init list with pin numbers

SleepTimeL = 1

pinList = [2, 3, 4, 17, 27, 22, 10, 9, 11, 5, 6, 13, 19, 26, 21, 20]

for i in pinList:

    GPIO.setup(i, GPIO.OUT)

    GPIO.output(i, GPIO.HIGH)


    GPIO.output(2, GPIO.LOW)

    print ("ONE")


    GPIO.output(3, GPIO.LOW)

    print ("TWO")


    GPIO.output(4, GPIO.LOW)

    print ("THREE")


    GPIO.output(17, GPIO.LOW)

    print ("FOUR")


    GPIO.output(27, GPIO.LOW)

    print ("FIVE")


    GPIO.output(22, GPIO.LOW)

    print ("SIX")


    GPIO.output(10, GPIO.LOW)

    print ("SEVEN")


    GPIO.output(9, GPIO.LOW)

    print ("EIGHT")


    GPIO.output(11, GPIO.LOW)

    print ("NINE")


    GPIO.output(5, GPIO.LOW)

    print ("TEN")


    GPIO.output(6, GPIO.LOW)

    print ("ELEVEN")


    GPIO.output(13, GPIO.LOW)

    print ("TWELVE")


    GPIO.output(19, GPIO.LOW)

    print ("THIRTEEN")


    GPIO.output(26, GPIO.LOW)

    print ("FOURTEEN")


    GPIO.output(21, GPIO.LOW)

    print ("FIFTEEN")


    GPIO.output(20, GPIO.LOW)

    print ("SIXTEEN")


    print ("Good bye!")

except KeyboardInterrupt:

  print ("Quit")

 # Reset GPIO settings

  • It is unclear what your question is. GPIO.cleanup() should restore pins to their initial state (which is INPUT HIGH) – Milliways 1 hour ago
  • The GPIO.cleanup() is what turns the LEDs off at the end of the program for some reason and the GPIO.output(bcm,GPIO.LOW) doesnt turn the LEDs off one by one which it should @Milliways – Leobd 1 hour ago
  • And the GPIO.cleanup() turns all the LEDs off at once, So basically this code only turns all the lights on at once, Prints up to 16 and then turns all the lights off at once – Leobd 42 mins ago
  • @Leobd, Ah, let me see. I guess your problem is the very common Rpi newbie who has bought a Arduino compatible, but NOT Rpi compatible low level trigger on relay problem of “Low-Level-Triggered-5V-Relay-Can-Be-Turned-On-By-Low-Level-Signal-But-Cannot-Be-Turned-Off-By-High-Level-Because-Rpi-3V-High-Level-Is-Lower-Than-Arduino’s-4.2V-High-Level-Required-To-Turn-Off-Relay-And-One-Get-Around-Is-To-Set-GPIO-Pin-To-Input-Mode-Or-Clean-Up-Which-Is-Equivalent-To-Make-GPIO-Pin-Set-To-Same-Level-As-Relay-Vcc-Of-5V-And-Therefore-Can-Turn-Off-Relay. – tlfong01 35 mins ago   
  • youtube.com/watch?v=TFt480sxNWg&t=694s This guy is using the same 12V relay and its working with the Pi. So there is no way for me to specify what “channel” that I want to turn off, I would need to use GPIO.cleanup() for that every time? @tlfong01 – Leobd 31 mins ago
  • @Leobd, See Appendix B of my answer to the following question: raspberrypi.stackexchange.com/questions/99988/…. – tlfong01 31 mins ago   
  • @Leobd, Well, clean up or set to input mode is a common newbie get around. But there is a 1% chance that your Rpi will silicon latch up and fry itself. You can search “latch up”, “latching up” in this or the other Rpi forum, to know more about latching up. One other safer solution is to step up GPIO pin’s 3V3 signal to 5V. – tlfong01 26 mins ago   
  • @Leobd, And if wish to know more why Low level trigger Arduino Relay is so many Rpi newbie’s sorrow, weeping in the dark, :(, you might like to read my posts of a very long story (more than 100 posts!) starting from here: raspberrypi.org/forums/viewtopic.php?f=37&t=77158#p1323061. Happy reading and learning! 🙂 – tlfong01 21 mins ago   
  • Thanks! Do you mean unplugging the two jumper wires that goes to the Rpi 5V and connecting them to the two 3v3 pins? Would that not fry the Rpi aswell since its 5V output from the relay? @tlfong01 – Leobd 19 mins ago
  • @tlfong01 And is there any 10+ channel relays for the Rpi? – Leobd 18 mins ago
  • Could you edit your question and include a close up photo of the relay control pins? In particular what connections have you made between the ground and voltage pins and the Pi? – joan 11 mins ago
  • @Leobd, and in case you are too lazy to read my long relay story, you might like to read carefully the first of the three more critical posts below: (1) raspberrypi.org/forums/… (2) raspberrypi.org/forums/… (3) raspberrypi.org/forums/…. Chart in post 1 summaries good guys and e bad guys. And a quick and dirty answer to your question is: “Almost all of the High Level Triggered Relays are OK for Rpi. – tlfong01 8 mins ago   
  • @Leobd, Sorry, I don’t understand your question of unplugging jumper wires, because I don’t wish to read it. I apologize that I almost never debug/troubleshoot user problems by reading their codes, photos, and wiring diagrams. I help by explaining why some work, some do not work, and point them references to learn to help themselves. Ah, sorry, now is jogging time for me. See you late this evening or tomorrow. Good luck. – tlfong01 57 secs ago   Edit   
  • The ground pins from the relay connects to Rpi pins 34 and 39. The 5V pins from the relay connects to the Rpi pin 2 and 4 @joan – Leobd 31 secs ago

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.

%d bloggers like this: