Skip to content

[PXCT-34] MicroPython review ch01 + ch03 #2242

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
@@ -1,28 +1,27 @@
---
featured: micropython
title: '0. Introduction to MicroPython'
description: 'Learn about Micropython'
title: 'Introduction to MicroPython'
description: 'Learn about the fundamentals of Micropython on Arduino boards.'
author: 'Pedro Lima'
hero_image: "./hero-banner.png"
---

# Intro to MicroPython

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

When using MicroPython on Arduino boards, the language is installed directly onto the microcontroller. This allows the board to interpret and run Python code. Once MicroPython is installed on your board (don't worry, we'll cover installation in a future article), you can start writing and executing Python scripts instantly.
![Placeholder graphic]()

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]()), 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 load them directly onto the microcontroller. This makes the development process much faster and more interactive.
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.

TODO: Image of code edit with imediate change
![TODO: Image of code edit with immediate change]()

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

Expand All @@ -36,21 +35,21 @@ The MicroPython installation includes several key components:

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

TODO: A diagram showing how `boot.py` and `main.py` are loaded during the boot process and how they fit into the file system could be useful here.
![TODO: A diagram showing how `boot.py` and `main.py` are loaded during the boot process and how they fit into the file system could be useful here.]()

## How to Program for MicroPython

Programming in MicroPython involves writing Python scripts in a text editor and uploading them to the board. Many IDEs, like Thonny or even Arduino IDE, provide support for MicroPython, allowing you to write and upload code easily.
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]().

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 would typically go into `main.py`. You can think of it as your "sketch" in Arduino terms.
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.

TODO: A flowchart here could be beneficial to illustrate how to structure a MicroPython project, with `main.py` at the top and helper modules branching off.
![TODO: A flowchart here could be beneficial to illustrate how to structure a MicroPython project, with `main.py` at the top and helper modules branching off.]()

## MicroPython vs. C++ for Electronics Projects

Expand All @@ -60,6 +59,6 @@ MicroPython offers a different approach to programming compared to the tradition
- **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.

TODO: A side-by-side table comparing typical tasks (like reading a sensor or blinking an LED) in MicroPython and C++ could help illustrate the differences.
![TODO: A side-by-side table comparing typical tasks (like reading a sensor or blinking an LED) in MicroPython and C++ could help illustrate the differences.]()

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.
Original file line number Diff line number Diff line change
@@ -1,71 +1,65 @@
---
featured: micropython-101
title: '1. Installing micropython'
description: 'Lern how to setup MicroPython'
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"
---

# My First Script
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 article, we'll guide you through creating your first MicroPython script on your Arduino board. We'll make an LED blink. A classic beginner project that introduces you to basic programming concepts in MicroPython.
## Hardware & Software Needed

## Requirements
For this tutorial, you will need a MicroPython compatible Arduino Board:

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

- **Arduino Boards Compatible with MicroPython**:
- Arduino Nano 33 BLE
- Arduino Nano 33 IoT
- Arduino Nano RP2040 Connect
- Arduino Portenta H7
- Arduino Nicla Vision
You will also need the following software installed:

### Software (Editor)
- [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython).

- **MicroPython-Compatible Editor**:
- [Arduino IDE with MicroPython Support](https://www.arduino.cc/en/software).

## Introducing the Example: Blinking an LED
## 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.

## Step-by-Step Guide

### 1. Open Your Editor

Launch your MicroPython-compatible editor (e.g., Arduino IDE or Thonny IDE).
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.

### 2. Connect Your Board
***Need help installing MicroPython on your board? Visit the [MicroPython installation guide]().***

Ensure your Arduino board is connected to your computer via USB.
## First Script (LED Blink)

### 3. Write the Code
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.

Copy and paste the following code into your editor:
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.]()

```python
import machine
import time

led = machine.Pin(25, machine.Pin.OUT)
2. Copy and paste the following code into your editor:
```python
import machine
import time

while True:
led.value(1)
time.sleep(1)
led.value(0)
time.sleep(1)
```
led = machine.Pin(25, machine.Pin.OUT)

**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.
while True:
led.value(1)
time.sleep(1)
led.value(0)
time.sleep(1)
```

### 4. Run the Script
***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.***

Click the **Run** or **Upload** button in your editor to transfer the script to your board.
3. Click the **Run** or **Upload** button in your editor to transfer the script to your board.

### 5. Observe the LED
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 your board should start blinking at one-second intervals.
![LED blinking on your board.]()

## Programming Concepts Explained

Expand Down Expand Up @@ -159,12 +153,12 @@ while True:

## Conclusion

Congratulations! You've written and modified your first MicroPython script on an Arduino board. This simple exercise introduced you to:
Congratulations! You've written and modified your first MicroPython script on an Arduino board. This exercise introduced you to:

- Importing modules
- Initializing hardware components
- Using loops
- Controlling time delays
- Importing modules (`machine`, `time`)
- Initializing hardware components (LED)
- Using loops (`while`)
- Controlling time delays (`time.sleep()`)

Although simple 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 the operations you will be performing when writing your own programs and are present in the industry at large.