bananaboat
Posts: 32
Joined: Sun Oct 21, 2018 10:11 am

Simple switch with extended cable

Wed Mar 25, 2020 3:14 pm

Hi,

Using this code...:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(18)
    if input_state == False:
        print('Button Pressed')
        time.sleep(0.2)
...works until I add an extended cord to it (like 2-3 meters). When I do it keeps printing "Button Pressed" although the button has not been pressed. It tried different kind of cables but each one of them kept having the same problem.

I followed these guidelines using a raspberry model A
connecting the cable to GPIO 18 and 9 for 'ground'.
Description: Raspbian GNU/Linux 9.4 (stretch)
Release: 9.4
When i disconnect the extended cable, this problem does not occur and all works as intended.

Whats the deal with this?

Cheerz!

User avatar
davidcoton
Posts: 4909
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Simple switch with extended cable

Wed Mar 25, 2020 3:26 pm

You may be picking up RF interference. Try putting a small capacitor between GPIO input and ground, at the Pi end. 0μ1F should do.
Signature retired

bananaboat
Posts: 32
Joined: Sun Oct 21, 2018 10:11 am

Re: Simple switch with extended cable

Thu Mar 26, 2020 9:35 am

davidcoton wrote:
Wed Mar 25, 2020 3:26 pm
You may be picking up RF interference. Try putting a small capacitor between GPIO input and ground, at the Pi end. 0μ1F should do.
0μ1F?

The smallest i could find is 0,1μF 50V will that be okay?

User avatar
Burngate
Posts: 6290
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Simple switch with extended cable

Thu Mar 26, 2020 9:48 am

0μ1F = 0,1μF = 0.1μF = 0u1F

Half the world uses a fullstop as a decimal separator, while the other half uses a comma. The rest of the world can't find either of those on their keyboard, so they put the multiplier (p, n, μ, m, k, M, G, T, ...) in its place. And many of us can't find the μ so we use u instead.

So that should be fine.

bananaboat
Posts: 32
Joined: Sun Oct 21, 2018 10:11 am

Re: Simple switch with extended cable

Thu Mar 26, 2020 10:51 am

Burngate wrote:
Thu Mar 26, 2020 9:48 am
0μ1F = 0,1μF = 0.1μF = 0u1F

Half the world uses a fullstop as a decimal separator, while the other half uses a comma. The rest of the world can't find either of those on their keyboard, so they put the multiplier (p, n, μ, m, k, M, G, T, ...) in its place. And many of us can't find the μ so we use u instead.

So that should be fine.
Got it!

@davidcoton and @burngate thanks!

bananaboat
Posts: 32
Joined: Sun Oct 21, 2018 10:11 am

Re: Simple switch with extended cable

Fri Apr 10, 2020 11:23 am

davidcoton wrote:
Wed Mar 25, 2020 3:26 pm
You may be picking up RF interference. Try putting a small capacitor between GPIO input and ground, at the Pi end. 0μ1F should do.
1 last followup question...

Each capacitor has a positive and negative side. What i did is to connect the positive side of the capacitor to the GPIO pin and circle it back to connect the (2nd capacitor) negative side towards ground. Is that the correct way?

And would it be a bad thing leaving the capacitor to ground out completely?

User avatar
neilgl
Posts: 2111
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Simple switch with extended cable

Fri Apr 10, 2020 4:40 pm

Any reason for using 2 capacitors not just one?

User avatar
Burngate
Posts: 6290
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Simple switch with extended cable

Fri Apr 10, 2020 6:10 pm

bananaboat wrote:
Fri Apr 10, 2020 11:23 am
Each capacitor has a positive and negative side. What i did is to connect the positive side of the capacitor to the GPIO pin and circle it back to connect the (2nd capacitor) negative side towards ground. Is that the correct way?
I'm not sure I understand quite what you're describing, here. But maybe a simple description of what a capacitor is, and how it works, would help.

At its simplest it's just a couple of conductive plates separated by an insulator. If something pulls electrons off one plate, that leaves a positive charge on it that attracts electrons onto the other plate. Moving the electrons round takes energy, that can be recovered when they move back to where they started from.

The capacitance (in Farads, a measure of how much effort is required to move how many electrons) depends on the area of the plates and how far apart they are, and you can get them very close if you can coat one of them with an oxide layer to act as the insulator.

But that requires you to keep one plate always positive compared to the other - if it ever goes negative, the oxide decomposes, and you no longer have an insulator. Hence the + and - ends.

So what you want to do is connect the positive end to the GPIO - which is never negative with respect to ground - and the negative end to ground - which is always the least positive bit of your circuit.

Then any RF picked up has to work hard to move the electrons around, and so the GPIO will be less sensitive to it.

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

Re: Simple switch with extended cable

Fri Apr 10, 2020 6:36 pm

There might be more problems besides too many caps. Mentioning + end hints that these are not 0.1uf ceramics but maybe thousands of times bigger which may not work for RF noise suppression. Sometimes electrolytics act more like inductors than caps depending on frequency.

Also worth mentioning really troublesome setups may need a low value resistor between wire and cap for a fully functional RC filter. Since OP wants 2 caps and is asking about leaving ground pin off looks like a diagram might help:

Code: Select all

                   100r
  ,-------------resistor---------,------gpio
switch         (optional?)       = .1u
  '------------------------------'---ground

User avatar
neilgl
Posts: 2111
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Simple switch with extended cable

Fri Apr 10, 2020 7:05 pm

I would also change the python code to,

Code: Select all

GPIO.add_event_detect(button_pin, GPIO.FALLING, callback=button_handler, bouncetime=300)
with a bouncetime set to (say) 300ms

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

Re: Simple switch with extended cable

Fri Apr 10, 2020 9:31 pm

introducing a schmitt trigger circuit between the input wire and the GPIO terminal is also a good way to debounce.
"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!"

bananaboat
Posts: 32
Joined: Sun Oct 21, 2018 10:11 am

Re: Simple switch with extended cable

Sun Apr 12, 2020 9:49 am

neilgl wrote:
Fri Apr 10, 2020 7:05 pm
I would also change the python code to,

Code: Select all

GPIO.add_event_detect(button_pin, GPIO.FALLING, callback=button_handler, bouncetime=300)
with a bouncetime set to (say) 300ms
I am a noob when it comes to python programming.

What i got is this (error free) but no matter if i _detect to FALLING or RISING the scripts seems to be looping continually:

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#LED1:
GPIO.setup(40,GPIO.OUT)
#LED2:
GPIO.setup(36,GPIO.OUT)
print ('standing by...')

def buttonEventHandler (pin):
    print('Button Pressed & LED on')
    #LED1 ON:
    GPIO.output(40,GPIO.HIGH)
    #LED2 ON:
    GPIO.output(36,GPIO.HIGH)
    #cam3 ##seconds off:
    execfile("/home/pi/cam_delay.py")
    time.sleep(5)
    print('Delay finished & LEDz off')
    GPIO.output(40,GPIO.LOW)
    GPIO.output(36,GPIO.LOW)

GPIO.setmode(GPIO.BOARD)
#GPIO.setup(18, GPIO.IN)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(18, GPIO.RISING, callback=buttonEventHandler, bouncetime=300)

GPIO.cleanup()

Return to “Troubleshooting”