How to Code a Raspberry Pi Pico: MicroPython in 20 Minutes
In this article
The easiest way to code a Raspberry Pi Pico is MicroPython: you drag one file onto the board, then write Python in a free editor called Thonny. The method differs if you want C/C++ or Arduino language, which needs different tooling and suits timing-critical projects. For a first program, MicroPython is the right choice, and it works the same on the original Pico and the Pico W.
Before You Start: The 4-Item Checklist
You need a Raspberry Pi Pico or Pico W, a micro-USB cable (data-capable, not charge-only), any computer running Windows, macOS or Linux, and a web browser to download two free files. A breadboard, an LED and a 330-ohm resistor are optional but make the first project more satisfying than a blinking onboard LED. Nothing else is required; no soldering is needed for the first program because the pre-soldered header pins or even just the board's test point work fine.
Load MicroPython Onto the Pico in 5 Steps
- Hold down the white BOOTSEL button on the Pico while you plug the USB cable into your computer, then release the button. The board appears as a USB drive named RPI-RP2.
- Download the MicroPython firmware for your board (the Pico and Pico W use different files) from the official Raspberry Pi MicroPython page, saving the .uf2 file to your computer.
- Drag the .uf2 file onto the RPI-RP2 drive. The drive disconnects and the board reboots in about 2 seconds; the green LED may blink once.
- Install Thonny from thonny.org if you don't have it, then open it and go to Run > Select interpreter, choose MicroPython (Raspberry Pi Pico) and the COM or USB port the board is on.
- Click Stop/Restart in Thonny. You should see the MicroPython version banner in the Shell panel at the bottom, which confirms the board is talking to the editor.
Your First Program: Blink the LED
The Pico has a built-in LED wired to GPIO pin 25, so no external parts are needed. Type this into the main Thonny editor window:
from machine import Pin
import time
led = Pin(25, Pin.OUT)
while True:
led.value(1)
time.sleep(0.5)
led.value(0)
time.sleep(0.5)
Click Run, choose Raspberry Pi Pico when asked, and save the file as main.py on the board. The LED starts blinking once per second immediately. Saving it as main.py is the trick that makes the program run automatically every time the Pico gets power, with no computer attached — that is what turns your code into a standalone gadget.
Reading a Pin Instead of Writing One
Change one line to read a button or sensor instead: button = Pin(14, Pin.IN, Pin.PULL_DOWN), then print(button.value()) in the loop. Wire a push button between GPIO 14 and the 3V3 pin and the Shell prints 1 while pressed, 0 while released. That read-write pair covers most beginner projects: sensors in, actions out.
How to Tell It Worked
Three signs confirm success at each stage: the RPI-RP2 drive appears after step 1, the MicroPython banner appears in Thonny's Shell after firmware loads, and the LED blinks at the steady half-second rhythm after you run the code. If you unplug the Pico and power it from a USB phone charger, a saved main.py starts on its own within about 2 seconds of power-up. Pressing the button and seeing the Shell output change confirms input pins work too.
When the Pico Won't Connect or Blink
| Symptom | Cause | Fix |
|---|---|---|
| No RPI-RP2 drive appears | Charge-only USB cable, or BOOTSEL released too early | Use a data USB cable; hold BOOTSEL until the cable is fully seated |
| Thonny shows no MicroPython banner | Wrong interpreter selected, or wrong firmware file for Pico vs Pico W | Re-check Run > Select interpreter; re-flash the matching .uf2 file |
| LED doesn't blink but code runs | Typo in Pin(25) or missing while-loop indentation | Python needs 4-space indents inside loops; retype the loop lines |
| Program doesn't run after unplugging | File saved to the computer instead of the board, or named wrongly | Save as main.py on the Pico itself; the name must be lowercase |
| Port not found in Thonny | Another program grabbed the serial port | Close other terminal or editor apps, unplug and replug the Pico |
Where Each Language Fits After MicroPython
MicroPython handles most hobby projects: LEDs, sensors, motors, small robots and anything where 10 milliseconds of timing slack doesn't matter. Move to C/C++ with the official Pico SDK when you need precise timing, faster loops or the smallest possible power draw; the trade-off is a longer setup and compile step instead of instant Run. Arduino language sits in between — you use the Arduino IDE with the Pico board package, which is comfortable if you already know Arduino from other boards. All three can read the same GPIO pins; only the toolchain changes.
What to Learn Next
Three built-in features carry most beginner projects: PWM (dim an LED or control a servo with machine.PWM), ADC (read analog voltage on GPIO 26-28 with machine.ADC), and I2C (talk to displays and sensor modules with machine.I2C). Each is one import plus a few lines in the same Thonny setup you already have working. On a Pico W, the network module adds Wi-Fi, which opens up web-controlled projects without any extra hardware.