Using Google's AI (Teachable Machine) to Control an Arduino

by Diego Ojeda in Circuits > Arduino

3793 Views, 3 Favorites, 0 Comments

Using Google's AI (Teachable Machine) to Control an Arduino

logo.png
logo.png

In this project, we will see how to use the Teachable Machine to give instructions to an Arduino Nano. We will use it to move a servo but you can do whatever you want with it.

Supplies

We will need a computer with a webcam, an Arduino, a servo motor, and internet access.

Creating a Model With Teachable Machine

teachableMachine1.png
teachableMachine2.png
teachableMachine3.png
teachableMachine4.png
teachableMachine5.png

Go to Image Model - Teachable Machines. Create the different classes (objects) you want to create. Give each class a name that you can recognize later. You can upload images for each class using the webcam. It's usually a good idea to include an empty class (one with only the background). Next, click on "Train model." Once the model has loaded, you can make sure that it works well using the webcam.

Next, we are going to export the model by doing as follows: Click on "Export model", then select "Tensorflow" (not Tensorflow.js), then "Keras," and finally "Download my model." Extract that file and save the files in a folder.

You should have 2 files: one named "keras_model.h5" and another named "labels.txt."

Install Python 3.10

python1.png
python2.png

Now we are going to install Python. To do this, we will go to the Microsoft Store and install it from there. For this tutorial, I'm using Python 3.10.

Next, we will install different modules using pip. To check if you have "pip" installed, type the following command in the terminal: py -m ensurepip --upgrade. Normally, it should be installed, but if you don't have it installed, follow this tutorial here.

Before we start installing the packages, we need to make Windows accept files with more than 260 characters long. To do this, follow this tutorial here.

Next, enter the following commands in the Windows terminal:

pip install opencv-python
pip install "tensorflow<2.11"
pip install pyserial


Now, place the files "keras_model.h5" and "labels.txt" in a folder called "keras." Then, put the program "TeachableMachineaArduino.py" at the same level as the "keras" folder.

Modify the Program

programa1.png
programa2.png
programa3.png

To make the program work, you need to make the following modifications:


1) On line 15, set the COM port to which your Arduino is connected.


2) On lines 136 and 138, replace "Class1" and "Class2" with the names you have given to your classes in Teachable Machine. If you have more than 2 classes, you need to add them here.


3) OPTIONAL: You can change the desired precision by modifying line 89. By default, the precision is set to 90%.


Connect your Arduino via USB and run the Python code. If everything works correctly, you should see a window appear that detects the different classes.


If you encounter the error "Could not find the DLL(s) 'msvcp140_1.dll'. TensorFlow requires that these DLLs be installed in a directory that is named in your %PATH% environment variable...", you need to go to this link, install the corresponding version, and run the program. If you still have the error after restarting your computer, try copying those files (msvcp140_1.dll and msvcp140.dll) to the directory where you have Python installed and make sure that folder is included in your system's %PATH% environment variable.

Program the Arduino

To check the numbers received by the Arduino, we can use the "Comunication.ino" program.

If we want to use the Teachable Machine's results to move one servo or another, we can use the "MoverServos.ino" program.

This program is straightforward and serves as an example. Now it's up to you to create more significant projects with Teachable Machine and Arduino!