skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Controlling Relay Switch using GPIO

Fri May 30, 2014 6:14 am

Hi all,
I have recently bought a relay for experimenting a bit with home automation using the raspberry pi,but before I play around wih AC current I thought I'd first try out the relay with DC current
Below is a diagram for the relay(given by the manufacturer) and I have connected it exactly that way with the external load as an LED connected to a 9v battery.
Image
I connected VCC on the relay to Port #2(5v0) on the Pi,GND on the relay to Port#6(GND) on the Pi and finally the RL 1 pin on the Relay board to Port#7(GPIO 4) on the Pi.
I then booted the Pi and turned on the GPIO however the LED did not switch on as it should have. :?
Am I doing anything wrong? :?:
Is the coil switching voltage too low?

User avatar
Gert van Loo
Posts: 2482
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Controlling Relay Switch using GPIO

Fri May 30, 2014 3:18 pm

Most relays need more energy then a GPIO port can deliver. Search for GPIO and relay. There must be a hundred posts about transistors & relays.**
Oh, and you might have blown up your GPIO port as I don't see a protection diode on that board.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Controlling Relay Switch using GPIO

Sat May 31, 2014 4:33 am

If you want to play with 120/240 VAC I recommend a solid state relay. UL/CE approved solid state relays are the only way to play with mains voltages.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

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

Re: Controlling Relay Switch using GPIO

Sat May 31, 2014 2:07 pm

The board has a transistor switch and diodes built in so you are OK that way.
http://s3.amazonaws.com/emimg/Products/ ... MANUAL.PDF

You did not mention a dropping resistor with the LED and 9V battery. If you don't have a proper resistor then the LED is destroyed.

Each relay circuit has 2 sets of contacts. N.O. (Normally Open) to Common, and N.C. (Normally Closed) to Common.

The LED circuit must be connected to C and either N.O. or N.C. depending on how you want the circuit to operate. See the chart on page 6 of your manual.
For example if you want the LED to come on with GPIO on (High) then connect your circuit to C and the N.O. terminal.

What code are you using to turn GPIO on and off?
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Sat May 31, 2014 4:15 pm

Firstly I have checked multiple times that the LED is still working and indeed it was,
also I connected the +ve terminal of the 9v battery to the Common terminal of the relay, the -ve terminal of the battery straight to the +ve wire of the LED and the NO pin on the relay to the-ve wire of the LED.
(Since i want it to turn on when I turn on the GPIO)
I also checked to see if the LED was turning on when i connected to NC and again as expected it was.
I cant figure out whats wrong :cry:

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Sat May 31, 2014 4:19 pm

I just used a simple GPIO code to switch it on:

Code: Select all

 GPIO.setmode(GPIO.BOARD)
 GPIO.setup(7,GPIO.OUT)
 GPIO.output(7,True)

I have also tested this code with an LED directly connected to the GPIO pin and GND pin on the RPi and it worked perfectly!

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

Re: Controlling Relay Switch using GPIO

Sat May 31, 2014 4:42 pm

Do you hear the relay click when activated?
Have you tried the 2nd relay circuit? (RL2 & Relay 2)

Test the relay by removing the GPIO from the RL1 and then connect RL1 to VCC (5V).
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 3:12 am

No I did not hear the relay click.
Should I connect RL 1 and VCC on the relay board?
Or should I connect RL 1 to VCC on the Pi(5v0)?
And lastly is there any foolproof way to test if the relay itself is functioning as it's supposed to and isn't faulty?

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

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 4:13 am

skvrahul wrote:No I did not hear the relay click.
Should I connect RL 1 and VCC on the relay board?
Or should I connect RL 1 to VCC on the Pi(5v0)?
And lastly is there any foolproof way to test if the relay itself is functioning as it's supposed to and isn't faulty?
Most relays give an audible click..
You should disconnect the GPIO from the RL1
Leave everything else connected. (5V on RPi to VCC at JP3)
You can use the 2nd VCC at JP4 and put a jumper wire to connect VCC to RL1.
Move jumper to RL2 to test the 2nd relay if you wish.
This is the foolproof way to test the relay board.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

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

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 7:11 am

Can you post a photo of how you have it wired?
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 8:50 am

You should disconnect the GPIO from the RL1
Leave everything else connected. (5V on RPi to VCC at JP3)
You can use the 2nd VCC at JP4 and put a jumper wire to connect VCC to RL1.
Move jumper to RL2 to test the 2nd relay if you wish.
This is the foolproof way to test the relay board.
I tried this and sadly nope! nothing :(
Could the problem somehow be that the relay is not receiving enough voltage from the Pi?
Or do you guys think the relay is just faulty
I shall put up a picture of the wiring soon....

User avatar
Gert van Loo
Posts: 2482
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 9:41 am

Richard-TX wrote:If you want to play with 120/240 VAC I recommend a solid state relay. UL/CE approved solid state relays are the only way to play with mains voltages.
I have to emphasize that. It looks to me the board is NOT suited for 120/240AC.
I am pretty sure there is not enough distance between the copper tracks on controlling side (Pi side) and the power side.
I had a quick search at the WWW and somebody suggested 8mm distance for US 120V.
That board seems to have ~0.5mm.
I know the manual shows main power but please: DON'T USE THAT FOR MAINS !

(I know you still have not go it working but that may be your guardian angel protecting you :D )

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

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 1:07 pm

skvrahul wrote:
You should disconnect the GPIO from the RL1
Leave everything else connected. (5V on RPi to VCC at JP3)
You can use the 2nd VCC at JP4 and put a jumper wire to connect VCC to RL1.
Move jumper to RL2 to test the 2nd relay if you wish.
This is the foolproof way to test the relay board.
I tried this and sadly nope! nothing :(
Could the problem somehow be that the relay is not receiving enough voltage from the Pi?
Or do you guys think the relay is just faulty
I shall put up a picture of the wiring soon....
The RPi should be able to power the relay if you have a good power supply of 1A or more with a simple wired keyboard and mouse.
Usually the PRi would crash if you take too much power.
Do you have a multimeter to test voltages?
Was this a DIY kit that you assembled and soldered yourself?
It is possible that the 3V3 from the GPIO is not enough to turn on the transistor switch. The control circuit was likely made for 5V logic. But the direct connect test RL1 to VCC (5V) should have worked regardless.
A picture of your setup would be helpful.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 4:12 pm

Here are pictures of my setup:
(To make things easier for picture purposes i removed the power cable while taking the picture however the setup was exactly the same while I was testing)
Image
Image
Image
Image

BTW the bright light in the first image is NOT the LED its just the camera flash. The LED is on the right and its off.

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 4:22 pm

Do you have a multimeter to test voltages?
Was this a DIY kit that you assembled and soldered yourself?
I realized that a multimeter would be essential for my project and ordered one off of eBay.
Is there anything you suggest I can do to better understand the problem here once I get the multimeter?
And no this wasn't a DIY kit, I ordered it off this website:
http://embeddedmarket.com/products/Rela ... terfacing/

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

Re: Controlling Relay Switch using GPIO

Sun Jun 01, 2014 4:30 pm

skvrahul wrote:
Do you have a multimeter to test voltages?
Was this a DIY kit that you assembled and soldered yourself?
I realized that a multimeter would be essential for my project and ordered one off of eBay.
Is there anything you suggest I can do to better understand the problem here once I get the multimeter?
And no this wasn't a DIY kit, I ordered it off this website:
http://embeddedmarket.com/products/Rela ... terfacing/
It appears that you have the VCC and GND wires swapped...
On the RPi you have green wire on GND, on the relay board the green wire is on VCC.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 3:28 am

Ooops! That was just while I was setting it up for the photograph nevertheless I shall swap the wires and try once again

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 3:44 am

No :/ I tried swapping them and started the Pi again and the LED didn't turn on.I have a strong feeling there's something wrong with the relay.
Once I get a multimeter what could I do to test it?

johndough
Posts: 254
Joined: Sun Jan 13, 2013 2:00 pm

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 9:21 am

skvrahul wrote:No :/ I tried swapping them and started the Pi again and the LED didn't turn on.I have a strong feeling there's something wrong with the relay.
Once I get a multimeter what could I do to test it?
Hi

First off it states that the thing has to be soldered together.

Secondly it states that it requires a 5 or 6 volt regulated supply connecting.. This power is used for the transistor and electro-magnet .... Common connection to ground.

##########

The board needs at least 5volts, preferably 6 with sufficient wattage to energise and hold the relay coil.

The coil is 100 ohms ish, needs 80% to pull in, and drop out at 5% (I think the drop out is more likely to be 50%) 0.36 watts.

Pull in requires upto 8 times the hold in, so to pull in and hold I would think that the power needed would be 3.6 watts or 600 milli-amps.

####

So.....................

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 9:54 am

So would soldering the wires somehow make it work? O.o
Doubting the sufficiency of the RPi voltage I even tried this by connecting VCC & GND on the Relay to a 6v battery pack instead of powering it with the Pi and that did not work either

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

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 12:41 pm

skvrahul wrote:No :/ I tried swapping them and started the Pi again and the LED didn't turn on.I have a strong feeling there's something wrong with the relay.
Once I get a multimeter what could I do to test it?
Reference the schematic here: http://s3.amazonaws.com/emimg/Products/ ... MANUAL.PDF
1. Make sure you have 5V at VCC to GND
2. Test transistor switch by probing between relay pin 5 to GND.
A convenient place to test would be on the anode side of the diode or the side closest to Q1... (same as pin 5).
When the RL1 is not energized (0V) then you will read 5V at pin 5. When RL1 energized at 5V (jumper to VCC), then pin 5 should drop to ~0V.

FYI here is the datasheet for the relay itself:
http://www.electromagneticrelays.net/pd ... ay-SC5.pdf
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

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

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 12:55 pm

Are you powering the LED with a 9v battery? if so it's probably blown :(

If you have a suitable resistor protecting the LED then make sure the LED is connected the right way round or it will not light.

I would suggest connecting the LED/battery circuit to the normally closed contacts on the relay, that way it should light up from the start and prove that the LED is okay ... then when the relay works the light will go out.

I notice you have a wire going to JP4 as well as two wires to JP3? What is the other end of the JP4 wire connected to?
Last edited by Cancelor on Mon Jun 02, 2014 1:11 pm, edited 1 time in total.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

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

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 12:57 pm

johndough wrote: Pull in requires upto 8 times the hold in, so to pull in and hold I would think that the power needed would be 3.6 watts or 600 milli-amps.

####

So.....................
Where did those numbers come from?
From the data sheet the coil power is 0.36W @ 6V.
6V on the 100 Ohm coil would be 60mA (0.36W)
So....
5V on the 100 Ohm coil would draw 50mA (0.25W)
Pull in is 80% of 6V or 4.8V which would draw 48mA (0.23W)
Drop out is 5% of 6V or 0.3V
Last edited by klricks on Mon Jun 02, 2014 1:02 pm, edited 1 time in total.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 1:01 pm

skvrahul wrote:So would soldering the wires somehow make it work?
The product manual says: "The Relay Board-Dual product is designed for experiments and is not suitable to be used in life support and mission critical products. It should be assembled and soldered under expert supervision"

That suggests to me that it comes as a kit that needs assembly and soldering. So, to clarify, did your Relay Board come as a kit, or was it already assembled and soldered.

And to emphasise what others have said - be careful of your safety and that of others who may come into contact with this board if you are going to use it for mains-powered home automation. Quoting the website and manual:
Warning � Work with high voltage section only if you are qualified & understand the circuit. Danger of electrocution. With above connections, the PCB of Relay board is also connected to AC High voltage line. DO NOT TOUCH PCB while in use and /or Plugged to mains AC.


How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

skvrahul
Posts: 11
Joined: Fri May 30, 2014 4:19 am

Re: Controlling Relay Switch using GPIO

Mon Jun 02, 2014 1:27 pm

I'm not really sure why it says that but the relay came pre-assembled all I did was connect to the Pi using the cables.

Return to “Interfacing (DSI, CSI, I2C, etc.)”