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.


Supplemental Guides


You Will Need

ESP8266 NodeMCU
MicroUSB Cable
Computer running Windows, Linux, or MacOSX


Background

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.


Downloads

Thonny

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.


Install Thonny from Thonny.org

Download the Thonny installer from thonny.org and run it.

Download Thonny from its website.
Download Thonny from its website.


Install Thonny using Pip

Open a terminal and enter pip install thonny. If the process fails, try running the same command again with elevated permissions (sudo).


Acquiring Micropython Firmware

From the Micropython website, download the binary (.bin) file for ESP8266 microcontrollers tagged as “latest”. Take note of its location in your filesystem.

Download the latest MicroPython firmware for ESP8266 microcontrollers.
Download the latest MicroPython firmware for ESP8266 microcontrollers.


Thonny Setup

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.

Install the esptool plug-in for Thonny.
Install the esptool plug-in for 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

Set the interpreter and the port.
Set the interpreter and the port.

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”.

Set the correct parameters for flashing the MicroPython firmware to an ESP8266 microcontroller.
Set the correct parameters for flashing the MicroPython firmware to an ESP8266 microcontroller.

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.

Thonny has completed flashing the firmware on the microcontroller.
Thonny has completed flashing the firmware on the microcontroller.

At the bottom of the Thonny window in the tab labeled “Shell” you should see text that resembles the image below.

MicroPython repl ready for input.
MicroPython repl ready for input.

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.

Issue stop command to microcontroller.
Issue stop command to microcontroller.

To test that the firmware is installed correctly, enter the following into the repl:

>>> import esp
>>> esp.check_fw()

You should get output that looks like the following image.

Verify MicroPython firmware on the ESP8266.
Verify MicroPython firmware on the ESP8266.

If you get to this point, then you are ready to start programming your ESP8266 with MicroPython!


Hello World

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):
    led.value(0)
    sleep(1)
    led.value(1)
    sleep(1)
A blinking LED program is the hardware version of a 'Hello World' program.
A blinking LED program is the hardware version of a 'Hello World' program.

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”.

Select the option to save the file to the 'MicroPython device'.
Select the option to save the file to the '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.

Name the file 'main.py'.
Name the file 'main.py'.

To run the program on the microcontroller press the play button at the top of Thonny.

Run the file on the microcontroller by pressing the play button.
Run the file on the microcontroller by pressing the play button.

You should now see the microcontroller runs the program in ‘main.py’ and blinks the built in LED ten times.

The NodeMCU running a blink script.
The NodeMCU running a blink script.

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