Get Started With Machine Learning on Arduino
Learn how to train and use machine learning models with the Arduino Nano 33 BLE Sense
This post was originally published by Sandeep Mistry and Dominic Pajak on the TensorFlow blog.
Important notice! The TensorFlow Lite Micro Library is no longer available in the Arduino Library Manager. This library will need to be manually downloaded, and included in your IDE.
Introduction
Arduino is on a mission to make machine learning simple enough for anyone to use. We’ve been working with the TensorFlow Lite team over the past few months and are excited to show you what we’ve been up to together: bringing TensorFlow Lite Micro to the Arduino Nano 33 BLE Sense. In this article, we’ll show you how to install and run several new TensorFlow Lite Micro examples that are now available in the Arduino Library Manager.
The first tutorial below shows you how to install a neural network on your Arduino board to recognize simple voice commands.
Next, we’ll introduce a more in-depth tutorial you can use to train your own custom gesture recognition model for Arduino using TensorFlow in Colab. This material is based on a practical workshop held by Sandeep Mistry and Don Coleman, an updated version of which is now online.
If you have previous experience with Arduino, you may be able to get these tutorials working within a couple of hours. If you’re entirely new to microcontrollers, it may take a bit longer.
We’re excited to share some of the first examples and tutorials, and to see what you will build from here. Let’s get started!
Note: The following projects are based on TensorFlow Lite for Microcontrollers which is currently experimental within the TensorFlow repo. This is still a new and emerging field!
Goals
- Learn the fundamentals of TinyML implementation and training.
- Use the Arduino_LSM9DS1 and Arduino_TensorFlowLite libraries
Hardware & Software Needed
- An Arduino Nano 33 BLE Sense board
- A Micro USB cable to connect the Arduino board to your desktop machine
- To program your board, you can use the Arduino Web Editor or install the Arduino IDE. We’ll give you more details on how to set these up in the following sections
- TensorFlow Lite Micro Library (download only available via GitHub).
The Arduino Nano 33 BLE Sense has a variety of onboard sensors meaning potential for some cool TinyML applications:
- Voice – digital microphone
- Motion – 9-axis IMU (accelerometer, gyroscope, magnetometer)
- Environmental – temperature, humidity and pressure
- Light – brightness, color and object proximity
Unlike classic Arduino Uno, the board combines a microcontroller with onboard sensors which means you can address many use cases without additional hardware or wiring. The board is also small enough to be used in end applications like wearables. As the name suggests it has Bluetooth® Low Energy connectivity so you can send data (or inference results) to a laptop, mobile app or other Bluetooth® Low Energy boards and peripherals.
Tip: Sensors on a USB stick – 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.
Microcontrollers and TinyML
Microcontrollers, such as those used on Arduino boards, are low-cost, single chip, self-contained computer systems. They’re the invisible computers embedded inside billions of everyday gadgets like wearables, drones, 3D printers, toys, rice cookers, smart plugs, e-scooters, washing machines. The trend to connect these devices is part of what is referred to as the Internet of Things.
Arduino is an open-source platform and community focused on making microcontroller application development accessible to everyone. The board we’re using here has an Arm Cortex-M4 microcontroller running at 64 MHz with 1 MB Flash memory and 256 KB of RAM. This is tiny in comparison to Cloud, PC, or mobile but reasonable by microcontroller standards.
There are practical reasons you might want to squeeze ML on microcontrollers, including:
- Function – wanting a smart device to act quickly and locally (independent of the Internet).
- Cost – accomplishing this with simple, lower cost hardware.
- Privacy – not wanting to share all sensor data externally.
- Efficiency – smaller device form-factor, energy-harvesting or longer battery life.
There’s a final goal which we’re building towards that is very important:
- Machine learning can make microcontrollers accessible to developers who don’t have a background in embedded development
On the machine learning side, there are techniques you can use to fit neural network models into memory constrained devices like microcontrollers. One of the key steps is the quantization of the weights from floating point to 8-bit integers. This also has the effect of making inference quicker to calculate and more applicable to lower clock-rate devices.
TinyML is an emerging field and there is still work to do – but what’s exciting is there’s a vast unexplored application space out there. Billions of microcontrollers combined with all sorts of sensors in all sorts of places which can lead to some seriously creative and valuable TinyML applications in the future.
TensorFlow Lite for Microcontrollers Examples
The inference examples for TensorFlow Lite for Microcontrollers are now packaged and available through the Arduino Library Manager making it possible to include and run them on Arduino in a few clicks. In this section we’ll show you how to run them. The examples are:
- micro_speech – speech recognition using the onboard microphone
- magic_wand – gesture recognition using the onboard IMU
- person_detection – person detection using an external ArduCam camera
For more background on the examples you can take a look at the source in the TensorFlow repository. The models in these examples were previously trained. The tutorials below show you how to deploy and run them on an Arduino. In the next section, we’ll discuss training.
How to Run the Examples Using Arduino Create Web Editor.
Once you connect your Arduino Nano 33 BLE Sense to your desktop machine with a USB cable you will be able to compile and run the following TensorFlow examples on the board by using the Arduino Create web editor:
Focus On The Speech Recognition Example
One of the first steps with an Arduino board is getting the LED to flash. Here, we’ll do it with a twist by using TensorFlow Lite Micro to recognise voice keywords. It has a simple vocabulary of “yes ” and “no.” Remember this model is running locally on a microcontroller with only 256 KB of RAM, so don’t expect commercial ‘voice assistant’ level accuracy – it has no Internet connection and on the order of 2000x less local RAM available.
Note the board can be battery powered as well. As the Arduino can be connected to motors, actuators and more this offers the potential for voice-controlled projects.
How To Run The Examples Using the Arduino IDE
Alternatively you can use try the same inference examples using Arduino IDE application.
First, follow the instructions in the next section Setting up the Arduino IDE.
In the Arduino IDE, you will see the examples available via the File > Examples > Arduino_TensorFlowLite menu in the ArduinoIDE.
Select an example and the sketch will open. To compile, upload and run the examples on the board, and click the arrow icon:
Training a TensorFlow Lite Micro Model For Arduino
Next we will use ML to enable the Arduino board to recognise gestures. 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.
The idea for this tutorial was based on Charlie Gerard’s awesome Play Street Fighter with body movements using Arduino and Tensorflow.js. In Charlie’s example, the board is streaming all sensor data from the Arduino to another machine which performs the gesture classification in Tensorflow.js. We take this further and “TinyML-ify” it by performing gesture classification on the Arduino board itself. This is made easier in our case as the Arduino Nano 33 BLE Sense board we’re using has a more powerful Arm Cortex-M4 processor, and an on-board IMU.
We’ve adapted the tutorial below, so no additional hardware is needed – the sampling starts on detecting movement of the board. The original version of the tutorial adds a breadboard and a hardware button to press to trigger sampling. If you want to get into a little hardware, you can follow that version instead.
IDE Setup
1. First, let's make sure we have the drivers for the Nano 33 BLE boards installed. Select the board manager in the left panel, and search for "Nano 33 BLE" and install the "Arduino Mbed OS Nano Boards".
2. Also, let's make sure we have all the libraries we need installed. The Arduino_LSM9DS1 can be installed in the library manager in the IDE:
3. Finally, we need to download the TensorFlow Lite Micro Library from the repository. It is not available in the library manager, so it needs to be installed manually.
Once the
.zip
has been downloaded, in the Arduino IDE, choose Sketch > Include Library > Add .ZIP Library and select the file.You may need to restart the IDE for it to work.
Streaming Sensor Data From the Arduino Board
First, we need to capture some training data. You can capture sensor data logs from the Arduino board over the same USB cable you use to program the board with your laptop or PC.
Arduino boards run small applications (also called sketches) which are compiled from .ino format Arduino source code, and programmed onto the board using the Arduino IDE or Arduino Create.
With the sketch we are creating we will do the following:
- Monitor the board’s accelerometer and gyroscope
- Trigger a sample window on detecting significant linear acceleration of the board
- Sample for one second at 119Hz, outputting CSV format data over USB
- Loop back and monitor for the next gesture
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 – this we can cover in another blog. For now, you can just upload the sketch and get sampling.
The complete sketch can be found below:
1/*2 IMU Capture3 This example uses the on-board IMU to start reading acceleration and gyroscope4 data from on-board IMU and prints it to the Serial Monitor for one second5 when the significant motion is detected.6 You can also use the Serial Plotter to graph the data.7 The circuit:8 - Arduino Nano 33 BLE or Arduino Nano 33 BLE Sense board.9 Created by Don Coleman, Sandeep Mistry10 Modified by Dominic Pajak, Sandeep Mistry11 This example code is in the public domain.12*/13
14#include <Arduino_LSM9DS1.h>15
16const float accelerationThreshold = 2.5; // threshold of significant in G's17const int numSamples = 119;18
19int samplesRead = numSamples;20
21void setup() {22 Serial.begin(9600);23 while (!Serial);24
25 if (!IMU.begin()) {26 Serial.println("Failed to initialize IMU!");27 while (1);28 }29
30 // print the header31 Serial.println("aX,aY,aZ,gX,gY,gZ");32}33
34void loop() {35 float aX, aY, aZ, gX, gY, gZ;36
37 // wait for significant motion38 while (samplesRead == numSamples) {39 if (IMU.accelerationAvailable()) {40 // read the acceleration data41 IMU.readAcceleration(aX, aY, aZ);42
43 // sum up the absolutes44 float aSum = fabs(aX) + fabs(aY) + fabs(aZ);45
46 // check if it's above the threshold47 if (aSum >= accelerationThreshold) {48 // reset the sample read count49 samplesRead = 0;50 break;51 }52 }53 }54
55 // check if the all the required samples have been read since56 // the last time the significant motion was detected57 while (samplesRead < numSamples) {58 // check if both new acceleration and gyroscope data is59 // available60 if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {61 // read the acceleration and gyroscope data62 IMU.readAcceleration(aX, aY, aZ);63 IMU.readGyroscope(gX, gY, gZ);64
65 samplesRead++;66
67 // print the data in CSV format68 Serial.print(aX, 3);69 Serial.print(',');70 Serial.print(aY, 3);71 Serial.print(',');72 Serial.print(aZ, 3);73 Serial.print(',');74 Serial.print(gX, 3);75 Serial.print(',');76 Serial.print(gY, 3);77 Serial.print(',');78 Serial.print(gZ, 3);79 Serial.println();80
81 if (samplesRead == numSamples) {82 // add an empty line if it's the last sample83 Serial.println();84 }85 }86 }87}
Visualizing Live Sensor Data Log From the Arduino Board
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.
- In the Arduino IDE, open the Serial Plotter Tools > Serial Plotter
- If you get an error that the board is not available, reselect the port: Tools > Port > portname (Arduino Nano 33 BLE)
- Pick up the board and practice your punch and flex gestures
- You’ll see it only sample for a one second window, then wait for the next gesture
- You should see a live graph of the sensor data capture (see GIF below)
When you’re done be sure to close the Serial Plotter window – this is important as the next step won’t work otherwise.
Capturing Gesture Training Data
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:
- Reset the board by pressing the small white button on the top
- Pick up the board in one hand (picking it up later will trigger sampling)
- In the Arduino IDE, open the Serial Monitor Tools > Serial Monitor
- If you get an error that the board is not available, reselect the port:
- Tools > Port > portname (Arduino Nano 33 BLE)
- Make a punch gesture with the board in your hand (Be careful whilst doing this!)
- Make the outward punch quickly enough to trigger the capture
- Return to a neutral position slowly so as not to trigger the capture again
- Repeat the gesture capture step 10 or more times to gather more data
- Copy and paste the data from the Serial Console to new text file called punch.csv
- Clear the console window output and repeat all the steps above, this time with a flex gesture in a file called flex.csv
- Make the inward flex fast enough to trigger capture returning slowly each time
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 outputform the Arduino straight to .csv file on the command line. With the Serial Plotter / Serial MOnitor windows close use:
1$ cat /dev/cu.usbmodem[nnnnn] > sensorlog.csv
Training in TensorFlow
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.
- Set up Python® environment
- Upload the punch.csv and flex.csv data
- Parse and prepare the data
- Build and train the model
- Convert the trained model to TensorFlow Lite
- Encode the model in an Arduino header file
The final step of the colab is generates the model.h file to download and include in our Arduino IDE gesture classifier project in the next section:
Let’s open the notebook in Colab and run through the steps in the cells – arduino_tinyml_workshop.ipynb
Classifying IMU Data
Next we will use model.h file we just trained and downloaded from Colab in the previous section in our Arduino IDE project:
We will be starting a new sketch, you will find the complete code below:
1/*2 IMU Classifier3 This example uses the on-board IMU to start reading acceleration and gyroscope4 data from on-board IMU, once enough samples are read, it then uses a5 TensorFlow Lite (Micro) model to try to classify the movement as a known gesture.6 Note: The direct use of C/C++ pointers, namespaces, and dynamic memory is generally7 discouraged in Arduino examples, and in the future the TensorFlowLite library8 might change to make the sketch simpler.9 The circuit:10 - Arduino Nano 33 BLE or Arduino Nano 33 BLE Sense board.11 Created by Don Coleman, Sandeep Mistry12 Modified by Dominic Pajak, Sandeep Mistry13 This example code is in the public domain.14*/15
16#include <Arduino_LSM9DS1.h>17
18#include <TensorFlowLite.h>19#include <tensorflow/lite/micro/all_ops_resolver.h>20#include <tensorflow/lite/micro/micro_error_reporter.h>21#include <tensorflow/lite/micro/micro_interpreter.h>22#include <tensorflow/lite/schema/schema_generated.h>23#include <tensorflow/lite/version.h>24
25#include "model.h"26
27const float accelerationThreshold = 2.5; // threshold of significant in G's28const int numSamples = 119;29
30int samplesRead = numSamples;31
32// global variables used for TensorFlow Lite (Micro)33tflite::MicroErrorReporter tflErrorReporter;34
35// pull in all the TFLM ops, you can remove this line and36// only pull in the TFLM ops you need, if would like to reduce37// the compiled size of the sketch.38tflite::AllOpsResolver tflOpsResolver;39
40const tflite::Model* tflModel = nullptr;41tflite::MicroInterpreter* tflInterpreter = nullptr;42TfLiteTensor* tflInputTensor = nullptr;43TfLiteTensor* tflOutputTensor = nullptr;44
45// Create a static memory buffer for TFLM, the size may need to46// be adjusted based on the model you are using47constexpr int tensorArenaSize = 8 * 1024;48byte tensorArena[tensorArenaSize] __attribute__((aligned(16)));49
50// array to map gesture index to a name51const char* GESTURES[] = {52 "punch",53 "flex"54};55
56#define NUM_GESTURES (sizeof(GESTURES) / sizeof(GESTURES[0]))57
58void setup() {59 Serial.begin(9600);60 while (!Serial);61
62 // initialize the IMU63 if (!IMU.begin()) {64 Serial.println("Failed to initialize IMU!");65 while (1);66 }67
68 // print out the samples rates of the IMUs69 Serial.print("Accelerometer sample rate = ");70 Serial.print(IMU.accelerationSampleRate());71 Serial.println(" Hz");72 Serial.print("Gyroscope sample rate = ");73 Serial.print(IMU.gyroscopeSampleRate());74 Serial.println(" Hz");75
76 Serial.println();77
78 // get the TFL representation of the model byte array79 tflModel = tflite::GetModel(model);80 if (tflModel->version() != TFLITE_SCHEMA_VERSION) {81 Serial.println("Model schema mismatch!");82 while (1);83 }84
85 // Create an interpreter to run the model86 tflInterpreter = new tflite::MicroInterpreter(tflModel, tflOpsResolver, tensorArena, tensorArenaSize, &tflErrorReporter);87
88 // Allocate memory for the model's input and output tensors89 tflInterpreter->AllocateTensors();90
91 // Get pointers for the model's input and output tensors92 tflInputTensor = tflInterpreter->input(0);93 tflOutputTensor = tflInterpreter->output(0);94}95
96void loop() {97 float aX, aY, aZ, gX, gY, gZ;98
99 // wait for significant motion100 while (samplesRead == numSamples) {101 if (IMU.accelerationAvailable()) {102 // read the acceleration data103 IMU.readAcceleration(aX, aY, aZ);104
105 // sum up the absolutes106 float aSum = fabs(aX) + fabs(aY) + fabs(aZ);107
108 // check if it's above the threshold109 if (aSum >= accelerationThreshold) {110 // reset the sample read count111 samplesRead = 0;112 break;113 }114 }115 }116
117 // check if the all the required samples have been read since118 // the last time the significant motion was detected119 while (samplesRead < numSamples) {120 // check if new acceleration AND gyroscope data is available121 if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {122 // read the acceleration and gyroscope data123 IMU.readAcceleration(aX, aY, aZ);124 IMU.readGyroscope(gX, gY, gZ);125
126 // normalize the IMU data between 0 to 1 and store in the model's127 // input tensor128 tflInputTensor->data.f[samplesRead * 6 + 0] = (aX + 4.0) / 8.0;129 tflInputTensor->data.f[samplesRead * 6 + 1] = (aY + 4.0) / 8.0;130 tflInputTensor->data.f[samplesRead * 6 + 2] = (aZ + 4.0) / 8.0;131 tflInputTensor->data.f[samplesRead * 6 + 3] = (gX + 2000.0) / 4000.0;132 tflInputTensor->data.f[samplesRead * 6 + 4] = (gY + 2000.0) / 4000.0;133 tflInputTensor->data.f[samplesRead * 6 + 5] = (gZ + 2000.0) / 4000.0;134
135 samplesRead++;136
137 if (samplesRead == numSamples) {138 // Run inferencing139 TfLiteStatus invokeStatus = tflInterpreter->Invoke();140 if (invokeStatus != kTfLiteOk) {141 Serial.println("Invoke failed!");142 while (1);143 return;144 }145
146 // Loop through the output tensor values from the model147 for (int i = 0; i < NUM_GESTURES; i++) {148 Serial.print(GESTURES[i]);149 Serial.print(": ");150 Serial.println(tflOutputTensor->data.f[i], 6);151 }152 Serial.println();153 }154 }155 }156}
- Create a new tab in the IDE. When asked name it model.h
- Open the model.h tab and paste in the version you downloaded from Colab
- Upload the sketch: Sketch > Upload
- Open the Serial Monitor: Tools > Serial Monitor
- Perform some gestures
- The confidence of each gesture will be printed to the Serial Monitor (0 = low confidence, 1 = high confidence)
- Congratulations you’ve just trained your first ML application for Arduino!
For added fun the Emoji_Button.ino example shows how to create a USB keyboard that prints an emoji character in Linux and macOS. Try combining the Emoji_Button.ino example with the IMU_Classifier.ino sketch to create a gesture controlled emoji keyboard.
Conclusion
It’s an exciting time with a lot to learn and explore in TinyML. We hope this blog has given you some idea of the potential and a starting point to start applying it in your own projects. Be sure to let us know what you build and share it with the Arduino community.
For a comprehensive background on TinyML and the example applications in this article, we recommend Pete Warden and Daniel Situnayake’s new O’Reilly book “TinyML: Machine Learning with TensorFlow on Arduino and Ultra-Low Power Microcontrollers.”
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.