Skip to content

Commit d0182e8

Browse files
First steps
1 parent 734cfb6 commit d0182e8

File tree

3 files changed

+311
-0
lines changed

3 files changed

+311
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
featured: micropython
3+
title: '0. Introduction to MicroPython'
4+
description: 'Learn about Micropython'
5+
author: 'Pedro Lima'
6+
hero_image: "./hero-banner.png"
7+
---
8+
9+
# Intro to MicroPython
10+
11+
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.
12+
13+
## MicroPython on Arduino Boards
14+
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.
16+
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.
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+
TODO: Image of code edit with imediate change
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.
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+
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.
40+
41+
## How to Program for MicroPython
42+
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.
44+
45+
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.
46+
47+
### Structuring Your Code
48+
49+
1. **Main Logic**: This would typically go into `main.py`. You can think of it as your "sketch" in Arduino terms.
50+
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.
51+
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.
52+
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.
54+
55+
## MicroPython vs. C++ for Electronics Projects
56+
57+
MicroPython offers a different approach to programming compared to the traditional C++ used in Arduino development. Here are a few key comparisons:
58+
59+
- **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.
60+
- **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.
61+
- **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.
62+
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.
64+
65+
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.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
---
2+
featured: micropython-101
3+
title: '1. Installing micropython'
4+
description: 'Lern how to setup MicroPython'
5+
author: 'Pedro Lima'
6+
hero_image: "./hero-banner.png"
7+
---
8+
9+
# My First Script
10+
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.
12+
13+
## Requirements
14+
15+
### Hardware Boards
16+
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
23+
24+
### Software (Editor)
25+
26+
- **MicroPython-Compatible Editor**:
27+
- [Arduino IDE with MicroPython Support](https://www.arduino.cc/en/software).
28+
29+
## Introducing the Example: Blinking an LED
30+
31+
Blinking an LED is a simple yet effective way to get started with MicroPython while still understanding how to control hardware using code.
32+
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).
38+
39+
### 2. Connect Your Board
40+
41+
Ensure your Arduino board is connected to your computer via USB.
42+
43+
### 3. Write the Code
44+
45+
Copy and paste the following code into your editor:
46+
47+
```python
48+
import machine
49+
import time
50+
51+
led = machine.Pin(25, machine.Pin.OUT)
52+
53+
while True:
54+
led.value(1)
55+
time.sleep(1)
56+
led.value(0)
57+
time.sleep(1)
58+
```
59+
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.
61+
62+
### 4. Run the Script
63+
64+
Click the **Run** or **Upload** button in your editor to transfer the script to your board.
65+
66+
### 5. Observe the LED
67+
68+
Once the script is running, the LED on your board should start blinking at one-second intervals.
69+
70+
## Programming Concepts Explained
71+
72+
Let's break down the key programming concepts used in this script:
73+
74+
### `machine` Module
75+
76+
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:
77+
78+
-**Pins:** Configure and control digital and analog pins.
79+
-**Timers:** Set up timers for scheduling tasks.
80+
-**Communication Interfaces:** Use protocols like I2C, SPI, and UART.
81+
-**Hardware-Specific Functions:** Access features unique to your microcontroller.
82+
83+
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.
84+
85+
### `time` Module
86+
87+
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:
88+
89+
-**time.sleep(seconds):** Pauses the execution of your script for the specified number of seconds. It accepts floating-point numbers for sub-second delays.
90+
-t**ime.ticks_ms():** Returns the number of milliseconds(ms) since the board was last reset.
91+
-**time.ticks_us()**: Returns the number of microseconds(us) since the board was last reset.
92+
-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.
93+
94+
### `while True` Loop
95+
96+
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.
97+
98+
### Code Breakdown
99+
100+
- **Import Modules**:
101+
102+
```python
103+
import machine
104+
import time
105+
```
106+
107+
We import the `machine` and `time` modules to access hardware functions and time delays.
108+
109+
- **Initialize the LED Pin**:
110+
111+
```python
112+
led = machine.Pin(25, machine.Pin.OUT)
113+
```
114+
115+
We create a `Pin` object named `led`, set to pin number `25`, and configure it as an output.
116+
117+
- **Infinite Loop**:
118+
119+
```python
120+
while True:
121+
led.value(1)
122+
time.sleep(1)
123+
led.value(0)
124+
time.sleep(1)
125+
```
126+
127+
Inside the loop, we:
128+
129+
- Turn the LED on by setting its value to `1`.
130+
- Wait for 1 second.
131+
- Turn the LED off by setting its value to `0`.
132+
- Wait for another second.
133+
- Repeat the cycle.
134+
135+
## Modification: Make the LED Blink Faster
136+
137+
Let's modify the script to make the LED blink faster. We'll change the delay from 1 second to 0.2 seconds.
138+
139+
### Modified Code
140+
141+
```python
142+
import machine
143+
import time
144+
145+
led = machine.Pin(25, machine.Pin.OUT)
146+
147+
while True:
148+
led.value(1)
149+
time.sleep(2)
150+
led.value(0)
151+
time.sleep(2)
152+
```
153+
154+
### Steps
155+
156+
1. Change the `time.sleep(1)` lines to `time.sleep(2)`.
157+
2. Upload the modified script to your board.
158+
3. Observe that the LED now blinks faster, turning on and off every 2 seconds.
159+
160+
## Conclusion
161+
162+
Congratulations! You've written and modified your first MicroPython script on an Arduino board. This simple exercise introduced you to:
163+
164+
- Importing modules
165+
- Initializing hardware components
166+
- Using loops
167+
- Controlling time delays
168+
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.
170+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
featured: micropython-101
3+
title: '1. Introduction to Arduino'
4+
description: 'Learn about the Arduino platform'
5+
author: 'Karl Söderby'
6+
hero_image: "./hero-banner.png"
7+
---
8+
9+
***This page is an introduction to the Arduino platform. If you are already familiar with Arduino, we recommend skipping to the next page.***
10+
11+
Arduino is a platform that enables students, teachers, hobbyists & professionals all over the world to build projects & applications that run on tiny computers.
12+
13+
The Arduino ecosystem is comprised of the hardware (a physical board with a tiny computer), software tools & services (Arduino IDE, Arduino Cloud), and the Arduino programming language, or "Arduino API".
14+
15+
## The Arduino Board
16+
17+
![The Arduino Nano ESP32.](assets/nano-esp32.png)
18+
19+
An Arduino development board is centered around a tiny computer that you program yourself to behave in specific ways. You can, for example, program a board to control a light, a motor, or to read the values of a temperature sensor.
20+
21+
The Arduino board is the connection with the physical world and can be used to control many different electronic circuits and devices. To name a few examples, an Arduino can be used to:
22+
- Create a light show with an LED strip,
23+
- Automatically open a door when you walk up to it,
24+
- A robotic arm that is controlled with a joystick,
25+
- A weather station recording data and posting it online.
26+
27+
## Hardware Required
28+
29+
This course is designed around two main components:
30+
- [Nano ESP32](https://store.arduino.cc/products/nano-esp32) - an Arduino board with a Wi-Fi® chip and antenna.
31+
- [Nano Screw Terminal Adapter](https://store.arduino.cc/products/nano-screw-terminal) - a carrier with screw terminal connections.
32+
33+
Additionally, as we progress in the course, we introduce the option of using third party components from [Seeed](https://www.seeedstudio.com/), which uses the **Grove connector standard**. These components can easily be connected to the Nano Screw Terminal Adapter via a [grove-to-male cable](https://store.arduino.cc/products/grove-4-pin-male-to-grove-4-pin-cable-5-pcs).
34+
35+
![Mount the Nano ESP32 on the Nano Screw terminal.](assets/esp32-terminal.png)
36+
37+
### Nano ESP32
38+
39+
The [Nano ESP32](https://store.arduino.cc/products/nano-esp32) is the board used in this course, which is very suitable for MicroPython due to its quick processor, large flash memory and Wi-Fi® enabled chip packed into a tiny circuit board.
40+
41+
***You can find out more about this board in the [Nano ESP32 documentation](/hardware/nano-esp32).***
42+
43+
### Nano Screw Terminal
44+
45+
The [Nano Screw Terminal Adapter](https://store.arduino.cc/products/nano-screw-terminal) is a carrier that you insert your Nano board into. With the carrier, you can very easily connect cables and secure them tightly with a screwdriver. This makes it easy to maintain and your circuits more robust.
46+
47+
***You can find out more about this board in the [Nano Screw Terminal Adapter documentation](/hardware/nano-screw-terminal-adapter).***
48+
49+
50+
## Microcontroller Basics
51+
52+
The tiny computer on the board, also known as the **microcontroller**, can be programmed and communicated with over USB. This microcontroller has very limited memory compared to the computers you are used to. For example, the board used in this course has about **30 000 times less RAM memory** than a modern computer, such as a Mac.
53+
54+
A microcontroller is designed to run the instructions it is programmed with, as soon as the board has power. These instructions happen very quickly, with thousands of instructions executed every second. How often they are executed can be altered in your program. You can for example pause the program for a second, and resume it again.
55+
56+
## Programming Basics
57+
58+
So how do we actually get the board to do what we want?
59+
60+
There are two ways of programming an Arduino board, either using the Arduino programming language (a subset of C/C++) or with MicroPython, an implementation of Python® specifically for microcontrollers. In this course, we will be using **MicroPython**.
61+
62+
### Arduino Programming Language
63+
64+
With the **Arduino programming language**, you write your program in what we call "sketches". A sketch is a file with the `.ino` extension, that you can edit inside the Arduino IDE. When you are happy with your sketch, you need to compile this file. A compiler checks for errors, and if successful, the sketch can be uploaded to your board. Once uploaded you replace the current program on your board.
65+
66+
The compiler is very strict and will point out where in your code you have a problem. If you are using a function from the Arduino language called `digitalWrite()`, but you write `digitalwrite()`, the code will not compile and you will get an error.
67+
68+
### MicroPython
69+
70+
Programming an Arduino using MicroPython is a slightly different experience. In this scenario, you install a version of Python on your board permanently, and then you send instructions to it. This means you can change the code for your board and load it in real time. MicroPython also implements a file system on your board.
71+
72+
## Summary
73+
74+
Over the years Arduino has released over a hundred different development boards, each different from the other. You choose the board depending on what you want to achieve, e.g. some boards have a Wi-Fi® module allowing you to connect to the Internet, and some have onboard sensors that allow you to record sensor data.
75+
76+
- [Next chapter: MicroPython Installation Guide](/micropython/micropython-course/course/installation)

0 commit comments

Comments
 (0)