ESP32 Machine Learning Simple App

Kirill Yatsenko
3 min readApr 5, 2021
Photo by Rock'n Roll Monkey on Unsplash

Intro

Recently I’ve started learning about Machine Learning and how I can integrate it with my embedded projects. Apparently, it’s not very hard. You don’t need any math background since you can utilize existing ML libraries which are easy to use. In this post, I’d like to introduce you to a “hello world” for Machine Learning and how you can run it on your ESP32 board. The example is taken from the TensorFlow official GitHub repository. I’ve just made some modifications so the output is more interesting than just logs.

Project Description

We will train our model to behave similarly to the sin function. The input would be an “x” value and the model should predict “sin x. I know it’s a big overload for using machine learning for such an easy task and you can just use a simple sin() function from the math.h header. But for learning purposes that’s alright. We will be using the TensorFlow platform for creating training and inference our model.

Prerequisites

Software: you should have ESP-IDF SDK installed on your system. Please check this guide. Also, clone the project’s repository from GitHub.

Hardware: only ESP32 is required

The flow of the code

The flow is the following: the “x” value would be an input to the model but it should be of the same type as that we trained our model. Then Tensorflow Lite Interpreter will fetch and run our model. In the end, we will get some output that we can use.

From the TinyML book

The model should be trained on the powerful hardware and then it should be minified to a small size so we can use it in the small platforms e.g ESP32. Luckily you don’t need to do this since the model is already included in the project. If you are interested in how to create and train ML models by using Google Colab free platform visit this site.

Running the demo

Clone this repository, navigate to the project’s folder, and run the following command.

idf.py flash monitor

After you’ve flashed the project you will see LED fading and lightning. Also in the terminal, you should see the logs from the board. This is our machine learning model predicting the values. How cool is that!

Possible issues

Any ESP32 dev board should work but you could have LED on another pin or it even may not be here, if so, you can use an external LED. Change the pin number inside output_handler.cc.

Changes that I’ve made

Comparing to the original project the output was the logs of “x” input and predicted “y” value. I found it boring. So I’ve modified the code and now it’s changing LED light intensity base on the “y” value.

output_handler.cc is the file responsible for controlling LED.

Credits

  1. TinyML Book by Pete Warden & Daniel Situnayake
  2. Learn ESP32 Course

Thanks for reading!

--

--