Ghwana
Posts: 77
Joined: Mon Nov 13, 2017 12:04 pm

8 relay GPIO pins not working

Mon Nov 13, 2017 12:39 pm

Hi, I'm a complete novice, I've been looking at different forums, but have not come up with a solution yet.

I'm using RPi 3 Model B - latest Raspian

I'm trying to run simple Python 3 - 6.3 code to test an 8 way 5v relay which has an external 5v power sourse, which I've grd onto GPIO grd pin.

please see code below, I'm I missing something

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

# init list with pin numbers

pinList = [4,17,27,22,18,23,24,25]

# loop through pins and set mode and state to 'low'

for i in pinList:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)

# time to sleep between operations in the main loop

SleepTimeL = 0.2

# main loop

try:
while True:

for i in pinList:
GPIO.output(i, GPIO.HIGH)
time.sleep(SleepTimeL);
GPIO.output(i, GPIO.LOW)

pinList.reverse()

for i in pinList:
GPIO.output(i, GPIO.HIGH)
time.sleep(SleepTimeL);
GPIO.output(i, GPIO.LOW)

pinList.reverse()

# End program cleanly with keyboard
except KeyboardInterrupt:
print (" Quit")

# Reset GPIO settings
GPIO.cleanup()

There has been some info on forums, saying the 3V3 should be connected to COM on the relay board - Pins showing as below

VCC
GND
IN1
IN2
IN3
IN4
IN5
IN6
IN7
IN8
COM
GND

When running the code, there are not Syntax errors at all, but when connecting relays, nothing happening, just power light on.
According to Keystudio website VCC & GND are for 5v external power! Doesn't say anything about the COM??

Please help!!?

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: 8 relay GPIO pins not working

Mon Nov 13, 2017 4:04 pm

when posting code use the code tags otherwise ur code looses its indentations.

"test an 8 way 5v relay which has an external 5v power sourse, which I've grd onto GPIO grd pin." you put 5v power to GPIO ground?

does the relay module have a pin labelled jd-vcc like this type?.. http://www.considerit.co.uk/wp-content/ ... /relay.jpg
theres generally a jumper between VCC and JD VCC the jumper is removed for 3.3v raspberry or left in place to use 5v for arduiino.

i think the type ive posted is the best relay module for RPi, in this instance.
JD-VCC 5v
VCC 3.3v
GND to rpi GND and external power GND if used, personally i just powered it from the rpi 5v out and used a single GND.
then you will need to pull the GPIO low to turn on and high to turn off. (this is due to the optocoupler taking power from VCC at 3.3v then returning that to your GPIO, so if the GPIO is high 3.3v it will not create a circuit, pulling it low to 0v provides the ground for the optocoupler and energises the relay using 5v )
but if its not the relay module i have linked then it may not work, post a pic of ur module if its different.


if its still not working try a simple short bit of code, this should turn a relay on for 5 seconds, off for 5 seconds and repeat until its manually stopped.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  # using Broadcom numbering not Board pin numbers
relay1 = 21  # relay1 is BCM pin 21 (Board pin 40) in this instance but adjust for what number you are using.
GPIO.setup(relay1, GPIO.OUT, initial=1)  # pin 21 set ouput and high, meaning relay is off

while 1:
    GPIO.output(relay1, 0)  # relay turns on
    time.sleep(5)
    GPIO.output(relay1, 1)  # relay turns off
    time.sleep(5)

to work out your BCM numbers youll need to use some kind reference like this https://www.raspberrypi-spy.co.uk/wp-co ... 00x900.png found by google'ing "RPi pin out", or you could use GPIO.setmode(GPIO.BOARD) then use the actual board pin numbers rather than the numbering of the broadcom GPIO chip.

Return to “Beginners”