Flashing MicroPython

In this tutorial, you will learn how to flash MicroPython on a board; this way, you will be able to use python algorithms on your dev boards.

For the development of this tutorial, was used an ESP8266, and in the final is presented to you three example algorithms written in python to upload to your board. The first example shows you how to blink a LED, the second present to you how to obtain button state information from the board, and the third example shows you how to create interruptions.

Parts Required

As has been said, this tutorial firstly shows how to flash the ESP board and then presents some implementation examples. For each part, the following is needed:

Flash with Micropython

  • Python previously installed;
  • CH340 drivers previously installed, you can download it here;
  • MicroPython files for ESP8266, you can download it here;
  • ESP8266 dev board.

Example 1 – Blink a led

In this example is used the onboard LED but you can use one LED that is not on the dev board.

Example 2 – REPL

Example 3 – Interruptions

In this example is used the onboard LED and Button but you can use individual components that are not on the dev board.

Flashing MicroPython

In this section, you will learn how to install MicroPython on your board. The board used is an ESP8266. This development board has a CH340 converter, which converts the USB to a serial UART interface. This way, the first step of this tutorial will be to download and install CH340 drivers – you can download them here. After this, install python on your computer.

Since these installations are so simple, they will not be approached here – any doubt leave a comment in the post.

Now that you have all the pre-requisites installed proceed to install the esptool with the cmd. This is the tool that will be used to erase your ESP’s current flash and install a new one, in this case, the MicroPython. Firstly press Windows+R to open cmd and then run the command:

 pip install esptool

Now clean ESP8266 flash with the following command: esptool.py –port {port that your board is connected to} erase_flash. To know the port that your board is connected to, open the Device Manager Window and then open the Ports section (Figure 1). If you connect the board and no device appears then you may incorrectly install the CH340 drivers or not installed them at all.

Figure 1 – Device Manager Window. Here you can find the COM that your device is connected to.

As you can see in Figure 1, the ESP8266 is connected to COM4, this way the command that will be used to erase the present flash will be:

esptool --port COM4 erase_flash

Now download the MicroPython file here and then use in the cmd the following command to install it to the board: esptool.py –port {port} –baud 115200 write_flash –flash_size=detect -fm dout 0 {path to MicroPython file you downloaded}.

In the presented case of this tutorial, the command that will be used is the following:

esptool --port COM4 --baud 115200 write_flash --flash_size=detect -fm dout 0 esp8266-20210618-v1.16.bin

The installation should be quick and then it is done; your board is now flashed with MicroPython.

Example 1 – Blink a LED

Now that you have flashed the board with MicroPython is time to do the “Hello world” algorithm of microcontrollers, an algorithm to blink a LED. In the code below, make sure to change the Pin number to the correct of your board. In our case, we used the onboard LED, which can be controlled with the Pin 2.

from machine import Pin
import time

led = Pin(2, Pin.OUT)

while True:
    led.on()
    time.sleep_ms(500)
    led.off()
    time.sleep_ms(500)

We recommend the use of an IDE like the Visual Studio Code to upload the code to the board since there you can use the ampy package. To install it, use the following commanding the terminal of the IDE: pip install adafruit-ampy. Now that you have it installed, to upload the code to your development board use the following command ampy –port {port} put {file to upload}. Since our port is the COM4 and the file to upload is named main.py, for our specific example the command to run will be:

ampy --port COM4 put main.py

If you have any doubt about ampy package you can run the followin command:

ampy --help

Example 2 – REPL

In order to obtain responses from your board, you can use PuTTY. Download it here and install it. After this, configure the PuTTY like Figure 2, changing the COM to the one you connected your board. Press Open.

Figure 2 – Configuration of PuTTy for this specific case.

A window with black background should be opened. Press Ctrl+C to exit from the loop. Write the following code in Putty to obtain the state of the onboard button, as you can see in Figure 3.

Note that you may have to change the Pin used in the presented algorithm depending on the board you are using. In this case, the onboard button is Pin 0.

from machine import Pin
btn = Pin(0,Pin.IN)
btn.value()
Figure 3 – Using PuTTy to get the button state.

Example 3 – Interruptions

With the implementation of the code bellow is possible to use interruptions. To do this was used the onboard LED and button.

from machine import Pin
 import time

 led = Pin(2, Pin.OUT)
 btn = Pin(0, Pin.IN)

 def int_func(pin):
    if btn.value() == 1:
        led.on()
    else:
        led.off()
 
btn.irq(int_func)

To upload the code to the board use the same command of Example 1:

ampy --port COM4 put main.py

References

[1] https://docs.micropython.org/en/latest/esp8266/tutorial/intro.html#intro

1 thought on “Flashing MicroPython

Leave a Reply

Your email address will not be published. Required fields are marked *