2

Many people control their Pi Pico using Thonny but I rarely use the Desktop and prefer to program from the command line.

How can I control micropython on a Pico from the command line?

asked Apr 2 at 1:43

1 Answer 1

3

The mpremote command line tool provides an integrated set of utilities to remotely interact with, manage the filesystem on, and automate a MicroPython device over a serial connection.

There is documentation for mpremote but it is not specific to the Pi or Pico.

There is a new version mpremote 1.26.0 dated 2025年08月09日
There is a also new version of MicroPython

Install mpremote

The first step is to install mpremote and this needs to be installed into a virtual environment.

The following is the procedure to install;
I use the hidden directory ~/.local/myenv but you can use any location.

mkdir ~/.local/myenv && cd .local && python -m venv --system-site-packages myenv
source ~/.local/myenv/bin/activate
pip install --user mpremote

To upgrade to the latest version use pip install mpremote --upgrade

When completed run deactivate

Ensure ~/.local/bin is on your path

The simplest way to use this tool is just by invoking it without any arguments: mpremote.

mpremote automatically detects and connects to the first available USB serial device and provides an interactive terminal that you can use to access the REPL and your program’s output.
Serial ports are opened in exclusive mode, so running a second (or third, etc) instance of mpremote will connect to subsequent serial devices, if any are available.

More usefully mpremote supports being given a series of commands at the command line (see link above) which will perform various actions in sequence on a remote Pico.
This documentation provides details and other examples and is worthwhile studying but can be confusing at first. Simple examples are listed below.

The following examples all use LedTimer.py stored on the Pi (or other controlling computer) but you can use any micropython program.

Run a Program

To run a program on the Pico use mpremote run LetTimer.py
This will execute the file (stored on your Pi) directly from RAM on the Pico without copying it to the filesystem.

List Programs on the Pico

mpremote fs ls

The latest mpremote adds a tree command
mpremote fs tree -h
Which shows all files e.g.

tree :
:/
├── [ 508] LedTimer.py
├── [ 135] LedW2.py
├── [ 712] NetworkTest.py
├── [ 293] PicoMAC.py
└── lib
 ├── ntptime-0.1.0.dist-info
 │ ├── [ 94] METADATA
 │ └── [ 80] RECORD
 └── [ 1.3K] ntptime.py

Upload Programs to the Pico

mpremote fs cp LedTimer.py :LedTimer.py
This can also be used to upload library code to be imported e.g.
mpremote fs cp mfrc522.py :lib/mfrc522.py

Run Programs stored on the Pico

Connect to REPL mpremote then
import LedTimer Use Ctrl-] or Ctrl-x to exit

Set RTC time

mpremote rtc --set
Will set the Pico RTC to the host PC's current time.

Show the Contents of a File on the Pico

mpremote fs cat LedTimer.py

Shortcuts

mpremote automatically detects and connects to the first available USB serial device but if you have multiple devices you can specify which (by prepending an alias to the command)
e.g. mpremote a0 fs ls
There are a number of predefined aliases.

  • a0... Alias for connect /dev/ttyACM0
  • u0... Alias for connect /dev/ttyUSB0
  • c0... Alias for connect COM0

You can create your own aliases e.g. to use a specific Pico.
Identify the device with mpremote connect list then create a file ~/.config/mpremote/config.py with contents containing the Pico id similar to:-

commands = {
 "p0": "connect id:e66038b7130c4230",
 "p2": "connect id:5af27ed7d3cffe4a"
}
answered Apr 2 at 1:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.