|
| 1 | +--- |
| 2 | +author: 'Karl Söderby' |
| 3 | +hero_image: "./hero-banner.png" |
| 4 | +micropython_type: "101" |
| 5 | +featured: micropython-101 |
| 6 | +title: 'Introduction to MicroPython' |
| 7 | +description: 'Learn about the Arduino platform' |
| 8 | +--- |
| 9 | + |
| 10 | +MicroPython is an implementation of the popular Python® programming language, with a target for microcontrollers (hence the "micro"). |
| 11 | + |
| 12 | +Python has become one of the most popular languages worldwide, with one of its key characteristics being an easier language to learn. This makes it suitable for beginners that have little to no experience in writing text-based code but want to achieve great things. |
| 13 | + |
| 14 | +## Nano ESP32 / MicroPython Pinout |
| 15 | + |
| 16 | +The pins used in MicroPython are very different from what the pin numbering on your board states. In the pinout below, you can see in **green**, which pins you need to use when using MicroPython. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +Why is there a difference in pins? The MicroPython implementation follows the pin numbering on the ESP32-S3, making it compatible with all other boards based on the ESP32-S3. |
| 21 | + |
| 22 | +Therefore it is necessary to keep track on two sets of pins: |
| 23 | +- The **Nano ESP32 pins** which are labeled on the board. |
| 24 | +- The **MicroPython pins** which are used in the code. |
| 25 | + |
| 26 | +## The MicroPython Environment |
| 27 | + |
| 28 | +MicroPython includes the standard Python API, but also a series of modules that are hardware specific. After you have installed it in your board, commands written in Python that you send to the board will be executed directly. |
| 29 | + |
| 30 | +This is quite different from the "standard" Arduino (C++) way of programming, where you compile your code before sending it to your board. The MicroPython way allows you to change the program instantly. |
| 31 | + |
| 32 | +When using MicroPython, we don't write sketches, we write **scripts**. A script can be written in a [MicroPython compatible editor](https://labs.arduino.cc/en/labs/micropython) which can then be saved on the board. To send a script, you need to have the board connected to your computer. |
| 33 | + |
| 34 | +Since MicroPython implements a filesystem, you can access your board similar to how you access a USB drive. |
| 35 | + |
| 36 | +## MicroPython Editor |
| 37 | + |
| 38 | +A MicroPython editor is a special code editor with built-in functions for communicating with your board. |
| 39 | + |
| 40 | +There are several editors available, but in this course, we will use the [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython). |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +***The [next chapter](/micropython-course/course/installation) is dedicated to using the Arduino Lab for MicroPython editor.*** |
| 45 | + |
| 46 | +## Connect to Your Board |
| 47 | + |
| 48 | +Any time we want to interact and program our Nano ESP32, we need to connect to it. This requires the board to have MicroPython installed, which is covered in the [next chapter (installation)](/micropython-course/course/installation). |
| 49 | + |
| 50 | +To connect to your board, click on the **"Connect"** button in the editor. |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +## What is a Script? |
| 55 | + |
| 56 | +A MicroPython script is a piece of code that can be run on your board. It could contain just the simplest mathematic equation, `1+1` (which is 2), or a script that controls a robotic arm that serves you a cup of tea. |
| 57 | + |
| 58 | +Inside a MicroPython editor, you simply write the code you want to run and click on the **"Run"** button. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +If there is anything wrong with your code, you will receive an error. If not, it will run the instructions in the scripts from top to bottom. To stop the script, you can click on the **"Stop"** button right next to the Run button. |
| 63 | + |
| 64 | +## Loops |
| 65 | + |
| 66 | +If you have plenty of experience in Arduino, you will be used to the concept of a **loop**. If you don't, you can read more about it [here](www.arduino.cc/reference/en/language/structure/sketch/loop/). This is not a requirement when using MicroPython. You can write a program that either just runs once, or continues to loop over and over. |
| 67 | + |
| 68 | +To make a program loop continuously, you will need to use a **while loop** inside your script. |
| 69 | + |
| 70 | +```python |
| 71 | +while(True): |
| 72 | + # code placed here will execute over and over again |
| 73 | + # in a similar fashion to the void loop() function |
| 74 | +``` |
| 75 | + |
| 76 | +## Main & Boot Files |
| 77 | + |
| 78 | +There are two files that are important to consider in the MicroPython environment: |
| 79 | +- `boot.py` - a script that will run as the board starts (in other words, **boots**). |
| 80 | +- `main.py` - a script that will run immediately after `boot.py`. |
| 81 | + |
| 82 | +You can consider `main.py` file to be your "main" script as it will execute any time after the board boots. Inside a MicroPython editor, we can open this file and edit it directly. When we save, we save it to the board, and the instructions are updated. To access your files, click the **"Files"** button. |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +This is a bit different if you are used to programming Arduino boards using the Arduino IDE / C++ because there you need to compile and upload for every change you make. |
| 87 | + |
| 88 | +## Saving Files |
| 89 | + |
| 90 | +As MicroPython has a file system, it is possible to save your code directly on the board. You can edit your files freely and the changes will **not save automatically**, but if you click on the **"Save"** button, this will overwrite anything previously stored in that file. |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +If you save it on for example the `main.py` file, you can disconnect the board, come back another day, connect the board, and your file will be there! It works like a normal file you edit on your computer. |
| 95 | + |
| 96 | +***Be cautious with the "Save" option, as it will overwrite everything, and there's no way of recovering it.*** |
| 97 | + |
| 98 | +## REPL |
| 99 | + |
| 100 | +The **REPL**, also often referred to as the **terminal**, is a command line interface inside your editor. When you input instructions or a message, the **interpreter** on your Arduino will evaluate it, and return the result. |
| 101 | + |
| 102 | +The REPL / Terminal is accessed by clicking on the **"Terminal"** button in the top right corner. |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +In the could for example write `1+1`, which the REPL will reply with a `2`. This computation is performed on your Arduino, and sent back to you! |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +When we run a script, and don't worry, we will come back to this topic, we can use the `print()` function to print things in the REPL. This is a great way of knowing what goes on on your board. An example is: |
| 111 | + |
| 112 | +```python |
| 113 | +print(1+1) |
| 114 | +print("Computation performed, yay!") |
| 115 | +``` |
| 116 | + |
| 117 | + |
| 118 | +## Modules |
| 119 | + |
| 120 | +As you progress with MicroPython, you will come across a very important term: **modules**. |
| 121 | + |
| 122 | +Modules are code files that can be imported into your script which provides an additional set of functionalities. For example, the `time` module allows you to control how often something should occur, or the `machine` module contains hardware specific functions. To use a module, we can simply `import` it in the script, as follows: |
| 123 | + |
| 124 | +```python |
| 125 | +# import the "time" module |
| 126 | +import time |
| 127 | + |
| 128 | +# import the "pin" function from the "machine" module |
| 129 | +from machine import Pin |
| 130 | +``` |
| 131 | + |
| 132 | +Once the module is imported, you can use it in your script, like: |
| 133 | + |
| 134 | +```python |
| 135 | +import time |
| 136 | + |
| 137 | +# freeze the program for one second |
| 138 | +time.sleep(1) |
| 139 | +``` |
| 140 | + |
| 141 | +## External Modules |
| 142 | + |
| 143 | +Not all modules are built-in to the MicroPython installation. This would be impossible because we have a very limited memory space. |
| 144 | + |
| 145 | +For example, to use a module for a specific sensor, we need to install it **externally**. Installing external modules can be done in several different ways, but below we will demonstrate a safe and straightforward approach. |
| 146 | + |
| 147 | +### Install External Modules |
| 148 | + |
| 149 | +To install an external module, we are going to use something called `mip`. With `mip`, we can install a module on the board directly, by connecting to Wi-Fi and downloading the module directly to the board. So how do we do this? |
| 150 | + |
| 151 | +Below is a script that first connects to Wi-Fi®, and then downloads a specific module. In this case, we will provide a URL to a file stored on GitHub (the raw file). In this case, we will install the `lis3dh` module, which will be used later on in this course. |
| 152 | + |
| 153 | +Please note that you need to add your own Wi-Fi® network and password in the `WIFI_NETWORK` and `WIFI_PASSWORD` fields. |
| 154 | + |
| 155 | +```python |
| 156 | +""" |
| 157 | +This script first connects to Wi-Fi, |
| 158 | +then installs the module specified |
| 159 | +in the URL variable. |
| 160 | +""" |
| 161 | + |
| 162 | +import network |
| 163 | +import mip |
| 164 | + |
| 165 | +WIFI_NETWORK='YOUR_NETWORK_NAME' |
| 166 | +WIFI_PASSWORD='YOUR_NETWORK_PASSWORD' |
| 167 | + |
| 168 | +# add the URL of the module you want to install |
| 169 | +URL = "https://raw.githubusercontent.com/tinypico/tinypico-micropython/master/lis3dh%20library/lis3dh.py" |
| 170 | + |
| 171 | +wlan = network.WLAN(network.STA_IF) |
| 172 | +wlan.active(True) |
| 173 | +wlan.connect(WIFI_NETWORK, WIFI_PASSWORD) |
| 174 | + |
| 175 | +print() |
| 176 | +print("Connected to ",WIFI_NETWORK) |
| 177 | + |
| 178 | +mip.install(URL) |
| 179 | +``` |
| 180 | + |
| 181 | +Once you run the script successfully, you will have installed the module on your board. |
| 182 | + |
| 183 | +### Removing Modules |
| 184 | + |
| 185 | +If you want to uninstall a module, you can delete it in the editor using the "Files" tab. This is a good idea to do if you have installed many modules, as eventually you will run out of memory space. |
| 186 | + |
| 187 | +Select the file you want to remove, and click on the **"Delete"** icon. You will be prompted to remove the file. |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | +### Troubleshooting |
| 192 | + |
| 193 | +**1. Invalid URL:** if you have specified an invalid URL, you will get an error that looks like this: |
| 194 | + |
| 195 | +``` |
| 196 | +Package not found: https://micropython.org/pi/v2/package/6/<url>/latest.json |
| 197 | +``` |
| 198 | + |
| 199 | +Double check to make sure your URL is correct if this occurs. |
| 200 | + |
| 201 | +**2. OSError: -202:** |
| 202 | + |
| 203 | +The following error can occur when running the installation script. |
| 204 | + |
| 205 | +``` |
| 206 | +Traceback (most recent call last): |
| 207 | + File "<stdin>", line 24, in <module> |
| 208 | + File "mip/__init__.py", line 1, in install |
| 209 | + File "mip/__init__.py", line 1, in _install_package |
| 210 | + File "mip/__init__.py", line 1, in _install_json |
| 211 | + File "urequests.py", line 180, in get |
| 212 | + File "urequests.py", line 76, in request |
| 213 | +OSError: -202 |
| 214 | +``` |
| 215 | + |
| 216 | +Some things to check are: |
| 217 | +- Did you enter your network credentials properly? |
| 218 | +- Did you already connect to Wi-Fi®? |
| 219 | + |
| 220 | +If the error persist, you can try to "soft-reset" the board, by clicking the **"Reset"** button, and run the script again. |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | +## Summary |
| 225 | + |
| 226 | +In this chapter, we learned a little bit about the key components in the MicroPython environment. |
| 227 | + |
| 228 | +- [Next chapter: Installation & Setup](/micropython-course/course/installation) |
0 commit comments