MicroPython with the ESP32 guide series

How to write and execute a MicroPython program

In this lesson, I will show you how to write and execute a simple MicroPython program. I'll do this with Thonny IDE in two different ways.

I'll show you how to run a simple MicroPython program that prints "Hello World from MicroPython." in the shell. 

First, I'll show you how to write the program in the REPL and in a new file tab.

Next, I'll show you how to load an existing file that contains a MicroPython program from your computer's file system.

Run a program on the REPL

First, start your Thonny IDE editor. Open the file browser tools in the left side of the editor. Ensure that your ESP32 is connected to your computer. Files stored on the ESP32 will appear in the lower left side of the editor.

The main components of the Thonny editor.

If you have just flashed your ESP32 with the MicroPython interpreter, you will only see the boot.py file in the MicroPython device segment.

Let's write a very simple program.

First, we'll run the program on the Shell. Then, we'll in which we'll store the same program so that we can run it in the future without having to write it from scratch.

Here's the program:

>>> print("Hello World from MicroPython.")

Hello World from MicroPython.

Simple, right?

When you type this program in the REPL and hit the Enter key, the interpreter will execute it and show the response in the shell.

The interpreter has just executed my one-line program.

Remember, this single-line program was executed on the ESP32, not my host computer. When you type a command in the REPL and hit the Enter key, the command is evaluated by the REPL running on the target device immediately.

Run a program from a file

Another way to execute MicroPython programs, particularly useful for programs that are more than a few lines in size, is, of course, to store them in a file. So, I just copied my single command, my very small program, into a file. The program contains two lines. The first one is a commend, and starts with the "#" symbol. The second line is the actual instruction that prints out some text.

A tiny MicroPython program.

To save the program, click on the icon that looks like an old-fashioned floppy disk.

To save a program, click on the floppy disk button and select the target.

Thonny gives me a choice of where is it that I'd like to save this program, either my computer or the MicroPython device. In this instance, I'm going to go for the computer and that will give me the option to store it somewhere. So, let's say I'm going to put it on this location and just say helloworld.py as the file system and save that.

Note: regardless of where you save the file, execution will always take place on the target device.

To run the program from the file, click on the green "play" button.

Press "play" to run a program.

There are few nuisances in this system that we are going to explore a little later. Those nuisances have to do with dependencies.

For example, what if there is a module that is required by this program which is not stored on the MicroPython device? Then, you're not going to just be able to upload and execute this file on the device. You will also need to take care of those dependencies.

And there are a few examples later on in this course where I show you how that works.

All right. Obviously, we're going to do a lot more into how to write and execute programs on the ESP32 using MicroPython. But in this quick introduction, I just want to show you the simplest possible way of doing that.

Learn MicroPython for the ESP32

With this video course, you will learn how to use the
MicroPython programming language with the ESP32 micro-controller.


MicroPython is the perfect language for anyone looking for the easiest (yet still powerful) way to program a micro-controller.

Last Updated 9 months ago.

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}