TinyML: Serial to Google Drive

TinyML
MS Windows
Python
Arduino Nano 33 BLE
Lab Notes for Chapter 02 of TinyML Cookbook 2E
Author

Dennis Chua

Published

January 28, 2024

The first two exercises presented in the second chapter of TinyML Cookbook provide an easy introduction to communicating with a microcontroller board. In my case, I used an Arduino Nano board to download a simple sketch (a C++ program, in Arduino-speak). Later I ran a Python script to fetch data from the board and store it as a log file in my Google Drive. In both situations, the host PC communicates with the microcontroller over the serial port.

My lab setup: Arduino Nano 33 BLE v2 board (the “Tiny Machine Learning Kit”) and the Arduino IDE.

The sketch instructs the microcontroller to emit a text message every two seconds. The Arduino IDE provides an enclosed environment for the task, allowing us to download the code to read-only memory (ROM). Once the microcontroller is ready, it proceeds through an endless loop, emitting the text message via the PC serial port, which in turn is captured and displayed by the Arduino IDE console.

TinyML Cookbook 2E: 01_serial.ino

Moving on to the Python part, the script to read the text message needs to have exclusive access to the serial port. This means we need to end the Arduino IDE once we’ve downloaded the sketch to the board.

TinyML Cookbook 2E: 02_serial_to_gdrive.py

One question that comes up is identifying the system file for the serial port. In my set up, a Windows 11 host, this is COM3. But the book’s Python code identifies this in the UNIX way, i.e. /dev/ttyACM0. It’s a quick fix to replace this device name with COM3. Just to confirm, we can run the following script beforehand to enumerate all the serial ports in the host system.

import serial.tools.list_ports as port_list
ports = list(port_list.comports())
for p in ports:
    print (p)

This Python script returns COM3 - USB Serial Device (COM3) and COM1 - Communications Port (COM1)

The book instructs us in preparing our Google Drive to accept OAuth validated access from the Python script. Anyone tyring this for the first time has to log in to their Google Cloud account and create a new project, a step that can be confusing as the Google Cloud interface has change since TinyML Cookbook 2E’s publication. When provisioning the new Google Drive API, the book presents the basic pieces of the process. On the Google Cloud end however, the particular panels for configuring the credentials are in different places. All in all the task can be accomplished, albeit with a bit of patience and rooting about the Google Cloud web app.

In the end, we are rewarded with a log file created in our Google Drive. The Python script is time-bound as it writes data to this text file. The output is simply the text message repeated a few times. This is the same text we encoded in the Arduino sketch and sent to the microcontroller beforehand.