ikequan
Posts: 3
Joined: Sun Feb 01, 2015 2:25 am

Relay is off when GPIO is set to high(True) and vice versa

Sun Feb 01, 2015 2:57 am

Hello pals. This is my first post and i hope i have posted in the right place.
I'm working on a project which i intend to turn light on/off using my raspberry pi and a 5v 8 ch replay. I followed some steps online to setup my raspberry pi and relay but when i set the GPIO(pin 7) to high(true) the replay turns off and when the GPIO is set to low(false) the replay turn on. Below is how i did my setup
1. RPI pin 4 to VCC pin of replay
2. RPI pin 6 to GND pin of replay
3. RPI pin 7 to IN1 pin of replay

Please am using raspberry pi model b+
Please help fix this problem thank you.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Relay is off when GPIO is set to high(True) and vice ver

Sun Feb 01, 2015 10:54 am

Can you post a link to the type of relay board you've got?

It sounds like you have one that uses an inverted input, so setting the line Low turns the relay on. If you have, then that should be easy to get around by changing the program logic so it sets the pin low when it wants to turn something on.

ikequan
Posts: 3
Joined: Sun Feb 01, 2015 2:25 am

Re: Relay is off when GPIO is set to high(True) and vice ver

Sun Feb 01, 2015 9:13 pm

Thank you rpdom for your quick reply
this is the link to the relay am using: http://www.aliexpress.com/item/2pcs-lot ... 92610.html

this is my python code:

#!/usr/bin/env python

#pretty obvious, but you'll want these dependencies to be installed for this script to work
import RPi.GPIO as GPIO, time, requests, os

def cls():
os.system(['clear','cls'][os.name == 'nt'])

DEBUG = 1

RED = 23
GREEN = 24
BLUE = 25

GPIO.setmode(GPIO.BCM)
GPIO.setup(RED, GPIO.OUT)
GPIO.setup(GREEN, GPIO.OUT)
GPIO.setup(BLUE, GPIO.OUT)

response = requests.get('http://YourWebsite.com/test.php')

while response.text != 'exit':
response = requests.get('http://YourWebsite.com/test.php')

colrs = response.text
r = colrs[0]
g = colrs[1]
b = colrs[2]

cls()

if response.text == 'exit':
print (colrs)
else:
print ("R" + r + ", "),
print ("G" + g + ", "),
print ("B" + b)
print

if r == '1':
GPIO.output(RED,True)
else:
GPIO.output(RED,False)
if g == '1':
GPIO.output(GREEN,True)
else:
GPIO.output(GREEN,False)
if b == '1':
GPIO.output(BLUE,True)
else:
GPIO.output(BLUE,False)

#time.sleep(0.25) #uncomment this if you want the polling loop to be slower

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Relay is off when GPIO is set to high(True) and vice ver

Sun Feb 01, 2015 9:16 pm

ikequan wrote:this is my python code:
Indentation is important in Python.
Please use a [/b] block when posting code

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Relay is off when GPIO is set to high(True) and vice ver

Sun Feb 01, 2015 9:31 pm

ikequan wrote: I followed some steps online to setup my raspberry pi and relay but when i set the GPIO(pin 7) to high(true) the replay turns off and when the GPIO is set to low(false) the replay turn on.
That's 100% normal for the cheap relay boards from China. You just have to turn your logic upside down.

Use this

Code: Select all

import RPi.GPIO as GPIO
.... # same stuff as you've got # ...

# reverse logic values because relays work backwards
ON=False
OFF=True
GPIO.output(BLUE, ON)
GPIO.output(BLUE, OFF)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

ikequan
Posts: 3
Joined: Sun Feb 01, 2015 2:25 am

Re: Relay is off when GPIO is set to high(True) and vice ver

Mon Feb 02, 2015 2:45 am

@DougieLawson thank you for the reply..

I reversed the logic of the python code and seems well but i think there is some problem with the relay still...even when the Raspberry Pi is turned off and the bulb connected relay is still plugged to the wall, the bulb seems to be on i.e. the relay is still closed when off and opened when on.

Please can anyone help me with this?

User avatar
SpazzTechTom
Posts: 15
Joined: Sat Jan 31, 2015 12:50 am
Location: Glen Rock, PA. USA
Contact: Website

Re: Relay is off when GPIO is set to high(True) and vice ver

Mon Feb 02, 2015 3:13 am

ikequan wrote:@DougieLawson thank you for the reply..

I reversed the logic of the python code and seems well but i think there is some problem with the relay still...even when the Raspberry Pi is turned off and the bulb connected relay is still plugged to the wall, the bulb seems to be on i.e. the relay is still closed when off and opened when on.

Please can anyone help me with this?
Some relays are normally open and some relays are normally closed. Some relays are both. It sounds like you may have a normally closed type relay which means when there is no current flowing through the control coil, the relay is closed, and the light is on. When current flows through the control coil, it opens, and the light turns off. From the link you gave I did not see a datasheet for the relays. If you have a datasheet, you should be able to tell for sure.
SpazzTechTom
Having fun with technology!

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay is off when GPIO is set to high(True) and vice ver

Mon Feb 02, 2015 3:32 am

ikequan wrote:@DougieLawson thank you for the reply..

I reversed the logic of the python code and seems well but i think there is some problem with the relay still...even when the Raspberry Pi is turned off and the bulb connected relay is still plugged to the wall, the bulb seems to be on i.e. the relay is still closed when off and opened when on.

Please can anyone help me with this?
On the board you linked to it seems that each relay has three screw terminals. I expect these are 'C','N/O' and 'N/C', i.e. Common, Normally Open and Normally Closed, although they might not be labelled like that. I also think you are using 'C' and 'N/C'. Try the other terminal (using 'C' and 'N/O').

klricks
Posts: 7154
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Relay is off when GPIO is set to high(True) and vice ver

Mon Feb 02, 2015 3:33 am

ikequan wrote:@DougieLawson thank you for the reply..

I reversed the logic of the python code and seems well but i think there is some problem with the relay still...even when the Raspberry Pi is turned off and the bulb connected relay is still plugged to the wall, the bulb seems to be on i.e. the relay is still closed when off and opened when on.

Please can anyone help me with this?
A relay usually has 3 contacts Normally Open NO, Normally Closed, NC and Common.
If you use NO and common then when the relay is off (not energized) the contacts are open (OFF).
If you use NC and common then you will get the opposite. The contacts are closed (ON) when the relay is off. This condition is also true if the relay board is not powered at all. The external circuit will be on.
So you probably want to use the NO and common terminals.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay is off when GPIO is set to high(True) and vice ver

Mon Feb 02, 2015 3:35 am

Is there an echo in here?

Return to “Troubleshooting”