scaiff
Posts: 6
Joined: Sun Aug 23, 2015 3:52 pm

Relay closing causing interrupt on unconnected pin

Sun Aug 23, 2015 5:26 pm

I'm looking for a little help because I cant figure out what is going wrong with my build.

Overall:
The project is a IR motion sensor attached to (and powered by) a Raspberry pi 2. When the PIR is triggered it causes an interrupt in a python script which responds by opening a relay and consequently cutting the 110v ac line to the outlet a dehumidifier is attached to.

The Build:
I'm using this PIR: https://www.amazon.com/gp/product/B008A ... UTF8&psc=1
and this relay: https://www.amazon.com/gp/product/B00E0 ... UTF8&psc=1

The main build is a extra deep outlet box. Inside the box is the relay, a 5v power supply scavenged from an old usb charger and an outlet. The 110v ac line enters the box and divides to the 5v power supply and the common pole of the relay. The 110 neutral connects to the outlet and the ground of the 5v power supply. Another line runs from the normally closed pole of the relay to the hot side of the outlet.

The relay is optically isolated: 5v from the usb power supply runs to the ry-vcc of the relay, the jumper is removed and the neutral of the power supply runs to the GND on the same header as the ry-vcc. The relay is triggered by a low on the IN1

The only connections to the Pi is one line from 3.3v to the VCC and one line from a GPIO to IN1.

The problem:
The python script works great as long as there is nothing attached to the load of the outlet. When motion is detected the pi sets the relay GPIO low and the hot line to the outlet is cut. After the delay set by the script the pi sets the GPIO for the relay back high and it re-closes and restores power to the outlet.

The problem is as soon as any load (even my voltmeter) is attached to the outlet. The pi will cut the power just fine. However, the instant (literally no delay at all) it recloses the outlet the pi senses an interrupt on the completely separate pin the PIR is attached to and switches the relay back open.

I cant figure out what is causing a changing load (or any load at all) on an outlet on the other side of an optically isolated relay to cause the Pi to sense an interrupt on a GPIO pin that is in no way associated with the relay. Do I have a bad relay board or bad pi? I'm driving a dehumidifier so there could be some EMI but I thought the opto relay should take care of that. I would think it was a bug in my script except for the fact that it works flawlessly when there is no load on the outlet.

Thanks for any help!

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 4:18 am

possible read here first
viewtopic.php?f=28&t=118595&p=805228&hi ... 3V#p805228
viewtopic.php?f=37&t=110849&p=761157&hi ... ck#p761157
http://www.oddwires.com/content/songlesrdrelay.pdf

actually the RPI GPIO side is separated from the relay coil side by opto.
if the VCC jumper is open and the relay side pin is powered by a separate 5V, the GND connector should be connected to that PS and NOT to the RPI GND??

the relay coil side is separated from the OUTPUT side by the relay itself.
( no GND connection allowed )

but this circuit is from a different maker
http://kll.engineering-news.org/kllfusi ... ircuit.png

2 reasons why that 5V blocks are bad anyhow:
-1- with 5V RPI VCC to opto you actually connect 5V to a GPIO pin what should be only 3.3V
with 3.3V power to the 2LED + resistor in serial might not work to allow proper current.

-2- the circuit uses a inverted logic, you need to set the GPIO pin to OUTPUT and HIGH to not have a current in the LEDs and not have the relays energized. i hear about a situation with a arduino who looses its power but the relay board VCC was still on and all relays and OUTPUT loads fired up, with bad damage.

i did not find one, but it would be better to have a board where the GPIO sends its 3.3V active via a resistor in the opto led back to RPI GND.
and no VCC wiring from RPI to board at all.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 4:39 am

I think you should unplug this right away as you clearly have no clue and are likely to kill yourself or someone else. DO IT NOW!

I'll explain why in my next post. Don't wait for it, just unplug everything.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 4:50 am

scaiff wrote: The main build is a extra deep outlet box. Inside the box is the relay, a 5v power supply scavenged from an old usb charger and an outlet. The 110v ac line enters the box and divides to the 5v power supply and the common pole of the relay. The 110 neutral connects to the outlet and the ground of the 5v power supply.
Do you mean the Neutral of the 5V PSU? I hope so.
Another line runs from the normally closed pole of the relay to the hot side of the outlet.
It would be better to use the Normally Open pole and invert your software logic. Then the outlet is not live unless the software turns it on. Your way means the outlet is live even if the Pi is not running.
The relay is optically isolated: 5v from the usb power supply runs to the ry-vcc of the relay, the jumper is removed and the neutral of the power supply runs to the GND on the same header as the ry-vcc.
Seriously, you are going to die. Neutral may be at ground potential, but it might not. And some creative electrician may have wired the socket backwards (which will not affect ac loads, but will be quite visible when your Pi explodes).
The relay is triggered by a low on the IN1
I found I had to remove the LED on those boards and replace it with a short to make the relay work.
The only connections to the Pi is one line from 3.3v to the VCC and one line from a GPIO to IN1.
Actually, you need a common gound connection too, which you have provided by unconventional means.
The problem:
There is more than one problem...

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 5:07 am

So, your solution should be:

Power cord into the electrical box. The cord contains three wires: live, neutral and ground.

Ground connects to the outlet ground only (unless there is a Ground on the 5V PSU, in which case you should connect it there too), and if everything is in a metal box, ground the metal box too.

Live connects to the relay common pin screw terminal and to the Live input of the 5V PSU.

Neutral connects to the outlet neutral and the Neutral input of the 5V PSU.

Relay NO screw terminal connects to the outlet live. You might prefer to use NC, but that's up to you.

That's the mains stuff.

The other stuff is low voltage, and connects to the pins on the relay board, and the Pi is getting power from the 5V PSU.

Disconnect the jumper. Short the LEDs with a tiny piece of wire soldered across them. Actually, this is optional, but I'd be surprised if the relay works without doing it.

Connect GND to Pi GND.

Connect IN1 to a GPIO

Connect VCC to Pi 3.3V

Connect RY-VCC to Pi 5V

The relay will operate when IN1 is pulled low.

I am not an electrician, so you follow these instructions at your own risk. I bear no responsibility for the consequences of following these instructions.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 5:31 am

Actually, you might not die. You might be using 'neutral' to mean the negative pin of the 5V dc PSU. Or you might not. You need to brush up on your terminology.

scaiff
Posts: 6
Joined: Sun Aug 23, 2015 3:52 pm

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 10:25 pm

ame wrote:So, your solution should be:

Power cord into the electrical box. The cord contains three wires: live, neutral and ground.

Ground connects to the outlet ground only (unless there is a Ground on the 5V PSU, in which case you should connect it there too), and if everything is in a metal box, ground the metal box too.

Live connects to the relay common pin screw terminal and to the Live input of the 5V PSU.

Neutral connects to the outlet neutral and the Neutral input of the 5V PSU.

Relay NO screw terminal connects to the outlet live. You might prefer to use NC, but that's up to you.

That's the mains stuff.

The other stuff is low voltage, and connects to the pins on the relay board, and the Pi is getting power from the 5V PSU.

Disconnect the jumper. Short the LEDs with a tiny piece of wire soldered across them. Actually, this is optional, but I'd be surprised if the relay works without doing it.

Connect GND to Pi GND.

Connect IN1 to a GPIO

Connect VCC to Pi 3.3V

Connect RY-VCC to Pi 5V

The relay will operate when IN1 is pulled low.

I am not an electrician, so you follow these instructions at your own risk. I bear no responsibility for the consequences of following these instructions.
This is exactly what I have done except that RY-VCC is connected to 5v PSU and the GND of the relay board is attached to the 5v neutral of the 5v PSU (PSU has no GND 110v or 5v). There are two pins marked GND on the relay board but they are identical (tied together with a tracing).

I want the outlet tied into the NC pole of the relay because I want the outlet to default to on (with or without the Pi attached), basically I want it to the a normal outlet that my Pi has the ability to turn off.

The whole reason I put a 5v PSU in the box is I was under the impression that by powering the relay coil with a separate PSU I could maintain the opto isolation of the PI from the 5v and 110v stuff and protect the Pi from 'that which might go wrong'. Am I misled, is there an advantage to powering the relay coil from the pi 5v?

Sorry if my terminology was off, and I intermingled GND and Neutral (and set off your 'you're gonna die speach'). There is only one GND involved: the GND from the 110v romex (bare copper line) entering the outlet box and that is tied to the green GND screw of the outlet only (plastic box). Everything else is neutrals. (The relay board and 5v PSU label the 5v return (the wire that is not hot) GND and what they mean is neutral, right? ....None of the low voltage stuff has a true ground, it is all neutral, correct? The plug my pi uses is only a 2 prong so there not a true ground to the Pi......At least this is what I thought, did I miss something massive here?)

Thanks for all the help and answers!

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 10:39 pm

You say you are powering the relay coil with a separate PSU, so what are you powering the Pi with?

You need a connection from the relay board GND to the Pi. If you want to maintain isolation you'll have to cut the GND trace somewhere, and then you'll need two power supplies.

scaiff
Posts: 6
Joined: Sun Aug 23, 2015 3:52 pm

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 11:01 pm

ame wrote:You say you are powering the relay coil with a separate PSU, so what are you powering the Pi with?
Standard usb power supply powering the Pi: plugged into the wall and the Pi. Inside the outlet box I built there is another power supply. It was a 5v usb socket phone charger (I tested it, its is putting out 5v) that I pulled the board out of (bridge rectifier/transformer and associated stuff) removed the usb port and soldered leads from the +5v and Gnd.
You need a connection from the relay board GND to the Pi. If you want to maintain isolation you'll have to cut the GND trace somewhere, and then you'll need two power supplies.
With this relay board you don't use a GND from the board to the Pi. Pi supplies 3.3v and the GPIO becomes the ground when it goes low. GPIO high = no current flow (3.3v in each direction), GPIO low = out the 3.3v, in the GPIO, and relay activates. Yes, 3.3v is enough to power both led's (indicator and opto), the relay toggles as it should. Yes, I think its a strange design for relay board too, and I would have made it differently but what do I know....I'm posting on the internet for help.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Mon Aug 24, 2015 11:33 pm

scaiff wrote:
ame wrote:You say you are powering the relay coil with a separate PSU, so what are you powering the Pi with?
Standard usb power supply powering the Pi: plugged into the wall and the Pi. Inside the outlet box I built there is another power supply. It was a 5v usb socket phone charger (I tested it, its is putting out 5v) that I pulled the board out of (bridge rectifier/transformer and associated stuff) removed the usb port and soldered leads from the +5v and Gnd.
You need a connection from the relay board GND to the Pi. If you want to maintain isolation you'll have to cut the GND trace somewhere, and then you'll need two power supplies.
With this relay board you don't use a GND from the board to the Pi. Pi supplies 3.3v and the GPIO becomes the ground when it goes low. GPIO high = no current flow (3.3v in each direction), GPIO low = out the 3.3v, in the GPIO, and relay activates. Yes, 3.3v is enough to power both led's (indicator and opto), the relay toggles as it should. Yes, I think its a strange design for relay board too, and I would have made it differently but what do I know....I'm posting on the internet for help.
Ah, there are two power supplies. That makes more sense.

And yes, you are right, the optoisolator is isolated in the way you describe. For my project I am using the Pi's 5V line to power the relay, so I need a connection to Pi ground.

My guess is that the problem is electrical noise somewhere which affects the GPIO that is reading the PIR state. Relays in particular will produce a nice noisy spike when they operate, which is probably being coupled to the Pi due to the proximity of the hardware. You could try some software debouncing on the PIR input signal and see if that helps, or some decoupling capacitors at various points where power is being used.

How is the PIR connected to the GPIO? It looks like a 5V device. Is the output pin 5V when active? If so, have you done something to drop it down to 3.3V?

I'm sorry I overreacted concerning the mains electrical wiring, but your description wasn't clear until now.

scaiff
Posts: 6
Joined: Sun Aug 23, 2015 3:52 pm

Re: Relay closing causing interrupt on unconnected pin

Tue Aug 25, 2015 12:02 am

My guess is that the problem is electrical noise somewhere which affects the GPIO that is reading the PIR state. Relays in particular will produce a nice noisy spike when they operate, which is probably being coupled to the Pi due to the proximity of the hardware. You could try some software debouncing on the PIR input signal and see if that helps, or some decoupling capacitors at various points where power is being used.
I don't know enough about electronics yet to have a handle on decoupling capacitor, but from my think knowledge this sounds promising....care to elaborate?
How is the PIR connected to the GPIO? It looks like a 5V device. Is the output pin 5V when active? If so, have you done something to drop it down to 3.3V?
Im running the PIR of the 3.3 r.pi line, it seems to work just fine.

The problem boils down to R.Pi seeing a high on the PIR GPIO when the relay changes state.....it doesn't seem like something that 'should' happen and it makes me wonder if maybe my relay board is bad and 'leaking' somehow back to the pi. If this is the case Im wondering if the pi was damaged already.
I'm sorry I overreacted concerning the mains electrical wiring, but your description wasn't clear until now.
No worries, I would rather have someone worry about me unnecessarily than not worry when it was necessary.

This is completely off the current line of what I was doing, but I'm tempted to just build it without a microcontroller at all. Im thinking a high on PIR triggering a transistor to charge a CAP (from a powersupply) that then drains (over 10-20 minutes) through a POT across another transistor that would open 3.3v (from a power supply) to the relay board and open the relay shutting off the outlet. With the right value of the resistor POT and CAP size I should be able to get the 10 to 20 minute opening of the relay I am trying to cause using the pi. I have no idea if I could make it work....6 weeks ago I didn't know how a resistor worked.. but I would learn a bunch and it would be fun to build.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Tue Aug 25, 2015 12:13 am

scaiff wrote:
My guess is that the problem is electrical noise somewhere which affects the GPIO that is reading the PIR state. Relays in particular will produce a nice noisy spike when they operate, which is probably being coupled to the Pi due to the proximity of the hardware. You could try some software debouncing on the PIR input signal and see if that helps, or some decoupling capacitors at various points where power is being used.
I don't know enough about electronics yet to have a handle on decoupling capacitor, but from my think knowledge this sounds promising....care to elaborate?
Google is your friend. Basically they are small-value caps that you sprinkle liberally onto your circuit to reduce noise that comes in through the power supply.
How is the PIR connected to the GPIO? It looks like a 5V device. Is the output pin 5V when active? If so, have you done something to drop it down to 3.3V?
Im running the PIR of the 3.3 r.pi line, it seems to work just fine.

The problem boils down to R.Pi seeing a high on the PIR GPIO when the relay changes state.....it doesn't seem like something that 'should' happen and it makes me wonder if maybe my relay board is bad and 'leaking' somehow back to the pi. If this is the case Im wondering if the pi was damaged already.
Well, it's easy to find out. Modify your program so that it prints a message when it turns the relay on and off. Now, disconnect the relay and activate the PIR. If you see only one message "relay on" then you know the software is fine. Now reconnect the relay and activate the PIR. If you see many "relay on" messages then you know the relay is causing interference. I reckon that could be handled in software though.
I'm sorry I overreacted concerning the mains electrical wiring, but your description wasn't clear until now.
No worries, I would rather have someone worry about me unnecessarily than not worry when it was necessary.

This is completely off the current line of what I was doing, but I'm tempted to just build it without a microcontroller at all. Im thinking a high on PIR triggering a transistor to charge a CAP (from a powersupply) that then drains (over 10-20 minutes) through a POT across another transistor that would open 3.3v (from a power supply) to the relay board and open the relay shutting off the outlet. With the right value of the resistor POT and CAP size I should be able to get the 10 to 20 minute opening of the relay I am trying to cause using the pi. I have no idea if I could make it work....6 weeks ago I didn't know how a resistor worked.. but I would learn a bunch and it would be fun to build.
Doing it in analog would also be fine. You might be able to make a discrete circuit, or you might be able to build something around the venerable 555 timer IC. Also the PIR probably has an adjustable 'on' time. If it can be stretched to 20 minutes you should be able to connect the PIR output directly to the relay input.

scaiff
Posts: 6
Joined: Sun Aug 23, 2015 3:52 pm

Re: Relay closing causing interrupt on unconnected pin

Tue Aug 25, 2015 12:40 am

Well, it's easy to find out. Modify your program so that it prints a message when it turns the relay on and off. Now, disconnect the relay and activate the PIR. If you see only one message "relay on" then you know the software is fine. Now reconnect the relay and activate the PIR. If you see many "relay on" messages then you know the relay is causing interference. I reckon that could be handled in software though.
Tried a variant of this, it is definitely in hardware. I could handle it in software except the dehumidifier has two modes: Fan only and Fan + Compressor and it changes back and forth arbitrarily. When it changes I get the false high from the PIR. I could make the software require the high for some arbitrary time period (2 sec say) before cutting the power. I really want the build to be right, however, and correcting in software a problem I can't figure out in hardware makes my teeth itch. (Also it means something is happening I don't understand and I'm not comfortable with a 110v outlet in my house that is doing things I don't understand.)
Doing it in analog would also be fine. You might be able to make a discrete circuit, or you might be able to build something around the venerable 555 timer IC. Also the PIR probably has an adjustable 'on' time. If it can be stretched to 20 minutes you should be able to connect the PIR output directly to the relay input.
I've never run into a 555 before (like I said, I only started playing with electronics for the first time in my life 6 weeks ago) but Ill look it up.

Thanks again for all the good responses, I really appreciate it. Have a great night!

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Relay closing causing interrupt on unconnected pin

Thu Sep 03, 2015 9:06 am

sorry i bring this up again, but inhere
viewtopic.php?p=806614#p806614
i questioned the design of most of that relay blocks / inverted GPIO output logic /

today i found a advert where i ask you to check on circuit on page 4.
http://etteam.com/productPi/ET-OPTO%20R ... %20HAT.pdf
i think this is how it should be done.
p.s. i did not test that circuit or product and i am not related to that company.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Fri Sep 04, 2015 1:00 am

The cheap relay boards from China are fine, but most of them are designed for Arduino and other 5V microcontrollers. In my application I removed the indicator LED, because it caused too much voltage drop. The technique of using two LEDs in series (one optoisolator and one indicator) is very common. I would have preferred to see the LED driven by the output of the optoisolator, in parallel with the relay coil.

I agree that the design could be improved, but it is far cheaper for me to hack on a cheap relay module than to buy the parts and make my own (improved) circuit. The key is to find the schematic of whatever board you are using and not just guess how it should be hooked up.

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Relay closing causing interrupt on unconnected pin

Fri Sep 04, 2015 2:51 am

ame wrote:The relay board are fine
so you not think the inverted output
GPIO LOW = OPTO active and RELAY energized
is a problem/danger?

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Relay closing causing interrupt on unconnected pin

Fri Sep 04, 2015 2:53 am

KLL wrote:
ame wrote:The relay board are fine
so you not think the inverted output
GPIO LOW = OPTO active and RELAY energized
is a problem/danger?
No.

scaiff
Posts: 6
Joined: Sun Aug 23, 2015 3:52 pm

Re: Relay closing causing interrupt on unconnected pin

Fri Sep 04, 2015 9:44 pm

So I have it all sorted out. I tested just about every possible solution including different GPIO pins, a new relay board, shorting the indicator LEDs from the relay board, physical separation of all the components. Nothing made any difference.

In the process of testing, a basement fridge that is on the same breaker, but in no other way connected, kicked on and caused the false interrupt to be thrown. From this I conclude it is voltage dip from the powersupply of the Pi itself that is showing up as the false interrupt. I corrected the issue in software (wait half a second after the interrupt and look at the status of the PIR before acting) and the system has been working flawlessly for a couple days.

Thanks for all the help.

Return to “Troubleshooting”