Getting started with Arduino Machine Learning and Edge Impulse: Keywords detection

In this tutorial, we want to cover how to get started with Arduino Machine Learning using Edge Impulse. We want to detect some basic keywords using machine learning and Arduino. In this tutorial, we will create a custom dataset using Edge Impulse to collect data. At the end of this post, you will be able to deploy a machine learning model on your Arduino Nano 33 BLE. It can recognize some custom keywords such as color names so that we can control an RGB WS2812 strip using voice commands.

Machine Learning at the edge is a really interesting topics and it is very useful to give to the devices the capability to execute some tasks. Moreover, using Machine Learning at the edge will help you to overcome privacy problems because the computation happens locally.

Edge Impulse is somehow revolutionizing the way we build machine learning models opening it to all people even if they don’t have machine learning knowledge. Using a simple and intuitive Web interface you can create your dataset, develop your machine learning model and train it.

How to develop a Machine Learning model with Arduino

Generally speaking, to build a machine learning model to use with Arduino you have to follow these steps:

  • Create your dataset
  • Build your machine learning model
  • Train it
  • Evaluate the accuracy
  • Deploy the model (in this tutorial, we will deploy this model on Arduino Nano 33 BLE)

Usually, to accomplish all the steps described above, you have to use Tensorflow lite micro and Arduino. With Arduino and Edge Impulse, all these processes are simplified because we can use a Web interface and we don’t have to deal with libraries, quantizations, and so on. Let’s see how to do it. Before starting, you have to create an Edge Impulse free account and a new project.

More useful resources on Machine Learning:

How to build a Tensorflow lite model for Arduino
How to build a KNN classifier for ESP32
Arduino Nano 33 BLE with EdgeImpulse Google dataset
Deep learning with ESP32-CAm: Image classification

How to create a custom dataset using Arduino and Edge Impulse

The first step, as described before, is creating a custom dataset. We will acquire samples directly from Arduino 33 Nano BLE microphone. In this tutorial, we want to recognize three different keywords:

  • Red
  • Green
  • Blue

Moreover, to make our model smarter we will acquire noise and unknown words. If you like, you can use Edge Impulse dataset to do it.

Then, click on the dataset item on the left menu and start acquiring samples of the red words. To do it, repeat several times the red words. The result is shown in the picture below:

Edge impulse dataset samples with Arduino Nano 33 BLE. Build a dataset for Machine Learning model with Arduino

Once you have acquired the samples, you have to split it:

How to split samples in your dataset in Edge Impulse

Repeat this process for all the keywords you want to detect. Moreover, you have to create another two datasets:

  • noise
  • unknown words

In the end, you will have 5 different labels. During the dataset creation, you should use a different voice so that the model will perform better. So ask for help from your friends. Be aware that the more samples you acquire more accurate will be your model.

Before going on, you have to split the dataset in two groups:

  • training
  • testing

Also in this case, Edge Impulse helps you to split your dataset. Go go to the dashboard and click on the balance dataset.

Creating your Edge Impulse Machine Learning model for Arduino Nano

Once you have created your dataset, the next step is building your machine learning model. Edge Impulse has several signal processing blocks that can be used to extract features from the samples acquire and learning blocks. In this case, we want to recognize the human voice, for this reason, we will use Mel Frequency Cepstral Coefficients (MFCC) audio processing and as a learning block a Neural Network. The final result is shown here:

Audio processing (MFCC)

Under MFCC block, you can check the features that will be extracted from the samples we acquired in the step before. These features will feed the Neural Network model.

If you select one of the samples acquired, you can visualize the DSP of the signal acquired:

Machine learning signal features

Neural Network block

Under neural network block, you can set some parameters. Usually, you need to change only the number of the training cycles.

Finally, we you are satisfied, you can start training your model. This is very fast and after the process complete, you have some important parameters for evaluating your model:

Edge Impulse Machine Learning model for Arduino Nano 33 BLE

You can improve your model, adding more samples. If you are satisfied you can download the result and you can use it with Arduino Nano 33 BLE.

How to use Machine Learning with Arduino Nano 33 BLE

In this last part, we will use the Edge Impulse Machine Learning with Arduino Nano 33 BLE. As said before, when you are satisfied with the accuracy of your model, you can download it and make it ready for Arduino. You can download everything you need to use Machine Learning with Arduino as a library. Therefore you have to import it into your Arduino IDE.

In this tutorial, we will use the Edge Impulse Machine Learning with Arduino to control a WS2812 LEDs. After you have imported the library, open the file named * _microphone_continous.

We have to modify this file adding the lines to hande the WS2812 LEDS. In the top of file add the following line:

#include <Adafruit_NeoPixel.h>
Code language: C++ (cpp)

Next, add this line:

// Neopixel Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, 2, NEO_GRB + NEO_KHZ800);
Code language: C++ (cpp)

In the setup() method, add this:

// Neopixel LEDS strip.begin(); strip.setBrightness(255); strip.show();
Code language: C++ (cpp)

Finally, in the loop() method, look for:

ei_printf(" %s: %.5f\n", result.classification[ix].label, result.classification[ix].value);
Code language: C++ (cpp)

and add the following lines:

for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) { ei_printf(" %s: %.5f\n", result.classification[ix].label, result.classification[ix].value); if (result.classification[ix].value > 0.6) { if (result.classification[ix].label[0] == 'B') { strip.fill(strip.Color(0,0,200)); } else if (result.classification[ix].label[0] == 'R') { strip.fill(strip.Color(200,0,0)); } else if (result.classification[ix].label[0] == 'G') { strip.fill(strip.Color(0,200,0)); } strip.show(); }
Code language: C++ (cpp)

That’s all. Enjoy your new machine learning model running on Arduino 33 Nano BLE.

Testing the Edge Impulse Machine Learning with Arduino Nano 33 BLE

In this last step, you can compile and run the code for Arduino Nano 33 BLE.

Now you can speak one of the keywords used before:

  • red
  • blue
  • green

If you have trained your model in the right way, your WS2812 Leds will change color.

Wrapping up

In this post, we have covered how to use Edge Impulse with Arduino Nano 33 BLE to build a machine learning model that can recognize some keywords. We have applied the model to a real use case controlling the WS2812 LEDs using your voice. You have learned how to create a dataset using Edge Impulse and Arduino, how to build a machine learning model and finally how to train it.

Using Edge Impulse, all the process gets simpler. Let me know what kind of project you will build.