Raspi Course

MicroPython

In this part, we will install micropython on the pico, then setup a development environment to code with the pico.

Install MicroPython

Before we can do any coding, we need to get the MicroPython firmware on to the Pi Pico.

step 1:

Download the MicroPython firmware for the Pi Pico from the Raspberry Pi website

step 2:

Plug the pico into your computer via USB and drag and drop the firmware file onto the pico. Follow the video below for a visual guide.

Setup Development Environment

step 3:

Make sure you have python3 installed on your machine. You should be able to access it in your terminal using python3 or python

Use python3 --version or python --version to check if you have access to python3.

Now we'll setup a new pico project that will give us intellisense and type checking inside vscode or cursor.

step 4:

Create a new python project with venv.

mkdir new_project
cd new_project
python3 -m venv .venv
source .venv/bin/activate
mkdir new_project
cd new_project
python3 -m venv .venv
source .venv/bin/activate
mkdir new_project
cd new_project
python3 -m venv .venv
source .venv/bin/activate
mkdir new_project
cd new_project
python3 -m venv .venv
source .venv/bin/activate

Now we need to isntall the micropython and raspberry pi pico stubs so that the code editor can give us type checking and intellisense. You can find a list of all the stubs here: https://github.com/Josverl/micropython-stubs/tree/main/publish, the rest of this guide assumes you're using the stubs for the pico 2 w.

step 5:

Install the micropython stubs.

pip install git+https://github.com/Josverl/micropython-stubs.git@main#subdirectory=publish/micropython-stdlib-stubs
pip install git+https://github.com/Josverl/micropython-stubs.git@main#subdirectory=publish/micropython-v1_24_1-rp2-rpi_pico_w-stubs
pip install git+https://github.com/Josverl/micropython-stubs.git@main#subdirectory=publish/micropython-stdlib-stubs
pip install git+https://github.com/Josverl/micropython-stubs.git@main#subdirectory=publish/micropython-v1_24_1-rp2-rpi_pico_w-stubs
pip install git+https://github.com/Josverl/micropython-stubs.git@main#subdirectory=publish/micropython-stdlib-stubs
pip install git+https://github.com/Josverl/micropython-stubs.git@main#subdirectory=publish/micropython-v1_24_1-rp2-rpi_pico_w-stubs
pip install git+https://github.com/Josverl/micropython-stubs.git@main#subdirectory=publish/micropython-stdlib-stubs
pip install git+https://github.com/Josverl/micropython-stubs.git@main#subdirectory=publish/micropython-v1_24_1-rp2-rpi_pico_w-stubs
step 5:

Add the following to your .vscode/settings.json file. Create .vscode/settings.json if it doesn't exist.

.vscode/settings.json
{
"python.languageServer": "Default",
"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticSeverityOverrides": {
"reportMissingModuleSource": "none"
},
"python.analysis.extraPaths": [
".venv/lib/python3.*/site-packages"
],
"python.analysis.ignore": [
"rp2"
],
"editor.formatOnSave": true,
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.variableTypes": true,
"micropython.executable": ".venv/bin/micropython",
"python.analysis.autoImportCompletions": true
}
.vscode/settings.json
{
"python.languageServer": "Default",
"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticSeverityOverrides": {
"reportMissingModuleSource": "none"
},
"python.analysis.extraPaths": [
".venv/lib/python3.*/site-packages"
],
"python.analysis.ignore": [
"rp2"
],
"editor.formatOnSave": true,
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.variableTypes": true,
"micropython.executable": ".venv/bin/micropython",
"python.analysis.autoImportCompletions": true
}
.vscode/settings.json
{
"python.languageServer": "Default",
"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticSeverityOverrides": {
"reportMissingModuleSource": "none"
},
"python.analysis.extraPaths": [
".venv/lib/python3.*/site-packages"
],
"python.analysis.ignore": [
"rp2"
],
"editor.formatOnSave": true,
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.variableTypes": true,
"micropython.executable": ".venv/bin/micropython",
"python.analysis.autoImportCompletions": true
}
.vscode/settings.json
{
"python.languageServer": "Default",
"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticSeverityOverrides": {
"reportMissingModuleSource": "none"
},
"python.analysis.extraPaths": [
".venv/lib/python3.*/site-packages"
],
"python.analysis.ignore": [
"rp2"
],
"editor.formatOnSave": true,
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.variableTypes": true,
"micropython.executable": ".venv/bin/micropython",
"python.analysis.autoImportCompletions": true
}

Now vscode/cursor should be all setup to code micropython with the pico. Let's try this out.

step 6:

Create a new directory called src and add a file called main.py to it.

  • src
    • main.py
step 7:

Add the following code to main.py:

src/main.py
from machine import Pin
from time import sleep

led = Pin(15, Pin.OUT)

while True:
led.toggle()
sleep(1)
print("toggled led")
src/main.py
from machine import Pin
from time import sleep

led = Pin(15, Pin.OUT)

while True:
led.toggle()
sleep(1)
print("toggled led")
src/main.py
from machine import Pin
from time import sleep

led = Pin(15, Pin.OUT)

while True:
led.toggle()
sleep(1)
print("toggled led")
src/main.py
from machine import Pin
from time import sleep

led = Pin(15, Pin.OUT)

while True:
led.toggle()
sleep(1)
print("toggled led")

You should notice vscode/cursor is giving you type checking and intellisense. If it isn't, try restarting vscode/cursor.

mpremote

Now we actually need to run this code on the pico. And for that, we'll use mpremote.

step 8:

Install mpremote.

pip install mpremote
pip install mpremote
pip install mpremote
pip install mpremote
step 1:

Find the port of your Pi Pico.

For example, on my mac it came out as /dev/tty.usbmodem21301, so i'll use that for the rest of my examples. But make sure you're using the correct port for your pico.

ls /dev/tty.*
ls /dev/tty.*
ls /dev/tty.*
ls /dev/tty.*
step 10:

Run the code on the pico.

mpremote connect /dev/tty.usbmodem21301 mount ./src run ./src/main.py
mpremote connect /dev/tty.usbmodem21301 mount ./src run ./src/main.py
mpremote connect /dev/tty.usbmodem21301 mount ./src run ./src/main.py
mpremote connect /dev/tty.usbmodem21301 mount ./src run ./src/main.py

This command will mount the src directory to the pico and run the main.py file.

Now you should see the print statements being printed to the terminal window. But to see the LED blinking, you'll need to plug one into pin 15.

step 11:

Plug in the LED to pin 15.

You can follow the instructions here on how to plug in the LED. You should use a resistor, even though I often just leave it out for testing.

LED Instructions

Now you should be able to see the logs and the LED blinking.

Now you can try some slightly more complex code that will use PWM to fade the LED in and out.

src/main.py
from machine import Pin, PWM
import utime

pwm = PWM(Pin(15))
pwm.freq(5000)

while True:
# Ramp up
for duty in range(65535):
pwm.duty_u16(duty)
utime.sleep_us(1) # 1 microsecond delay


# Ramp down
for duty in range(65535, 0, -1):
pwm.duty_u16(duty)
utime.sleep_us(1) # 1 microsecond delay
src/main.py
from machine import Pin, PWM
import utime

pwm = PWM(Pin(15))
pwm.freq(5000)

while True:
# Ramp up
for duty in range(65535):
pwm.duty_u16(duty)
utime.sleep_us(1) # 1 microsecond delay


# Ramp down
for duty in range(65535, 0, -1):
pwm.duty_u16(duty)
utime.sleep_us(1) # 1 microsecond delay
src/main.py
from machine import Pin, PWM
import utime

pwm = PWM(Pin(15))
pwm.freq(5000)

while True:
# Ramp up
for duty in range(65535):
pwm.duty_u16(duty)
utime.sleep_us(1) # 1 microsecond delay


# Ramp down
for duty in range(65535, 0, -1):
pwm.duty_u16(duty)
utime.sleep_us(1) # 1 microsecond delay
src/main.py
from machine import Pin, PWM
import utime

pwm = PWM(Pin(15))
pwm.freq(5000)

while True:
# Ramp up
for duty in range(65535):
pwm.duty_u16(duty)
utime.sleep_us(1) # 1 microsecond delay


# Ramp down
for duty in range(65535, 0, -1):
pwm.duty_u16(duty)
utime.sleep_us(1) # 1 microsecond delay

Deploy

For developing on the pico, the command we were using before is great because it doesn't require us to upload all the code files every time we make a change. But if you want to be able to unplug the pico from your computer and have the python code keep running, you can use the following command to completely copy all the code and run it.

cd src && mpremote connect /dev/tty.usbmodem21301 cp -r ./* :
cd src && mpremote connect /dev/tty.usbmodem21301 cp -r ./* :
cd src && mpremote connect /dev/tty.usbmodem21301 cp -r ./* :
cd src && mpremote connect /dev/tty.usbmodem21301 cp -r ./* :

REPL

If you just want a repl to execute quick commands on the pico, you can use the following command.

mpremote connect /dev/tty.usbmodem21301 repl
mpremote connect /dev/tty.usbmodem21301 repl
mpremote connect /dev/tty.usbmodem21301 repl
mpremote connect /dev/tty.usbmodem21301 repl