-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!

Milliways
  • 59,890
  • 31
  • 101
  • 209
Jimothy
  • 1
  • 1
  • 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 '23 at 17:34
  • Post a photo of your wiring. I assume your button is connected to 3.3V, you should pull down your input. https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/6 – CoderMike Apr 14 '23 at 19:11

1 Answers1

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?

Clóvis Fritzen
  • 373
  • 1
  • 11