micropython-guardar-script-en-placa

Save the script on the device in MicroPython

  • 3 min

Let’s see how to save our program or script on a device so that the board executes the program autonomously (without a connected PC).

So far, we have created our first program (blink) and seen how it runs through the MicroPython REPL.

However, when we disconnect the device and reconnect it, the program is not preserved. That is, we have not saved our program on the device.

Logically, we want to be able to upload our program to the board, and have it execute the next time it restarts. So let’s see how to do it.

File system on the board

Normally, in an embedded system, when we want to save our program, we write it to the device, overwriting its configuration.

But in the case of MicroPython, we already have firmware loaded on the device. This is precisely the Python interpreter. So we cannot overwrite it.

Instead, MicroPython works with a file system, which is very similar to that of a computer, with directories and files.

  • Main file (main.py): This file runs automatically when the board starts up. It is the main entry point for any project.
  • Module files: You can create custom modules (e.g., sensor.py) and use them in your main program.

For example, our project might have the following structure,

/
├── main.py
├── sensor.py
├── config.json
└── data/
    └── log.txt

That is, to upload our project we need to upload our directory and file structure to the device. At boot, MicroPython will execute the main.py file.

Tools for file transfer

There are different IDEs and tools for transferring files to a MicroPython board. Let’s see how to do it in the most popular ones,

In Arduino Lab for MicroPython, you have an icon with a folder symbol. By clicking it, a window opens.

We can select files from our computer and upload them to the device’s memory.

micropython-subir-ficheros-1

In Thonny, to upload the file we simply have to choose File > Save as....

A window will appear with two options, where we can choose our device.

micropython-subir-ficheros-2

ampy (Adafruit MicroPython Tool) is a command-line tool that allows interaction with MicroPython boards from the terminal.

We can install it with this command:

pip install adafruit-ampy

To transfer files with ampy, we connect the board and obtain the serial port (for example, /dev/ttyUSB0 on Linux or COM3 on Windows). We run the following command,

ampy --port /dev/ttyUSB0 put file.py

To verify that the file has been transferred correctly, you can list the files on the board:

ampy --port /dev/ttyUSB0 ls

Transferring multiple files

In more complex projects, it is common to need to transfer multiple files to the board. Here’s how to do it:

In Arduino Lab we can upload folders from the window we have seen. If we have several files within the folder, it will upload them all.

Interestingly, it allows me to choose multiple files outside the folder (this part of the UI is frankly improvable).

In Thonny, we can activate a file explorer by clicking on View / Files

micropython-subir-ficheros-3

We can also create a small script to automate the transfer with ampy (useful in projects with many many many files).

import os
import subprocess

port = "/dev/ttyUSB0"  # Change this according to your port
files = ["main.py", "sensor.py", "config.json"]

for file in files:
    subprocess.run(["ampy", "--port", port, "put", file])