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.
Setup Development Environment
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.
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.
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
{
"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
}
{
"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
}
{
"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
}
{
"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.
from machine import Pin
from time import sleep
led = Pin(15, Pin.OUT)
while True:
led.toggle()
sleep(1)
print("toggled led")
from machine import Pin
from time import sleep
led = Pin(15, Pin.OUT)
while True:
led.toggle()
sleep(1)
print("toggled led")
from machine import Pin
from time import sleep
led = Pin(15, Pin.OUT)
while True:
led.toggle()
sleep(1)
print("toggled led")
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
.
pip install mpremote
pip install mpremote
pip install mpremote
pip install mpremote
ls /dev/tty.*
ls /dev/tty.*
ls /dev/tty.*
ls /dev/tty.*
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.
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.
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.
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
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
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
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