PeersC
Posts: 30
Joined: Fri Apr 01, 2016 6:35 pm

Relay Control

Wed Jan 18, 2017 11:42 am

Hi there,

This may sound totally dumb and I am obviously missing something.

I have bought a shedload of 10A 250V relay boards with protection diodes, led's and surface mounted transistor switches for my project, without a magnifying glass can't tell you which transistor (pnp or npn).

So each board has VCC and GND + input - Now I am not powering from my Pi have external power supply that shares ground - so I can get ground from anywhere on the circuit also have +5V + 12V and 3.3V without using the Pi's power rails. VCC is on 5V rail and GND is central

But the relay boards only seem to respond to input to ground not hi / lo signals from the GPIO
when I run my code the relay opens and stay open until the GPIO cleanup()

Any Ideas???

Cheers from Peers

ElEscalador
Posts: 680
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Relay Control

Wed Jan 18, 2017 1:11 pm

We can't help you without a photo and/or a model of your relays and a drawing of how you have it wired up. Some operate at different voltages. Some require voltage at more than one terminal - sometimes from different sources is ideal...
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

PeersC
Posts: 30
Joined: Fri Apr 01, 2016 6:35 pm

Re: Relay Control

Wed Jan 18, 2017 2:07 pm

Here is the code I am testing with - problems uploading photo cos of file sizes - but it's damned simple attaching vcc on relay board with +5V Gnd goes to mutual shared ground and input goes to pin 17 on PI relay switches when I initiate pin 17 and switches off when I do the cleanup. If I manually connect the relay to ground it works - so it would seem that the relay boards need to be switched to ground rather than a gpio hi signal ???

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

# setup relay to output pin 17

RELAY = 17

GPIO.setup(RELAY,GPIO.OUT)
print "Starting Program"
time.sleep(5)
print "pin 17 now set hi"
GPIO.output(RELAY,1)
time.sleep(20)
print "pin 17 now set lo"
GPIO.output(RELAY,0)
time.sleep(20)
print "pin 17 now set hi"
GPIO.output(RELAY,1)
time.sleep(20)
print "pin 17 now set lo"
GPIO.output(RELAY,0)
time.sleep(5)
print "end"

GPIO.cleanup()

Any ideas how to switch a ground signal maybe???#


Peers

ElEscalador
Posts: 680
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Relay Control

Wed Jan 18, 2017 3:02 pm

I don't know python, but isn't sleep(20) a call to sleep for 20 milliseconds? That's too short a time for you to see a change. Try changing it to sleep(1000) for a one second delay. (unless I'm wrong about sleep() duration). And again, without your model number of your relay board I can't be sure, but plenty of them do require a switch to low to turn on - calling the pin high will keep it "off."
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

Moe
Posts: 230
Joined: Sun Jan 25, 2015 2:44 pm

Re: Relay Control

Wed Jan 18, 2017 10:22 pm

First thing that jumps out - you're using the BCM numbering scheme so have you connected the right pin? BCM17 is physical pin 11 I think. Use GPIO.setmode(GPIO.BOARD) to refer to physical pin numbers.

It looks like the relay turns 'ON' when you ground it, so we can safely assume it is active low. It turns 'OFF' when you set the GPIO back to float, so it looks like the module is pulled up to Vcc internally, which makes sense.

My guess would be that the GPIO pulls low by default as soon as you set it to output, turning the relay ON. Then you set 'high' but 3.3V is too low to register as 'high' at the relay, i.e. with respect to a Vcc of 5V. The relay specs should tell you what voltages it expects. Alternatively, see what happens when you connect the relay control pin to 0V and 5V directly.
Submarine communication systems engineer and amateur robot enthusiast.

User avatar
DougieLawson
Posts: 36302
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Relay Control

Wed Jan 18, 2017 10:29 pm

Moe wrote:First thing that jumps out - you're using the BCM numbering scheme so have you connected the right pin? BCM17 is physical pin 11 I think. Use GPIO.setmode(GPIO.BOARD) to refer to physical pin numbers.
ALL sample code supplied by the RPF uses BCM numbering. So the current recommendation is to stick with that.

http://pinout.xyz is the definitive way to map numbers to pins to special functions.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Moe
Posts: 230
Joined: Sun Jan 25, 2015 2:44 pm

Re: Relay Control

Thu Jan 19, 2017 9:00 pm

ALL sample code supplied by the RPF uses BCM numbering. So the current recommendation is to stick with that.
Off topic, but I find that totally bizarre. This question is a perfect example of why it's a bad idea - even if the OP has connected correctly, the pin references are ambiguous.

Broadcom clearly have their own reasons for the abstraction but there's no reason for the end user to follow it. They've even given us the 'board' option so we don't have to. In my experience, if you give people the challenge of correlating two signal names with two physical pins, they get it wrong about 70% of the time :? .
Submarine communication systems engineer and amateur robot enthusiast.

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

Re: Relay Control

Thu Jan 19, 2017 9:20 pm

Moe wrote:Broadcom clearly have their own reasons for the abstraction but there's no reason for the end user to follow it. They've even given us the 'board' option so we don't have to. In my experience, if you give people the challenge of correlating two signal names with two physical pins, they get it wrong about 70% of the time :? .
Broadcom have nothing to do with is and have used no abstraction.

They have given the names of the signals coming from their chip. That is what they are referred to internally and in all official documentation. Which physical pins the RPF decided to bring those signals out to is nothing to do with Broadcom.

Actually you could really confuse matters by referring to which "pin" on the actual Broadcom those signals are connected to, but that is not relevant to anyone other than the Pi designers.

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

Re: Relay Control

Fri Jan 20, 2017 11:17 am

PeersC wrote:Here is the code I am testing with - problems uploading photo cos of file sizes - but it's damned simple attaching vcc on relay board with +5V Gnd goes to mutual shared ground and input goes to pin 17 on PI relay switches when I initiate pin 17 and switches off when I do the cleanup. If I manually connect the relay to ground it works - so it would seem that the relay boards need to be switched to ground rather than a gpio hi signal ???
So reading your post it seems like you are saying it works but in reverse so when GPIO is high relay is off. ?
This is just how some relay boards work A GPIO high will switch the relay off and a GPIO low will switch the relay on.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Relay Control

Fri Jan 20, 2017 11:46 am

pcmanbob wrote:
PeersC wrote:Here is the code I am testing with - problems uploading photo cos of file sizes - but it's damned simple attaching vcc on relay board with +5V Gnd goes to mutual shared ground and input goes to pin 17 on PI relay switches when I initiate pin 17 and switches off when I do the cleanup. If I manually connect the relay to ground it works - so it would seem that the relay boards need to be switched to ground rather than a gpio hi signal ???
So reading your post it seems like you are saying it works but in reverse so when GPIO is high relay is off. ?
This is just how some relay boards work A GPIO high will switch the relay off and a GPIO low will switch the relay on.
This is typical of a lot of relay boards - the coil +ve is hardwired to the +V rail, and requires a ground at the other end of the coil to energise the relay, usually via a transistor and an opto-isolator. The 2 channel type I've been using does require a low from the Pi in order to click the relay 'On'.
There's a schematic on this page showing an example :
http://electronics.stackexchange.com/qu ... elay-board
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

PeersC
Posts: 30
Joined: Fri Apr 01, 2016 6:35 pm

Re: Relay Control

Sat Jan 21, 2017 5:35 am

Thanks for all the help.

I will try and resize some photos to post but I have used my camera on macro to take a look at the relay board. It is dead simple but the transistor switching the relay is a 2TY surface mount - which is PNP so explains why it switches to ground and not GPIO hi / lo (I believe hi is 3.3 and lo is somewhere about 1.5) to resolve the issue I'll need a second transistor at attach that to the relay board - shame there was no datasheet that I could have referred to in advance.

As regards the use of board v bcm - I am using a pi cobbler look alike and all the pins are nicely labelled in BCM format so it makes it very easy. The fact that the circuit went live as soon as I set pin 17 as output means I knew I was using the right pin but the PNP transistor is expecting a negative base input.

I'll have to have a dig round in my bits and bob's for some transistors that will work.

Let you know what happens .

Peers

User avatar
DougieLawson
Posts: 36302
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Relay Control

Sat Jan 21, 2017 10:32 am

Post the photos on http://imgur.com post the BBCode [IMG] links on here.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

PeersC
Posts: 30
Joined: Fri Apr 01, 2016 6:35 pm

Re: Relay Control

Sat Jan 21, 2017 11:13 am

Well Hope I got this right.

Images Here :- http://imgur.com/a/4Be4B

Put together A 2N5551 with a 10 K ohm resistor on the base and if in input is floating i.e not connected to anything then there is a bit of relay chatter - but when connected to 3.3 or GPIO works perfect.

I included a picture of my project board for you see as I can explain in more detail - the power supply is 12 volts at 6 amps, this then steps down to 5 volts at 3 amps to supply the pi, the screen and the bread board. The small bread board supply Black / GND , yellow / input to relay, Blue / +12 volt, green / NC and red / + 5 volt - From the bread board power I get 3.3 volts as well as from pi.

All working perfect now - just took a bit of fettleing.

Peers

Return to “Automation, sensing and robotics”