Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

GPIO electrical interference false positive on toggle button

Thu Aug 22, 2013 5:47 pm

Hi,

I've got my RPI rigged up with a toggle button connected to GPIO. I'm using the callback function to do some stuff when the button is pushed.

Which is all working just fine except when I run my espresso grinder which is plugged into the same outlet, this triggers the GPIO falling condition somehow.

I was originally using the GPIO internal pull down resistor, but since that didn't seem to fix this, I installed a 10k resistor in the circuit. I'm still having the same problem.

Any ideas how I could isolate this or protect? The falling condition is triggered 100% of the time that I use my espresso grinder.

Thanks for any ideas..

PiGraham
Posts: 4733
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: GPIO electrical interference false positive on toggle bu

Thu Aug 22, 2013 7:00 pm

If noise is generating a FALLING edge event maybe you need a pull-UP, to stop it falling.

You put pull-xx resistor to one supply rail and the switch to to other (through a series resistor e.g. 330R).
If the switch is open the pull-up holds the input high. When the switch is closed it holds the input low.

If there is a condition where the input is not held high or low it will be susceptible to interference.

What is you switch circuit?

Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

Re: GPIO electrical interference false positive on toggle bu

Thu Aug 22, 2013 7:06 pm

My circuit looks like the second diagram in this image:

Image

http://elinux.org/images/8/85/EGHS-PullUpDownSwitch.jpg

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 6277
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: GPIO electrical interference false positive on toggle bu

Thu Aug 22, 2013 7:20 pm

Adding a small capacitor (~1nF) across the switch contacts may help, although it might be better positioned close to the GPIO end, depending upon your cable length.
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

Re: GPIO electrical interference false positive on toggle bu

Thu Aug 22, 2013 7:42 pm

Regarding cable lengths: from the switch to breadboard is about 6 inches. Then GPIO ribbon cable is 1 foot. I could probably simply enough do the capacitor at the switch or the breadboard, if that's better.

Is it going to be obvious which way to connect the capacitor? Basically short circuiting the switch + and -, with + end of capacitor on the 3.3v?

Thanks!

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 6277
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: GPIO electrical interference false positive on toggle bu

Thu Aug 22, 2013 8:03 pm

Mr.DadaGuy wrote:Regarding cable lengths: from the switch to breadboard is about 6 inches. Then GPIO ribbon cable is 1 foot. I could probably simply enough do the capacitor at the switch or the breadboard, if that's better.

Is it going to be obvious which way to connect the capacitor? Basically short circuiting the switch + and -, with + end of capacitor on the 3.3v?
At the breadboard is probably easiest to try. In parallel with (short-circuiting) the switch - this will also "de-bounce" it. 1nF ceramic capacitors are "non-polarised" so there won't be "+ or -" ends.
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

PiGraham
Posts: 4733
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: GPIO electrical interference false positive on toggle bu

Thu Aug 22, 2013 8:18 pm

Of course your grinder could be really, really noisy. Does it interfere with audio equipment nearby?

The motor will have a suppressor across the terminals. It may have failed.

For the Pi, keep your wires short. Avoid open loops by running wires front point to point in pairs (twist them together).
Have one ground point. Decouple supply rails with capacitors. Filter the input pin with a small capacitor to ground.
The power supply wires could couple interference into the Pi. You could wind the Pi end around a ferrite (you see that on many laptop power supplies and some USB leads.

Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

Re: GPIO electrical interference false positive on toggle bu

Fri Aug 23, 2013 12:12 am

Thanks for the great ideas -- I've tried a couple but unfortunately no love.

I found some nice little ferrite cores:
http://www.radioshack.com/product/index ... cale=en_US
and wrapped around the USB power on the way in to RPi. No effect.

I picked up a couple of these capacitors
http://www.radioshack.com/product/index ... Id=2062362
and wired one from the GPIO to ground. Also no effect; the grinder still triggers the GPIO.

Should I try different capacitors?

What does this mean: "Decouple supply rails with capacitors"?

Thanks!!

User avatar
jbeale
Posts: 3878
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: GPIO electrical interference false positive on toggle bu

Fri Aug 23, 2013 1:01 am

If you are using the 2nd circuit, where a 10k resistor connects your GPIO pin to ground, that means a current of 150 uA (micro-amps) will be enough to raise the GPIO pin to the logic threshold (assuming it is 1.5 V). A capacitor across the resistor should help, you could try a larger value like 0.01 uF, and you can also try a smaller resistor, like 1k, that would mean you need 1.5 mA to reach the logic threshold. The capacitor will not help so much if it is on a breadboard and there is a long cable connecting the breadboard to the RPi GPIO pins, that could allow the noise pickup to happen in the cable between the capacitor and the RPi. If you are using just one GPIO pin, Ground, and +Vcc you might try a shielded cable where ground is the shield, and +V and signal are on wires inside the shield.

This is assuming the switch is normally open, so the GPIO pin rests normally closed, and enough AC current is coupled from the grinder is causing the GPIO to momentarily go high and low again.

If instead your switch is normally closed, and the noise is forcing it low despite < 1 ohm through the switch to the power supply rail, that would be some incredible noise that might also be close to resetting the CPU.

Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

Re: GPIO electrical interference false positive on toggle bu

Fri Aug 23, 2013 4:40 pm

The switch is normally open.

So based on what I'm reading here, I'm thinking I should reduce the length of cable between Pi and breadboard. So I'm going to put them pretty much on top of one another.

Also I'm going to switch to some shielded cable.

Can someone please suggest some good shielded cable that would be appropriate to connect the breadboard to the toggle buttons? I have some CL3R but it seems too thick gauge, and I don't see the shielding like I do in 75ohm cable. I've also got plenty of ethernet, VGA, and old USB cables. I'm also happy to buy whatever is the right thing.

Thanks!!

User avatar
jbeale
Posts: 3878
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: GPIO electrical interference false positive on toggle bu

Fri Aug 23, 2013 5:15 pm

If you want to sacrifice a USB cable for this project, that ought to work fine. That has four conductors inside the shield foil/braid and generally none of those conductors are actually connected to the shield, so in effect you have five separate conductors. The USB cable wires are probably stranded so it can be tricky to use directly into a solderless breadboard; you may want to solder on a short solid jumper wire for connection to the breadboard. (Or just dispense with the breadboard alltogether, for such a simple circuit.)

PiGraham
Posts: 4733
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: GPIO electrical interference false positive on toggle bu

Fri Aug 23, 2013 5:40 pm

I'd try smaller value pull-down and twisting the witch wires together before bothering with shielded cable.

Not much point screen wires to the switch and leaving a ribbon cable exposed.

I suspect the motor in the grinder is arcing excessively at the brushes and any suppressor in there is dud. I'd try the grinder near RF and audio equipment to see if makes lots of noise.
I'd try the Pi circuit with other motor appliances to see if it's unusually sensitive, or being hammered by excessive EMI.

If the grinder is at fault fit new brushes and suppressor.

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: GPIO electrical interference false positive on toggle bu

Fri Aug 23, 2013 6:09 pm

Mr.DadaGuy wrote:I've got my RPI rigged up with a toggle button connected to GPIO. I'm using the callback function to do some stuff when the button is pushed.
Please describe how you detect the pressed button! Do you use interrupts or do you detect the level via polling every ... milliseconds?

MfG, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

Re: GPIO electrical interference false positive on toggle bu

Fri Aug 23, 2013 8:54 pm

Code: Select all

GPIO.add_event_detect(gpio_btn_pump_sig, GPIO.HIGH, callback=pumpButton, bouncetime=1000)       # was RISING
Also, with reference to the interference by the grinder -- now it also is triggered by the espresso machine, and a small handheld smoothie blender, even from another socket.

So, the plan is to remove the ribbon cable and use the Adafruit Pi plate mounted directly to GPIO. Then I will use some chopped up USB cables (leaving the ferrite intact) as the wiring from the switches back to the Pi Plate.

What ya'll think? Really appreciate all the tips..

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: GPIO electrical interference false positive on toggle bu

Sat Aug 24, 2013 8:14 am

I've asked because I had a nearly identical problem: I would detect a level-changing on a GPIO from HIGH to LOW. The first idea was to use an interrupt with falling edge. (The fine thing with interupts is, to use nearly zero CPU-load.) But I had also the same "false positives" as you described. And none of the wires on GPIO was longer than 10cm. And I've also blocked with capcitors (very near to RasPi). But no success at all ... :(

But then I decided to poll every 500ms. No problems anymore ... :)
I'm using a binary (programmed in C), which detects this, so I haven't much CPU-load (about 1 or 2 seconds per hour).

But you code looks for me like python, java or what ever ... ?

The problem can be the use of interupts, a shortest "bad pulse" can trigger a "false event", and only the first edge is needed to create it; so you need a very, very good blocking of such pulses. I'm not sure if this is possible in practice?

Greetings, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

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

Re: GPIO electrical interference false positive on toggle bu

Sat Aug 24, 2013 2:43 pm

First things first, Kill the interference before it get to the PI.

!. Don't grind coffee while using the Pi

2. Plug the grinder in to a filtered power socket.

3, As your power adapter for the Pi got a ceramic filter on the out put
low voltage to the Pi, if not you can buy one to clip on.

4. It's best to kill the Devil before he waves is SPIKED FORK ( noise )
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

Re: GPIO electrical interference false positive on toggle bu

Sat Aug 24, 2013 4:30 pm

Hi FM81 - you gave me a great idea... based on the notion that the power surge would probably be very brief, and a person contacting a toggle pushbutton would take relatively much more time, I've done the following at the beginning of my callback function (this is python code):

Code: Select all

    
time.sleep(0.1)         # need to filter out the false positive of some power fluctuation
    if GPIO.input(gpio_btn_pump_sig) != GPIO.HIGH:
        return
The event handler looks like this:

Code: Select all

  
  GPIO.add_event_detect(gpio_btn_pump_sig, GPIO.HIGH, callback=pumpButton, bouncetime=1000)       # was RISING
So, if the event handler thinks GPIO is HIGH, we'll wait 1/10 of a second to see if it is still HIGH. If so, it's probably a user rather than a power spike.

Initial test is encouraging; it seemed to work! I might tweak that timing a bit... I think I still have to do some mods because my LCD goes wonky with the interference. So I'm going to move to I2C interface to see if that helps.

Really appreciate all the ideas.

PiGraham
Posts: 4733
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: GPIO electrical interference false positive on toggle bu

Sat Aug 24, 2013 5:55 pm

Mr.DadaGuy wrote:Initial test is encouraging; it seemed to work! I might tweak that timing a bit... I think I still have to do some mods because my LCD goes wonky with the interference.
Good to hear you are making some progress, but it does seem your appliances are extremely noisy! What sort of LCD are you using? Maybe the noise is on Pi output.

What you are doing will reduce the chances of a spurious operation, but the possibility is still there as long as the high level of noise is present. Does it matter if a false button event happens very occasionally?

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

Re: GPIO electrical interference false positive on toggle bu

Sat Aug 24, 2013 7:54 pm

Mr.DadaGuy wrote:Hi,

I've got my RPI rigged up with a toggle button connected to GPIO. I'm using the callback function to do some stuff when the button is pushed.

Which is all working just fine except when I run my espresso grinder which is plugged into the same outlet, this triggers the GPIO falling condition somehow.

I was originally using the GPIO internal pull down resistor, but since that didn't seem to fix this, I installed a 10k resistor in the circuit. I'm still having the same problem.

Any ideas how I could isolate this or protect? The falling condition is triggered 100% of the time that I use my espresso grinder.

Thanks for any ideas..
[ What is the current out put of your PI Power adapter if low.The grinder start up load can cause power to dip below power adapter out put]
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

Re: GPIO electrical interference false positive on toggle bu

Sat Aug 24, 2013 8:26 pm

Not sure if my appliances are any more noisy than usual, my design might be to blame. I have the ribbon cable running close to the AC line. Maybe even touching. I'm thinking this is creating some interference. So I'm going to rewire some things based on some things I've learned here.

My LCD is the standard 16x2, which is wired up according to this; http://learn.adafruit.com/drive-a-16x2- ... i/overview. I just purchased an LCD "backpack" i2c, so I've got a lot less wires running jumbling things up and hopefully less places for interference to creep in.

I do have a 2Amp power supply, and I have a ferrite around the pi end of the usb cable. I can put another around the power supply side of the cable if y'all think it will help.

Thanks

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

Re: GPIO electrical interference false positive on toggle bu

Sat Aug 24, 2013 10:00 pm

Mr.DadaGuy wrote:Hi,

I've got my RPI rigged up with a toggle button connected to GPIO. I'm using the callback function to do some stuff when the button is pushed.

Which is all working just fine except when I run my espresso grinder which is plugged into the same outlet, this triggers the GPIO falling condition somehow.

I was originally using the GPIO internal pull down resistor, but since that didn't seem to fix this, I installed a 10k resistor in the circuit. I'm still having the same problem.

Any ideas how I could isolate this or protect? The falling condition is triggered 100% of the time that I use my espresso grinder.

Thanks for any ideas..
[ By the way do you live in the UK, using standard 13amp switch sockets ]
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: GPIO electrical interference false positive on toggle bu

Sun Aug 25, 2013 8:54 am

Mr.DadaGuy wrote:I have the ribbon cable running close to the AC line. Maybe even touching. I'm thinking this is creating some interference. So I'm going to rewire some things based on some things I've learned here.
That should be the main reason for all your problems! It doesn't matter if these two (isolated) cables touch to each other or if there is a gap of some millimeters or centimeters between ...
Capacitive or inductive coupling will there exist in every case, and this is enough to create such "false positives"!
In theory shielding of the ribbon cable should help; bring the wires of the ribbon-cables to lower impedance should help too. But in practice it is the best way, to take the ribbon-cable far away enough from AC-line.

MfG, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

web
Posts: 5
Joined: Thu Mar 20, 2014 10:25 pm

Re: GPIO electrical interference false positive on toggle bu

Thu Mar 20, 2014 10:35 pm

Hello to all.
I've read this discussion and i have the same problems as the Mr.DadaGuy, i have the second scheme and 15 mt of wire.
I use the gpio 10, my project is to read the revolution of a gas meter.
I put a simple reed switch normally open near the magnet of gas meter, and every revolution makes a pulse, simple.
I have a simple script python, but i don't understand where i put the function python described into my script.
Can you help me with my script please?

#!/usr/bin/env python
import time
import datetime
import os.path
import pycurl
import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.add_event_detect(10, GPIO.RISING, bouncetime=5000)

def GetGas(arg):
print "gas 0.01"
gastotale=0
if os.path.isfile('/var/www/myscripts/gas/gastotale.txt'):
file = open("/var/www/myscripts/gas/gastotale.txt","r")
gastotale = float(file.read())
file.close()
gastotale = gastotale+0.01

file = open("/var/www/myscripts/gas/gastotale.txt","w")
file.write(str(gastotale))
file.close()

now = datetime.datetime.now()
fileday = '/var/www/myscripts/gas/'+now.strftime("%Y-%m-%d")+'.txt'
gasday = 0

if os.path.isfile(fileday):
file = open(fileday,"r")
gasday = float(file.read())
file.close()
gasday = gasday+0.01

file = open(fileday,"w")
file.write(str(gasday))
file.close()

oem = pycurl.Curl()
oem.setopt(oem.URL,'http://emoncms.org/input/post.jsonnode= ... apikey=xxx')
oem.perform()

GPIO.add_event_callback(10, GetGas, bouncetime=5000)

while True:
time.sleep(300)

Mr.DadaGuy
Posts: 23
Joined: Thu Aug 22, 2013 5:42 pm

Re: GPIO electrical interference false positive on toggle bu

Thu Mar 20, 2014 11:06 pm

Hi,

Couple of tips, hope this helps...

First, you might want to have a look at this:
http://stackoverflow.com/questions/4191 ... me-main-do

Second, if you surround your code with the "Code" markup on this forum it will be easier for us to read.

Third, basically what I figured out is that the electrical noise event that was happening was extremely brief, much quicker than someone pushing a button and letting go. So the first thing my callback function would do is sleep for a very short time, and then check to see if the GPIO is still HIGH.

This has completely solved my problem.

Code: Select all

def my_event_callback_function():
    print('Callback function called!')
    time.sleep(0.01)         # need to filter out the false positive of some power fluctuation
        if GPIO.input(10) != GPIO.HIGH:
            print('quitting event handler because this was probably a false positive')
            return       
        # otherwise, do your button handling code here :) 
        print('Button was actually pressed, or GPIO event actually happened')

GPIO.add_event_callback(10, my_event_callback_function)


web
Posts: 5
Joined: Thu Mar 20, 2014 10:25 pm

Re: GPIO electrical interference false positive on toggle bu

Fri Mar 21, 2014 7:48 am

Hi Mr.DadaGuy, sorry but i don't had seen the code button :)
Ok, now i'll try to put your parts of code into my script and test it, i hope to solve this annoying problem.. in the meantime this is my script, thank u so much!!

Code: Select all


#!/usr/bin/env python
import time
import datetime
import os.path
import pycurl
import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.add_event_detect(10, GPIO.RISING, bouncetime=5000)

def GetGas(arg):

        gastotale=0
        if os.path.isfile('/var/www/myscripts/gas/gastotale.txt'):
                file = open("/var/www/myscripts/gas/gastotale.txt","r")
                gastotale = float(file.read())
                file.close()
        gastotale = gastotale+0.01

        file = open("/var/www/myscripts/gas/gastotale.txt","w")
        file.write(str(gastotale))
        file.close()

        now = datetime.datetime.now()
        fileday = '/var/www/myscripts/gas/'+now.strftime("%Y-%m-%d")+'.txt'
        gasday = 0

        if os.path.isfile(fileday):
                file = open(fileday,"r")
                gasday = float(file.read())
                file.close()
        gasday = gasday+0.01

        file = open(fileday,"w")
        file.write(str(gasday))
        file.close()

        oem = pycurl.Curl()
        oem.setopt(oem.URL, 'http://emoncms.org/input/post.json?node=0&csv=0,'+str(gasday)+',0,0,'+str(gastotale)+'&apikey=3dddcb9e5b8c35ecf2d4cfceedfb6ccf')
        oem.perform()

GPIO.add_event_callback(10, GetGas, bouncetime=5000)

laststate = 0
while True:
    if(GPIO.input(10) == 1 ):
       if (laststate == 0):
           gastotale == gastotale+0.01
           laststate=1
    else:
       laststate=0
    time.sleep(30)

Return to “Troubleshooting”