silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

GPIO Electrical Interference

Sun Apr 26, 2015 12:12 am

Hello,
I modified some code that I found readily available (see below). My raspberry is connected to a relay that when triggered sends a push event and emails me. It current is sensing a normally closed contact on the relay. I also have the ability to use the normally open side of the relay. Unfortunately it appears my code is being triggered by some phantom voltage being picked up by the raspberry. I removed the relay and used a magnetic contact and still had the same issue.

What is the best way to deal with this? Should I connect the NO contact and also test for that condition? Or convert the 16vAC down to a tolerable GPIO pin? Though since this is a doorbell there might always be ~3VAC

Code: Select all

import pycurl, json
from StringIO import StringIO
import RPi.GPIO as GPIO

#setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)

# set to pull-up (normally closed position)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#setup InstaPush variables
# add your Instapush Application ID
appID = "<redacted>"

# add your Instapush Application Secret
appSecret = "<redacted>"
pushEvent = "DoorbellStatus"
pushMessage = "Doorbell Rang"

# use this to capture the response from our push API call
buffer = StringIO()

# use Curl to post to the Instapush API
c = pycurl.Curl()

# set API URL
c.setopt(c.URL, 'https://api.instapush.im/v1/post')

#setup custom headers for authentication variables and content type
c.setopt(c.HTTPHEADER, ['x-instapush-appid: ' + appID,
			'x-instapush-appsecret: ' + appSecret,
			'Content-Type: application/json'])


# create a dict structure for the JSON data to post
json_fields = {}

# setup JSON values
json_fields['event']=pushEvent
json_fields['trackers'] = {}
json_fields['trackers']['message']=pushMessage
#print(json_fields)
postfields = json.dumps(json_fields)

# make sure to send the JSON with post
c.setopt(c.POSTFIELDS, postfields)

# set this so we can capture the resposne in our buffer
c.setopt(c.WRITEFUNCTION, buffer.write)

# uncomment to see the post sent
#c.setopt(c.VERBOSE, True)


# setup an indefinite loop that looks for the door to be opened / closed
while True:

	GPIO.wait_for_edge(23, GPIO.RISING)
	print("Doorbell Rang\n")
        execfile ("gmailimage.py")

	# in the door is opened, send the push request
	c.perform()
	

	# capture the response from the server
	body= buffer.getvalue()

	# print the response
	print(body)

	# reset the buffer
	buffer.truncate(0)
	buffer.seek(0)

	# print when the door in closed
	GPIO.wait_for_edge(23, GPIO.FALLING)
	print("Doorbell Free\n")

# cleanup
c.close()
GPIO.cleanup()



ghp
Posts: 1488
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO Electrical Interference

Sun Apr 26, 2015 5:48 am

Hello,

when the relay is switched on/ off, the magnetic field changes can induce a current spike on the switch side wires.
The internal resistors might be too high to supress these spikes. Making the resistors in input path much lower will help.

Try using a separate pullup resistor of 1k connected to 3.3V (I have seen in your code that you use PUD_UP).
In addition to this, a small capacitor of 10n from the GPIO to GND could help. Together with this, a series resistor of 100 makes a low pass filter against switch bounces.

Keep relay coil connector wires and switch wires apart.

Regards,
Gerhard

silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

Re: GPIO Electrical Interference

Sun Apr 26, 2015 11:12 am

Gerhard,
I don't believe the relay is causing the false detections, as this is happening at random times when the relay isn't being tripped. If the 1k and cap will help I can try that.

Thanks

ghp
Posts: 1488
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO Electrical Interference

Sun Apr 26, 2015 1:09 pm

Hello,

random errors are strange. Could you post a wiring diagram of your setup ?

Gerhard

User avatar
r4049zt
Posts: 113
Joined: Sat Jul 21, 2012 1:36 pm
Contact: Website

Re: GPIO Electrical Interference

Sun Apr 26, 2015 3:43 pm

As soon as I saw the question with 16V AC in it, I imagine all those 0 to +3.3V rated transistors in your poor rPi getting smoked.
Buy yourself a 1x2 inch pinboard on 1/10 inch pitch, a bag of cheap fet or transistors (it does not matter which, as long as their Vgs or Vbe is sufficient), a bag of assorted 1/8 watt resistors, some bog standard diodes and at least one LED. The green ones are quite nice. ONLY after a multimeter has checked that you've made something to buffer doorbell to 0V = OFF 3.3V = ON should you consider connecting to a GPIO pin of the pi, and replacing the twin AA battery power supply at your buffer testing board with power from the GPIO 0V and 3.3V pins. Interference is the least of your worries until you've got this much done.

silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

Re: GPIO Electrical Interference

Sun Apr 26, 2015 4:08 pm

Here is a brief overview of wiring

Relay takes in 16vac when doorbell rings. NO output from relay to Gpio and relay comm to Gpio. Raspberry triggers well off relay just some stray that causes raspberry to trigger. I'm sure it's on the raspberry. I repaced relay withh magnetic alarm sensor and getting random triggers

User avatar
r4049zt
Posts: 113
Joined: Sat Jul 21, 2012 1:36 pm
Contact: Website

Re: GPIO Electrical Interference

Sun Apr 26, 2015 4:20 pm

ok. so 16Vac presumably to the coil of the relay might close it.
Does it rattle and hum ?
You've not said where you get your 3.3V from (relying on the GPIO internal pullup might be sensible, but might warm up your chip).
You might want to check your code to see what pullup resistors are in place. Please can you post here some ascii representation of the circuit while the relay is closed, and the circuit while the relay is open.

Many "interference" type problems arise from one of the relay states being "not connected" "undefined", and you've not said enough yet to rule those out.

User avatar
mikronauts
Posts: 2779
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: GPIO Electrical Interference

Sun Apr 26, 2015 4:48 pm

I'd use an AC input opto isolator, and look for the zero crossing events on the Pi side.

No pulses = bell not pressed

120Hz pulses = bell ringing
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

ghp
Posts: 1488
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO Electrical Interference

Sun Apr 26, 2015 5:32 pm

Basically this wiring seems ok. The approach to use a relais to separate AC and DC circuitry is fine.
So one relais switch side is connect to a input pin, the other is connected to GND (you wrote GPIO). And the two coil connectors are only connected to some remote AC voltage, no connection to GPIO or GND.

The software looks ok. So I still think its a wiring problem. Without AC applied, the system assumes that the contact is closed. So when a wire has a loose connection (bad solder point, wire broken, bad connectors), there will be sporadic triggers. Even very short spikes on ac side could result in very short movements of the contacts.

Another possibility are 'burned contacts', having had too much current in a previous live and contact resistance is too high (I have some old switches in my collection having a few kOhm in closed state). But this is what you have checked with a replacement switch.

- Carefully shaking the assembly could give indications whether loose connections are there.
- Disconnect the AC-side temporarily, when then no sporadic triggers are occurring, trouble is induced from this side.
- Too high contact resistance is found by measuring input voltage GND to GPIO-Pin. Should be very close to zero when contacts are closed.
- Nevertheless I would recommend to use a 1k pullup GPIO to 3.3V. This increases current in the input loop to 3.3mA, well higher than some current spikes induced from cables. The condensator helps removing spikes.

Good luck
Gerhard

silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

Re: GPIO Electrical Interference

Sun Apr 26, 2015 5:53 pm

thank you for the abundance of help. I'm overwhelmed and hopefully can answer all the questions here

I don't hear the relay rattling and huming

Im getting the 3.3v for the NC from (GPIO.PUD_UP) in the code.

Concerning the below are you saying not having the NO side of the relay connected could be causing issues?
"Many "interference" type problems arise from one of the relay states being "not connected" "undefined", and you've not said enough yet to rule those out."

The opto isolator was another approach I looked into. So basically I need one of these https://www.sparkfun.com/products/314?g ... gQodk2EA3A and filter the voltage going into it?

The wires right now are just twisted together and black tapped as Im testing and working out kinks. I could solder or barrel connector them together. The environment they are in has nothing that is going to shake them

I have removed the AC side and put magnetic contacts in place and have experienced the same issues. Do I need shielded wire for the 15 foot run from the raspberry to the relay and 20 foot shielded cable for the doorbell to the relay? rIght now I used doorbell wire from the doorbell to the relay and garage door wire from the relay to the raspberry

Another avenue I was going to take is coding it to check for another condition. As I have cameras that will sense motions, I can query the cameras xml and see if a value is set. Not sure if this is wise to code for it and hiding the problem. I would rather not code and hide an issue.

boyoh
Posts: 1468
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: GPIO Electrical Interference

Sun Apr 26, 2015 5:58 pm

silvering wrote:Hello,
I modified some code that I found readily available (see below). My raspberry is connected to a relay that when triggered sends a push event and emails me. It current is sensing a normally closed contact on the relay. I also have the ability to use the normally open side of the relay. Unfortunately it appears my code is being triggered by some phantom voltage being picked up by the raspberry. I removed the relay and used a magnetic contact and still had the same issue.

What is the best way to deal with this? Should I connect the NO contact and also test for that condition? Or convert the 16vAC down to a tolerable GPIO pin? Though since this is a doorbell there might always be ~3VAC

Code: Select all

import pycurl, json
from StringIO import StringIO
import RPi.GPIO as GPIO

#setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)

# set to pull-up (normally closed position)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#setup InstaPush variables
# add your Instapush Application ID
appID = "<redacted>"

# add your Instapush Application Secret
appSecret = "<redacted>"
pushEvent = "DoorbellStatus"
pushMessage = "Doorbell Rang"

# use this to capture the response from our push API call
buffer = StringIO()

# use Curl to post to the Instapush API
c = pycurl.Curl()

# set API URL
c.setopt(c.URL, 'https://api.instapush.im/v1/post')

#setup custom headers for authentication variables and content type
c.setopt(c.HTTPHEADER, ['x-instapush-appid: ' + appID,
			'x-instapush-appsecret: ' + appSecret,
			'Content-Type: application/json'])


# create a dict structure for the JSON data to post
json_fields = {}

# setup JSON values
json_fields['event']=pushEvent
json_fields['trackers'] = {}
json_fields['trackers']['message']=pushMessage
#print(json_fields)
postfields = json.dumps(json_fields)

# make sure to send the JSON with post
c.setopt(c.POSTFIELDS, postfields)

# set this so we can capture the resposne in our buffer
c.setopt(c.WRITEFUNCTION, buffer.write)

# uncomment to see the post sent
#c.setopt(c.VERBOSE, True)


# setup an indefinite loop that looks for the door to be opened / closed
while True:

	GPIO.wait_for_edge(23, GPIO.RISING)
	print("Doorbell Rang\n")
        execfile ("gmailimage.py")

	# in the door is opened, send the push request
	c.perform()
	

	# capture the response from the server
	body= buffer.getvalue()

	# print the response
	print(body)

	# reset the buffer
	buffer.truncate(0)
	buffer.seek(0)

	# print when the door in closed
	GPIO.wait_for_edge(23, GPIO.FALLING)
	print("Doorbell Free\n")

# cleanup
c.close()
GPIO.cleanup()


A circuit diagram will be very useful ,to get answers
To your problem. You are going through a period of
trial and error, when somebody might have the answered.
Do this before you frie your Pi, random noise can be
very hard to find
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

User avatar
r4049zt
Posts: 113
Joined: Sat Jul 21, 2012 1:36 pm
Contact: Website

Re: GPIO Electrical Interference

Sun Apr 26, 2015 6:17 pm

I second the proposal to add a 1k pullup, as that would help to make the 3V3 "open relay" state more strongly defined than the internal pullup got (thanks for sharing that line of code).

ghp
Posts: 1488
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO Electrical Interference

Sun Apr 26, 2015 6:41 pm

15 ft is 5m, which is VERY long for the high impedance pullup. Try placing the relay close to the raspberry (say 30 cm / 1 ft max).
If not possible, place the 1k Resistor to Vcc and add a low pass filter (0.1uf, 100 Ohm) to the input to eliminate EMI.

Rethink the code: change it from edge detect to level detect: if more than (0.3 sec ?) high, then trigger the internal action. This elminates the spike responsiveness.

Regards,
Gerhard

silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

Re: GPIO Electrical Interference

Mon Apr 27, 2015 2:19 am

So I plan on adding a 1k resistor and then shorten the wire from the relay NC to the raspberry. The 1k resistor goes from Pin1(3.3) to the GPIO Im using (23)? Then I need to turn off the internal resistor?

ghp
Posts: 1488
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO Electrical Interference

Mon Apr 27, 2015 6:08 am

The internal resistor should be enabled. It protects the gpio circuitry against damage just in case the wire is deconnected and the pin is touched by hand.
gpio_in.JPG
gpio_in.JPG (46.13 KiB) Viewed 6097 times
Regards,
Gerhard

silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

Re: GPIO Electrical Interference

Mon Apr 27, 2015 4:41 pm

i have a question onthe schematic as its not clear to me. it appears I take the 1k resistor from 3.3v pin to the gpio Im using as shown below, right?

http://www.element14.com/community/serv ... sistor.JPG
https://learn.adafruit.com/playing-soun ... ut-buttons

ghp
Posts: 1488
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO Electrical Interference

Mon Apr 27, 2015 5:50 pm

Hello,

to make it clear I added the relay contact to the schematic.
schema.jpg
schema.jpg (11.93 KiB) Viewed 6037 times
The circuit is using a pullup of 1k from GPIO to VCC=3.3V.
In addition to this, there is a low pass filter with 100 Ohm, 0.1uF. This prevents stray RF-input to be propagated into the GPIO. Edge frequency is some 15kHz (1/ ( 2 * pi * R * C)). Does not affect relais contact, as this is < 10Hz.
The 100 Ohm are choosen to be small enough to take GPIO input to 0.3V, 0..0.8V is low-input

Regards,
Gerhard

see also viewtopic.php?f=32&t=76368

silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

Re: GPIO Electrical Interference

Mon Apr 27, 2015 6:25 pm

thank you. I will test and post results!

silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

Re: GPIO Electrical Interference

Tue Apr 28, 2015 4:26 pm

Thanks for clearing up your drawing I thought those 1's were 4's . So nearing 24 hours. Had one false so far with 3v3 to GPIO. Tonight Ill add the 100 ohm RESISTOR (?) I believe, right? Prior in this thread we talked about capacitors.

ghp
Posts: 1488
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: GPIO Electrical Interference

Tue Apr 28, 2015 6:24 pm

One false, not that bad. How many false trigger did you have before ?
The 100 Ohm is only useful if you add the 0.1uF too. Both form this fancy low pass filter.

silvering
Posts: 12
Joined: Sun Apr 26, 2015 12:04 am

Re: GPIO Electrical Interference

Tue Apr 28, 2015 11:37 pm

it depends on the day how many false's I received. I ordered the capacitor ( already have 100r resistor) and will put that into play. I guess my other option is shorten the length from relay to raspberry or get shielded wire.

volt74
Posts: 1
Joined: Thu Jun 28, 2018 10:15 am

Re: GPIO Electrical Interference

Thu Jun 28, 2018 10:22 am

i had also phantom button press as i place my raspberry directly into home distribution cabinet. After two days testing of various python scripts I finally find this post. Using external pullup with all (1k, 0.1k, 100nF) it works like a charm. Big thanks :D .

User avatar
Z80 Refugee
Posts: 358
Joined: Sun Feb 09, 2014 1:53 pm

Re: GPIO Electrical Interference

Thu Jun 28, 2018 1:51 pm

ghp wrote:
Tue Apr 28, 2015 6:24 pm
The 100 Ohm is only useful if you add the 0.1uF too. Both form this fancy low pass filter.
Not entirely true, a series resistor also protects the GPIO pin in the event of it becoming enabled as an output.

Beginners Guide to Wiring Things to the GPIO
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

Return to “Python”