This tutorial guides you through setting up your Raspberry Pi Pico, installing MicroPython firmware, connecting an LED, and writing the code to make it blink using the Thonny IDE.
Raspberry Pi Pico: The microcontroller board.
Micro-USB Cable: For connecting the Pico to your computer (ensure it's a data cable, not just a charging cable).
Thonny IDE: A user-friendly Python IDE that's perfect for MicroPython.
A Breadboard: (Optional, but highly recommended for easy connections).
An LED (Light Emitting Diode): Any color will do.
A Resistor: 220 Ohm to 330 Ohm (e.g., 220Ω, 330Ω). This is crucial to protect your LED from burning out.
Jumper Wires: Male-to-male jumper wires.
If you don't have Thonny installed, download it from the official website:
Download Thonny: https://thonny.org/ Follow the installation instructions for your operating system (Windows, macOS, Linux).
Your Pico needs the MicroPython firmware to understand the Python code you'll write.
Prepare the Pico:
Press and hold the BOOTSEL button on your Raspberry Pi Pico.
While holding BOOTSEL, plug the micro-USB cable into the Pico and then into your computer.
Release the BOOTSEL button.
Your computer should recognize the Pico as a USB mass storage device named RPI-RP2.
Download MicroPython Firmware:
Go to the official Raspberry Pi Pico MicroPython downloads page: https://www.raspberrypi.com/documentation/microcontrollers/micropython.html#drag-and-drop-micropython
Download the latest .uf2 file for the Raspberry Pi Pico (usually named something like rp2-pico-xxxx.uf2).
Drag and Drop Firmware:
Locate the downloaded .uf2 file.
Drag and drop this .uf2 file directly onto the RPI-RP2 drive that appeared on your computer.
The Pico will automatically reboot once the file transfer is complete. The RPI-RP2 drive will disappear.
We'll use a digital output pin on the Pico. The Pico has a built-in LED connected to GP25, but we'll use an external one for this tutorial to demonstrate connecting external components. Let's use GP15.
Safety First: Always unplug your Pico from USB before making circuit changes.
Identify LED Legs:
The longer leg of an LED is the anode (positive, +).
The shorter leg is the cathode (negative, -).
Connect the Resistor:
Connect one leg of the 220-330 Ohm resistor to the longer leg (anode) of the LED.
Connect to Pico:
Connect the other leg of the resistor to GP15 on your Raspberry Pi Pico.
Connect the shorter leg (cathode) of the LED directly to a GND (Ground) pin on your Raspberry Pi Pico. There are several GND pins; any will work.
Visual Aid:
Raspberry Pi Pico
____________________
| |
| GP15 ---|-- Resistor --- Longer_Leg_LED
| | |
| GND ----|---------------- Shorter_Leg_LED
|__________________|
If using a breadboard, it would look something like this:
(Pico Pin GP15) -------> | Resistor | --------> | LED Anode (Long) |
|
| LED Cathode (Short) |
|
(Pico Pin GND) --------> | (Breadboard Ground Rail) | <--------
Open Thonny: Launch the Thonny IDE.
Configure Interpreter:
Go to Tools > Options...
Select the "Interpreter" tab.
From the dropdown menu, select "MicroPython (Raspberry Pi Pico)".
For "Port," select the COM port that corresponds to your Pico. If you're unsure, disconnect your Pico, check the list, then reconnect and see which new port appears. On macOS/Linux, it might be something like /dev/ttyACM0 or /dev/ttyUSB0. On Windows, it will be a COM port (e.g., COM3).
Click "OK".
Connect to Pico:
In the Thonny "Shell" pane (at the bottom), you should now see a message like "MicroPython vX.X.X on Raspberry Pi Pico" and a >>> prompt. This means Thonny is successfully connected to your Pico. If not, recheck your interpreter settings and cable connection.
Write the Code:
In the main editor window of Thonny (the top pane), type the following MicroPython code:
from machine import Pin
import time
# Define the LED pin. We connected our LED to GP15.
# The Pico's onboard LED is connected to GP25.
led_pin = Pin(15, Pin.OUT) # Set GP15 as an output pin
while True:
led_pin.value(1) # Turn LED on (1 is HIGH)
print("LED ON") # Print to Thonny's Shell
time.sleep(1) # Wait for 1 second
led_pin.value(0) # Turn LED off (0 is LOW)
print("LED OFF") # Print to Thonny's Shell
time.sleep(1) # Wait for 1 second
Save the Code to Pico:
Go to File > Save as...
Choose "Raspberry Pi Pico" (this saves the file directly onto the Pico's flash memory).
Name the file main.py.
Why main.py? When the Raspberry Pi Pico powers up, it automatically looks for a file named main.py and runs it. This means your LED will start blinking as soon as the Pico is powered, even without being connected to Thonny.
Run the Code:
Click the green "Run current script" button (the triangle icon) in Thonny.
You should see your LED start blinking!
In the Thonny Shell, you'll also see "LED ON" and "LED OFF" messages printing every second.
from machine import Pin: This line imports the Pin class from the machine module, which is part of MicroPython's standard library for controlling hardware pins.
import time: This imports the time module, which provides functions for time-related tasks, like pausing the program.
led_pin = Pin(15, Pin.OUT):
This creates a Pin object.
15 specifies that we are controlling GPIO pin 15 (GP15).
Pin.OUT configures this pin as an output pin, meaning the Pico will send voltage out through this pin.
while True:: This creates an infinite loop, so the code inside will repeat forever. This is common for microcontroller programs that need to run continuously.
led_pin.value(1): Sets the voltage on led_pin to HIGH (typically 3.3V on the Pico). This turns the LED ON.
print("LED ON"): This sends the text "LED ON" to the Thonny Shell (the console connected to the Pico). Useful for debugging.
time.sleep(1): Pauses the execution of the program for 1 second (1000 milliseconds).
led_pin.value(0): Sets the voltage on led_pin to LOW (0V). This turns the LED OFF.
time.sleep(1): Pauses again for 1 second.
Change Blink Speed: Try changing time.sleep(1) to time.sleep(0.5) for a faster blink, or time.sleep(2) for a slower blink.
Use Onboard LED: If you don't have an external LED, you can use the Pico's built-in LED by changing led_pin = Pin(15, Pin.OUT) to led_pin = Pin(25, Pin.OUT). (Remember to remove the external LED circuit if you do this).
Vary Brightness (PWM): For more advanced control, explore PWM (Pulse Width Modulation) with led_pin = PWM(Pin(15)). This allows you to control the LED's brightness.
Congratulations! You've successfully blinked an LED with your Raspberry Pi Pico using MicroPython and Thonny. This foundational project opens the door to countless more exciting robotics and electronics projects.