Data Ninja:

Data & Visual Analytics
Software Development
Hardware


Python as a Platform

Flashing Micropython onto Microcontrollers

Python as a Highly Versatile Language

Python is a very versatile programming language that works great for many purposes. A single-page website or a microservice can be setup within 15 minutes in Python, compared to roughly 30 minutes to 1 hour in C#. Here are some example projects that can be built within 5 minutes to 15 minutes:

Why Python as a Platform is Great?

Beyond Python's cross-platform capability, there are specific versions of Python that can run on microcontrollers, without a Operating System (OS). In this sense, Python acts as the operating platform that interacts with the hardware directly. There are great benefits for such mode of operations:

Microcontrollers

Both microcontrollers and microcontrollers can be defined as integrated circuits on a small chip so as to perform computing and controlling functions. For the differences between microprocessors and microcontrollers, see the great explanation on this webpage .
ESP8266-ESP01-S
ESP8266-ESP01-S Programmer

On the left is the WiFi-enabled ESP8266 ESP01 microchip. This module is a self-contained SOC (System On a Chip) that doesn’t necessarily need another microcontroller to manipulate inputs and outputs. The chip displayed here has 1MB flash and 4MB memory storage. It runs at 80mhz by default and can be tweaked to run at 160mhz. That's a lot of processing power, considering the size of the ESP01. The ESP01 is very thin, about 2mm (1mm for the PCB board and 1mm for the chips on board).

On the right is the 'programmer' for ESP01. Nothing fancy, it's just a adapter from the UART (serial) to USB. It allow the ESP module to be connected to PC for two key tasks: 1) Flash new firmware and 2) Upload & Delete Python scripts onto the ESP.


Flashing Micropython

ESP01 can come with variety of firmwares. The most common one is the Espressif which use C or Java alike language. The Python that is specifically written and optimized for microchips is called 'Micropython'. The package is roughly between 0.5mb to 1.0mb, depending on the version. There are specific versions that are developed for different chips. For this exercise, we need the version developed specifically for ESP8266.

Warning

Latest version of Micropython is slightly more than 0.5mb. Make sure the ESP01's flash size is larger than the size of the firmware

To flash Micropython, following hardware and software components are needed:


Below are the steps for flashing Micropython onto ESP8266-ESP01-S:

  1. Step 1: Mount EP01 onto Programmer
  2. Mount the ESP01 as shown in the image on the left below.

    Insert ESP01 into USB, WHILE connecting GPIO-0 to GROUND, using the short breadboard cable. Then use 'device manager' on Windows to find out the PORT NUMBER of the programmer.

    ESP8266-ESP01-S Mounted on Programmer
    ESP8266-ESP01-S Pins Layout

  3. Step 2: Install Driver for the Programmer

    When displaying the Port Number in 'Device Manager'. Look for the chip type of the programmer. The used in here is CH340, the driver can be downloaded from here

    Reboot the PC and reconnect the programmer with ESP01 mounted into the PC's USB port. To test whether ESP01 chip is in downloading (or flashing) mode, run python -m esptool --port COM7 flash_id. Note the COMX, number should be changed according to the actual port number shown. If the chip is in downloading mode, info of the chip will be displayed, as shown in the image on the right below.

    SERIAL PORT Number
    Verifying for Flashing Mode

  4. Step 3: The real stuff: Flashing Micropython
  5. There are two ways to do this:
    • The Easier Way:Use the flasher program. Note: set baud rate to 115200.
    • The Flexible Way:Command line. Using the example codes below:
    • 
                              cd C:\Users\Username\Downloads
      
                              python -m esptool --port COM7 erase_flash
      
                              python -m esptool --port COM7 --baud 115200 write_flash --flash_size=detect -fm dio 0 esp8266-20190529-v1.11.bin
                          

Connecting to ESP01 and Start Pythoning

Unplug and reconnect the programmer (still with ESP01 mounted), but without grounding GPIO-0 to GND. Now connect using putty (speed set to 115200). How the ESP01-S will work as in the RELP (interactive) mode. Try the following code to make sure it is working.

                
                    # Returning the current CPU frequency
                    import machine
                    machine.freq()
                
            

More helpful information on setting up the ESP01 can be found on the official page here. Examples of instruction can be found there including:

  1. Enable remote access for REPL
  2. Connect to WiFi AP

The next article will be focusing on the uploading and management of scripts on the ESP module using Ampy

© 2022 - DataNinja.ml