Image of eBits Academy fundamentals illustration

MicroPython

  • January 20, 2022
  • |
  • Jesper Nielsen

Why use MicroPython? - MicroPython is easy and quick to implement. It is not as low-level as C, C++, and Arduino, making your code more intuitive and faster to write. On the other hand, MicroPython cannot match the lightweight processor and memory use achievable with C and C++.

MicroPython is packed with useful features, including an interactive REPL (read-eval-print loop), which lets you write code live at the prompt for an ESP32. You can manually switch an LED on and off without building and compiling your code. 

The aim is to be as compatible as possible with ordinary Python code, making it easy to transfer code from desktop programs to embedded systems. As a hobbyist, you can therefore build exciting microcontroller projects with a pleasant language like Python. 

 

 

CONTENTS

DOWNLOAD

To get started with MicroPython, download Thonny from Thonny, Python IDE for beginners. The site provides downloads for Windows, Mac, and Linux.

THONNY

Installing Thonny is straightforward; simply follow the guide below.

MICROPYTHON

Now that Thonny is installed, download MicroPython firmware from its website: MicroPython - Python for microcontrollers 
The image below shows some available MicroPython versions. It is best to choose the latest stable version for stability and fewer bugs. Newer versions also let you import more libraries into your projects.
Download the file to your computer/Mac; it will be used to install MicroPython through Thonny. 

FIND THE COM PORT

When installing MicroPython, you need to identify the COM port used by your ESP32. To find it, right-click the Windows icon in the left-hand corner --> Device Manager.

Then select Ports (COM & LPT). ESP32 may, on the board shown, be named Silicon Labs CP210x USB to UART Bridge (COMX).

INSTALLING MICROPYTHON

To install MicroPython on your ESP32, first connect the board to your computer/Mac. Then select Run -> Select interpreter.

 

Then select MicroPython (ESP32), in the dropdown menu.

 

In the lower dropdown menu, select the COM port where your ESP32 is detected. It is listed as Silicon Labs CP210x USB to UART Bridge. If you cannot find yours, return to the section FIND THE COM PORT

Then select the port and the firmware downloaded earlier. Choose firmware for the exact ESP32 variant. Back up files on the board: the following option erases its flash contents: Erase flash before installing
Then click Install. The window will look as shown below. Wait until installation finishes.

 

COMPILING CODE

To compile and run code on the ESP32, save the project by selecting File -> Save As.

 Then select MicroPython device.

Name the project main.py. At startup, MicroPython runs boot.py and then looks for a file named main.py.
Then press the green button on the toolbar.

 

HELLO WORLD!

Let us try a simple program that switches an LED on GPIO2 on and off on the ESP32 board. Check the board schematic: not all ESP32 boards have a built-in LED on GPIO2. Adjust the pin number or connect an external LED with a series resistor. Enter the code below in Thonny.

from machine import Pin
from time import sleep

led = Pin(2,Pin.OUT)

for _ in range(1000):
       led.value(1)
       sleep(1)
       led.value(0)
       sleep(1)
Save the project, then press the green button.

ADDING A SENSOR

You can also add sensors such as the DHT11 temperature and humidity sensor. Here MicroPython shows one of its strengths: coding is incredibly easy. Import dht and specify the pin where the DHT11 sensor is connected on the ESP32. Then call temperature() or humidity(), depending on which reading you want. The link below provides a code snippet and detailed documentation for the DHT11 sensor with MicroPython.

13. Temperature and Humidity — MicroPython 1.17 documentation

from machine import Pin
import dht
import time
 
while True:
    sensor = dht.DHT11(Pin(23))
    sensor.measure()
 
    print('Temperature = %.2f' % sensor.temperature())
    print('Humidity = %.2f' % sensor.humidity())
    time.sleep(3)

When the code above is compiled and run in Thonny, you can read the shell output. In this example, temperature and humidity are printed every 3 seconds.

REPL

MicroPython provides a REPL (read-eval-print loop), an interactive programming environment. You can program the ESP32 live without a separate compile and build step. For example, you can switch the built-in LED on and off as you wish. This is very useful for quickly testing code. The video below illustrates some helpful functions, such as printing the internal temperature for monitoring.

 

Documentation for Further MicroPython Features

The link contains documentation for setting up drivers, WiFi, timers, SPI, I2C, and more:

Getting started with MicroPython on the ESP32 — MicroPython 1.17 documentation 

Leave a comment

Please note, comments need to be approved before they are published.