magearlik
Posts: 9
Joined: Sat Jun 20, 2015 9:35 am

Connect Relay to Raspberry Pi and Extension Cord [Australia]

Thu Jan 21, 2016 10:11 am

Ok so I just purchased an 8 channel Sainsmart relay from ebay to use with my Raspberry Pi. I have followed the Youtube tutorial here [https://www.youtube.com/watch?v=3Pikhq99q5E] and it all makes sense up until the part about connecting the power cord to the relay. I live in Australia and regular power cords here have 3 wires - red, black and green usually (and I presume these are positive, negative and neutral). More info here [http://www.yunhuanelectric.com/Australi ... ndard.html]. We also use 240v AC outlets. So if I were to follow this tutorial I presume I only need to use the red and black wires and ignore the neutral wire? Also instead of cutting the power cord for the device couldn't I just connect the relay between the power and the plug of an extension cord then connect whatever device I wanted to that? Any help would be appreciated. Thanks.
likethis.png
likethis.png (10.24 KiB) Viewed 3704 times

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

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Thu Jan 21, 2016 10:23 am

With 240V main electricity - if you aren't sure, don't do it! It's dangerous.

Checking the link you gave, I see that the three pin on your electrical system are named slightly different to ours.

The red or brown wire is connected to the "Active" pin (What we in the UK would call "Live"). That's the really dangerous one.

The black or blue wire is connected to the "Neutral" pin. That's the return wire for the current.

The green or green/yellow wire is connected to Ground (or "Earth"). That's a safety wire.

To switch the current with a suitable relay in an proper enclosure, you would cut the red (or brown) wire.

The black (or blue) wire and the green (or green and yellow) if fitted wires should be connected directly.

The best plan is to either get a ready-built mains controller, or get an electrician to wire it up for you.

User avatar
davidcoton
Posts: 4153
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Thu Jan 21, 2016 10:52 am

+1 on rpdom's answer.

Mains power can kill. Treat it with respect. All your connections must be properly insulated (not just sticky tape). All mains connections must be enclosed, either in a plastic or a metal fingerproof housing. If metal, the housing must be connected to earth/ground.

Make sure mains is NOT applied to boards loose on a bench, even for testing.

Never disconnect an existing earth/ground connection without immediately providing an equivalent replacement connection. Never "ignore" the earth/ground wire, especially when making or modifying an extension lead.

AC supplies do not have positive and negative, they are "Line" and "Neutral". Strictly both these are "live" and may have dangerous voltages. (UK terminology used, others may vary but the principle is the same. Note cable colours also vary.)

Be aware of the appropriate rules and regulations for mains electricity in your country. Get a local electrician's advice.

Again, if you aren't sure, don't do it! It's dangerous.
Signature retired

magearlik
Posts: 9
Joined: Sat Jun 20, 2015 9:35 am

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Sat Jan 23, 2016 1:20 pm

Ok so at potential risk of blowing up my Rpi, I got this working. I discovered that I only needed to connect the positive wire to the relay and leave the ground and neutral wires intact. So with my extension cord I discovered the wires meant:

Brown = Hot (postive)
Blue = Neutral
Green/Yellow = Ground (negative)

(you can check your country here: https://en.wikipedia.org/wiki/Electrical_wiring)

So I only needed to chop the brown wire of my extension cord and connect it to the relay and the Raspberry Pi just like this person did here:

https://www.youtube.com/watch?v=3Pikhq99q5E

Afterward you can connect the relay to the Raspberry Pi and whatever GPIO you like. I used GPIO 17. To test I wrote two simple scripts. One that sets GPIO to HIGH (which turns whatever device is connected to my relay ON), and another that sets the GPIO to LOW (which turns the device OFF). I tested by connecting my extension cord to a lamp.

Off.py
--------
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.LOW)

On.py
--------
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)

Here is a very crude diagram of my setup:

[attachment=0]Relay-Connection.png[/attachment]

Basically just watch the video and follow everything in it and then connect the postive wire from your power cord to the relay just like the brown cord is connected in this video.
Attachments
Relay-Connection.png
Relay-Connection.png (25.02 KiB) Viewed 3624 times
Last edited by magearlik on Sun Jan 24, 2016 7:05 am, edited 1 time in total.

User avatar
davidcoton
Posts: 4153
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Sat Jan 23, 2016 4:10 pm

Do not refer to the wires of an AC connection as "positive" and "negative". It is wrong, misleading, and potentially dangerous to others.

See the earlier posts for the correct terminology.
Signature retired

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Sat Jan 23, 2016 11:57 pm

You should be aware that the relays on Sainsmart boards are not suited for connection to mains voltage. One reason is that the relay itself only has basic insulation (1500Vrms test voltage from contacts to coil). To meet electrical safety standards you would need a relay that is designed for reinforced insulation. These relays have a much larger distance from coil to contacts.

Another very important fact to be aware of is that practically every relay board marketed to hobbyists is unsafe for use with mains voltage because the traces that connect to mains voltage (relay contacts) are placed too close to traces that are connected to the low voltage side (GPIO of the Pi). So even if the relays used on the board would meet reinforced insulation requirements, the board itself does not.
If the board was correctly done you would see a minimum of 6.4mm distance but there are boards that only have a few mm distance or less. These board are totally unsafe when connected to mains voltage. If you have a look at the back side of the different Sainsmart boards and check the distance from the power traces and the low voltage traces you will see what I am talking about.

The need for reinforced insulation is based on real world voltage transients that occur on the mains voltage wiring. You don't want your Pi to suddenly give you or a loved one an electric shock.

magearlik
Posts: 9
Joined: Sat Jun 20, 2015 9:35 am

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Sun Jan 24, 2016 5:12 am

From my understanding, although I am not an Electricial Engineer, the Sainsmart relays[http://www.sainsmart.com/8-channel-dc-5 ... logic.html] are marketed to be used with a micro-controller such as the Raspberry pi or Arduino . They use 5V for power and 3.3v (which you get from gpio) to power the relay switch on or off. They can deal with up to AC250V 10A or DC30V 10A. If you look around the internet many hobbyists have used these devices for do things such as:

- http://www.instructables.com/id/Raspber ... ight-Show/
- http://www.instructables.com/id/Raspber ... /?ALLSTEPS
- http://itsbrent.net/2013/03/hacking-my- ... pberry-pi/

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Sun Jan 24, 2016 3:51 pm

Yes, well I am an electrical engineer, and as I said many these relay boards marketed to hobbyists are not meeting electrical safety requirements and can be unsafe to use with mains voltage. If someone was in the business of manufacturing say a gadget that contains a microcontroller board (such as the Pi) and include a relay and tell customers that this gadget can be used to control mains voltage loads, then they could not use the type of relay that is mounted on most of these hobby relay boards, and they would need to make the circuit board in such a way that there is enough distance between traces connected to mains voltage and traces connected to the microcontroller.

It would be only in a case where the gadget would be inside a grounded or plastic box with no accessible parts that they could get away with not providing reinforced isolation. Before selling the gadget they would need to pass electrical safety tests, and there is no way a design such as the Sainsmart relay boards would pass. It is misleading and dangerous by the manufacturers to market these boards for hobby use, suggesting that the users connect mains voltage to these boards.
It is not that it the relay boards would cost any more if relays with reinforced isolation where used, and the board was designed correctly with enough distance for safety. It is just that the manufacturers don't care. But users that install their creations in their houses are most of the time not aware of the danger. Even if the installation was done correctly so nobody could access the mains voltage wiring, it only takes some dust accumulation or humidity or simply just time until a voltage transient on the mains voltage wiring can show up on the low voltage circuitry.
You can survive and electrical shock, but it totally depends on what path the current takes in you body, and the timing relative to you heartbeat. As I have seen several relay boards that are totally insane from electrical safety perspective I sometimes make comments to posts like yours, just to make people aware of the danger. It is OK to experiment but make sure you don't leave your circuitry where someone else can touch it, or install it and forget it and after a while it becomes a safety issue.

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

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Sun Jan 24, 2016 4:09 pm

This is one reason I build my own relay boards, for personal use (yes, using those same relays). I use prototyping board, remove any conductors from the board that aren't required, make sure there is a good gap between any low-voltage and high-voltage conductors and then encapsulate as much as I can of the circuit to provide an even greater level of safety. Then I fit it in a sealed plastic box.

Safety is the key.

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Connect Relay to Raspberry Pi and Extension Cord [Austra

Sun Jan 24, 2016 4:33 pm

here is a sketch that shows a relay with reinforced isolation (top) and basic isolation (bottom).
the grey circles show the distance between mains voltage pins and coil pins. When circuit board traces are added the difference in isolation distance becomes even larger.
Attachments
relays1.jpg
relays1.jpg (12.16 KiB) Viewed 3406 times

Return to “General discussion”