chrispercol
Posts: 5
Joined: Sun Nov 15, 2015 9:59 am

GPIO.HIGH turns relay on when off expected?

Mon Dec 25, 2017 9:24 pm

Hi,

I'm using a relay to switch a power extension and think I may have incorrectly wired the relay. I would expect the code below to switch the power off but it switches the power on?

I have wired the first two connectors on K1 of this relay board I bought on Amazon.

https://www.amazon.co.uk/dp/B01BAFLMCI/ ... 41_TE_dp_1

Am I correct in thinking that GPIO.LOW should actually turn the relay on and if so how do I fix this?

Code: Select all

#!/usr/bin/python  
import RPi.GPIO as GPIO  
import time  
  
GPIO.setmode(GPIO.BCM)  
  
# init list with pin numbers  
  
pinList = [17]  
  
# 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 = 2  
  
# main loop  
  
try:  
  GPIO.output(17, GPIO.HIGH)  
  print "ONE"  
  time.sleep(SleepTimeL);   
  print "Good bye!"  
  
# End program cleanly with keyboard  
except KeyboardInterrupt:  
  print " Quit"  
  
  # Reset GPIO settings  
  GPIO.cleanup() 
Ta,

Chris

gordon77
Posts: 5035
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO.HIGH turns relay on when off expected?

Mon Dec 25, 2017 10:06 pm

Yes these relays are normally active low, meaning on when gpio is low.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13088
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: GPIO.HIGH turns relay on when off expected?

Mon Dec 25, 2017 10:47 pm

read this, it might answer your questions: viewtopic.php?f=91&t=83372&p=1225448#p1225448

pcmanbob
Posts: 9462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: GPIO.HIGH turns relay on when off expected?

Mon Dec 25, 2017 11:18 pm

As many people before you have found the 3.3V if the gpio is not high enough to turn the relay off.

You may find you will need to add some interface hardware to get this relay card to work with you pi.

viewtopic.php?f=28&t=197227
Last edited by pcmanbob on Tue Dec 26, 2017 9:15 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

chrispercol
Posts: 5
Joined: Sun Nov 15, 2015 9:59 am

Re: GPIO.HIGH turns relay on when off expected?

Mon Dec 25, 2017 11:40 pm

mahjongg wrote:
Mon Dec 25, 2017 10:47 pm
read this, it might answer your questions: viewtopic.php?f=91&t=83372&p=1225448#p1225448
Thanks,

I think I am using a 5v pin. I'll use a 3.3v pin and see if the relay behaves as expected.

Chris

User avatar
Cancelor
Posts: 776
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: GPIO.HIGH turns relay on when off expected?

Tue Dec 26, 2017 6:17 am

Each relay has a block of three blue terminal connectors; connecting the power extension through pins 2 and 3 instead of pins 2 and 1 will invert the behaviour.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

chrispercol
Posts: 5
Joined: Sun Nov 15, 2015 9:59 am

Re: GPIO.HIGH turns relay on when off expected?

Tue Dec 26, 2017 11:41 am

Cancelor wrote:
Tue Dec 26, 2017 6:17 am
Each relay has a block of three blue terminal connectors; connecting the power extension through pins 2 and 3 instead of pins 2 and 1 will invert the behaviour.
Thanks, that's much appreciated. I will rewire and test.

My existing solution does work (inverted) but I felt a little uncomfortable using mains electricity and a relay which wasn't behaving as expected.

I'll be happy when on = on rather than off :D

Thanks again,

Chris

Return to “Automation, sensing and robotics”