MicroPython is an implementation of Python 3 for microcontrollers. It’s an awesome firmware that combines the powerful features of Python programming language with the low-level access of microcontrollers. Following is a list of microcontrollers supported by the MicroPython framework.
Arch Mix, Actinius icarus, Arduino Nano RP2040 Connect, Arduino Primo, B_L072Z_LRWAN1, B_L475E_IOT01A, blueio_tag_evim, C3 mini, CERB40, Discovery F4, Discovery F411, Discovery F429, Discovery F7, Discovery F769, Discovery Kit H7, Discovery L476, Discovery L496G, dvk_bl652, E407, ESP32, ESP32-C3, ESP32-C3 USB, ESP32 D2WD, ESP32 OTA, ESP32 SPIRAM, ESP32-S2, ESP32-S2 WROVER, ESP32-S3, ESP8266, evk_nina_b1, evk_nina_b3, F405 Express, F407VE, F407ZG, Feather M0 Express, Feather nRF52840 Express, Feather RP2040, Feather S2, Feather S2 Neo, Feather S3, GARATRONIC_NADHAT_F405, GARATRONIC_PYBSTICK26_F411, H407, H743VI, HYDRABUS, ibk_blyst_nano, idk_blyst_nano, ItsyBitsy M4 Express, ItsBitsy RP2040, Lego Hub No.6, LILYGO TTGO Lora32, LIMIFROG, M5 Stack Atom, micro:bit v1, MicroE Clicker 2 STM32, Micromod STM32, MikroE Quail, MIMXRT1010_EVK, MIMXRT1015_EVK, MIMXRT1020_EVK, MIMXRT1050_EVK, MIMXRT1060_EVK, MIMXRT1064_EVK, Mini SAM M4, NETDUINO PLUS 2, Nucleo F091RC, Nucleo F401RE, Nucleo F411RE, Nucleo F412ZG, Nucleo F413ZH, Nucleo F429ZI, Nucleo F439ZI, Nucleo F446RE, Nucleo F722ZE, Nucleo F746ZG, Nucleo F767ZI, Nucleo G744RE, Nucleo H743ZI, Nucleo H743ZI2, Nucleo L073RZ, Nucleo L432KC, Nucleo L452RE, Nucleo L476RG, Nucleo WG55, Nucleo WL55, nrf52840 mdk usb dongle, pca10000, pca10001, pca10028, pca10031, pca10040, pca10056, pca10059, pca10090, Pico LiPo, Pro Micro RP2040, Pro S3, Pyboard D-series SF2, Pyboard D-series SF3, Pyboard D-series SF6, Pyboard Lite v1.0, Pyboard v1.0, Pyboard v1.1, PYBSTICK26 RP2040, QT Py RP2040, Raspberry Pi Pico, Seeduino XIAO, SAMD21 Xplained Pro, SIL WESP32, STM32F439, S2 Mini, S2 Pico, Teensy 4.0, Teensy 4.1, Tiny2040, TinyPICO, Tiny S2, Tiny S3, Thing Plus RP2040, Trinket M0, USBDONGLE_WB55, Xennon, Wio Terminal D51R, WiPy, wt51822_s4at
Well, the above list seems too lengthy. This list will only expand in the future. No doubt, MicroPython already supports all major microcontrollers and embedded platforms.
Apart from supporting a wide variety of hardware platforms, MicroPython facilitates a large set of standard Python libraries on the software side. These libraries include array, binascii, builtins, cmath, collections, errno, gc, hashlib, heapq, io, json, math, os, random, re, select, socket, ssl, struct, sys, time, uasyncio, zlib, and _thread. It also facilitates several hardware-specific libraries like Bluetooth, btree, cryptolib, framebuf, machine, micropython, neopixel, network, and uctypes. It also provides many microcontroller-specific libraries called port-specific libraries. Many libraries are available as umodules that are original libraries rewritten for special needs. Even the embedded engineers are free to rewrite or modify any MicroPython library.
MicroPython is an excellent tool for designing and implementing your embedded systems with the power of Python language. In this tutorial, we will discuss setting up the development environment of MicroPython using uPyCraft IDE. Let us start exploring MicroPython firmware with the popular microcontroller boards – ESP8266 and ESP32.
Setting up MicroPython development environment
To start programming microcontrollers with MicroPython, all you need is a microcontroller board of your choice and a computer. Depending on your hobby or requirement, you can select electronic components like sensors and displays. We have chosen ESP866 and ESP32 boards to work with the MicroPython framework. The first step towards starting your MicroPython journey will be setting up a development environment on a computer system. This essentially involves four steps –
- Installing Python: MicroPython is a subset of Python 3. Therefore, you need to install the latest Python3.x on your computer system.
- Installing uPyCraft IDE: Several IDE can be used to write MicroPython scripts. The uPyCraft IDE is the most straightforward. It let you get right to MicroPython programming in a few minutes.
- Uploading MicroPython firmware to the selected microcontroller: The MicroPython scripts won’t work on a microcontroller until the MicroPython firmware is sitting inside it. So, before you start writing actual working Python scripts to control embedded hardware, you need to upload the firmware to the microcontroller of your choice. This tutorial will show how MicroPython firmware is uploaded in ESP8266 and ESP32.
- Get your hands dirty with excellent Python scripts: Once the firmware is loaded in ESP8266/ESP32 or other supported microcontrollers, you can write, upload, and run embedded programs in Python scripts on the selected microcontroller.
We will get through all these steps in this tutorial and finally test a LED blink program on ESP8266.
Installing Python in Windows
To install Python in Windows, browse the Python download page and download the installation file. Double click the installation file to start the Python setup. Remember to check the option “Add Python 3.x to PATH” before continuing the setup. Within a few seconds, the setup will complete and a window showing message “Setup was successful” will pop up. Close the window. You have Python installed on your Windows computer.
Installing uPyCraft IDE in Windows
The uPyCraft is the most uncomplicated programming environment to get straight to MicroPython programming for embedded devices. The project can be found on the DFRobot’s Github link. The setup for Windows can be downloaded from this link. Download the setup file and double click. A window will pop up, prompting you to install SourceCodePro font. Click ok and continue.
The IDE does not require installation. As you will click ok, the IDE will open up within a few seconds, as shown below.
Installing Python in Linux
To install Python 3.x and pip python package manager in Linux, type the following commands in the terminal window and press enter.
$ sudo apt install python3 python3-pip
If the Python is successfully installed, its version can be checked by executing the following command.
Installing uPycraft in Linux
UPyCraft IDE does not have sound support for Linux systems. For MicroPython programming in Linux Ubuntu, I prefer Thonny IDE instead of uPyCraft IDE. The official uPyCraft download page has also removed the setup link for Linux distribution.
However, the uPyCraft can be installed in a Linux distribution from its source code. Download the source code ZIP from this Github link and follow the instructions for installing uPyCraft in Linux provided in the same link.
Installing Python in Mac OS X
To install Python 3.x in Mac OS X, type the following commands in the terminal window and press enter.
$ brew install python3
Installing uPycraft in Mac OS X
Unfortunately, uPyCraft is again not the best IDE for MicroPython programming in Mac OS X. The setup link for Mac on the uPyCraft download page is missing. Similarly, you can prefer Thonny IDE for Mac computers.
However, uPyCraft can be installed on Mac using the source code. Download the source code ZIP from this Github link and follow the instructions for installing uPyCraft in Mac provided in the same link. Note that uPyCraft is reported to behave strangely in Mac systems.
Uploading MicroPython firmware to ESP8266
Once the uPyCraft or Thonny IDE is installed on your computer system, next you need to upload the MicroPython firmware to your microcontroller. If you are working with ESP8266, it is pretty simple to upload the firmware on the ESP board using uPyCraft IDE. The MicroPython firmware for various microcontrollers and hardware platforms is available at this link. For ESP8266, the MicroPython firmware can be downloaded from this link.
Now, connect ESP8266 with the computer via a USB cable. Navigate to Tools-> Serial and select the COM port ESP8266 is connected. This is COM5 in our case.
If the COM port is not shown in the uPyCraft IDE, the ESP8266 is not installed with the CH340 driver, or the USB cable does not have data wires. The cables that come with power banks or chargers usually do not have data wires. So, you need a proper USB cable with data wires. The drivers can be downloaded from the Silicon Labs website.
Next, navigate to Tools-> Board and select ESP8266.
To upload the MicroPython Firmware, navigate to Tools -> BurnFirmware.
A window will pop up. Select the following options in this window.
Firmware choose: Select the downloaded firmware file (.bin file)
Now hold the Flash/Boot button on the ESP8266 board and click OK.
You can release the Boot/Flash button after clicking ok.
This will follow by erasing the flash memory on ESP8266 and uploading the MicroPython firmware.
If you see an error shown in the image below, ESP8266 is not the boot mode. You did not press the boot/flash button properly. Repeat the above process from selecting a port to burning firmware and remember to press the boot/flash button properly before clicking ok in the burnFirmware window.
Exploring uPycraft IDE
uPyCraft IDE is simple, efficient, and easy to use. The IDE’s interface is mainly divided into five sections.
- Menu bar: This bar contains the main menu of the IDE, which includes Files, Edit, Tools, and Help tabs.
- Folder and files: There is a file explorer on the left-hand side. This shows the file currently uploaded in the flash memory of the ESP8266/ESP32, files stored on an SD card, built-in library files, and the files stored in the workspace. If ESP8266/ESP32 is connected with the IDE (by clicking on the connect button in Tools), the device folder shows the files uploaded to the flash memory of ESP8266/ESP32. If the firmware is successfully uploaded to the ESP8266/ESP32, this folder essentially has a boot.py file. Any python script to be run can be saved by any name like LEDblink.py or sendEmail.py, etc.. If the script to run is already uploaded to ESP8266/ESP32, it will show in the device folder. This script always runs after boot.py.
The SD folder shows the files stored in the SD card of the microcontroller. This folder works only with boards with an SD card slot like PyBoard. The built-in IDE library files are shown in the uPy_lib folder. The workspace directory shows the files that are stored in the workspace folder on your computer. To update this directory with the IDE, navigate to File->Reflush Directory.
Later, this directory can be changed by navigating to Tools-> InitConfig.
- Editor: This is the space where your write Python scripts to control embedded hardware.
- MicroPython Shell/Terminal: This is a debug console in uPyCraft IDE. You can type commands here that you want to execute immediately in ESP8266/ESP32. It also shows errors, serial messages, and program execution state.
- Tools: There is an array of quick icons for various tools on the right-hand side.
These tools are described as follows:
New: To create a new file in the editor.
Open file: To open a file from the workspace folder.
Save file: To save changes in a currently open file.
Download and run: To upload the main Python script to ESP8266/ESP32 and start program execution on the board.
Stop: To stop the program execution on ESP8266/ESP32.
Connect/Disconnect: To connect or disconnect with ESP8266/ESP32 board via serial communication (USB cable). Before connecting, you need to select the board and serial port from the Tools menu.
Undo: Undo last change in the code editor.
Redo: Redo the last change in the code editor.
Syntax check: To run a syntax check in the currently open script tab.
Clear: To clear the Shell/Terminal messages.
Writing and uploading Python scripts to ESP boards
First of all, select the board and serial port from the Tools menu. Then, connect by clicking the connect button in the Tools icons. Now, you can write your Python script for the intended embedded application. The firmware must be already uploaded in ESP8266/ESP32. After writing the script and checking for errors, click on the Download and run button in the Tools icons to upload the main execution script in ESP8266/ESP32. You can stop the execution of the script by clicking the stop button in the Tools icons. The Shell/Terminal window shows any serial messages communicated while executing code on ESP8266/ESP32 are shown in the Shell/Terminal window.
Blinking LED on ESP8266 using MicroPython
After uploading MicroPython firmware, write the following code in the code editor.
from machine import Pin
from time import sleep
led = Pin(2, Pin.OUT)
Note that the above is a Python script and loop bodies are neatly indented. Check for syntax errors and click on “Download and Run” button. The script will upload in ESP8266/ESP32 and the onboard LED will start blinking.[Link to demonstration video ESP03-DV]
You may also like:
Filed Under: Electronic Projects, ESP8266, Featured Contributions, Python, Tutorials