|
| 1 | +--- |
| 2 | +title: 'My First Script' |
| 3 | +description: 'Learn how to write a basic MicroPython script to blink an LED.' |
| 4 | +author: 'Pedro Lima' |
| 5 | +tags: [MicroPython, Blink] |
| 6 | +micropython_type: test |
| 7 | +--- |
| 8 | + |
| 9 | +In this tutorial, we will create our very first MicroPython script that will run on an Arduino board. Starting of simple, we will make an LED blink, a classic beginner project that will get us familiar with the MicroPython programming environment. |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +Before we start, let's check the requirements: |
| 14 | + |
| 15 | +### MicroPython Compatible Arduino Boards |
| 16 | + |
| 17 | +MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards: |
| 18 | + |
| 19 | +- [Portenta C33](https://store.arduino.cc/products/portenta-c33) |
| 20 | +- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi) |
| 21 | +- [Portenta H7](https://store.arduino.cc/products/portenta-h7) |
| 22 | +- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite) |
| 23 | +- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected) |
| 24 | +- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect) |
| 25 | +- [Nicla Vision](https://store.arduino.cc/products/nicla-vision) |
| 26 | +- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble) |
| 27 | +- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/nano-33-ble-rev2) |
| 28 | +- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2) |
| 29 | +- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32) |
| 30 | + |
| 31 | + |
| 32 | +### Software Requirements |
| 33 | + |
| 34 | +- [Arduino Lab for Micropython](https://labs.arduino.cc/en/labs/micropython) - Arduino Lab for MicroPython is an editor where we can create and run MicroPython scripts on our Arduino board. |
| 35 | + |
| 36 | +***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)*** |
| 37 | + |
| 38 | +## Board and Editor Setup |
| 39 | + |
| 40 | +1. Open the [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) application. |
| 41 | +2. Plug the Arduino board into the computer using a USB cable. |
| 42 | +  |
| 43 | +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: |
| 44 | +  |
| 45 | + |
| 46 | +***Need help installing MicroPython on your board? Visit the [MicroPython installation guide](/micropython/first-steps/install-guide).*** |
| 47 | + |
| 48 | +## First Script (LED Blink) |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | +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. |
| 53 | +  |
| 54 | + |
| 55 | +2. Copy and paste the following code into your editor: |
| 56 | + ```python |
| 57 | + import machine |
| 58 | + import time |
| 59 | + |
| 60 | + # The pin used for built-in LED varies |
| 61 | + # GIGA: 0 (green), Nano ESP32: 0 (green), Nano RP2040 Connect: 25 |
| 62 | + # Nano BLE Sense: 13 |
| 63 | + led = machine.Pin(25, machine.Pin.OUT) |
| 64 | + |
| 65 | + while True: |
| 66 | + led.value(1) |
| 67 | + time.sleep(1) |
| 68 | + led.value(0) |
| 69 | + time.sleep(1) |
| 70 | + ``` |
| 71 | + |
| 72 | + ***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`.*** |
| 73 | + |
| 74 | +3. Click the **Run** button in your editor to transfer the script to your board. |
| 75 | +  |
| 76 | + |
| 77 | +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. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +### Code Breakdown |
| 82 | + |
| 83 | +Let's take a look at the code, line by line, to understand what is happening: |
| 84 | + |
| 85 | +- **Import Modules**: |
| 86 | + |
| 87 | + ```python |
| 88 | + import machine |
| 89 | + import time |
| 90 | + ``` |
| 91 | + |
| 92 | + We import the `machine` and `time` modules to access hardware functions and time delays. |
| 93 | + |
| 94 | +- **Initialize the LED Pin**: |
| 95 | + |
| 96 | + ```python |
| 97 | + led = machine.Pin(25, machine.Pin.OUT) |
| 98 | + ``` |
| 99 | + |
| 100 | + We create a `Pin` object named `led`, set to pin number `25`, and configure it as an output. |
| 101 | + |
| 102 | +- **Infinite Loop**: |
| 103 | + |
| 104 | + ```python |
| 105 | + while True: |
| 106 | + led.value(1) |
| 107 | + time.sleep(1) |
| 108 | + led.value(0) |
| 109 | + time.sleep(1) |
| 110 | + ``` |
| 111 | + |
| 112 | + Inside the loop, we: |
| 113 | + |
| 114 | + - Turn the LED on by setting its value to `1`. |
| 115 | + - Wait for 1 second. |
| 116 | + - Turn the LED off by setting its value to `0`. |
| 117 | + - Wait for another second. |
| 118 | + - Repeat the cycle. |
| 119 | + |
| 120 | +## Understanding Programming Concepts |
| 121 | + |
| 122 | +Let's break down the key programming concepts used in this script: |
| 123 | + |
| 124 | +### `machine` Module |
| 125 | + |
| 126 | +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: |
| 127 | + |
| 128 | +- **Pins:** Configure and control digital and analog pins. |
| 129 | +- **Timers:** Set up timers for scheduling tasks. |
| 130 | +- **Communication Interfaces:** Use protocols like I2C, SPI, and UART. |
| 131 | +- **Hardware-Specific Functions:** Access features unique to your microcontroller. |
| 132 | + |
| 133 | +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. |
| 134 | + |
| 135 | +### `time` Module |
| 136 | + |
| 137 | +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: |
| 138 | + |
| 139 | +- `time.sleep(seconds)` Pauses the execution of your script for the specified number of seconds. It accepts floating-point numbers for sub-second delays. |
| 140 | + |
| 141 | +### `while True` Loop |
| 142 | + |
| 143 | +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. |
| 144 | + |
| 145 | +## Modification: Make the LED Blink Faster |
| 146 | + |
| 147 | +Let's modify the script to make the LED blink faster. We'll change the delay from 1 second to 0.2 seconds. |
| 148 | + |
| 149 | +### Modified Code |
| 150 | + |
| 151 | +```python |
| 152 | +import machine |
| 153 | +import time |
| 154 | + |
| 155 | +led = machine.Pin(25, machine.Pin.OUT) |
| 156 | + |
| 157 | +while True: |
| 158 | + led.value(1) |
| 159 | + time.sleep(0.5) # 0.5 seconds |
| 160 | + led.value(0) |
| 161 | + time.sleep(0.5) # 0.5 seconds |
| 162 | +``` |
| 163 | + |
| 164 | +### Steps |
| 165 | + |
| 166 | +1. Change the `time.sleep(1)` lines to `time.sleep(0.5)`. |
| 167 | +2. Upload the modified script to your board. |
| 168 | +3. Observe that the LED now blinks faster, turning on and off every 0.5 seconds. |
| 169 | + |
| 170 | +## Summary |
| 171 | + |
| 172 | +Great work! We have now written and modified our first MicroPython script on an Arduino board. This exercise introduced: |
| 173 | + |
| 174 | +- Importing modules (`machine`, `time`) |
| 175 | +- Initializing hardware components (LED) |
| 176 | +- Using loops (`while`) |
| 177 | +- Controlling time delays (`time.sleep()`) |
| 178 | + |
| 179 | +These concepts are key for a vast majoraty of operations we can perform while writing MicroPython programs. |
| 180 | + |
0 commit comments