1

I have two scripts. One turns a relay on, the other off.

As you can see bellow, lightson.py sets GPIO pin 17 to HIGH and lightsoff.py sets GPIO pin 17 to LOW. High should be on and low off. But to turn the light on I have to run lightsoff.py (LOW). Is there a different way I should be doing this?

LIGHTSON.PY

import RPi.GPIO as GPIO

def lights_on():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(17,GPIO.OUT)
    GPIO.output(17,GPIO.HIGH)
    return '<img class="right" src="lightson.png" width="32" height="32">'

output = lights_on()    
f = open('/var/www/html/viv/lightsout.html', 'w')
print >> f, output
f.close()

LIGHTSOFF.PY

import RPi.GPIO as GPIO

def lights_off():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(17,GPIO.OUT)
    GPIO.output(17,GPIO.LOW)
    return '<img class="right" src="lightsoff.png" width="32" height="32">'

output = lights_off()    
f = open('/var/www/html/viv/lightsout.html', 'w')
print >> f, output
f.close()
  • 1
    What type of relay do you have? There are 2 types - normally open and normally closed relays. If you have a normally closed relay, then sending High will open the relay and cause the light to go off, whereas if you have a normally open relay sending High means you are closing the circuit and activating the light. – Phil B. Apr 5 '16 at 12:11
  • I'm not 100% sure to be honest, first time using one. Sorry for the C/P but this is the name from Amazon: DROK® 5V 2-Channel Relay Board AC 250V/DC 30V Extension Development Module with Optocoupler Protection for Arduino/DSP/ARM/ARM/MSP430 – Sauced Apples Apr 5 '16 at 12:14
  • @SaucedApples Things that turn on when they're grounded, have nearly 0 volts or simply "lower than supply"-voltage going to them, are typically referred to as "active low". The opposite is called "active high". NPN and PNP transistors are good examples of this and, apparently, so is your relay. There's absolutely nothing wrong with it but you could add an NPN transistor before the relay if you want to invert the logic. One thing, though, is that you need power to turn off your relay - this might a safety concern although it's not a defect as such. Are there pins and jumpers on the relay? – jDo Apr 5 '16 at 12:21
  • @jDo is there a way to fix this then or just one of those live with it things? – Sauced Apples Apr 5 '16 at 12:24
  • @jDo yes currently nothing is attached just testing scripts to control. GND > GPIO GND, IN1 > GPIO 23, IN2 > GPIO 17, VCC > GPIO 5V – Sauced Apples Apr 5 '16 at 12:26
3

As Phil B mentioned in the comments, there are two types of relays: normally open and normally closed. Open means "connection broken" and closed means the opposite. Adding to that, there are two "trigger" types: active high and active low. Active high means that writing a logical high activates the thing (akin to NPN/PNP transistors).

It seems OP's relay module is normally open and active low. See the picture below for the explanation of why it's active low in this case.

Active low relay module

Thus, the python code is working fine, the relay is working, nothing's broken but it just works with a counter-intuitive logic.

Beware that there might be safety issues associated with normally closed relays since they will turn on if the controlling circuit is removed; i.e. don't use normally closed relays for your boiler/water heater/nuclear reactor.

To invert the logic, one can turn on/off the relay by cutting it's ground connection using a transistor + flyback diode instead.

0

There are 3 terminals on each relay. Keep the connection going to the center terminal. Move the other connection on the relay to the other (unused) terminal.

  • Did you see that OP is working with this relay module? It might support both active high and active low but I'm not sure – jDo Apr 5 '16 at 13:41
  • These relays are active high only, which means a high from the gpio will cause the relay to energize. – Garnett Haines Apr 5 '16 at 13:46
  • I am going to downvote this as it appears you have not fully read the question and comments. EDIT: Forgot I'm not on SO so don't have enough rep. – Sauced Apples Apr 5 '16 at 13:51
  • "These relays are active high only" I think that's unlikely or flat out wrong since OP has to write a LOW to energize the relays. You can't both be right; I know that much :) – jDo Apr 5 '16 at 13:59
  • I think OP is not saying the relay energizes with a low. I think he is saying the light comes on with a low. If I am incorrect then I apologize for the confusion. I use these relays all the time and have never come across this problem before. A low has always energized the relays. – Garnett Haines Apr 5 '16 at 14:06

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.