I'm working on making a Doorbell powered by a raspberry pi,

I connected the existing wires from the button to the raspberry pi at GPIO pin 18 and the Ground Pin. I'm using the following while true loop to detect the button press.

GPIO.setup(Relay_Ch1,GPIO.OUT) #Relay Ch1
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Detect Bell Press
while True:
    input_state = GPIO.input(18)
    if input_state == False:

        print('Bell Pressed')

        # Start Buzzer
        GPIO.output(Relay_Ch1, GPIO.LOW)
        print('Relay Opened')

        # Play Sound trough Sonos HTTP API
        GET(sonos, async=True, resp=True)
        print('Sonos API Send')

        # POST with form-encoded data
        POST(push, params=payload, async=True, resp=True) 
        print('Push Request send')


        # Stop Buzzer
        GPIO.output(Relay_Ch1, GPIO.HIGH)


This is working great, except whenever I put a load on the power circuit where the raspberry pi power adapter is plugged in, it will for some reason trigger the script.

Would it be better to connect one of the wires to the 3.3v and the other to a GPIO pin, and then check if it's high, instead of pulling it to ground?


First, it sounds like you might be overloading your supply power circuit. (I assume you are using a wall outlet.) If that is the case, you need to address the root cause of the power supply fluctuations.

Second, what is the quality of the power adapter you are using for the RPi? Is it rated at 2 amps or more? Also, what else do you have plugged into the RPi that is drawing current?

Third, instead of changing the pins around, I would add a larger pull up resistor to reduce the current drawn by the button.

Finally, depending on the length of the transient power fluctuations induced by the external load, you could try modifying the if statement to require a period of time for the button signal to be low. The theory is that a human pressing the button will hold the signal low longer than the signal generated by the external load. This solution ignores the safety issue of resolving the source of the transient power fluctuations.

  • It's on a dedicated 20Amp 230v circuit, with only power adapters for my home network cabinet, which pulls a max of 600 watts. So that should not be the issue. I'm using a 5V 2.4A Adafruit power supply, and I have a Relay hat plugged into the raspberry pi aswel.
    – John Cadac
    Jul 15 '17 at 16:55
  • 20 amps should be more than sufficient as well as the Adafruit power supply. Based on the info you provided, I would try to filter out the transient triggers b6 programmatically requiring a certain amount of time for the switch signal to be in the low state. Jul 15 '17 at 21:37
  • The only other suggestion I have is to try using the Gpiozero library. It has some very easy to use button functions which may be tolerant of the transient triggers. Jul 15 '17 at 21:39
  • I went the route of checking if it has been pressed for 0.1 sec. And that seemed to have solved my issue.
    – John Cadac
    Jul 22 '17 at 10:16

You don't mention having a pullup or pulldown resistor in your circuit. (Daddy the Runner) mentions it, but has other solutions as well. I have several pi's with buttons and door alarm sensors. As long as I have the pullups the inputs don't float (float is where the Pi sees various voltages based on electrical noise). This site shows some circuits: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches/

So, your Pi will read the GPIO and with the pullup it will see positive 3.3 v until the button is pressed. When the button is pressed, it grounds (through the resistor so no power short) and the GPIO now sees 0 v.

You can solder the resistors into the wire junction and secure with electrical tape and don't need a circuit board. Good luck!

  • 1
    The third line of his code sets up the internal p/u resistor. Jul 16 '17 at 4:03
  • You are correct re the internal pull-up, however often that internal alone isn't sufficient once a switch is added.
    – caiman
    Jul 17 '17 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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