Skip to content

[PXCT-198] Review first steps. #2262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: 'Introduction to MicroPython'
description: 'Learn about the fundamentals of Micropython on Arduino boards.'
author: 'Pedro Lima'
hero_image: "./hero-banner.png"
tags: [MicroPython, Introduction]
---

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
---
featured: micropython-101
title: '1. Introduction to Arduino'
description: 'Learn about the Arduino platform'
author: 'Karl Söderby'
hero_image: "./hero-banner.png"
title: 'Installing MicroPython'
description: 'Learn how to install MicroPython on your Arduino board.'
author: 'Pedro Lima'
tags: [MicroPython, Installation]
---

# Installing MicroPython

In this article, we'll walk you through the process of installing MicroPython on your Arduino board. By the end of this guide, you (and your board) will be ready to write and run your first MicroPython script. Let's get started!
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 you begin, make sure you have the following:
Before we start, let's check the requirements:

### Supported Arduino Boards
MicroPython is officially supported on several Arduino boards. Here’s a list of compatible boards:
### 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)
Expand All @@ -35,50 +36,90 @@ MicroPython is officially supported on several Arduino boards. Here’s a list o


### Software Requirements
- **Arduino Labs for Micropython**: Ensure you have the [latest version](https://labs.arduino.cc/en/labs/micropython) of the IDE. (Optional)
- **MicroPython Firmware Installer**: [This installer](https://labs.arduino.cc/en/labs/micropython-installer) is needed to upload the MicroPython firmware onto your Arduino board. You can download it from the official Arduino website under the MicroPython section.

## How to setup your board
- [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. If you haven’t already, download the [Micropython Firmware Installer](https://labs.arduino.cc/en/labs/micropython-installer) and launch it.
2. Plug your board, it should be recognized by the installer.
![Arduino Nano ESP32 detected!](./assets/board-selected.png)
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/board-selected.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/flashed.png)
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/flashed.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

Run the downloaded file, and move it to your **"Applications"** folder.

![Installation MacOS.]()

### IDE setup
#### Windows

#### Offline IDE
Unzip the downloaded file, and run the executable file (`.exe`).

After downloading the [latest version](https://labs.arduino.cc/en/labs/micropython) of the IDE you can use it for the first time by:
- Unpack the compressed folder you downloaded.
- Execute "Arduino Lab for MicroPython.exe"
![Installation Windows]()

#### Online IDE
### Online Setup

Alternatively, you can use the IDE without installing it by using the online version available [here](https://lab-micropython.arduino.cc/).
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
## Connecting Board and IDE

1. Plug your Arduino board into your computer using a USB cable.
2. Press the connection button on the top left corner of the window. This will look like a plug and socket as in this image:
TODO: ADD IMAGE OF CONNECTION
3. Select the COM port where your board is connected.
At this point in the tutorial, we have
- Installed MicroPython on our board
- Downloaded and installed the editor (or opted for the online version).

You are now ready to upload your first sketch.
TODO: A 4 step screenshot to explain the processimage
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.]()
2. Press the connection button on the top left corner of the window.
![Connect the editor to the board.]()
3. The connected Arduino board should appear, and we can click it:
![Select board.]()

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 the following: `print("Hello World!")`
![Write hello world.]()
2. Click on the play symbol (RUN).
![Run the script.]()
3. 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.]()

## Troubleshooting

If you run into any issues during installation, here are some common problems and solutions:

### 1. **Board Not Detected**
- **Solution**: Ensure that your board is properly connected and the correct USB drivers are installed. Try using a different USB cable or port.
- **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

### 2. **Unable to Flash Firmware**
- **Solution**: Double-check that the MicroPython Firmware Installer was able to burn 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 serial port.
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 - Part Three]()


By following these steps, you should be able to successfully install MicroPython on your Arduino board and run your first script. Stay tuned for more tutorials to help you get the most out of MicroPython!
For information on how to uploadyour first sketch please check [](). TODO: We need to add here the guide for first sketch once it is linkable
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,51 @@
title: 'My First Script'
description: 'Learn how to write a basic MicroPython script to blink an LED.'
author: 'Pedro Lima'
hero_image: "./hero-banner.png"
tags: [MicroPython, Blink]
---

In this tutorial, we'll guide create our very first MicroPython script that will run on an Arduino board. We'll make an LED blink, a classic beginner project that introduces you to basic MicroPython programming concepts.
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.

## Hardware & Software Needed
## Requirements

For this tutorial, you will need a MicroPython compatible Arduino Board:
Before we start, let's check the requirements:

- [Arduino Nano 33 BLE]()
- [Arduino Nano ESP32]()
- [Arduino Nano RP2040 Connect]()
- [Arduino GIGA R1 WiFi]()
- [Arduino Portenta H7]()
- [Arduino Nicla Vision]()
### MicroPython Compatible Arduino Boards

You will also need the following software installed:
MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards:

- [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython).
- [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)
- [Opta](https://store.arduino.cc/products/opta)
- [Opta Wifi](https://store.arduino.cc/products/opta-wifi)
- [Opta RS485](https://store.arduino.cc/products/opta-rs485)
- [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/arduino-nano-33-ble-rev2)
- [Arduino Nano 33 BLE Sense](https://store.arduino.cc/products/arduino-nano-33-ble-sense)
- [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)

## Board and Editor Setup

Blinking an LED is a simple yet effective way to get started with MicroPython while still understanding how to control hardware using code.
### 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/)***

1. Connect your Arduino board to your computer via USB.
2. Open the Arduino Lab for MicroPython application.
3. Click on the **Connect** button, and select the board from the list.
## Board and Editor Setup

1. Open the [Arduino Lab for MicroPython]() application.
2. Plug the Arduino board into the computer using a USB cable.
![Connect board to computer.]()
3. Press the connection button on the top left corner of the window.
![Connect the editor to the board.]()
4. The connected Arduino board should appear, and we can click it:
![Select board.]()

***Need help installing MicroPython on your board? Visit the [MicroPython installation guide]().***

Expand All @@ -44,6 +62,9 @@ Once your board is connected, we can start writing code! Below you will find a b
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:
Expand All @@ -53,44 +74,19 @@ Once your board is connected, we can start writing code! Below you will find a b
time.sleep(1)
```

***Note: On some boards, the built-in LED might be on a different pin. For example, on the Arduino Nano RP2040 Connect, the built-in LED is on pin `25`. Check your board's documentation to confirm the correct pin number.***
***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** or **Upload** button in your editor to transfer the script to your board.
3. Click the **Run** button in your editor to transfer the script to your board.
![Run the script.]()

Once the script is running, the LED on your board should start blinking at one-second intervals. This means your MicroPython script has loaded successfully.
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.]()

## Programming Concepts Explained

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.
-t**ime.ticks_ms():** Returns the number of milliseconds(ms) since the board was last reset.
-**time.ticks_us()**: Returns the number of microseconds(us) since the board was last reset.
-In our script, ``time.sleep(1)`` pauses the program for one second. This creates a delay between turning the LED on and off, controlling the blink rate.

### `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.

### Code Breakdown

Let's take a look at the code, line by line, to understand what is happening:

- **Import Modules**:

```python
Expand Down Expand Up @@ -126,6 +122,31 @@ A `while True` loop creates an infinite loop, allowing the code inside it to run
- 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.
Expand All @@ -140,25 +161,25 @@ led = machine.Pin(25, machine.Pin.OUT)

while True:
led.value(1)
time.sleep(2)
time.sleep(0.5) # 0.5 seconds
led.value(0)
time.sleep(2)
time.sleep(0.5) # 0.5 seconds
```

### Steps

1. Change the `time.sleep(1)` lines to `time.sleep(2)`.
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 2 seconds.
3. Observe that the LED now blinks faster, turning on and off every 0.5 seconds.

## Conclusion
## Summary

Congratulations! You've written and modified your first MicroPython script on an Arduino board. This exercise introduced you to:
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 the operations you will be performing when writing your own programs and are present in the industry at large.
These concepts are key for a vast majoraty of operations we can perform while writing MicroPython programs.