Skip to content

Commit 0904d57

Browse files
authored
Merge pull request #2242 from arduino/karlsoderby/rev-micropython-firststeps
MicroPython review ch01 + ch03
2 parents d0182e8 + 05eee48 commit 0904d57

File tree

2 files changed

+56
-63
lines changed

2 files changed

+56
-63
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
---
2-
featured: micropython
3-
title: '0. Introduction to MicroPython'
4-
description: 'Learn about Micropython'
2+
title: 'Introduction to MicroPython'
3+
description: 'Learn about the fundamentals of Micropython on Arduino boards.'
54
author: 'Pedro Lima'
65
hero_image: "./hero-banner.png"
76
---
87

9-
# Intro to MicroPython
10-
118
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.
129

1310
## MicroPython on Arduino Boards
1411

15-
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.
12+
![Placeholder graphic]()
13+
14+
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.
1615

17-
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.
16+
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.
1817

1918
## Running Programs in MicroPython
2019

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

23-
TODO: Image of code edit with imediate change
22+
![TODO: Image of code edit with immediate change]()
2423

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.
24+
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!
2625

2726
### How it Works
2827

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

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

39-
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.
38+
![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.]()
4039

4140
## How to Program for MicroPython
4241

43-
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.
42+
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]().
4443

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

4746
### Structuring Your Code
4847

49-
1. **Main Logic**: This would typically go into `main.py`. You can think of it as your "sketch" in Arduino terms.
48+
1. **Main Logic**: This goes into the `main.py` file. You can think of it as your "sketch" in Arduino terms.
5049
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.
5150
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.
5251

53-
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.
52+
![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.]()
5453

5554
## MicroPython vs. C++ for Electronics Projects
5655

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

63-
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.
62+
![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.]()
6463

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

content/micropython/03.micropython/00.first-steps/02.first-sketch/first-sketch.md

+43-49
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,65 @@
11
---
2-
featured: micropython-101
3-
title: '1. Installing micropython'
4-
description: 'Lern how to setup MicroPython'
2+
title: 'My First Script'
3+
description: 'Learn how to write a basic MicroPython script to blink an LED.'
54
author: 'Pedro Lima'
65
hero_image: "./hero-banner.png"
76
---
87

9-
# My First Script
8+
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.
109

11-
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.
10+
## Hardware & Software Needed
1211

13-
## Requirements
12+
For this tutorial, you will need a MicroPython compatible Arduino Board:
1413

15-
### Hardware Boards
14+
- [Arduino Nano 33 BLE]()
15+
- [Arduino Nano ESP32]()
16+
- [Arduino Nano RP2040 Connect]()
17+
- [Arduino GIGA R1 WiFi]()
18+
- [Arduino Portenta H7]()
19+
- [Arduino Nicla Vision]()
1620

17-
- **Arduino Boards Compatible with MicroPython**:
18-
- Arduino Nano 33 BLE
19-
- Arduino Nano 33 IoT
20-
- Arduino Nano RP2040 Connect
21-
- Arduino Portenta H7
22-
- Arduino Nicla Vision
21+
You will also need the following software installed:
2322

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

26-
- **MicroPython-Compatible Editor**:
27-
- [Arduino IDE with MicroPython Support](https://www.arduino.cc/en/software).
28-
29-
## Introducing the Example: Blinking an LED
25+
## Board and Editor Setup
3026

3127
Blinking an LED is a simple yet effective way to get started with MicroPython while still understanding how to control hardware using code.
3228

33-
## Step-by-Step Guide
34-
35-
### 1. Open Your Editor
36-
37-
Launch your MicroPython-compatible editor (e.g., Arduino IDE or Thonny IDE).
29+
1. Connect your Arduino board to your computer via USB.
30+
2. Open the Arduino Lab for MicroPython application.
31+
3. Click on the **Connect** button, and select the board from the list.
3832

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

41-
Ensure your Arduino board is connected to your computer via USB.
35+
## First Script (LED Blink)
4236

43-
### 3. Write the Code
37+
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.
4438

45-
Copy and paste the following code into your editor:
39+
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.
40+
![Open main.py file.]()
4641

47-
```python
48-
import machine
49-
import time
50-
51-
led = machine.Pin(25, machine.Pin.OUT)
42+
2. Copy and paste the following code into your editor:
43+
```python
44+
import machine
45+
import time
5246

53-
while True:
54-
led.value(1)
55-
time.sleep(1)
56-
led.value(0)
57-
time.sleep(1)
58-
```
47+
led = machine.Pin(25, machine.Pin.OUT)
5948

60-
**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.
49+
while True:
50+
led.value(1)
51+
time.sleep(1)
52+
led.value(0)
53+
time.sleep(1)
54+
```
6155

62-
### 4. Run the Script
56+
***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.***
6357

64-
Click the **Run** or **Upload** button in your editor to transfer the script to your board.
58+
3. Click the **Run** or **Upload** button in your editor to transfer the script to your board.
6559

66-
### 5. Observe the LED
60+
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.
6761

68-
Once the script is running, the LED on your board should start blinking at one-second intervals.
62+
![LED blinking on your board.]()
6963

7064
## Programming Concepts Explained
7165

@@ -159,12 +153,12 @@ while True:
159153

160154
## Conclusion
161155

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

164-
- Importing modules
165-
- Initializing hardware components
166-
- Using loops
167-
- Controlling time delays
158+
- Importing modules (`machine`, `time`)
159+
- Initializing hardware components (LED)
160+
- Using loops (`while`)
161+
- Controlling time delays (`time.sleep()`)
168162

169-
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.
163+
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.
170164

0 commit comments

Comments
 (0)