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
Lines changed: 61 additions & 0 deletions
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
Lines changed: 114 additions & 0 deletions
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+

0 commit comments

Comments
 (0)