PyRasp

1. Switch a pin

You will be able to

  • make any header pin high (3.3 V) or low (0 V),
  • light an LED through a resistor without damaging it,
  • explain why a pin is called an output.

You need: the Pico, one LED, one 330 Ω resistor. Problem: First blink.

The idea

A GPIO pin is a tiny switch connected to the inside of the chip. When it is an output, the chip drives it:

Inside the Pico, GP13 is switched to 3.3 V or to 0 V. High: current flows from GP13 through the 330 ohm resistor and the LED to ground, and the LED lights. Low: no current, the LED is off.

“High” does not mean more electricity. It means 3.3 volts relative to ground. Current flows from 3.3 V, through the resistor and the LED, to ground (0 V).

The resistor is not decoration. An LED with no resistor tries to take far more current than the pin can give; the resistor sets a safe, sensible current (about 5 mA here).

The exact tools

WhatMicroPython
Make a pin an outputPin(13, Pin.OUT)
High / lowled.on(), led.off(), led.value(1)
Flip itled.toggle()
Read back an outputled.value()
The on-board LEDPin(25, Pin.OUT) — GP25, not on the header

On this Pico, Pin(13) is GP13, the pin labelled GP13 on header position P17.

Build it

Wire the LED from GP13, through the 330 Ω resistor, to GND. In main.py:

from machine import Pin
import utime

led = Pin(13, Pin.OUT)

led.on()
utime.sleep(1)
led.off()
utime.sleep(1)
led.on()

Press Run. Then change the last line to led.off() and run again.

Fun fact. A red LED needs about 1.8 V to light. The Pico gives 3.3 V, so the resistor takes the extra 1.5 V. That is why the same LED on a 5 V board needs a bigger resistor.

What you should see

The LED comes on for one second, goes off for one second, then comes on and stays on. The console shows nothing — this program has nothing to say yet.


Common mistakes

What you seeWhyFix
Nothing lightsThe LED is backwardsThe longer leg (anode) goes to the resistor/GP13, the flat side to GND
Nothing lightsThe resistor is in the wrong placeGP13 → resistor → LED → GND, in that order
The LED is dimA very large resistor220–330 Ω is right for a 3.3 V board
NameError: name 'Pin' is not definedThe import is missingfrom machine import Pin
A warning about currentNo resistor, or a shortAdd the resistor; never wire GP13 straight to GND

Check yourself

  1. What voltage is on GP13 when it is low?
  2. Why does the LED need a resistor?
  3. What does Pin(25) control, and where is it?

Answers: 0 V · to limit the current · the on-board LED, not a header pin.

Remember

  • Pin(n, Pin.OUT) makes a pin an output; on(), off() and toggle() drive it.
  • High = 3.3 V, low = 0 V; current needs a path to ground.
  • Always use a resistor with an LED.

Go further

  • Make the on-board LED blink with Pin(25) — problem The Pico's own LED.
  • Make two LEDs take turns — Alternating lights.
  • Try led.value(1) and led.value(0) instead of on()/off(). They are the same thing.

Next: Lesson 2 — Time and patterns