Discover MicroPython on ESP32 with your first script
One of the easiest things to do on a new board is to blink the built-in LED. This is like the famous Hello World text displayed in the terminal when learning a new programming language. Here is a script that blinks the built-in LED every second. The goal here is to show you an overview of programming in MicroPython.
from machine import Pin
import time
pin_led = Pin(25, mode=Pin.OUT)
while True:
pin_led.on()
time.sleep(1)
pin_led.off()
time.sleep(1)
Note
It is recommended that you know the basics of Python syntax before getting started with MicroPython.
Run the MicroPython script on your ESP32 board
First, make sure you have correctly configured your IDE software for your board.
On Thonny IDE, it is better to save the code on your computer and not directly on your board. Thonny IDE will send the code directly to the MicroPython interpreter (via the REPL) when running the script. To save the script on your ESP32 afterward, just go to File> Save as …
You will notice that the Python script does not automatically run when powering up the ESP32 . To fix this, you must save the script on the board with a specific name main.py . This is not usually a problem during prototyping, but it’s needed when the MicroPython script is permanently run on your ESP32.
A detailed explanation of a MicroPython script
The layout of a MicroPython script is as follows:
Each MicroPython script begins with importing the modules necessary to function correctly. This is equivalent to
#include
in the Arduino code for external libraries:
#include
<SPI.h>
,
#include
<SD.h>
…
The difference with MicroPython is that even basic objects must be imported . As a reminder, Python is an object-oriented language: all Python elements are considered as an object. For example, the ESP32 input/output pins will be seen and handled like objects. MicroPython objects can be used naturally without being an expert in object-oriented programming.
In the example script, we import the
Pin
submodule from the machine module. The
machine
module includes most of the MicroPython specific objects. The
Pin
module is actually a class that defines the operation of an input/output pin (GPIO). We create a
pin_led
object that corresponds to the integrated LED pin. This pin is defined as a digital output. The
Pin
object that we created has functions to modify its state:
.on()
we turn on the LED and
.off()
we turn off the LED.
Note
The ESP32’s built-in LED is connected to pin number 2 (GPIO2)
To visually see the blinking, add a delay of one second with the
sleep()
function of the
time
module.
The code that generates the blinking is contained in an infinite loop to blink … endlessly.
If you used to use Arduino code, you would notice that there are no more the
setup()
and
loop()
functions: the script is only executed once. Therefore, we must not forget the infinite loop to have a behavior similar to the Arduino sketch. A code is available to have the same structure as in Arduino code with a
setup()
function and a
loop()
function.
Improve the existing blink script in MicroPython
Thanks to the many functions of MicroPython, we can shorten the blink script. Indeed, the
.toggle()
function lets you invert the state of an output: If the pin is in a high state
(3.3V)
then the pin is in the low state
( 0V)
after executing the
.toggle()
function, and so on.
Thus, the new code (with a slight blink delay) is the following:
from machine import Pin
import time
pin_led = Pin(25, mode=Pin.OUT)
while True:
pin_led.toggle()
time.sleep_ms(250)
You will notice, that
time.sleep()
has been replaced by
time.sleep_ms()
for clarity. In fact, 3 functions let you stop the program for a specific time, each with a level of precision:
time.sleep(duration_in_seconds)
time.sleep_ms(duration_in_milliseconds)
time.sleep_us(duration_in_microseconds)
Note
1 second = 10 3 milliseconds = 10 6 microseconds
So in these three lines, we will pause the script for 250 ms:
time.sleep(0.005)
time.sleep_ms(5)
time.sleep_us(5000)
Note
As with the Arduino code, these functions stop the program: it cannot do other background tasks such as detecting the press of a button, measuring voltages …
You have seen an overview of programming ESP32 boards with MicroPython. I hope this hype you to learn more about this new language. So I encourage you to read a more detailed tutorial on handling I/O with MicroPython with a practical example at the end.