O have tried to do this so many times, so many websites, so many books, and it just won't work. I do not know how to wire it so that the LED lights up when I press the button. I'm pretty sure that I have the correct type of button, and it works when I only tell it to just light the LED, so I am so confused.

Here is one of the more recent codes I have used.

from gpiozero import LED, Button
from signal import pause 
led = LED(18)
button = Button(23)  
button.when_pressed = led.on
button.when_released = led.off


Here is the wiring I used. enter image description here enter image description here enter image description here enter image description here

There you go, my real wiring. -_-

  • Does it work if you do the following sudo pigpiod then pigs pud 23 u and then press the button? – joan Nov 27 '19 at 17:43
  • @joan no it does not, should I buy new buttons or wires? Why is this not working? Really appreciate you guy's help – Robotechnology Nov 30 '19 at 1:47
  • In that case you have not wired as shown in the schematic. Edit your question and provide clear photos allowing us to follow the connections. – joan Nov 30 '19 at 8:27
  • A quick test is to run ./monitor.py and see if there are any button presses (ignore GPIO 28/29). – joan Nov 30 '19 at 8:34
  • @Joan I did it joan all my real wiring – Robotechnology Dec 4 '19 at 1:43

If you are using a Raspberry Pi 3 then try this code:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM) # Setting BCM mode
GPIO.setup(4,GPIO.IN,pull_up_down = GPIO.PUD_DOWN) #Setting Button Input with Pull down Resistor notation
while True:
        if (GPIO.input(4)==0): # Cheking Button Condition
            print("Button 1 Pressed")
            print("Push Button")

    except KeyboardInterrupt:

For Connection wiring take reference from this: http://www.toptechboy.com/wp-content/uploads/2015/06/raspberry-pi-LEDs.jpg

And if you like to follow this: https://www.hackster.io/hardikrathod/push-button-with-raspberry-pi-6b6928

  • Just asking, did it work for you? – Robotechnology Nov 30 '19 at 1:44
  • Because so many things have not worked, I don't exactly have much faith in the internet, so did you try this? Did it work? – Robotechnology Nov 30 '19 at 1:48
  • Yes...it is working great. Sorry for the late reply. I was using two buttons with the raspberry pi and controlled the 2 respective LEDs. – Lucifer Dec 2 '19 at 5:04

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.