-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2260 from arduino/content/micropython/micropython…
…-revamp [PXCT-198] MicroPython New Documentation
- Loading branch information
Showing
150 changed files
with
4,451 additions
and
2,738 deletions.
There are no files selected for viewing
Binary file added
BIN
+764 KB
.../micropython/00.first-steps/00.intro-micropython/assets/micropython-arduino.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+736 KB
content/micropython/00.first-steps/00.intro-micropython/assets/run-script.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions
61
content/micropython/00.first-steps/00.intro-micropython/intro-micropython.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: 'Introduction to MicroPython' | ||
description: 'Learn about the fundamentals of Micropython on Arduino boards.' | ||
author: 'Pedro Lima' | ||
tags: [MicroPython, Introduction] | ||
micropython_type: test | ||
--- | ||
|
||
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. | ||
|
||
## MicroPython on Arduino Boards | ||
|
||
![MicroPython with Arduino](assets/micropython-arduino.png) | ||
|
||
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. | ||
|
||
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. | ||
|
||
## Running Programs in MicroPython | ||
|
||
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. | ||
|
||
![Running a script.](assets/run-script.gif) | ||
|
||
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! | ||
|
||
### How it Works | ||
|
||
The MicroPython installation includes several key components: | ||
|
||
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: | ||
- `main.py`: This script runs automatically when the board boots up. It's where you can put the main logic of your program. | ||
- `boot.py`: This script runs before `main.py` and is often used for setting up configurations like WiFi connections or hardware initialization. | ||
|
||
These files are fully editable, allowing you to control how your board starts and operates. | ||
|
||
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`. | ||
|
||
## How to Program for MicroPython | ||
|
||
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). | ||
|
||
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. | ||
|
||
### Structuring Your Code | ||
|
||
1. **Main Logic**: This goes into the `main.py` file. You can think of it as your "sketch" in Arduino terms. | ||
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. | ||
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. | ||
|
||
## MicroPython vs. C++ for Electronics Projects | ||
|
||
MicroPython offers a different approach to programming compared to the traditional C++ used in Arduino development. Here are a few key comparisons: | ||
|
||
- **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. | ||
- **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. | ||
- **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. | ||
|
||
## Summary | ||
|
||
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. |
Binary file added
BIN
+495 KB
...ent/micropython/00.first-steps/01.install-guide/assets/installation-success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+173 KB
content/micropython/00.first-steps/01.install-guide/assets/repl-print.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+216 KB
content/micropython/00.first-steps/01.install-guide/assets/select-board-ide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+511 KB
content/micropython/00.first-steps/01.install-guide/assets/select-board.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.8 KB
content/micropython/00.first-steps/01.install-guide/assets/usb-comp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+186 KB
content/micropython/00.first-steps/01.install-guide/assets/write-code-run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions
114
content/micropython/00.first-steps/01.install-guide/installing-micropython.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
--- | ||
featured: micropython-101 | ||
title: 'Installing MicroPython' | ||
description: 'Learn how to install MicroPython on your Arduino board.' | ||
author: 'Pedro Lima' | ||
tags: [MicroPython, Installation] | ||
micropython_type: test | ||
--- | ||
|
||
# Installing MicroPython | ||
|
||
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! | ||
|
||
## Requirements | ||
|
||
Before we start, let's check the requirements: | ||
|
||
### MicroPython Compatible Arduino Boards | ||
|
||
MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards: | ||
|
||
- [Portenta C33](https://store.arduino.cc/products/portenta-c33) | ||
- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi) | ||
- [Portenta H7](https://store.arduino.cc/products/portenta-h7) | ||
- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite) | ||
- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected) | ||
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect) | ||
- [Nicla Vision](https://store.arduino.cc/products/nicla-vision) | ||
- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble) | ||
- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/nano-33-ble-rev2) | ||
- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2) | ||
- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32) | ||
|
||
|
||
### Software Requirements | ||
|
||
- [MicroPython Firmware Installer](https://labs.arduino.cc/en/labs/micropython-installer) - the firmware installer is needed to install MicroPython on our Arduino board. | ||
- [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. | ||
|
||
***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)*** | ||
|
||
## Install MicroPython | ||
|
||
1. First, download the [Micropython Firmware Installer](https://labs.arduino.cc/en/labs/micropython-installer) and launch it. | ||
2. Connect your board to your computer, it should be recognized by the installer. | ||
![Arduino Nano ESP32 detected!](./assets/select-board.png) | ||
3. Press **INSTALL MICROPYTHON**. A loading animation will appear. | ||
|
||
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! | ||
![Firmware Successfully Uploaded!](./assets/installation-success.png) | ||
|
||
## Editor Setup | ||
|
||
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. | ||
|
||
### Offline Setup | ||
|
||
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: | ||
|
||
#### MacOS | ||
|
||
Unzip the downloaded file, and run the application. | ||
|
||
#### Windows | ||
|
||
Unzip the downloaded file, and run the executable file (`.exe`). | ||
|
||
### Online Setup | ||
|
||
Alternatively, we can use the IDE without the need of installing anything. Simply visit the link below: | ||
- [Arduino Lab for MicroPython (online)](https://lab-micropython.arduino.cc/). | ||
|
||
## Connecting Board and IDE | ||
|
||
At this point in the tutorial, we have | ||
- Installed MicroPython on our board | ||
- Downloaded and installed the editor (or opted for the online version). | ||
|
||
We will now try out running a script on the board, to make sure things are working properly. | ||
|
||
1. Plug the Arduino board into the computer using a USB cable. | ||
![Connect board to computer.](assets/usb-comp.png) | ||
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: | ||
![Connect to the board in the editor.](assets/select-board-ide.png) | ||
|
||
We have now set up all necessary steps for running a script! | ||
|
||
## Running a Test Script | ||
|
||
With the installation and setup complete, let's try out running a very simple script: **Hello World!** | ||
|
||
1. In the text area field of the editor, write `print("Hello World!")`, and then click on the play symbol (RUN). | ||
![Write hello world.](assets/write-code-run.png) | ||
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! | ||
![Hello world from the board.](assets/repl-print.png) | ||
|
||
## Troubleshooting | ||
|
||
If you run into any issues during installation, here are some common problems and solutions: | ||
|
||
- **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! | ||
|
||
- **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. | ||
|
||
## Summary | ||
|
||
By following these steps, you should be able to successfully install MicroPython on your Arduino board and run your first script. | ||
|
||
### Next Steps | ||
|
||
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. | ||
- [First Steps - My First Script](/micropython/first-steps/first-script) | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+251 KB
content/micropython/00.first-steps/02.first-script/assets/open-files.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+50.2 KB
content/micropython/00.first-steps/02.first-script/assets/run-script.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+216 KB
content/micropython/00.first-steps/02.first-script/assets/select-board-ide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.8 KB
content/micropython/00.first-steps/02.first-script/assets/usb-comp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
180 changes: 180 additions & 0 deletions
180
content/micropython/00.first-steps/02.first-script/first-script.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
--- | ||
title: 'My First Script' | ||
description: 'Learn how to write a basic MicroPython script to blink an LED.' | ||
author: 'Pedro Lima' | ||
tags: [MicroPython, Blink] | ||
micropython_type: test | ||
--- | ||
|
||
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. | ||
|
||
## Requirements | ||
|
||
Before we start, let's check the requirements: | ||
|
||
### MicroPython Compatible Arduino Boards | ||
|
||
MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards: | ||
|
||
- [Portenta C33](https://store.arduino.cc/products/portenta-c33) | ||
- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi) | ||
- [Portenta H7](https://store.arduino.cc/products/portenta-h7) | ||
- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite) | ||
- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected) | ||
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect) | ||
- [Nicla Vision](https://store.arduino.cc/products/nicla-vision) | ||
- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble) | ||
- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/nano-33-ble-rev2) | ||
- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2) | ||
- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32) | ||
|
||
|
||
### Software Requirements | ||
|
||
- [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. | ||
|
||
***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)*** | ||
|
||
## Board and Editor Setup | ||
|
||
1. Open the [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) application. | ||
2. Plug the Arduino board into the computer using a USB cable. | ||
![Connect board to computer.](assets/usb-comp.png) | ||
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: | ||
![Connect to the board in the editor.](assets/select-board-ide.png) | ||
|
||
***Need help installing MicroPython on your board? Visit the [MicroPython installation guide](/micropython/first-steps/install-guide).*** | ||
|
||
## First Script (LED Blink) | ||
|
||
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. | ||
|
||
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. | ||
![Open main.py file.](assets/open-files.png) | ||
|
||
2. Copy and paste the following code into your editor: | ||
```python | ||
import machine | ||
import time | ||
|
||
# The pin used for built-in LED varies | ||
# GIGA: 0 (green), Nano ESP32: 0 (green), Nano RP2040 Connect: 25 | ||
# Nano BLE Sense: 13 | ||
led = machine.Pin(25, machine.Pin.OUT) | ||
|
||
while True: | ||
led.value(1) | ||
time.sleep(1) | ||
led.value(0) | ||
time.sleep(1) | ||
``` | ||
|
||
***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`.*** | ||
|
||
3. Click the **Run** button in your editor to transfer the script to your board. | ||
![Run the script.](assets/run-script.png) | ||
|
||
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. | ||
|
||
![LED blinking on your board.](assets/blink.gif) | ||
|
||
### Code Breakdown | ||
|
||
Let's take a look at the code, line by line, to understand what is happening: | ||
|
||
- **Import Modules**: | ||
|
||
```python | ||
import machine | ||
import time | ||
``` | ||
|
||
We import the `machine` and `time` modules to access hardware functions and time delays. | ||
|
||
- **Initialize the LED Pin**: | ||
|
||
```python | ||
led = machine.Pin(25, machine.Pin.OUT) | ||
``` | ||
|
||
We create a `Pin` object named `led`, set to pin number `25`, and configure it as an output. | ||
|
||
- **Infinite Loop**: | ||
|
||
```python | ||
while True: | ||
led.value(1) | ||
time.sleep(1) | ||
led.value(0) | ||
time.sleep(1) | ||
``` | ||
|
||
Inside the loop, we: | ||
|
||
- Turn the LED on by setting its value to `1`. | ||
- Wait for 1 second. | ||
- Turn the LED off by setting its value to `0`. | ||
- Wait for another second. | ||
- Repeat the cycle. | ||
|
||
## Understanding Programming Concepts | ||
|
||
Let's break down the key programming concepts used in this script: | ||
|
||
### `machine` Module | ||
|
||
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: | ||
|
||
- **Pins:** Configure and control digital and analog pins. | ||
- **Timers:** Set up timers for scheduling tasks. | ||
- **Communication Interfaces:** Use protocols like I2C, SPI, and UART. | ||
- **Hardware-Specific Functions:** Access features unique to your microcontroller. | ||
|
||
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. | ||
|
||
### `time` Module | ||
|
||
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: | ||
|
||
- `time.sleep(seconds)` Pauses the execution of your script for the specified number of seconds. It accepts floating-point numbers for sub-second delays. | ||
|
||
### `while True` Loop | ||
|
||
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. | ||
|
||
## Modification: Make the LED Blink Faster | ||
|
||
Let's modify the script to make the LED blink faster. We'll change the delay from 1 second to 0.2 seconds. | ||
|
||
### Modified Code | ||
|
||
```python | ||
import machine | ||
import time | ||
|
||
led = machine.Pin(25, machine.Pin.OUT) | ||
|
||
while True: | ||
led.value(1) | ||
time.sleep(0.5) # 0.5 seconds | ||
led.value(0) | ||
time.sleep(0.5) # 0.5 seconds | ||
``` | ||
|
||
### Steps | ||
|
||
1. Change the `time.sleep(1)` lines to `time.sleep(0.5)`. | ||
2. Upload the modified script to your board. | ||
3. Observe that the LED now blinks faster, turning on and off every 0.5 seconds. | ||
|
||
## Summary | ||
|
||
Great work! We have now written and modified our first MicroPython script on an Arduino board. This exercise introduced: | ||
|
||
- Importing modules (`machine`, `time`) | ||
- Initializing hardware components (LED) | ||
- Using loops (`while`) | ||
- Controlling time delays (`time.sleep()`) | ||
|
||
These concepts are key for a vast majoraty of operations we can perform while writing MicroPython programs. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
title: First Steps | ||
--- |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+216 KB
content/micropython/01.basics/00.digital-io/assets/select-board-ide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.