Skip to content

Commit 47f140b

Browse files
Added Module information
1 parent cdbd551 commit 47f140b

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
72.4 KB
Loading
Lines changed: 112 additions & 0 deletions
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.

0 commit comments

Comments
 (0)