semiotmic2022
Based on:
The code of this section is here
Learn More →
Arduino is one of the main actors in making Machine Learning available on simple devices. In this seminar, we will work on how to install and run TensorFlow Lite Micro examples on a Arduino Nano 33 BLE Sense.
The Arduino Nano 33 BLE Sense board is one of the most used devices for TinyML experiments. It is an Arm Cortex-M4 microcontroller running at 64 MHz with 1MB Flash memory and 256 KB of RAM.
As the name suggests, it has Bluetooth LE connectivity so you can send data (or inference results) to a laptop, mobile app or other BLE boards and peripherals.
Connecting the BLE Sense board over USB is an easy way to capture data and add multiple sensors to single board computers without the need for additional wiring or hardware — a nice addition to a Raspberry Pi, for example.
To program this board, you can use the Arduino Web Editor or install the Arduino IDE.
The inference examples for TensorFlow Lite for Microcontrollers are packaged and available through the Arduino Library manager. For example, the micro_speech
allows to recognize, using TensorFlow Lite Micro, voice keywords. It has a simple vocabulary of “yes” and “no”.
Learn More →
Remember this model is running locally (no Internet connection) on a microcontroller with only 256KB of RAM, so don’t expect commercial ‘voice assistant’ level accuracy.
Regarding power use, the figures below show the evolution when "thinking" and when showing the results with the LED on.
This section describes the steps to deploy on a Nano 33 the TinyML code necessary to "understand" some specific movements. The objective is to detail the process involved so that all the necessary elements, from the creation of the dataset to the final deployment on the HW are shown.
DISCLAIMER:
This experiment is simply an example on how to use ML to perform a task. It is oriented to non-experts on neural networks and the Tensorflow framework.
Training neural network models is a long and difficult process, easily full of frustration…
This example relates to what is known as predictive maintenance. The idea is to recognize a pair of movements that are supposed to indicate the regular movement of a machine and alert when a non-standard movement is detected
We’ll capture motion data from the Arduino Nano 33 BLE Sense board, import it into TensorFlow to train a model, and deploy the resulting classifier onto the board.
Following the steps below sets up the Arduino IDE application used to both upload inference models to your board and download training data from it in the next section. There are a few more steps involved than using Arduino Create web editor because we will need to download and install the specific board and libraries in the Arduino IDE.
Arduino_TensorFlowLite
libraryArduino_LSM9DS1
library:Finally, plug the micro USB cable into the board and your computer and:
First, we need to capture some training data. You can capture sensor data logs from the Arduino board.
We’ll be using a pre-made sketch IMU_Capture.ino
which does the following:
The sensors we choose to read from the board, the sample rate, the trigger threshold, and whether we stream data output as CSV, JSON, binary or some other format are all customizable in the sketch running on the Arduino. There is also scope to perform signal preprocessing and filtering on the device before the data is output to the log. For now, you can just upload the sketch and get to sampling.
With that done, we can now visualize the data coming off the board. We’re not capturing data yet — this is just to give you a feel for how the sensor data capture is triggered and how long a sample window is. This will help when it comes to collecting training samples.
Learn More →
To capture data as a CSV log to upload to TensorFlow, you can use Arduino IDE > Tools > Serial Monitor to view the data and export it to your desktop machine:
Note the first line of your two csv files should contain the fields aX,aY,aZ,gX,gY,gZ.
Linux tip: if you prefer you can redirect the sensor log output from the Arduino straight to a .csv file on the command line. With the Serial Plotter / Serial Monitor windows closed use:
$ cat /dev/cu.usbmodem[nnnnn] > sensorlog.csv
In this example we recorded two movements: lateral.csv
and updown.csv
We’re going to use Google Colab to train our machine learning model using the data we collected from the Arduino board in the previous section. Colab provides a Jupyter notebook that allows us to run our TensorFlow training in a web browser. A locally running Jupyter notebook could obviously used, too.
The Colab will guide you through the following steps:
The final step generates the model.h
file to download and include in our Arduino IDE gesture classifier project in the next section.
Learn More →
Upload the shared file: arduino_tinyml_seminar.ipynb
Learn More →
Now, by combining in a project the model.h
file we just trained and downloaded from Colab in the previous section and the IMU_Classifier.ino
file, we obtain our movement classifier.
Congratulations, you’ve just trained your first ML application for Arduino!