While doing any programming task, it is important to be working in an environment that will your maximize productivity. This guide will take you through setting up an environment for MicroPython.
I have used many different setups for programming microcontrollers with MicroPython and I have found that the best and most streamlined way of working is with a program called Thonny. Thonny is a Python IDE that is great for beginners and is also very useful for programming microcontrollers with MicroPython. The rest of this article will go over getting Thonny setup for a ESP8266 NodeMCU, flashing the MicroPython firmware, and writing a simple program for an ESP8266 microcontroller using MicroPython.
If you are using Linux, I recommend installing Thonny using pip. On Windows and MacOSX I recommend installing Thonny through the download links on its website.
Download the Thonny installer from thonny.org and run it.
Open a terminal and enter pip install thonny. If the process fails, try running the same command again with elevated permissions (sudo).
pip install thonny
From the Micropython website, download the binary (.bin) file for ESP8266 microcontrollers tagged as “latest”.
Take note of its location in your filesystem.
Plug in your ESP8266 NodeMCU microcontroller to your computer using a micro USB cable and launch Thonny.
Immediately navigate to “Tools > Manage plug-ins…” and enter “esptool” into the search bar. Then click on the “Find packages from PyPI” button. An entry for esptool should appear in the space below. Click on the “install” button to install it. After it is finished installing the plug-ins window can be closed. Then close and reopen Thonny.
Next navigate to “Run > Select Interpreter”. Under the “Which interpreter or device should Thonny use for running your code?” label, select “MicroPython (ESP8266)”.
Select your NodeMCU’s port under “Port”. If you do not know how to find your device’s port, learn here
After setting the interpreter and the port, click on the button that says “Open the dialog for installing or upgrading
MicroPython on your device”. Select your device’s port again, and browse for the
MicroPython firmware that you downloaded earlier for the “Firmware” field. Make sure that “Erase flash before installing” is checked then click “Install”.
Thonny will now
erase any existing firmware and then flash the MicroPython firmware on your NodeMCU. This may take a few minutes.
After the firmware is finished installing, you may close all of the open dialogs.
At the bottom of the Thonny window in the tab labeled “Shell” you should see text that resembles the image below.
If the last line of the repl is not >>> then your REPL is not ready for input. To fix this press the stop button at the top of the Thonny window. This often means that the microcontroller is running something and as a result needs to be stopped before being given REPL commands.
To test that the firmware is installed correctly, enter the following into the repl:
>>> import esp
You should get output that looks like the following image.
If you get to this point, then you are ready to start programming your ESP8266 with MicroPython!
Lets write a quick “Hello World” program to demonstrate MicroPython on the ESP8266 NodeMCU. Copy the following code into the editor area in Thonny.
from machine import Pin
from time import sleep
led = Pin(16, Pin.OUT)
for _ in range(10):
After writing the program navigate to “File > Save As”. You will then get a window asking whether to save the file to your computer or the microcontroller. Select “MicroPython device”.
Now name the file “main.py” and click “Ok”. It is important to name the file “main.py” because the MicroPython always runs “boot.py” first then looks for “main.py” to run second.
To run the program on the microcontroller press the play button at the top of Thonny.
You should now see the microcontroller runs the program in ‘main.py’ and blinks the built in LED ten times.
You have now written and run your first program using MicroPython. I encourage you to experiment by testing your own code in ‘main.py’.
Next guide: Basic GPIO Input and Output with a NodeMCU and Micropython