Skip to main content

MicroPython

MicroPython

Install Thonny (for MicroPython)

See https://wiki.seeedstudio.com/XIAO-RP2040-with-MicroPython/ or jump to the next – TL;DR; section.

TL;DR

  • https://thonny.org/
  • pip install thonny
  • launch thonny
  • Tools > Options... > Interpreter : MicroPython(RP2040)
  • reboot rp2040 in boot mode (boot button pressed while reset released)
  • click install or update MicroPython (bottom)
  • ok => you're now talking/interpreting python from the rp2040
  • select pico as target (pico and xiao-rp2040 act the same)

Hello World

For this "Hello World", we will "just" make a LED blink.

The sections were created to help you starting with MicroPython and to give the Fab Academy 2025 programming recitation.

Please note that for the next code and simulations, examples will be given on PIN25, which is the blue builtin LED of your XIAO RP2040, or the green LED of your Raspberry Pico board. On the related Wokwi simulation, we will use PIN0 with a connected LED just because the (very) small LED represented on the board is a bit too discrete.


Loop : "Infinite" while

Blinking the onboard blue led, minimalistic code. Based on an inifinite while because the condition next to it is declared "always true", so that it will last forever.

import machine
import time

# Set pin 25 (= blue led on XIAO RP2040, builtin led on RPi Pico) as an output
led = machine.Pin(25, machine.Pin.OUT)

# Infinite loop to blink the LED
while True:
led.toggle() # Change LED's status
time.sleep(0.5) # Wait for 0.5 seconds

Wokwi simulation : https://wokwi.com/projects/421430264769320961


Loop : "conditional" while

This code will set a variable "a" to the value 10, then decrement it (a-=1) on each loop until a becomes 0. The code will then stop. This will thus make your LED blink 5 times. (every loop toggle its status)

import machine
import time

# Set pin 25 (= blue led on XIAO RP2040, builtin led on RPi Pico) as an output
led = machine.Pin(25, machine.Pin.OUT)

a = 10

# Make your led blink 5 times (=10/2 because toggle)
while a > 0:
a -= 1 # equivalent to a=a-1
led.toggle() # Change LED's status
time.sleep(0.5) # Wait for 0.5 seconds

Wokwi simulation : https://wokwi.com/projects/421430452019854337


Loop : for

import machine
import time

# Set pin 25 (= blue led on XIAO RP2040, builtin led on RPi Pico) as an output
led = machine.Pin(25, machine.Pin.OUT)

# Make your led blink 5 times (=10/2 because toggle)
for _ in range(10):
led.toggle() # Change LED's status
time.sleep(0.5) # Wait for 0.5 seconds

Wokwi simulation : https://wokwi.com/projects/421430516926718977


Function Call

import machine
import time

# Set pin 25 (= blue led on XIAO RP2040, builtin led on RPi Pico) as an output
led = machine.Pin(25, machine.Pin.OUT)

def blink_led(times):
for _ in range(times):
led.on()
time.sleep(0.2)
led.off()
time.sleep(0.3)

for i in range(5):
blink_led(i) # Blink i times
time.sleep(2) # Wait for 2 seconds

Wokwi simulation : https://wokwi.com/projects/421430616754324481


Using arrays

This code will define an array containing five different values and then will make the LED blink that amount of times (using the blink_led() function).

import machine
import time

# Array with 5 pre-defined values
values = [3, 7, 2, 5, 1]

# Set pin 25 (= blue led on XIAO RP2040, builtin led on RPi Pico) as an output
led = machine.Pin(25, machine.Pin.OUT)

def blink_led(times):
for _ in range(times):
led.on()
time.sleep(0.2)
led.off()
time.sleep(0.3)

for i in values:
blink_led(i) # Blink i times
time.sleep(2) # Wait for 2 seconds

Wokwi simulation : https://wokwi.com/projects/421517543955841025


Branching : if then else

import machine
import time

# Set pin 25 (= blue led on XIAO RP2040, builtin led on RPi Pico) as an output
led = machine.Pin(25, machine.Pin.OUT)

def blink_led(times):
for _ in range(times):
led.on()
time.sleep(0.2)
led.off()
time.sleep(0.3)

for i in range(5):
if i % 2 == 0: # if i is even
blink_led(2)
else:
blink_led(1) # if i is odd
time.sleep(2)