Getting Started with MicroPython
By: Anuradha Wickramarachchi
Tags: Micropython, Programming, IoT, Python, Software

Getting Started with MicroPython

Tutorial on getting started with programming an ESP32 development board using MicroPython

Python is one of the most popular languages of the decade. It is very popular among researchers and machine learning enthusiasts. It has developed to the point that we can now program microcontrollers using python language. MicroPython in fact intends to bridge the knowledge gap between hardware programmers and software programmers.

Image from Wiki (https://en.wikipedia.org/wiki/MicroPython)

In this article, we will discuss how we can setup MicroPython in an ESP32 development board.

Flashing the ESP32

Every time, we program some microcontroller we flash the firmware together with the program code. This is typical as most of the programs written for microcontrollers are compiled rather than interpreted. However, in MicroPython we burn the firmware first, followed by the transfer of program code. The firmware reads the program code (or byte code) and runs the program.

Screenshot by Author

First, we need to download the latest MicroPython firmware from the official website. You can download it from here for ESP32. The downloaded file will have a naming similar to mine, esp32-idf3–20200902-v1.13.bin . For flashing ESP32 you also need the esptool program, which can be installed from here.

Check your esptool installation as follows;

(iot) anuradhawick@Anuradhas-MacBook-Pro ~ % esptool.py -h

Now that your flashing tool is working, erase the flash using the following command.

esptool.py --chip esp32 --port /dev/tty.usbserial-0001 erase_flash

Here, --port is the location of the ESP32 connected to your computer. For mine it was /dev/tty.usbserial-0001 . You can easily find it using the command; Check with your OS as it might differ, this is in macOS.

ls /dev/tty.*

You can do the flashing using the following command.

esptool.py --chip esp32 --port /dev/tty.usbserial-0001 --baud 460800 write_flash -z 0x1000 esp32-idf3–20200902-v1.13.bin

Verifying the MicroPython Firmware

The easiest way to verify the installation is directly connecting to the serial port. For this, you can use the following command followed by a hard reset of the board (using button).

(iot) anuradhawick@Anuradhas-MacBook-Pro ~ % screen -port /dev/tty.usbserial-0001 11520

Note that I specify a baud rate which is quite common in serial communication. Having done that, if it is successful you should see the following screen.

Screenshot by Author

Tip: In order to exit the serial connection press ctrl+a+d and then type killall screen . Otherwise, the program might not get successfully terminated.

Using the REPL (Read-Eval-Print-Loop)

Now our setup is complete, we can simply screen the serial port as I’ve shown in the previous step to open the REPL console. This is a python interpreter. We can simply write any Python interpreter command to get started. However, it is easier to use the ESP32 board using rshell (I will explain the reason later). Install it from here. Then you can access it via the following command;

(iot) anuradhawick@Anuradhas-MacBook-Pro micropython % rshell -p /dev/tty.usbserial-0001

See the following screenshot to have an idea!

Screenshot by Author

You can jump into the REPL using repl command here.

Screenshot by Author

An Example Program

Let’s try a small example program to blink an LED. The docs are available here for numerous experiments.

>>> import machine

>>> led = machine.Pin(13, machine.Pin.OUT)

>>> led.on()

>>> led.off()

When you run the above code with properly setup ESP32 board you should see the bulb turn on and off.

Image by Author

We can write a simple blinker as follows;

Tip: You can quit the reply window by pressing ctrl+x

import machine

import time led = machine.Pin(13, machine.Pin.OUT) while True:

led.on()

time.sleep(0.5)

led.off()

time.sleep(0.5)

Now if you reset the board the blinker will stop. Now imaging we want to run this program at the bootup.

Copying Python Code into the ESP32

Now let’s see how we can make a program run at once when the board boots up. For this, we need to add a file named main.py . The code inside this file will get executed automatically when we boot the board.

When you are inside the rshell (not REPL of rshell) type ls . You should see the content of the folder you are currently in. When you type ls /pyboard/ you will see a file named boot.py (possibly nothing else in a fresh board). The location /pyboard/ is inside your board. See the following screenshot for reference.

Screenshot by Author

Now let’s put our code in main.py and copy it into the board using the following command. Then reset the board.

cp main.py /pyboard/

Now you should be able to see the light blinking without us having to write any more code.

Congratulations now you know how to program your board using MicroPython. Hope you enjoyed this article.

Cheers :)