Skip to content

Commit a6cb320

Browse files
authored
Merge pull request #2260 from arduino/content/micropython/micropython-revamp
[PXCT-198] MicroPython New Documentation
2 parents 2da5715 + 6687c73 commit a6cb320

File tree

150 files changed

+4451
-2738
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+4451
-2738
lines changed
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: 'Introduction to MicroPython'
3+
description: 'Learn about the fundamentals of Micropython on Arduino boards.'
4+
author: 'Pedro Lima'
5+
tags: [MicroPython, Introduction]
6+
micropython_type: test
7+
---
8+
9+
MicroPython is a lightweight implementation of Python 3 designed to run on microcontrollers and embedded systems. Think of it as a mini-version of Python, tailored for hardware with limited resources like memory and processing power. Despite its smaller size, MicroPython retains the simplicity and flexibility of Python, making it an excellent option for programming hardware.
10+
11+
## MicroPython on Arduino Boards
12+
13+
![MicroPython with Arduino](assets/micropython-arduino.png)
14+
15+
When using MicroPython on Arduino boards, the software is first installed on your Arduino. This allows the board to interpret and run Python code. Once MicroPython is installed on your board (don't worry, we'll cover this [here](/micropython/first-steps/install-guide)), you can start writing and executing Python scripts instantly.
16+
17+
Unlike traditional development approaches, where you compile code and then flash it to the board, with MicroPython you write Python scripts and run them instantly on your Arduino. This makes the development process much faster and more interactive.
18+
19+
## Running Programs in MicroPython
20+
21+
Once MicroPython is installed, you can start programming by writing scripts and uploading them to the board. These scripts are interpreted in real-time, meaning you can make quick changes and see immediate results, streamlining the development process.
22+
23+
![Running a script.](assets/run-script.gif)
24+
25+
MicroPython also includes a simple file system where your scripts are stored. For example, when you write a script, it is saved directly on the board and can be executed immediately without compiling. You can also save other scripts that can be activated from the main script!
26+
27+
### How it Works
28+
29+
The MicroPython installation includes several key components:
30+
31+
1. **File System**: MicroPython has a small file system built into the microcontroller. You can store Python scripts and configuration files on the board itself. Common files include:
32+
- `main.py`: This script runs automatically when the board boots up. It's where you can put the main logic of your program.
33+
- `boot.py`: This script runs before `main.py` and is often used for setting up configurations like WiFi connections or hardware initialization.
34+
35+
These files are fully editable, allowing you to control how your board starts and operates.
36+
37+
2. **Base Modules**: MicroPython comes with built-in modules for working with hardware like pins, sensors, and communication protocols (I2C, SPI, etc.). This includes essential modules like `machine`, `network`, and `time`.
38+
39+
## How to Program for MicroPython
40+
41+
Programming in MicroPython involves writing Python scripts in a text editor and then running them on your board. For this, we can use the [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython).
42+
43+
When writing MicroPython code, it's essential to think in terms of **modularity**. A good practice is to break down your code into smaller, reusable modules rather than writing everything in one large file. This approach makes it easier to manage and maintain code, especially for larger projects.
44+
45+
### Structuring Your Code
46+
47+
1. **Main Logic**: This goes into the `main.py` file. You can think of it as your "sketch" in Arduino terms.
48+
2. **Helper Modules**: Break down your code into smaller modules for specific tasks, such as controlling a sensor or managing a display. These modules can be imported into `main.py` as needed.
49+
3. **Interrupts and Background Tasks**: If you're dealing with hardware, you may also need to work with interrupts or periodic tasks, which can be handled in dedicated modules.
50+
51+
## MicroPython vs. C++ for Electronics Projects
52+
53+
MicroPython offers a different approach to programming compared to the traditional C++ used in Arduino development. Here are a few key comparisons:
54+
55+
- **Ease of Use**: Python’s syntax is generally more accessible for beginners. It is less verbose and easier to read, which can speed up the learning process.
56+
- **Real-Time Interactivity**: With MicroPython, you can write and test code interactively, without needing to compile. This makes it faster to experiment and troubleshoot hardware setups.
57+
- **Resource Efficiency**: C++ is more efficient in terms of memory and speed, making it a better option for projects that need to squeeze every bit of performance out of the hardware. MicroPython, on the other hand, prioritizes ease of development over raw performance, but it is still capable of handling many common hardware tasks.
58+
59+
## Summary
60+
61+
In summary, MicroPython provides a powerful and flexible way to develop electronic projects, especially for those familiar with Python. Its ability to run on microcontrollers like Arduino boards makes it an attractive option for both beginners and experienced developers who want a fast and efficient workflow.
Loading
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
featured: micropython-101
3+
title: 'Installing MicroPython'
4+
description: 'Learn how to install MicroPython on your Arduino board.'
5+
author: 'Pedro Lima'
6+
tags: [MicroPython, Installation]
7+
micropython_type: test
8+
---
9+
10+
# Installing MicroPython
11+
12+
In this article, we will go through the necessary tools needed to install and run MicroPython on an Arduino board. By the end of this guide, we will be ready to write and run our first MicroPython script. Let's get started!
13+
14+
## Requirements
15+
16+
Before we start, let's check the requirements:
17+
18+
### MicroPython Compatible Arduino Boards
19+
20+
MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards:
21+
22+
- [Portenta C33](https://store.arduino.cc/products/portenta-c33)
23+
- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi)
24+
- [Portenta H7](https://store.arduino.cc/products/portenta-h7)
25+
- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite)
26+
- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected)
27+
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect)
28+
- [Nicla Vision](https://store.arduino.cc/products/nicla-vision)
29+
- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble)
30+
- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/nano-33-ble-rev2)
31+
- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2)
32+
- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32)
33+
34+
35+
### Software Requirements
36+
37+
- [MicroPython Firmware Installer](https://labs.arduino.cc/en/labs/micropython-installer) - the firmware installer is needed to install MicroPython on our Arduino board.
38+
- [Arduino Lab for Micropython](https://labs.arduino.cc/en/labs/micropython) - Arduino Lab for MicroPython is an editor where we can create and run MicroPython scripts on our Arduino board.
39+
40+
***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)***
41+
42+
## Install MicroPython
43+
44+
1. First, download the [Micropython Firmware Installer](https://labs.arduino.cc/en/labs/micropython-installer) and launch it.
45+
2. Connect your board to your computer, it should be recognized by the installer.
46+
![Arduino Nano ESP32 detected!](./assets/select-board.png)
47+
3. Press **INSTALL MICROPYTHON**. A loading animation will appear.
48+
49+
Once the firmware is installed a "Installation successful" message will appear. At this point you can safely close the installer as your board is now ready for tinkering!
50+
![Firmware Successfully Uploaded!](./assets/installation-success.png)
51+
52+
## Editor Setup
53+
54+
After installing MicroPython on your board, we will need an editor to write code. We now have the option to choose the **offline** or **online** option.
55+
56+
### Offline Setup
57+
58+
First, download the latest version of [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) for your operative system. See instructions for MacOS / Windows below:
59+
60+
#### MacOS
61+
62+
Unzip the downloaded file, and run the application.
63+
64+
#### Windows
65+
66+
Unzip the downloaded file, and run the executable file (`.exe`).
67+
68+
### Online Setup
69+
70+
Alternatively, we can use the IDE without the need of installing anything. Simply visit the link below:
71+
- [Arduino Lab for MicroPython (online)](https://lab-micropython.arduino.cc/).
72+
73+
## Connecting Board and IDE
74+
75+
At this point in the tutorial, we have
76+
- Installed MicroPython on our board
77+
- Downloaded and installed the editor (or opted for the online version).
78+
79+
We will now try out running a script on the board, to make sure things are working properly.
80+
81+
1. Plug the Arduino board into the computer using a USB cable.
82+
![Connect board to computer.](assets/usb-comp.png)
83+
2. Press the connection button on the top left corner of the window. The connected Arduino board should appear (by its port name), and we can click it:
84+
![Connect to the board in the editor.](assets/select-board-ide.png)
85+
86+
We have now set up all necessary steps for running a script!
87+
88+
## Running a Test Script
89+
90+
With the installation and setup complete, let's try out running a very simple script: **Hello World!**
91+
92+
1. In the text area field of the editor, write `print("Hello World!")`, and then click on the play symbol (RUN).
93+
![Write hello world.](assets/write-code-run.png)
94+
2. After running it, you should see `Hello World!` in the black box. This is the board sending the **"Hello World!"** back to you, because the script is run on the board, not on the computer. This means everything is successful, and you are ready to start writing MicroPython scripts!
95+
![Hello world from the board.](assets/repl-print.png)
96+
97+
## Troubleshooting
98+
99+
If you run into any issues during installation, here are some common problems and solutions:
100+
101+
- **Board Not Detected** - if you cannot locate your board, ensure that your board is properly connected and the correct USB drivers are installed. Try using a different USB cable, or double tap the reset button on the board. If a light starts fading, it means it is in default mode, and is ready to be installed!
102+
103+
- **Unable to Install Firmware** - double-check that the MicroPython Firmware Installer was able to install the firmware and that your board is compatible (it will not show up on the installer if it is not). Also, verify that no other software is using the same serial port.
104+
105+
## Summary
106+
107+
By following these steps, you should be able to successfully install MicroPython on your Arduino board and run your first script.
108+
109+
### Next Steps
110+
111+
This tutorial is **Part Two** of the **"First Steps"** series. We recommend you following the next tutorial that will allow you to control the Arduino board's LED.
112+
- [First Steps - My First Script](/micropython/first-steps/first-script)
113+
114+
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
title: 'My First Script'
3+
description: 'Learn how to write a basic MicroPython script to blink an LED.'
4+
author: 'Pedro Lima'
5+
tags: [MicroPython, Blink]
6+
micropython_type: test
7+
---
8+
9+
In this tutorial, we will create our very first MicroPython script that will run on an Arduino board. Starting of simple, we will make an LED blink, a classic beginner project that will get us familiar with the MicroPython programming environment.
10+
11+
## Requirements
12+
13+
Before we start, let's check the requirements:
14+
15+
### MicroPython Compatible Arduino Boards
16+
17+
MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards:
18+
19+
- [Portenta C33](https://store.arduino.cc/products/portenta-c33)
20+
- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi)
21+
- [Portenta H7](https://store.arduino.cc/products/portenta-h7)
22+
- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite)
23+
- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected)
24+
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect)
25+
- [Nicla Vision](https://store.arduino.cc/products/nicla-vision)
26+
- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble)
27+
- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/nano-33-ble-rev2)
28+
- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2)
29+
- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32)
30+
31+
32+
### Software Requirements
33+
34+
- [Arduino Lab for Micropython](https://labs.arduino.cc/en/labs/micropython) - Arduino Lab for MicroPython is an editor where we can create and run MicroPython scripts on our Arduino board.
35+
36+
***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)***
37+
38+
## Board and Editor Setup
39+
40+
1. Open the [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) application.
41+
2. Plug the Arduino board into the computer using a USB cable.
42+
![Connect board to computer.](assets/usb-comp.png)
43+
3. Press the connection button on the top left corner of the window. The connected Arduino board should appear (by its port name), and we can click it:
44+
![Connect to the board in the editor.](assets/select-board-ide.png)
45+
46+
***Need help installing MicroPython on your board? Visit the [MicroPython installation guide](/micropython/first-steps/install-guide).***
47+
48+
## First Script (LED Blink)
49+
50+
Once your board is connected, we can start writing code! Below you will find a basic example, that will flash the built in LED on your board every second.
51+
52+
1. First, open the `main.py` file on your board. We write in this file, because once saved, the code will run even if you reset the board.
53+
![Open main.py file.](assets/open-files.png)
54+
55+
2. Copy and paste the following code into your editor:
56+
```python
57+
import machine
58+
import time
59+
60+
# The pin used for built-in LED varies
61+
# GIGA: 0 (green), Nano ESP32: 0 (green), Nano RP2040 Connect: 25
62+
# Nano BLE Sense: 13
63+
led = machine.Pin(25, machine.Pin.OUT)
64+
65+
while True:
66+
led.value(1)
67+
time.sleep(1)
68+
led.value(0)
69+
time.sleep(1)
70+
```
71+
72+
***Note: The built-in LED pin varies from board to board. For example, on the Arduino Nano RP2040 Connect, the built-in LED is on pin `25`.***
73+
74+
3. Click the **Run** button in your editor to transfer the script to your board.
75+
![Run the script.](assets/run-script.png)
76+
77+
Once the script is running, the LED on the board should start blinking at one-second intervals. This means our MicroPython script has loaded successfully.
78+
79+
![LED blinking on your board.](assets/blink.gif)
80+
81+
### Code Breakdown
82+
83+
Let's take a look at the code, line by line, to understand what is happening:
84+
85+
- **Import Modules**:
86+
87+
```python
88+
import machine
89+
import time
90+
```
91+
92+
We import the `machine` and `time` modules to access hardware functions and time delays.
93+
94+
- **Initialize the LED Pin**:
95+
96+
```python
97+
led = machine.Pin(25, machine.Pin.OUT)
98+
```
99+
100+
We create a `Pin` object named `led`, set to pin number `25`, and configure it as an output.
101+
102+
- **Infinite Loop**:
103+
104+
```python
105+
while True:
106+
led.value(1)
107+
time.sleep(1)
108+
led.value(0)
109+
time.sleep(1)
110+
```
111+
112+
Inside the loop, we:
113+
114+
- Turn the LED on by setting its value to `1`.
115+
- Wait for 1 second.
116+
- Turn the LED off by setting its value to `0`.
117+
- Wait for another second.
118+
- Repeat the cycle.
119+
120+
## Understanding Programming Concepts
121+
122+
Let's break down the key programming concepts used in this script:
123+
124+
### `machine` Module
125+
126+
The machine module is a built-in MicroPython library that provides direct access to your board's hardware components. It allows you to control and interact with the microcontroller's features, such as:
127+
128+
- **Pins:** Configure and control digital and analog pins.
129+
- **Timers:** Set up timers for scheduling tasks.
130+
- **Communication Interfaces:** Use protocols like I2C, SPI, and UART.
131+
- **Hardware-Specific Functions:** Access features unique to your microcontroller.
132+
133+
In our script, we use the `machine.Pin` class to interact with a specific pin on the board. By creating a `Pin` object, we can control the voltage level of that pin, which in turn controls the LED.
134+
135+
### `time` Module
136+
137+
The time module provides functions for managing time-related tasks. It allows you to add delays, measure time intervals, and schedule events. Key functions include:
138+
139+
- `time.sleep(seconds)` Pauses the execution of your script for the specified number of seconds. It accepts floating-point numbers for sub-second delays.
140+
141+
### `while True` Loop
142+
143+
A `while True` loop creates an infinite loop, allowing the code inside it to run repeatedly. This is essential for tasks that need to run continuously, like blinking an LED.
144+
145+
## Modification: Make the LED Blink Faster
146+
147+
Let's modify the script to make the LED blink faster. We'll change the delay from 1 second to 0.2 seconds.
148+
149+
### Modified Code
150+
151+
```python
152+
import machine
153+
import time
154+
155+
led = machine.Pin(25, machine.Pin.OUT)
156+
157+
while True:
158+
led.value(1)
159+
time.sleep(0.5) # 0.5 seconds
160+
led.value(0)
161+
time.sleep(0.5) # 0.5 seconds
162+
```
163+
164+
### Steps
165+
166+
1. Change the `time.sleep(1)` lines to `time.sleep(0.5)`.
167+
2. Upload the modified script to your board.
168+
3. Observe that the LED now blinks faster, turning on and off every 0.5 seconds.
169+
170+
## Summary
171+
172+
Great work! We have now written and modified our first MicroPython script on an Arduino board. This exercise introduced:
173+
174+
- Importing modules (`machine`, `time`)
175+
- Initializing hardware components (LED)
176+
- Using loops (`while`)
177+
- Controlling time delays (`time.sleep()`)
178+
179+
These concepts are key for a vast majoraty of operations we can perform while writing MicroPython programs.
180+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: First Steps
3+
---
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)