PyRasp

3. Read a pin

You will be able to

You need: a push button, a 330 Ω resistor, an LED. Problems: Button mirror, Reaction game.

The idea

An input pin does not drive anything. It looks at the voltage on its net and answers one question: is this closer to 0 V or 3.3 V?

That only works if the net has a defined voltage. A pin connected to nothing is floating: it picks up stray charge and can read high, low, or change when you move your hand. The fix is a pull — a weak resistor inside the chip that gently ties the pin to 3.3 V (pull-up) or 0 V (pull-down).

Pin.PULL_UP: a resistor inside the Pico ties GP12 to 3.3 V and the button connects it to ground, so it reads 1 when released and 0 when pressed. Pin.PULL_DOWN: the resistor ties GP12 to ground and the button connects it to 3.3 V, so it reads 0 when released and 1 when pressed.

The exact tools

WhatMicroPython
An inputPin(12, Pin.IN)
An input with a pull-upPin(12, Pin.IN, Pin.PULL_UP)
An input with a pull-downPin(12, Pin.IN, Pin.PULL_DOWN)
Read itbutton.value() → 0 or 1

Build it

Wire a button from GP12 to 3V3, and the LED from lesson 1. Use a pull-down, so the pin reads 0 when nothing is pressed:

from machine import Pin
import utime

button = Pin(12, Pin.IN, Pin.PULL_DOWN)
led = Pin(13, Pin.OUT)

while True:
    led.value(button.value())
    utime.sleep_ms(10)

Press Run and hold the button. The LED is a mirror: on while the button is held, off otherwise.

Fun fact. The Pico's internal pulls are about 50–80 kΩ — a very weak spring. A wire can easily overpower one, which is why a button only needs a direct connection to 3V3 or GND.

What you should see

The LED follows the button in real time. The console stays empty.


Common mistakes

What you seeWhyFix
The LED flickers on its ownThe input is floatingAdd Pin.PULL_DOWN (or PULL_UP)
The button works backwardsThe pull does not match the wiringButton to 3V3 → PULL_DOWN; to GND → PULL_UP
Nothing happensThe button straddles two different rowsPut its legs in the correct two nets
The board reports damage5 V reached a GPIOButtons go to 3V3, never 5 V

Check yourself

  1. What does a floating input read?
  2. Button to 3V3: which pull do you need?
  3. Why is utime.sleep_ms(10) inside the loop?

Answers: anything — it is undefined · pull-down · to sample repeatedly without starving the board.

Remember

  • Inputs read a voltage; they do not drive one.
  • A floating pin is undefined — always give it a pull.
  • PULL_DOWN + button to 3V3 reads 0 when released, 1 when pressed.

Go further

Next: Lesson 4 — Remember state