One of the most common ways of coding ESP8266, working with it and probably the simplest is by using ArduinoIDE. It might also be known that ESP8266 does not need any changes to be made on hardware level for us to code it with ArduinoIDE. This means, that ESP8266 comes with inbuilt Firmware that supports coding through ArduinoIDE. But as one advances in making projects, the flexibility that MicroPython offers gives more power to a creator. Hence, in this article, let’s take a look into how to use MicroPython on ESP8266.
There are many methods to put MicroPython on ESP8266. Few of them use flashing tools like ‘COM flash’ but the simplest among them all is the one using Thonny IDE. One might be aware of Thonny if they have used MicroPython before. Anyway, Thonny is a popular IDE platform used to write, debug, and run MicroPython/Python code. It supports multiple boards like Raspberry Pi Pico, ESP8266 Generic boards, ESP32 boards and others. The simple “Interpreter” option enables us to change from one board to others.
The other main reason why Thonny is used in this tutorial to put MicroPython on ESP8266 is because when we upload the firmware on the ESP board, we don’t need any other flashing tools. This makes the process of installing/updating firmware very easy. After this is done, let’s code using Thonny to blink on-board LED using MicroPython.
First, we need to set up our computer/laptop to upload the code. This can be done in 3 simple steps:
- Download and Install Thonny IDE
- Add MicroPython to ESP8266
- Upload blink-LED code
To download Thonny, go to “thonny.org” and a link to download will be right on the top. Click on the link to download Thonny that is correct for your system specification. The file will automatically download once you click on the link. Open the downloaded file and the installation process will automatically begin.
The installation process will ask few standard questions. The installation process is simple and will only take a few seconds to finish up. After this, open Thonny.
As shown in the figure, Thonny IDE will have the top space to write the code and the top bar will have the options to save file, edit, view, run and help. In the bottom interpreter called “shell” will show the output and also have a few options at the bottom right end of the screen, using this we can change the interpreter.
How to install MicroPython for ESP8266?
Click on the bottom right corner of the Thonny window. It shows “Python” or other interpreter that would be selected by default. Click on that and on “Configure interpreter…” to change the interpreter that we want.
This will open a window as shown in the figure. Click on the interpreter options and select “MicroPython (ESP8266)”.
Now click on “Install or update firmware”. This opens another window where you’ll have to select port and “Browse” firmware file (.bin file) to install. Let’s see how to do that first.
Go to “micropython.org” and on the top bar click on “Download”. In this page, you should be able to browse (scroll down and search) for ESP8266. Click on the 1MB file and download the latest stable version .bin file (in downloads or in the folder you desire. You should be able to locate the file location later). Now go back to Thonny where we left off (interpreter window) and click on “browse…” button and select this .bin file. Leave the rest of the settings as it is (default settings). Make sure “Erase flash before installing” is selected. Now, connect the ESP8266 board to your computer/laptop. Then you can see the port to which it is connected. Select this port in the Thonny Interpreter window. And click on “Install”. You can see in the same window that it erases the flash and rewrites the desired firmware. You should get a “Done!” after the process is successfully completed.
Note: If you get any error or if the installation process does not happen as desired, then search for “Getting started with MicroPython on the ESP8266” official website and it should lead you to documentation in the official website, “micropython.org”. Use this page to debug your trouble.
After successfully installing MicroPython on ESP8266, close the window, and click on “Stop” red button on the top bar to restart Thonny. If the shell still shows/asks to restart, select “MicroPython (ESP8266)” again from the bottom right corner. This should not give you any errors and you should be able to type code in the top workspace.
Components required for blinking the on-board LED using MicroPython:
- Connecting cable (USB mini to USB)
How to blink the on-board LED using MicroPython
Write the following code in the space provided and press the green run button. This should tell the ESP8266 to blink the LED according the code specification of delay.
led = machine.Pin(16, machine.Pin.OUT)
“import machine” module helps to work with functions related to hardware. “import time” module helps to work with time related functions. The next line is used to assign a variable and declare the pin 16 – which is the on-board LED pin number – as output. The “while(1):” is an always true case so the next part will run forever(as long as the board is connected). The “led.on()” function will turn the LED ON and the “time.sleep()” function will create a delay of half a second (because of 0.5). Similarly, the “led.off()” function will turn the LED OFF and again, a delay of half a second is created. This completes the code part of simply blinking the LED.
This is the beginning step to coding ESP8266 with MicroPython and the process of installing MicroPython firmware on ESP8266 is explained in the simplest form with one of the easiest tools and process. This beginner friendly guide helps hobbyists and learner to take a step ahead and learn to use the advantages and flexibility of MicroPython on small microcontroller boards like ESP8266.