-1

I'm trying to code a button to turn and LED on and off.

from machine import Pin

led = Pin(16, Pin.OUT)
button = Pin(17, Pin.IN)


while True:
    if button.value()==1:
        led.high()
        print("hello")
    if button.value()==0:
        led.low()

This is all running on a Pi Pico W. I notice the LED turns on anytime a wire is connected to pin 17, even if the wire isn't plugged into anything else. When I push the button it lights up brighter, and prints Hello.

Any help would be greatly appreciated, thanks!

2
  • why do you believe that the problem is caused by the code? ... please add all of the available information to your question
    – jsotola
    Apr 14 at 17:34
  • Post a photo of your wiring. I assume your button is connected to 3.3V, you should pull down your input. projects.raspberrypi.org/en/projects/…
    – CoderMike
    Apr 14 at 19:11

1 Answer 1

1

It may be a case of floating pin. Try inserting a (for example) 10k resistor between the pin and GND. How do pull-down resistors work?

1
  • Or just configure pull down on the pin.
    – CoderMike
    Apr 14 at 21:09

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.