Skip to content
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
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.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ MicroPython is a lightweight implementation of Python 3 designed to run on micro

## MicroPython on Arduino Boards

![Placeholder graphic]()
![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]()), you can start writing and executing Python scripts instantly.

Expand All @@ -20,7 +20,7 @@ Unlike traditional development approaches, where you compile code and then flash

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 immediate change]()
![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!

Expand All @@ -36,8 +36,6 @@ 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.]()

## 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]().
Expand All @@ -50,8 +48,6 @@ When writing MicroPython code, it's essential to think in terms of **modularity*
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.]()

## 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:
Expand All @@ -60,6 +56,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.]()
## 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 not shown.
Binary file not shown.
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.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ MicroPython is officially supported on several Arduino boards. Here’s a list o

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)
![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/flashed.png)
![Firmware Successfully Uploaded!](./assets/installation-success.png)

## Editor Setup

Expand All @@ -63,20 +63,16 @@ First, download the latest version of [Arduino Lab for MicroPython](https://labs

#### MacOS

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

![Installation MacOS.]()
Unzip the downloaded file, and run the application.

#### Windows

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

![Installation Windows]()

### 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/).
- [Arduino Lab for MicroPython (online)](https://lab-micropython.arduino.cc/).

## Connecting Board and IDE

Expand All @@ -87,24 +83,20 @@ At this point in the tutorial, we have
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.]()
![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 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.]()
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

Expand Down
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.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ MicroPython is officially supported on several Arduino boards. Here’s a list o

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.]()
![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]().***

Expand All @@ -56,7 +54,7 @@ MicroPython is officially supported on several Arduino boards. Here’s a list o
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.]()
![Open main.py file.](assets/open-files.png)

2. Copy and paste the following code into your editor:
```python
Expand All @@ -78,11 +76,11 @@ Once your board is connected, we can start writing code! Below you will find a b
***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.]()
![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.]()
![LED blinking on your board.](assets/blink.gif)

### Code Breakdown

Expand Down
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.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ In this guide, we will be using some additional electronic components:

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.]()
![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]().***

Expand All @@ -83,7 +81,7 @@ Connect an LED to the Arduino board, following the circuit diagram below:
- Connect the anode (+) of the LED to a digital output pin.
- Connect the cathode (-) of the LED through a resistor to `GND`.

![LED circuit.]()
![LED circuit.](assets/LED.png)

***You can also use the built-in LED on your board, if you do not have an external LED.***

Expand Down Expand Up @@ -133,16 +131,14 @@ When a digital input pin is not connected to a definite HIGH or LOW voltage, it

These internal resistors are built into the microcontroller and can be enabled in your code, eliminating the need for external resistors.

![We can create a image here to explain that]()

### Example: Pull-Up Mode

In pull-up mode, the input pin is internally connected to a HIGH voltage level. When the input device (like a button) is activated and connects the pin to GND, the pin reads LOW (`0`).

- Connect one side of the button to **GND**.
- Connect the other side to a digital input pin.

![Pull-up mode circuit.]()
![Pull-up mode circuit.](assets/pull-up.png)

After completing the circuit diagram, copy the following code into your editor, and run the script.

Expand Down Expand Up @@ -181,7 +177,7 @@ In pull-down mode, the input pin is internally connected to GND. When the input
- Connect one side of the button to **3.3V** (or **5V**, depending on your board's logic level).
- Connect the other side to a digital input pin.

![Pull-down mode circuit.]()
![Pull-down mode circuit.](assets/pull-down.png)

After completing the circuit diagram, copy the following code into your editor, and run the script.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ In this guide, we will be using some additional electronic components:

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.]()
![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]().***

Expand Down Expand Up @@ -87,7 +85,7 @@ For this example, we will need the following external components:

Connect the photoresistor to the Arduino board, following the circuit diagram below:

![Photoresistor circuit.]()
![Photoresistor circuit.](assets/photoresistor.png)

After completing the circuit diagram, copy the following code into your editor, and run the script.

Expand Down Expand Up @@ -126,7 +124,7 @@ PWM is especially useful in applications where true analog output is not possibl

The main advantage of PWM is that it allows you to control analog-like behavior using digital pins, adding versatility to your projects while keeping power consumption efficient.

![How PWM works.]()
![How PWM works.](assets/pwm.gif)

### Code Example: Dimming an LED with PWM

Expand All @@ -141,7 +139,7 @@ For this example, we will need the following external components:

Connect the LED to the Arduino board, following the circuit diagram below:

![Photoresistor circuit.]()
![LED circuit.](assets/LED.png)

After completing the circuit diagram, copy the following code into your editor, and run the script.

Expand Down
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.
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.
11 changes: 3 additions & 8 deletions content/micropython/03.micropython/01.basics/02. loops/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ MicroPython is officially supported on several Arduino boards. Here’s a list o

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.]()
![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]().***

Expand All @@ -63,11 +61,8 @@ To better understand these loops, let’s imagine them as tasks at the supermark

- **for loops** - imagine walking down a supermarket aisle with a shopping list that specifies exactly how many items to pick up, one by one, in order. Once you’ve gathered all the items on your list, your task is complete. This is like a `for` loop iterating over a sequence, handling each specified item one at a time.

![How for loops work.]()

- **while loops** - imagine going to the supermarket to buy a certain product that’s on sale, as long as it stays in stock. You keep coming back, day after day, until the sale ends or the stock runs out. In a `while` loop, you keep “coming back” as long as a condition (like the sale continuing) remains true.

![How while loops work.]()

## For Loops

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ MicroPython is officially supported on several Arduino boards. Here’s a list o

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.]()
![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]().***

Expand Down
Loading