MicroPython Data Acquisition

Sensors take measurements of the physical environment such as pressure, temperature, position, distance, and sound. These measurements are the basis for most data-driven engineering applications. Raw measurements often require cleansing (filtering) to remove bad samples such as outliers, noise, or drift. Microcontrollers and microprocessors facilitate the collection and processing of data locally to the device. For example, a self-driving car may collect 25 GB/hr of data to enable self driving capabilities. Airplane jet engine collects approximately 50,000 GB/hr of data to monitor the health of the engine and detect early warning signs. Embedded systems use microcontrollers to collect the data and microprocessors analyze the data to extract valuable insights and recommend corrective actions. Cloud computing performs the analysis after the data is transferred to an online repository. Edge computing is a growing focus area to perform most of the calculations locally and send summary information to online repositories.

MicroPython / µPython

Most microcontrollers run custom build C code or similar lower-level programming languages build for limited resources. MicroPython can run on more capable microcontrollers (Arduino, ESP32, Teensy, Raspberry Pi Pico, and others). MicroPython is valuable for quick prototyping and reduced programming complexity. CircuitPython (from AdaFruit) is similar to MicroPython, but with an objective to be compatible with CPython. The community for MicroPython is larger with more supported boards.

Load Firmware

The example in this tutorial is with an Espressif ESP32 board.

Get started by loading MicroPython firmware on the ESP32 microcontroller.

Method 1: MicroPython IDE

The easiest method is Arduino Lab or Thonny to load the firmware. Arduino Lab for MicroPython is a lightweight editor to connect with an Arduino board, upload code, transfer files, and interact with a REPL (read–eval–print loop) terminal. Thonny is a Python Integrated Development Environment (IDE) for beginners and simplifies the process of running MicroPython code on a microcontroller. Thonny is a graphical interface that runs the esptool.py program for loading firmware. Open Thonny and select Tools...Options.

Select the correct port. The CP210x USB to UART Bridge VCP Driver may be required to communicate with the device over the USB connection. Also download and select the latest ESP32 firmware file. Press the Boot button on the ESP32 to enter a write mode for the flash memory.

Method 2: Command Terminal with esptool

The MicroPython documentation has instructions for loading the firmware with esptool.py.

pip install esptool

Run the esptool command from the command terminal. For Windows, search in the Device Manager for the correct COM port.

python -m esptool --chip esp32 --port COM5 erase_flash

For Linux and MacOS, search for a USB connected device such as /dev/ttyUSB0.

python -m esptool --chip esp32 --port /dev/ttyUSB0 erase_flash

Press the Boot button (bottom right corner) to enter a mode that allows the flash memory to be modified. Otherwise, there is an error such as "A fatal error occurred: Failed to connect to ESP32: Wrong boot mode detected (0x13)! The chip needs to be in download mode." See additional troubleshooting steps, if needed.

Next, run the command to load the firmware. Replace firmware.bin with the correct firmware name and the port COM5 with the correct port (such as /dev/ttyUSB0 for Linux).

python -m esptool --chip esp32 --port COM5 --baud 460800 write_flash -z 0x1000 firmware.bin

Beginning Application

Stop and restart the Thonny interpreter to verify that the firmware is installed correctly. Enter print('Hello, World!') in the text editor and save and run the program on the ESP32 MicroPython Device as hello.py.

The boot.py script runs whenever the ESP32 turns on, followed by main.py. Do not overwrite these files or the ESP32 could become non-responsive to commands and require a reflash of the firmware.

Next, run a program that blinks the ESP32 LED indefinitely. Save the file as blink.py.

import time
from machine import Pin
led=Pin(1,Pin.OUT)
while True:
    led.on()
    time.sleep(0.5)
    led.off()
    time.sleep(0.5)

Press the RST Reset button on the ESP32 to stop the program.

Activity

There are many inexpensive sensors to measure physical quantities.

For this activity, use a microcontroller (e.g. ESP32) and sensor(s) to record measurements and build a classifier or regression. Choose a machine learning model based on your goal. Use a classifier if you want to categorize data into discrete classes, or a regression model if you're predicting a continuous quantity. Train your model on a portion of the collected data. Test the model on a separate set of data to evaluate performance.


✅ Knowledge Check

1. What does the boot.py script do on the ESP32 when using MicroPython?

A. It runs a blinking LED program.
Incorrect. The boot.py script initializes the ESP32, but it doesn't necessarily run a blinking LED program.
B. It runs whenever the ESP32 turns on.
Correct. The boot.py script runs whenever the ESP32 turns on, before any other script like main.py.
C. It erases the flash memory.
Incorrect. The boot.py script doesn't erase the flash memory. It's a startup script.
D. It only runs when manually triggered.
Incorrect. The boot.py script runs automatically whenever the ESP32 is powered on or restarted.

2. What is the primary purpose of MicroPython?

A. To provide a heavy-weight solution for big servers.
Incorrect. MicroPython is designed for microcontrollers which are lightweight systems, not for heavy-duty server solutions.
B. To run on more capable microcontrollers like ESP32 and Raspberry Pi Pico.
Correct. MicroPython can run on more capable microcontrollers and is valuable for quick prototyping.
C. To replace the use of C++ in all applications.
Incorrect. While MicroPython provides an alternative to traditional C code on microcontrollers, it is not designed to replace C++ in all applications.
D. To run on all devices without any hardware constraints.
Incorrect. MicroPython is designed for microcontrollers, which means it operates within the constraints of these devices.