3

I'm trying to connect a small one line relay to my Raspberry Pi but the relay doesn't react. The red light on the relay is on the whole time.

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

GPIO.setmode(GPIO.BCM)

# init list with pin number

pinList = [27]

# loop through pin and set mode and state to 'high'

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

# time to sleep between operations in the main loop

SleepTimeL = 2

# main loop

try:

  GPIO.output(27, GPIO.LOW)
  print "FOUR"
  time.sleep(SleepTimeL);
  GPIO.cleanup()
  print "Good bye!"

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

  # Reset GPIO settings
  GPIO.cleanup()

Photo of Pi and Relay

4
  • What connections have you made between the Pi and the relay board? – joan Sep 19 '15 at 16:58
  • it is so weird because it works if I use wiringPI :/ but I don't know c so I would like that it works using python. The connection is: Pin 1 ---> VCC Pin 6 ----> GND Pin 13 ----> IN. Maybe there is a conflict between python code and c code so it doesn't work? – user30407 Sep 19 '15 at 17:28
  • I'm getting such alert in my terminal after I try to execute this code above: script1.py:14: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. – user30407 Sep 19 '15 at 17:40
  • Ignore the warning. It's not relevant. Does the relay operate when you connect a ground pin or a 3V3 pin (pin 1 or pin 17) to IN? – joan Sep 19 '15 at 19:05
1

My 8 relay module is set up to control the heating and air conditioning in a remote building. I run 5v to all the relay coils - that is the high side. I run GPIO pins to the low side. The GPIO pins are normally high - 3.3v. When a GPIO pin is made to go down it looks like ground to the relay to which it is attached and the relay operates/closes.

#      GPIO09 -  Turn on or off a specified relay so
#                it stays on or off after the program ends 
#                at power on all pins are in input state ready to sense hi or lo
#    

import RPi.GPIO as GPIO                      ## Import GPIO library
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)                     ## Use board pin numbering

pinarr=[3,5,7,11,13,15,19,21]
dirarr=["on","off"]
devarr=["Air Conditioning","Heater","Unused","Unused","Unused","Unused","Unused","Unused"]

##  Use a dictionary as a lookup table to give a name to the GPIO_function return code
pin_use = {0:"GPIO.OUT",1:"GPIO.IN",40:"GPIO.SERIAL",41:"GPIO.SPI",
42:"GPIO.I2C",43:"GPIO.HARD_PWM",-1:"GPIO.UNKNOWN"}

                   
for i in range(0,8):                         ##  set pins to be outputs and at 3.3 volts  
    GPIO.setup(pinarr[i], GPIO.OUT)

for i in range(0,8):
    print "\n"
                     
print("GPIO09 Starting")

##for i in range(0,8):                         ##  Print current relay usage   
##  fno = GPIO.gpio_function(pinarr[i])      ##  get function number
##  print "Relay " + str(i+1) + " pin " + str(pinarr[i]) + " usage is " +  pin_use[fno]
    
while 1 == 1:
    
    for i in range(0,8):                         ##  get and display pin on/off status  
        func = GPIO.input(pinarr[i])
        print "Relay " + str(i+1) + " " + devarr[i] + " pin " + str(pinarr[i]) + " is " + dirarr[func]

    print " "

## Ask user for relay #
    relno = raw_input("Enter number of relay to change: ")
    if not relno:
        print "exiting"
        raise SystemExit

    if(int(relno) < 1):
        print "Bad choice - exiting"
        raise SystemExit
                
    if(int(relno) > 8):
        print "Bad Choice - exiting"        
        raise SystemExit

    relno = int(relno)
    
    ans = raw_input("ON or OFF ?  1 or 0: ")

    if (ans == "1"):
        print "Turning on relay " + str(relno) + " Pin " + str(pinarr[relno -1])
        GPIO.output(pinarr[relno - 1],False)         ## Switch off pin = conduct
        for i in range(0,2):
            print "\n"

    if (ans == "0"):
        print "Turning off relay " + str(relno) + " Pin " + str(pinarr[relno -1])
        GPIO.output(pinarr[relno - 1],True)          ## Switch on  pin = non-conduct
        for i in range(0,2):
            print "\n"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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