How to program Raspberry Pi Pico with microPython

Posted by

blink an led with raspberry pi pico
blink an led with raspberry pi pico

In this article we will learn how to use Raspberry Pi Pico with microPython. This might have been the fastest ever I took to blink a LED on any board. It only requires a few simple steps and you are up and running.

You are going to need:

  • Thonny IDE
  • Computer (Desktop/laptop)
  • Raspberry Pi Pico
  • A LED (any color)
  • A resistor (between 1k ohm and 4k7 ohm)
  • Breadboard
  • Jumper wires

There is the official guide from the foundation for the Raspberry pi Pico, very similar to what we will do here. Start be installing the Thonny IDE from here.

Now we will supply the microPython firmware to the Pico. Press and hold the BOOTSEL button of the Pico while connecting its USB cable to the computer. Once the cable is connected to the computer, release BOOTSEL. You will see a new drive in your file explorer (e.g. Windows explorer).

Download the UF2 file from this source. Drag such file to the new drive that show up in the last paragraph, which is the Pico. It will reboot and you are good to go.

With the Pico connected to the computer, open the Thonny IDE and go to “Tools > Options > Interpreter”. Select “Micropython (Raspberry Pi Pico” and the COM port that represents your Pico (there will ideally be only one, select it). Click “OK”.

There is a console in the bottom of the Thonny IDE, which (if the above steps were successful) wil show something like this.

MicroPython v1.22.2 on 2024-02-22; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT

I have connected a LED to pin 18 of my Pico, with a series resistor of 1k Ohm. You can also use its onboard LED on pin 25.

The code

The code below will blink the LED every 0.5 seconds, using time.sleep(). Most hardware interactions in microPython will happen via the “machine” package.

from machine import Pin
import time

led = machine.Pin(18, machine.Pin.OUT)
while True:
     led.high()
     time.sleep(0.25)
     led.low()
     time.sleep(0.25)

More articles about the Raspberry Pi Pico are to follow, I want to explore it even more. Meanwhile, explore the ESP32-S2 with microPython in this post.

7 responses

Leave a Reply

Your email address will not be published. Required fields are marked *