DaveLinger
Posts: 14
Joined: Wed Sep 11, 2013 1:13 pm

[Beginner] "Phantom" button presses on GPIO?

Sun Sep 04, 2016 5:09 pm

Hi everyone!

I am working on a project with my raspberry pi which involves a momentary pushbutton to start a script.

I am using python to monitor the GPIO pin I am using.

Basically I have a pushbutton wired up between GPIO 27 and ground. When the button is pressed, ground is fed to GPIO 27. It's very simple.

I don't have the exact code in front of me, but I set GPIO 27 up as an input and set it to use the built-in pullup resistor configuration.

Then I run a loop checking for the value of that pin being "false", which causes my script to run.

This works fine MOST of the time. But what is odd is occasionally, the script will start on its own, without anyone pressing the button. At my house, it's maybe one per day. Sometimes less. So I adjusted the code. I made it so that if the pin is false, it sleeps for a tenth of a second and checks again. If it's still false, it runs the script. If it's true, it logs a "phantom press". But I've never logged a phantom press.

When I take my project to the shop across town, the script runs itself within a matter of minutes of being plugged in, and typically cannot make it half an hour without the script running itself.

What gives? Is this caused by a bad 110v electrical feed to the power supply? Electrical interference? If I switch the GPIO configuration to "pull down" resistor and make my switch connect to 3v3 instead, would that help me at all?

Thanks for the help!

User avatar
joan
Posts: 14079
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: [Beginner] "Phantom" button presses on GPIO?

Sun Sep 04, 2016 9:18 pm

Try a stronger external pull-up resistor, perhaps 10k between GPIO27 and the 3V3 pin. The internal resistors are relatively weak, being about 50k.

DaveLinger
Posts: 14
Joined: Wed Sep 11, 2013 1:13 pm

Re: [Beginner] "Phantom" button presses on GPIO?

Sun Sep 04, 2016 10:11 pm

Thanks for the reply!

So just to clarify, you are suggesting that I connect both my switch and a 10k ohm resistor to GPIO 27, then connect the other side of the switch to ground, and the other side of the resistor to 3v3?

Thanks again for the help!

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

Re: [Beginner] "Phantom" button presses on GPIO?

Mon Sep 05, 2016 8:34 am

HI Dave.

If you connect it like this it should solve your problem, as joan was suggesting
the 1K resistor is just to protect the GPIO in the event you set the pin to output by accident.
Image
you can of course still use GPIO 27 the diagram is just one I had already.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

DaveLinger
Posts: 14
Joined: Wed Sep 11, 2013 1:13 pm

Re: [Beginner] "Phantom" button presses on GPIO?

Mon Sep 05, 2016 8:45 pm

Thanks pcmanbob!

I wired in a 10k ohm resistor between my gpio pin and the 3v3 pin and fired it back up. The switch still works, and I didn't immediately get a phantom button press. However, about an hour after plugging it in, I did.

I did not (yet) wire in the 1k resistor, but you said that's just to protect the board in case I misconfigure it, so for now I'm trying to keep it simple and isolate the issue.

Should I try replacing the 10k resistor with a ~5k and see if that helps? How much power can I run from the 3v3 pin to a GPIO input pin without causing an issue?

Also, for what it's worth, I did not change my code/configuration, so the pi is still doing its own pullup resistor as well. Should I remove that config line and try it?

Thanks again.

User avatar
joan
Posts: 14079
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: [Beginner] "Phantom" button presses on GPIO?

Mon Sep 05, 2016 9:11 pm

You can try 5k without problem. The internal pull-up won't make much of a difference but it's probably best to not use it.

You can draw up to 16 mA from a GPIO. As a guide the hard-wired I2C resistor pull-ups are 1k8 on the Pi.

If you still have problems post a photo.

DaveLinger
Posts: 14
Joined: Wed Sep 11, 2013 1:13 pm

Re: [Beginner] "Phantom" button presses on GPIO?

Mon Sep 05, 2016 9:32 pm

Thanks for the help joan!

I will try replacing the 10k with a 5k, and removing the software pullup from my code and report back (with a photo if the results are negative).

Related question - I also have a toggle switch on another GPIO pin, configured the same way. It hasn't given me any problems (since I'm only checking its state very rarely, rather than several times per second like I am with the pushbutton.) I suppose if this is the case, I should just leave it as is? Is there a problem with leaving a toggle switch with a pullup in the "on" position all the time? Essentially power can flow from the voltage source, through the resistor, to ground 24/7 (it appears). I assume at 50k ohms that's not an issue?

DaveLinger
Posts: 14
Joined: Wed Sep 11, 2013 1:13 pm

Re: [Beginner] "Phantom" button presses on GPIO?

Tue Sep 06, 2016 1:44 am

Okay, so I still got a phantom button press an hour or so after I started up the pi. This is with a 4.7k resistor pulling up my button's GPIO pin, and the internal pull-up disabled.

Here is a pic of my breakout board: http://i.imgur.com/UpBneFR.jpg

It's a little messy and the photo is not great. Sorry. There's a 40 pin box connector above what you see, with a ribbon cable going to the pi.

You can see the 4.7k resistor between 3v3 and G27, then follow the yellow jumper down to row 18 where a white wire runs to a leg of my button.

You can see a red jumper going from G23 to row 1, then out another white wire. This is the GPIO output which powers my button's LED.

The third white wire runs from my ground rail to the other leg of the button AND the LED.

G22 and G18 run my blue and green LEDs, respectively. The other legs of those are soldered into the ground rail.

Finally G17 is my toggle switch, with the other leg going to ground. Behind the toggle switch and not visible in the photo is a jumper from a GPIO ground pin to my ground rail.

Hopefully this is enough info for someone to figure out what I am doing wrong here!

Thanks!

AussieSusan
Posts: 30
Joined: Tue Aug 28, 2012 10:48 pm

Re: [Beginner] "Phantom" button presses on GPIO?

Tue Sep 06, 2016 3:18 am

Wild guess but I suspect that those wires are quite long and you are picking up EMI (I can see a car in the background - garages and cars can be a very electrically noisy environment).
Keep the wires short.
Also why not just run 2 sires to the button and have the pull-up resistor on the board where it can be tied to the Vcc directly. This might save noise being picked up by the additional wire.
If this is the cause then you can shield the wires to stop the stray pulses being picked up.
Susan

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

Re: [Beginner] "Phantom" button presses on GPIO?

Tue Sep 06, 2016 9:14 am

Hi Dave.

Can we have a bit more info about the switch you are using and were it is located ? compared to your pcb and the PI

also does the switch operate often as you said as you said you were checking it several times a second.

I am just try to get a feel for your set up as the picture does not show the switch that is causing the problem.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

DaveLinger
Posts: 14
Joined: Wed Sep 11, 2013 1:13 pm

Re: [Beginner] "Phantom" button presses on GPIO?

Tue Sep 06, 2016 2:51 pm

AussieSusan:

The third wire going to the switch is to power an LED inside the switch that operates independently of the button presses, so I need three wires. The resistor to the VSS is already directly on the board.

Here is a pic of my project: http://i.imgur.com/aV1n6Nf.jpg

It's a little hard to see, but this is a 14x10x22 "box", with a 5x5" panel coming out of the front with my button.

Short description is it's a photo booth. Big red button is lit up, display shows a live mirrored preview from the webcam. Press the button and the button LED goes off, and the green LED comes on and blinks faster and faster until it stops and the SLR captures a picture. Repeats 4x, then the blue LED comes on to indicate that the photos are being printed. After the printer reports that it's idle, the loop ends and the button lights up again, ready for another press.

I am using an HDMI to VGA adapter for my display, and the webcam, SLR, and printer are connected via USB.

The white wires have a little bit of slack I could take out, and they are definitely heavier gauge than absolutely necessary - I just didn't have any smaller gauge wire around.

You can see the enclosure is made from welded steel rod, and it's sitting on a chest freezer in a garage around cars. Not an ideal location from an EMI perspective, I know, but I don't want to have to worry about the amount of EMI in any environment I want to use this in. This will ultimately be enclosed. The toggle switch is used to turn printing on and off. So if I want to troubleshoot everything without using up paper and ink, I turn the toggle off.

As for how often the switch operates, in actual use at an event (my upcoming wedding reception in 3 weeks), I expect it to be idle for no more than maybe 20 minutes tops between "uses".

Thanks again to everyone for your help! Should I try using Ethernet cable instead of these big wires? My understanding is that each wire pair in an Ethernet cable is shielded - and the wires are much smaller gauge.

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

Re: [Beginner] "Phantom" button presses on GPIO?

Tue Sep 06, 2016 4:42 pm

Hi Dave.

Thanks for the photo always helps to see the project over all to get a feel for size and cable runs etc.

first off don't worry about cable size to big is always better than to small, I don't think going to Ethernet cable will help as unless you buy screened cable most Ethernet cable is not screened and the screening is around all the wires together not around each wire anyway.

you could try routing your cable away from the back of the screen and those power supplies as much as possible to see if that helps.

normally you get phantom highs on switch cables when you get stray electrical interference, but as yours is normally high and is pulled low by the switch this should not apply.
so first off if you have a multi meter just measure the voltage between pi GND and each end of the resistor in the switch circuit with everything running of course. it should be 3.3V at both ends with the switch not pressed, this would rule out any high resistance or bad connections.
also as a test change your delay for phantom button press from tenth of a second to say half a second and then if you get any phantom presses in your log or in fact if you get any phantom running of your program.

I am afraid intermittent problems like this can be a bugger to fix as they happen so infrequently.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

DaveLinger
Posts: 14
Joined: Wed Sep 11, 2013 1:13 pm

Re: [Beginner] "Phantom" button presses on GPIO?

Wed Sep 07, 2016 12:23 am

bob,

Thanks again for your continuing help!

So before I read your reply this morning, I went ahead and replaced my wires with Ethernet cable. I used one twisted pair for my switch and ground, plus one other wire from a second pair for my LED power. I left the other 5 wires in the cable, as well as the outer sheath. As you suggested, there was no actual shielding.

The monitor and/or power supplies are not causing an issue (in my opinion), because this was happening to me when the only components in the picture were the pi and breakout board - no monitor and no power supplies except for the pi's.

The GOOD news, however, is that the problem may be fixed. I've left it for several hours with no phantom button presses. I'm going to leave it overnight tonight, and if it hasn't activated itself by tomorrow morning, I'll consider it "fixed".

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

Re: [Beginner] "Phantom" button presses on GPIO?

Wed Sep 07, 2016 10:24 am

Hi Dave

It may be that the change of cable has fixed the problem or may be you had a bad solder joint which was fixed when you changed the cable, either way Lets hope this has fixed it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

IanS
Posts: 231
Joined: Wed Jun 20, 2012 2:51 pm
Location: Southampton, England

Re: [Beginner] "Phantom" button presses on GPIO?

Wed Sep 07, 2016 10:28 am

The twists in the ethernet cabling are probably the 'fix'. The EMI going through alternate twists cancel out, so the overall effect is normally close to zero. There look to be enough devices (fan, press, etc) that EMI is a likely cause.

p.s. Very nicely maintain woodwork on the car. An early Ford? The beams are in the wrong place for a Morris Minor, which was the most common UK wood-framed car.

danny6869
Posts: 8
Joined: Sat Sep 13, 2014 5:30 pm

Re: [Beginner] "Phantom" button presses on GPIO?

Thu Jan 03, 2019 8:17 pm

So, did this actually fix your problem completely? (using the ethernet cable/wire?)

I am having a very similar problem. I have GPIO5 setup as a power button that starts up and shuts down my raspberry pi in an arcade machine. It's a large maching with an old-style arcade monitor inside. When I leave the raspberry pi with power after a proper "shutdown", it will startup on its own (sometime within 24 hours). Very frustrating for my setup.

DirkS
Posts: 9873
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: [Beginner] "Phantom" button presses on GPIO?

Thu Jan 03, 2019 8:29 pm

danny6869 wrote:
Thu Jan 03, 2019 8:17 pm
I have GPIO5 setup as a power button that starts up and shuts down my raspberry pi
How did you make that work? AFAIK you cannot start the Pi with GPIO5 (https://pinout.xyz/pinout/pin29_gpio5)

User avatar
Imperf3kt
Posts: 2547
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: [Beginner] "Phantom" button presses on GPIO?

Thu Jan 03, 2019 8:32 pm

DirkS wrote:
Thu Jan 03, 2019 8:29 pm
danny6869 wrote:
Thu Jan 03, 2019 8:17 pm
I have GPIO5 setup as a power button that starts up and shuts down my raspberry pi
How did you make that work? AFAIK you cannot start the Pi with GPIO5 (https://pinout.xyz/pinout/pin29_gpio5)
There is a dt overlay you can add which allows the Pi to start up and shut down by shorting physical pin 5 to ground (pin can be changed)
I assume this is what was meant.
55:55:44:44:4C
52:4C:52:42:41

Return to “Automation, sensing and robotics”