Skip to content

Commit 41cd4ac

Browse files
Merge branch 'content/micropython/micropython-revamp' of https://github.com/arduino/docs-content into content/micropython/micropython-revamp
2 parents c1b22fb + a920267 commit 41cd4ac

File tree

11 files changed

+250
-7
lines changed

11 files changed

+250
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: 'File System'
3+
description: 'Learn how to use the File Sytem in MicroPython.'
4+
author: 'Pedro Lima'
5+
tags: [MicroPython, REPL]
6+
---
7+
8+
9+
When working with MicroPython, we’re not limited to a single program like in traditional Arduino sketches. Instead, MicroPython provides a file system, enabling us to store and manage multiple files on our microcontroller. This opens up powerful capabilities for organizing code, managing assets, and creating modular projects.
10+
11+
In this article, we’ll explore how the MicroPython file system works, how to organize files effectively, and the typical structure of MicroPython projects.
12+
13+
## The MicroPython File System: Key Differences
14+
15+
In traditional Arduino programming, we upload a single compiled file directly to the microcontroller, where it runs immediately. With MicroPython, however, we work within a file system that can store multiple files. This file system allows us to:
16+
17+
- **Upload and Download Files**: We can save individual scripts, libraries, and assets directly on the device.
18+
- **Organize Project Files**: Create folders, save multiple scripts, and organize files for a modular approach.
19+
- **Edit Files Directly on the Device**: Modify files without needing to overwrite everything, making adjustments faster and more flexible.
20+
21+
## Accessing the MicroPython File System
22+
23+
To interact with the MicroPython file system, we’ll use Arduino Labs for MicroPython, which provides tools to manage and view files directly on our device. Here’s how to get started:
24+
25+
1. **Connect to Your Device**: Open Arduino Labs for MicroPython and establish a connection to our microcontroller.
26+
2. **Upload and Download Files**: Use the file manager to upload files from our computer to the microcontroller or download files back to our computer.
27+
3. **Organize Files**: We can create folders and store multiple files, making it easy to organize our project.
28+
29+
## Basic MicroPython File Structure
30+
31+
A typical MicroPython project includes a main file, boot script, libraries, and any supporting files our project needs. Here’s a standard layout:
32+
33+
```
34+
/ (Root Directory)
35+
├── boot.py
36+
├── main.py
37+
```
38+
39+
### Key Files
40+
41+
- **`boot.py`**: Runs once at startup, before `main.py`, and is typically used for system configurations that need to be set when the device first powers on.
42+
- **`main.py`**: This is the primary script, similar to the `setup()` and `loop()` structure in Arduino. It runs automatically after `boot.py` finishes.
43+
44+
## Example: Switching Execution from `main.py` to `favorite_things.py`
45+
46+
Let’s say we’re creating a project where `main.py` runs and, at a certain point, hands off control to `favorite_things.py` to perform a specific task. Here’s how we might organize it.
47+
48+
1. **Write the Main Script** (`main.py`): This script runs some initial code and then switches to executing `favorite_things.py`.
49+
2.Create the file "favourite_things.py".
50+
3. **Add Content to `favorite_things.py`**: In this case, we’ll simply print a message from `favorite_things.py`.
51+
52+
### Sample `main.py` Code
53+
54+
Here’s how `main.py` might look, printing an introductory message and then executing `favorite_things.py`:
55+
56+
```python
57+
def main():
58+
print("Getting the list of my favourite things...")
59+
60+
# Now execute favorite_things.py
61+
print("Switch to favorite_things.py...")
62+
try:
63+
with open("favorite_things.py") as f:
64+
exec(f.read())
65+
except OSError:
66+
print("Error: Could not open favorite_things.py")
67+
68+
if __name__ == "__main__":
69+
main()
70+
```
71+
72+
### Sample `favorite_things.py` Code
73+
74+
In `favorite_things.py`, we’ll keep it simple with a message:
75+
76+
```python
77+
# favorite_things.py
78+
print("Bears. Beets. Battlestar Galactica.")
79+
```
80+
81+
### Explanation
82+
83+
- **Switching Execution**: `main.py` starts by printing an introductory message, and then uses `exec(f.read())` to read and run the content of `favorite_things.py`.
84+
- **Running Another Script**: The `exec()` function allows `main.py` to execute `favorite_things.py` as if it were part of `main.py`, printing “Bears. Beets. Battlestar Galactica.” directly.
85+
86+
### Expected Output
87+
88+
When you run `main.py`, the output should look like this:
89+
90+
```
91+
Getting the list of my favourite things...
92+
Switch to favorite_things.py...
93+
Bears. Beets. Battlestar Galactica.
94+
```
95+
96+
This setup demonstrates how to use MicroPython’s file system to organize and execute multiple scripts, allowing for modular and readable code.
97+
98+
## Organizing Code with Modules and Libraries
99+
100+
When you start importing custom modules or libraries, MicroPython will automatically create a `/lib` folder to store them. This helps keep external libraries or reusable functions separate from your main code. You can use this structure to further organize your project, making it easy to manage larger codebases.
101+
102+
For example:
103+
104+
```
105+
/ (Root Directory)
106+
├── boot.py
107+
├── main.py
108+
├── /lib
109+
│ ├── my_custom_library.py
110+
```
111+
112+
### Using Libraries in `main.py`
113+
114+
After placing reusable code in `/lib`, you can import it directly in `main.py`:
115+
116+
```python
117+
from lib.my_custom_library import some_function
118+
119+
some_function()
120+
```
121+
122+
This structure allows for cleaner, more modular code that’s easy to scale as your project grows.
123+
124+
## Conclusion
125+
126+
MicroPython’s file system brings flexibility and structure to embedded programming, enabling a more organized and modular approach. By understanding the typical file structure and how to manage files, we’ll be able to create more complex and maintainable projects.
127+
128+
**Tips for Organizing Our MicroPython Projects**
129+
130+
- **Modularize Code with Additional Scripts**: Store reusable or specific tasks in separate files, making `main.py` clean and focused on high-level logic.
131+
- **Leverage the `/lib` Folder**: Use the automatically created `/lib` folder to keep libraries organized and separate from your main application code.
132+
- **Edit and Manage Files Easily**: Arduino Labs for MicroPython’s file management tools make it easy to upload, download, and modify files on the device.
133+
134+
With a well-organized file system, we can streamline our development process and unlock new possibilities with MicroPython!
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: 'Modules'
3+
description: 'Understanding Modules and how to use them.'
4+
author: 'Pedro Lima'
5+
tags: [MicroPython, Modules]
6+
---
7+
8+
9+
In this article, we’ll cover how modules work in MicroPython, explore a few built-in modules, and demonstrate how to install an external package like Modulino to extend our MicroPython project’s functionality.
10+
11+
## What Are Modules?
12+
13+
Modules are collections of functions, classes, and variables organized into separate files, which we can import and use in our main program. Modules allow us to:
14+
15+
- **Reuse Code**: We can import useful functionality instead of writing everything from scratch.
16+
- **Organize Code**: Breaking code into modules makes projects more readable and maintainable.
17+
- **Access Special Functionality**: Some tasks require advanced functions (e.g., time delays or hardware communication) that are available only through modules.
18+
19+
## Built-in Modules
20+
21+
MicroPython comes with a set of built-in modules that provide essential functions for programming tasks. For instance:
22+
23+
- **`time`**: This module allows us to add time delays, get timestamps, and measure durations in code.
24+
- **`os`**: Provides functions to manage the file system, such as creating files or listing directories.
25+
- **`machine`**: A module designed for hardware interaction, allowing us to control pins, I2C, and more.
26+
27+
We can access built-in modules directly without any installation. Here’s a quick example using the `time` module to add a delay:
28+
29+
```python
30+
import time
31+
32+
print("Starting countdown...")
33+
time.sleep(3) # Pauses the program for 3 seconds
34+
print("Countdown complete!")
35+
```
36+
37+
In this example, `time.sleep()` introduces a delay. Built-in modules like `time` are pre-installed with MicroPython, so we don’t need to install anything extra to use them.
38+
39+
## External Modules
40+
41+
Some modules aren’t included with the default MicroPython installation and need to be installed separately. External modules, often provided by the community or specific hardware packages, extend MicroPython’s functionality. For example, the **Modulino** library is an external module that provides tools for working with Arduino Modulinos.
42+
43+
To demonstrate how to use external modules, we’ll go through the steps to install the Modulino package on an Arduino board.
44+
45+
### Step 1: Install MicroPython on Your Board
46+
47+
Before we can install external modules, we need to have MicroPython running on our board. Use the [MicroPython Installer](https://labs.arduino.cc/en/labs/micropython-installer) to install it:
48+
49+
- Open the installer.
50+
- Connect the board to your computer.
51+
- Press the "Refresh" button if the board does not appear.
52+
- Click "**Install MicroPython**" and wait for the installation to complete.
53+
54+
### Step 2: Install the Modulino Package
55+
56+
To install the Modulino package, we’ll use `mpremote`, a tool that allows us to install MicroPython packages directly onto our board from the command line.
57+
58+
1. **Install `mpremote`**: Make sure Python is installed on your computer, then open a terminal and type:
59+
60+
```bash
61+
pip install mpremote
62+
```
63+
64+
2. **Connect to Your Board**: Find your board’s serial port by running:
65+
66+
```bash
67+
mpremote connect list
68+
```
69+
70+
This command should return something like:
71+
72+
```bash
73+
/dev/cu.usbmodem101 ecda3b60a4dccb3f 2341:056b Arduino Nano ESP32
74+
```
75+
76+
- Copy the port, e.g., `/dev/cu.usbmodem101`.
77+
78+
3. **Install the Modulino Package**: Use the following command to install the Modulino package, replacing `<PORT>` with your board’s port:
79+
80+
```bash
81+
mpremote connect <PORT> mip install github:arduino/arduino-modulino-mpy
82+
```
83+
84+
4. **Verify Installation**: After installation, check Arduino Labs for MicroPython. You should see a `/lib` folder with the Modulino library inside, indicating a successful installation.
85+
86+
![MicroPython Lab Files](./assets/microPythonLabsFiles.png)
87+
88+
## Organizing and Using Modules
89+
90+
With the Modulino package installed, we’ll see a `/lib` folder in Arduino Labs, where MicroPython automatically stores external modules. This directory structure is commonly used for organizing additional libraries, making it easy to import and use custom functions in our main program.
91+
92+
### Using an External Module
93+
94+
To use a function from the Modulino library, simply import it in `main.py`:
95+
96+
```python
97+
from modulino import distance_sensor # Example function
98+
99+
# Example usage
100+
distance = distance_sensor.read_distance()
101+
print("Distance:", distance)
102+
```
103+
104+
This setup keeps `main.py` clean and makes it easy to incorporate external functionality without crowding the main script.
105+
106+
## Conclusion
107+
108+
MicroPython modules—whether built-in or external—allow us to keep our code organized, reduce duplication, and add powerful functionality to our projects. Some modules, like `time`, are included by default, while others, like the Modulino library, require installation.
109+
110+
- **Built-in Modules**: Modules like `time` and `machine` are part of MicroPython and need no installation.
111+
- **External Modules**: Packages like Modulino must be installed separately, typically into the `/lib` folder.
112+
- **Using Modules**: Once installed, we can import modules into our main program to extend functionality and keep our code modular.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Test
3+
description: Test
4+
---

content/micropython/03.micropython/modulinos/02.modulino-distance/modulino-distance.md

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ The goals of this tutorial are:
2020

2121
For this tutorial, we will need the following hardware:
2222
- [Modulino Distance](https://store.arduino.cc/products/plug-and-make-kit)
23-
- [Arduino Nano to QWIIC Connector Carrier]()
2423
- [Arduino Nano ESP32](https://store.arduino.cc/products/nano-esp32?queryID=undefined) or [Arduino Nano RP2040 Connect](https://store.arduino.cc/en-se/products/arduino-nano-rp2040-connect)
2524

2625
We will also need the following software:

content/micropython/03.micropython/modulinos/03.modulino-pixels/modulino-pixels.md

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ The goals of this tutorial are:
2020

2121
For this tutorial, we will need the following hardware:
2222
- [Modulino Pixels](https://store.arduino.cc/products/plug-and-make-kit)
23-
- [Arduino Nano to QWIIC Connector Carrier]()
2423
- [Arduino Nano ESP32](https://store.arduino.cc/products/nano-esp32?queryID=undefined) or [Arduino Nano RP2040 Connect](https://store.arduino.cc/en-se/products/arduino-nano-rp2040-connect)
2524

2625
We will also need the following software:

content/micropython/03.micropython/modulinos/04.modulino-buzzer/modulino-buzzer.md

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ The goals of this tutorial are:
2020

2121
For this tutorial, we will need the following hardware:
2222
- [Modulino Buzzer](https://store.arduino.cc/products/plug-and-make-kit)
23-
- [Arduino Nano to QWIIC Connector Carrier]()
2423
- [Arduino Nano ESP32](https://store.arduino.cc/products/nano-esp32?queryID=undefined) or [Arduino Nano RP2040 Connect](https://store.arduino.cc/en-se/products/arduino-nano-rp2040-connect)
2524

2625
We will also need the following software:

content/micropython/03.micropython/modulinos/05.modulino-buttons/modulino-buttons.md

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ The goals of this tutorial are:
2020

2121
For this tutorial, we will need the following hardware:
2222
- [Modulino Buttons](https://store.arduino.cc/products/plug-and-make-kit)
23-
- [Arduino Nano to QWIIC Connector Carrier]()
2423
- [Arduino Nano ESP32](https://store.arduino.cc/products/nano-esp32?queryID=undefined) or [Arduino Nano RP2040 Connect](https://store.arduino.cc/en-se/products/arduino-nano-rp2040-connect)
2524

2625
We will also need the following software:

content/micropython/03.micropython/modulinos/06.modulino-knob/modulino-knob.md

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ The goals of this tutorial are:
2020

2121
For this tutorial, we will need the following hardware:
2222
- [Modulino Knob](https://store.arduino.cc/products/plug-and-make-kit)
23-
- [Arduino Nano to QWIIC Connector Carrier]()
2423
- [Arduino Nano ESP32](https://store.arduino.cc/products/nano-esp32?queryID=undefined) or [Arduino Nano RP2040 Connect](https://store.arduino.cc/en-se/products/arduino-nano-rp2040-connect)
2524

2625
We will also need the following software:

content/micropython/03.micropython/modulinos/07.modulino-thermo/modulino-thermo.md

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ The goals of this tutorial are:
2020

2121
For this tutorial, we will need the following hardware:
2222
- [Modulino Thermo](https://store.arduino.cc/products/plug-and-make-kit)
23-
- [Arduino Nano to QWIIC Connector Carrier]()
2423
- [Arduino Nano ESP32](https://store.arduino.cc/products/nano-esp32?queryID=undefined) or [Arduino Nano RP2040 Connect](https://store.arduino.cc/en-se/products/arduino-nano-rp2040-connect)
2524

2625
We will also need the following software:

content/micropython/03.micropython/modulinos/08.modulino-movement/modulino-movement.md

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ The goals of this tutorial are:
2020

2121
For this tutorial, we will need the following hardware:
2222
- [Modulino Movement](https://store.arduino.cc/products/plug-and-make-kit)
23-
- [Arduino Nano to QWIIC Connector Carrier]()
2423
- [Arduino Nano ESP32](https://store.arduino.cc/products/nano-esp32?queryID=undefined) or [Arduino Nano RP2040 Connect](https://store.arduino.cc/en-se/products/arduino-nano-rp2040-connect)
2524

2625
We will also need the following software:

0 commit comments

Comments
 (0)