1

I am building a simple application with a switch on the rasperry pi 3 b+. I have connected GPIO pin 18/32 through a 56k resistor to 3.3v (tried pin 1 and 17) to have it pulled up. The switch closes the GPIO to GND when pressed. This works fine so far.

I use it as follows:

GPIO.setup(23, GPIO.IN)
GPIO.add_event_detect(23, GPIO.FALLING, callback=btn, bouncetime=200)

However, when I trigger the light switch of the ROOM, it generates random inputs and calls the btn() callback.

I am not sure what is wrong. Did I realize the pull down correctly? Is it the wrong capacity?

2
  • I am not sure what is wrong - neither are we, because we don't know what you DID. What is pin 18/32? What is connected to what?
    – Milliways
    Dec 28, 2018 at 22:42
  • Are the room lights LED or incandescent? ... Some LED lights may create RF interference. In any case, any wire is an antenna... so, proper grounding and short wires are always a good idea... along with capacitors connected to a ground plane which short RF noise to ground rather than the circuit. Dec 29, 2018 at 1:51

1 Answer 1

1

You might try changing your bouncetime variable. I doubt that it is electrical interference -- you stated that it only produces the unwanted behavior when you use the switch and the bouncetime variable is used to 'debounce' the switch, so the time you are using is not sufficient. Increase it until the behavior goes away.

3
  • 1
    The room switch that causes the random inputs is actually the switch in the wall that turns on/off the light in the room. It is not at all connected to the pi. But in the meantime I was thinking about increasing the bouncetime to a reasonable value: I think I can expect the user to press the button longer than the interference from the room light switch may cause
    – ddd
    Dec 29, 2018 at 18:43
  • The bouncetime variable has nothing to do with how long the switch is pressed -- it is related to the noise that is present on the GPIO bus --specifically pin 23 in your case. There is a difference.
    – jinzai
    Dec 29, 2018 at 23:15
  • The switch is NOT connected to the Pi? What :/ Anyway a 1 second debounce should be fine. Unles you are like me and like turning the lights on and off quickly to make psuedo disco for your children then that will suck. The GPIO are very sensitive to rising and falling edges and wall switches before they are engaged can cause allot of edges due to mechanical/ electrical noise. typically 100~200ms should be fine.. in most cases
    – Piotr Kula
    Feb 26, 2019 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.