bizilux
Posts: 20
Joined: Thu Dec 01, 2016 6:30 pm

How to control relays in safe way (isolated) ?

Mon Sep 10, 2018 11:27 pm

why is this not working? i've spend couple of hours googling and trying different projects but none really shown me how to isolate raspberry pi the way i want to... (the simple way) basically utilizing JD-VCC function on relay modules.

wiring diagram
Image

here is the code...

Code: Select all

import RPi.GPIO as GPIO
import time

# set BCM_GPIO 17 as relay pin
RelayPin = 26

#setup function for some setup---custom function
def setup():
    GPIO.setwarnings(False)
    #set the gpio modes to BCM numbering
    GPIO.setmode(GPIO.BCM)
    #set RelayPin's mode to output,and initial level to LOW(0V)
    GPIO.setup(RelayPin,GPIO.OUT,initial=GPIO.LOW)

#main function
def main():

    while True:

        #disconnect
        GPIO.output(RelayPin,GPIO.LOW)
        time.sleep(3)

        #connect
        GPIO.output(RelayPin,GPIO.HIGH)
        time.sleep(3)


what am i missing?
I just want to turn relay on/off, i'll worry with relay connections later... each relay has audible sound and led light, but i don't get that at all... something is not working, and i tried 2 different relay boards... so its not that... i've configured something wrong

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

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 12:16 am

In your drawing you left out the required ground wire between the RPi and relay board.

Most relay boards are active LOW which means a LOW (0V) turns the relay ON and HIGH turns them OFF

I don't think you need the VCC from RPi to relay board if the relay board is powered separately.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

bizilux
Posts: 20
Joined: Thu Dec 01, 2016 6:30 pm

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 12:25 am

klricks wrote:
Tue Sep 11, 2018 12:16 am
In your drawing you left out the required ground wire between the RPi and relay board.

Most relay boards are active LOW which means a LOW (0V) turns the relay ON and HIGH turns them OFF

I don't think you need the VCC from RPi to relay board if the relay board is powered separately.
As far as i know, ground should not be connected between pi and relays, or else it doesnt become isolated anymore. Its a known design flaw of these boards because they have 2 ground connections. You only should ever use one i believe.

I think mixing low and high isnt an issue, it Should still work if you mix them, just in reverse.

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

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 1:44 am

bizilux wrote:
Tue Sep 11, 2018 12:25 am
klricks wrote:
Tue Sep 11, 2018 12:16 am
In your drawing you left out the required ground wire between the RPi and relay board.

Most relay boards are active LOW which means a LOW (0V) turns the relay ON and HIGH turns them OFF

I don't think you need the VCC from RPi to relay board if the relay board is powered separately.
As far as i know, ground should not be connected between pi and relays, or else it doesnt become isolated anymore. Its a known design flaw of these boards because they have 2 ground connections. You only should ever use one i believe.

I think mixing low and high isnt an issue, it Should still work if you mix them, just in reverse.
A ground connection is most definitely and absolutely required. There must be a return loop. What happens if you remove the (-) cable from your car battery?
Isolation is at the relay contacts not the control circuit.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

LTolledo
Posts: 3306
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 6:12 am

For real isolation (no wires between RPi and relay board) how about using WiFi controlled relay board for your project.

I've seen some available at Amazon and Ebay.....

of course you will have to modify (heavily?) your code....
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

PhatFil
Posts: 1672
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 7:20 am

LTolledo wrote:
Tue Sep 11, 2018 6:12 am
For real isolation (no wires between RPi and relay board) how about using WiFi controlled relay board for your project.

I've seen some available at Amazon and Ebay.....

of course you will have to modify (heavily?) your code....
I agree 100%... and it would remove the need for 90% of the code too ;)
check out the tasmota firmware that can be applied to a whole raft of commercially available wifi enabled products costing from £3-4 up
https://github.com/arendst/Sonoff-Tasmota/wiki and when it comes to control you can write your own code, but systems like home assistant
already exist. personally i like node red for control but ;)

however your problem,

1) connect gnd and when using 2x dc power supplies even different voltages they should share a common ground,,
2) the relays are probably turned on by pulling to gnd. but you can test this without the pi,
hook the relay upto 5v on its power pins as in your diagram and use 2x 1.5v batteries to simulate the pi logic voltage and connect to the control pins of the relay board to confirm if:
a) 3v is sufficient to wsitch and
b) if its pulling up to 3v or pulling down to gnd thats operating the relay ;)

after testing you should be good to go..

PhatFil
Posts: 1672
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 7:21 am

PhatFil wrote:
Tue Sep 11, 2018 7:20 am
LTolledo wrote:
Tue Sep 11, 2018 6:12 am
For real isolation (no wires between RPi and relay board) how about using WiFi controlled relay board for your project.

I've seen some available at Amazon and Ebay.....

of course you will have to modify (heavily?) your code....
I agree 100%... and it would remove the need for 90% of the code too ;)
check out the tasmota firmware that can be applied to a whole raft of commercially available wifi enabled products costing from £3-4 up
https://github.com/arendst/Sonoff-Tasmota/wiki and when it comes to control you can write your own code, but systems like home assistant
already exist. personally i like node red for control but ;)

however your problem,

1) connect gnd and when using 2x dc power supplies even different voltages they should share a common ground,,
2) the relays are probably turned on by pulling to gnd. but you can test this without the pi,
hook the relay board upto 5v on its power pins as in your diagram and use 2x 1.5v batteries to simulate the pi logic voltage and connect to the control pins of the relay board to confirm if:
a) 3v is sufficient to wsitch and
b) if its pulling up to 3v or pulling down to gnd thats operating the relay ;)

after testing you should be good to go..

User avatar
procount
Posts: 2209
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 8:42 am

You didn't provide a link to any datasheet, but it looks very similar to this smaller version at.... <redacted>.

Edit: since that link is longer valid, I've no idea what it was now and probably it was different to the actual device pictured, because a simple opto-isolator does not need a gnd connection. Therefore I've deleted my comments for the avoidance of future doubt.
Last edited by procount on Fri Apr 24, 2020 6:21 pm, edited 1 time in total.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

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

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 9:09 am

Many of these relay boards wont work with the pi ( assuming you have it connected correctly including a ground between the pi and the relay board ) because they are designed to work with a 5v gpio like the arduino uses.

This sticky explains why : https://www.raspberrypi.org/forums/view ... 8#p1225448

The simple answer is to use a ULN2803A between the pi gpio and the relay board, if you relay board is active low
or
using several 74HC125 if the relay board is active high.

if you need help testing your board to determine its activation state and connection of the interface chips just ask.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

bizilux
Posts: 20
Joined: Thu Dec 01, 2016 6:30 pm

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 11:05 am

okay, thanks everyone for your help, but i still haven't solved it. probably my inexperience has to do with it too, but that's why we are here :)

i do know that getting relays to work with pi is tricky, because pi only provides 3.3V. thats why im doing it JD-VCC way... because then i think it is suppose to be easier because JD-VCC provides 5V for switching of relays.

here is what i tried now...
the battery way, 2xAA batteries, aprox 3.2V, if i connect red to vcc and gnd to in1 or in2, etc... red status light on relay turns on. And then if i connect external 5V supply to JDvcc and gnd, relay switches on, based on which input pin i touch with ground wire of batteries. So ground wire of 3.2V batteries is switching relays on/off

Now regarding connecting ground of pi zero to relays... I found multiple sources saying this:
Now, let's look at how to connect the relay board so it is really isolated. Notice that board has a jumper between "Vcc" and "JD-Vcc". Remove it. For complete optical isolation, connect "Vcc" to Arduino +5 volts but do NOT connect Arduino Ground. Connect your Arduino Digital Output pins to "IN0", "IN1" etc. Connect a separate +5 supply to "JD-Vcc" and board Gnd. This will supply power to the transistor drivers and relay coils. Look at the diagram above. Only the part to the left of the small blue arrows is connected to Arduino.
link: https://arduino-info.wikispaces.com/RelayIsolation

The thing is, when i was messing around with arduinos and relays one year ago, i had this exact setup going on... JD-VCC and GND... but arduino only had 5v pin and in1,.. pins installed, no GND. now one year later with pi zero i cant get it to work

and i tried using multi meter with continuity mode, and GND pins on relay board are definitely connected, meaning that if i also connect pi zero to gnd pin on relay board, then pi zero will not be completely isolated from relays anymore right?

what am i trying to achieve with these relays? its a simple PI bartender where i will use 8 peristaltic pumps to pump liquid into glass. Thats why i want isolated relay board, and external power supply to relay board to protect PI from spikes in pumps etc...

regarding wifi relays... that seems unnecessarily complicated and would add quite a bit of cost if i would buy premade ones... im trying to keep costs down and 3d print parts etc... and its gonna be opensource etc... for other people to replicate, so cost is a factor here... wifi relays will be last resort...
This sticky explains why : https://www.raspberrypi.org/forums/view ... 8#p1225448

The simple answer is to use a ULN2803A between the pi gpio and the relay board, if you relay board is active low
or
using several 74HC125 if the relay board is active high.
i've seen that sticky, but its not really written for beginners lol, third of the stuff that is said in there doesn't make sense to me yet... and sadly i dont have either ULN2803A or 74HC125 right now with me... i do not yet understand how these weird spidery transistors work lol, i have yet to dive down that learning hole...


on a plus note... my A8 3D printer just arrived... so even if relays aint working right now, im as happy as i can be right now lol. it feels like Christmas came early :D

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

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 11:39 am

First your pi is isolated from the relays by the opto isolators that are mounted on the relay board, assuming your relay board matches the image
( might be a good idea to post a link to when you get your board from )

Second its normal and required to connect grounds between devices even when they are operating at different voltages because you need to complete the circuit, electricity flows round a circuit from positive to negative so if you remove the ground the circuit is no longer compete.

From what your describe in testing your relay board it would appear to be active low, so connecting a ground to the IN switches on the relay, even though it works using a battery it may not work with the pi as the battery can supply more current at 3.3v than the pi can.

You may find to get the relay board to work with the pi you will need to use the ULN2803A and drive the switching on the relay board at 5v, as many of the relay boards you can buy on line are designed to work at 5v as used by the arduino.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

bizilux
Posts: 20
Joined: Thu Dec 01, 2016 6:30 pm

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 12:39 pm

i am mainly trying to get this 8 channel relay working

5V Eight 8 Channel Relay Module With Optocoupler For Arduino PIC AVR DSP ARM


Image

i also tried 4 and 2 channel ones: https://www.aliexpress.com/item/Blue-re ... 4c4dlGMy7U and https://www.aliexpress.com/item/2-Chann ... 4c4dlGMy7U


i suppose i will try to connect GND pin from pi zero to relay later... i just dont want to damage pi zero or its gpio pins, I just bought it few days ago because SD card reader on my pi 3 B stopped working out of the blue, after i didnt use it for a year...

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

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 12:47 pm

So from the specification it clearly states "5v active low" , so getting the 3.3v of the gpio to drive this relay board directly if probably a non starter.

I would suggest you use a ULN2803A to drive the relay board from the pi . Digram to follow.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

bizilux
Posts: 20
Joined: Thu Dec 01, 2016 6:30 pm

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 12:56 pm

pcmanbob wrote:
Tue Sep 11, 2018 12:47 pm
So from the specification it clearly states "5v active low" , so getting the 3.3v of the gpio to drive this relay board directly if probably a non starter.

I would suggest you use a ULN2803A to drive the relay board from the pi . Digram to follow.
Right... That might be it then... Could you maybe make diagrwm in fritzing? :) Those are easiest to follow because we get to see pictures with it too :)

You would need to download relay board image from internet though i think...

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

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 1:12 pm

I don't use fritzing but this digram should be easy to follow.

Image

You can of course use any gpio pin order/selection you want to drive the ULN2803A I just chose those one to make it a clearer layout.

When buying your ULN2803A make use you get the exact part number including the A at the end as this is what makes it compatible with the 3.3v of the pi gpio.

As you can see we have the ground on the relay board and the pi connected to the ULN2803A this is to completed both circuits so that current can flow and is no risk to your pi.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PhatFil
Posts: 1672
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 2:04 pm

if 2x aa cells connectd to the board with +ve to vcc and -ve successfully switching the relays when touched to the control pins, the test proves a) you can probably use the pi (vvoltage supply differences as stated above could still be a factor, it was a quick rough test..) and you need to use internal pull ups on the gpio pins used to switch and pulling the pins to gnd will operate the relay as expected ;)

I too am a beginner with electronics and such forth and as such am more than happy to stand corrected, but when it comes to common ground connections the way it made sense to me was to think of it providing a common voltage datum of zero level,

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

Re: How to control relays in safe way (isolated) ?

Tue Sep 11, 2018 2:10 pm

PhatFil wrote:
Tue Sep 11, 2018 2:04 pm
if 2x aa cells connectd to the board with +ve to vcc and -ve successfully switching the relays when touched to the control pins, the test proves a) you can use the pi and its control voltage and you need to use internal pull ups on the gpio pins used to switch and pulling the pins to gnd will opperate the relay as expected ;)

I too am a beginner with electronics and such forth and as such am more than happy to stand corrected, but when it comes to common ground connections the way it made sense to me was to think of it providing a common voltage datum of zero level,
You can't use pull ups on a gpio output you can only use them on a gpio input.

The board is designed for 5v operation so yes using 2x1.5v batteries might just power the circuit to enable the relays but as I said 2x1.5v batteries can supply a lot more current that the pi gpio.

go read the link I posted to the sticky it will explain how the circuit works and why it does not work with the pi 3.3v gpio.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

bizilux
Posts: 20
Joined: Thu Dec 01, 2016 6:30 pm

Re: How to control relays in safe way (isolated) ?

Mon Sep 17, 2018 7:29 pm

good news,
i stopped complicating, at least for now when im still at prototyping stage, and just wired it the old fashioned way, connected the ground also, and now its working ofcourse... 3.3V is enough to turn on these relays.. i still dont know how safe it is when i will actually connect peristaltic pumps to it... i guess we will see, pi zero W is just 10€ luckily.

I did almost made a big mistake tho :D instead of putting jumper on JD-VCC and VCC, i've put it on VCC and GND. (those 3 pins are close to eachother)... i turn on pi zero, and i start to smell plastic... i was like what is that smell :D I didnt think much of it because i was just gluing stuff few minutes before and i thought it must be that. and then like 30seconds pass and i can see smoke rising... and im like great, another PI died...
well luckily jumper cables started burning and not raspberry pi lol, best possible scenario :lol:

thanks for picture pcmanbob, i ordered them and i'll wire them up when they come, because why not, i suppose its safer that way :)

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

Re: How to control relays in safe way (isolated) ?

Mon Sep 17, 2018 8:31 pm

The problem with trying to use 3.3v to switch the relays driven by 5v is that you can switch them on , but you might find that you cant switch them off, because, a gpio low will provide the ground to switch the relay on , but the 3.3v can match the 5v to stop current flow so the relay stays on.

Also you are using a gpio to ground 5v if there is any problem in the circuit you could end up with more than 3.3v on the gpio pin resulting in damage to your pi.

by using the ULN2803A you are grounding the 5v via the transistors in the chip and so keeping it away from the pi gpio.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

bizilux
Posts: 20
Joined: Thu Dec 01, 2016 6:30 pm

Re: How to control relays in safe way (isolated) ?

Tue Sep 18, 2018 8:12 am

yeah thats why i will still order them and hook them up when they arrive.

im pretty positive that its the same, but i would like to be sure nonetheless...
ULN2803A and ULN2803APG is the same? i can only find them with PG at the end

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

Re: How to control relays in safe way (isolated) ?

Tue Sep 18, 2018 10:35 am

bizilux wrote:
Tue Sep 18, 2018 8:12 am
yeah thats why i will still order them and hook them up when they arrive.

im pretty positive that its the same, but i would like to be sure nonetheless...
ULN2803A and ULN2803APG is the same? i can only find them with PG at the end
Yes that's fine there are several version numbers and the only difference I can see on the specifications sheet I have is slightly different power dissipation ratings.

The ULN2803APG has a max power rating of 1.47W with a max current per channel of 500mA, probably 50 times what you need to drive the relay card.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

KenMacD
Posts: 24
Joined: Fri Dec 07, 2012 12:09 am

Re: How to control relays in safe way (isolated) ?

Fri Apr 24, 2020 5:20 pm

klricks wrote:
Tue Sep 11, 2018 1:44 am
bizilux wrote:
Tue Sep 11, 2018 12:25 am
klricks wrote:
Tue Sep 11, 2018 12:16 am
In your drawing you left out the required ground wire between the RPi and relay board.

Most relay boards are active LOW which means a LOW (0V) turns the relay ON and HIGH turns them OFF

I don't think you need the VCC from RPi to relay board if the relay board is powered separately.
As far as i know, ground should not be connected between pi and relays, or else it doesnt become isolated anymore. Its a known design flaw of these boards because they have 2 ground connections. You only should ever use one i believe.

I think mixing low and high isnt an issue, it Should still work if you mix them, just in reverse.
A ground connection is most definitely and absolutely required. There must be a return loop. What happens if you remove the (-) cable from your car battery?
Isolation is at the relay contacts not the control circuit.
There's so many comments saying to hook up GND. I know this is an old post but it comes up in searches so I figured I should comment that you should not hook up GND. If you do you're breaking one layer of galvanic isolation provided by this board and making things worse, not better.

There is no 'return loop' through GND. The circuit for the LED in the optocoupler is from VCC to the GPIO pin. That's a circuit.

It's disappointing to see someone asking the 'safe way' and being given bad info.

emma1997
Posts: 773
Joined: Sun Nov 08, 2015 7:00 pm
Location: New England (not that old one)

Re: How to control relays in safe way (isolated) ?

Sat Apr 25, 2020 1:24 am

Disappointing to see so much FUD on the internet. Well maybe not so much disappointing as entertaining. lol

Highly unlikely separate grounds are going to make things any more 'safe'. These relays have all the isolation needed. In fact the main thing that optoisolator circuit does is often prevents the board from working at all. On more than one occasion I've had to short the LED just to get contact closure. Separate power supplies are rarely required either with such low power coils. Worst case maybe an elco cap across the rails but even that's rare.

In most situations best bet is to bypass that optoisolator circuit entirely and drive transistors directly:
Attachments
4_channel_relay_schematic4.JPG
4_channel_relay_schematic4.JPG (72.4 KiB) Viewed 1447 times
8channel-relay-board.JPG
8channel-relay-board.JPG (69.37 KiB) Viewed 1447 times

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

Re: How to control relays in safe way (isolated) ?

Sat Apr 25, 2020 8:05 am

emma1997 wrote:
Sat Apr 25, 2020 1:24 am
Highly unlikely separate grounds are going to make things any more 'safe'. These relays have all the isolation needed.
That may be true in parts of the world where the supply voltage is 110V. It is not true for 230V supplies.

Everything I've done with relays connected to the RPi has switched 24V (less than 48V is safe) then switched the mains with a 24V relay closer to the load.
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.

emma1997
Posts: 773
Joined: Sun Nov 08, 2015 7:00 pm
Location: New England (not that old one)

Re: How to control relays in safe way (isolated) ?

Wed Apr 29, 2020 11:05 pm

May be missing something but I don't see where line voltage is relevant. We are talking about coil to contacts breakdown. Personally tested with professional gear to be IIRC at least 2500v. That was for Tongling brand which cross to the Songle clones in OP.

BTW little known fact: USA also uses 220vac. Measured at wall socket of all major major appliances in my house. It's a two phase thing. Not sure if EU is single 220 or 440 between different runs.

As far as safety, I know 12dc can kill. Many years back I witnessed a fellow reach for a tool and put car battery across his chest. He is no longer with us. Many other similar documented cases. Milliamps fatal under the right conditions.

Anyway not such a huge difference 110 vs 220 because both will kill you if damp and different hands. You cant let go either way.

Return to “Beginners”