|
1 | 1 | ---
|
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.' |
5 | 4 | author: 'Pedro Lima'
|
6 | 5 | hero_image: "./hero-banner.png"
|
7 | 6 | ---
|
8 | 7 |
|
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. |
10 | 9 |
|
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 |
12 | 11 |
|
13 |
| -## Requirements |
| 12 | +For this tutorial, you will need a MicroPython compatible Arduino Board: |
14 | 13 |
|
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]() |
16 | 20 |
|
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: |
23 | 22 |
|
24 |
| -### Software (Editor) |
| 23 | + - [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython). |
25 | 24 |
|
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 |
30 | 26 |
|
31 | 27 | Blinking an LED is a simple yet effective way to get started with MicroPython while still understanding how to control hardware using code.
|
32 | 28 |
|
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. |
38 | 32 |
|
39 |
| -### 2. Connect Your Board |
| 33 | +***Need help installing MicroPython on your board? Visit the [MicroPython installation guide]().*** |
40 | 34 |
|
41 |
| -Ensure your Arduino board is connected to your computer via USB. |
| 35 | +## First Script (LED Blink) |
42 | 36 |
|
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. |
44 | 38 |
|
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.]() |
46 | 41 |
|
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 |
52 | 46 |
|
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) |
59 | 48 |
|
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 | + ``` |
61 | 55 |
|
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.*** |
63 | 57 |
|
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. |
65 | 59 |
|
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. |
67 | 61 |
|
68 |
| -Once the script is running, the LED on your board should start blinking at one-second intervals. |
| 62 | +![LED blinking on your board.]() |
69 | 63 |
|
70 | 64 | ## Programming Concepts Explained
|
71 | 65 |
|
@@ -159,12 +153,12 @@ while True:
|
159 | 153 |
|
160 | 154 | ## Conclusion
|
161 | 155 |
|
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: |
163 | 157 |
|
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()`) |
168 | 162 |
|
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. |
170 | 164 |
|
0 commit comments