|
| 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 | + |
0 commit comments