In this project we use the color and proximity sensor on the Arduino Nano 33 BLE Sense. We also need the new Arduino_KNN library. This library uses Tiny Machine Learning. In this project we will distinguish objects based on color.
This project is based on the Arduino project.
At the beginning of the program you indicate how many objects you want to recognize and what they are called. 30 ”sample readings” are taken from each object. When this process is finished and your Arduino starts to recognize objects, these values will be added to the database of that object. We have written the project so that all settings are configured in the serial monitor.
- Level - Medium 45% 45%
- Duration - 30/40 min 35% 35%
- Costs - € 36,95 complete 25% 25%
Step 2: programming Arduino BLE Sense
How does the code work:
In this project we will take a closer look at exactly how it all works. In principle you can just use the code, but because we use a more advanced code and a new (beta) library, we explain this in detail.
Of course, we are the first to import the necessary libraries. The library for the proximity / light sensor of the Arduino Nano 33 BLE Sense (Arduino_APDS9960.h) and the new machine learning library (Arduino_KNN.H).
Then we create a number of variables. One to indicate how many variables a color consists of. In this case we use RGB colors so we have three. One is about the amount of objects. This is adjusted in the void setup to a value of your choice. Then there is a variable that indicates how many color readings must be in the database before we start. This is in our project 30. There is also a String () array that keeps track of the object names and variables for how the color is stored and how bright the color should be minimal.
After defining the variables, we put them in a database. Now it is time for the setup.
In the void setup we start the serial port and send an initial message. Then we ask how many objects you want to distinguish. Now you can type a number and send it to the Arduino. This reads it out, adjusts the variable that is about the amount of objects. Then we go to defining the objects. We do this by holding sample colors above the sensor. The maximum is 3000 objects. Please note that there is a distinction based on color.
For each object you are asked for a name for this object. You type the name in the serial monitor and send it to the Arduino. Your object's label will now be changed to the name you entered. Now place your object above the sensor of the Arduino 33 BLE Sense and move the object for the best effect. 30 measurements are quickly made and stored. Repeat this process for each object.
Now that you have defined all your objects you can start recognizing them. You will be asked if you want to hold an object above the sensor and then one measurement will be taken. No new measurement is taken until the object has been removed from above the sensor. This prevents a spam of measurements. Every measurement you make in the "loop" is added to the database you made in the "void setup" so that the Arduino knows better and better which object is which color.
At the moment we do notice that there are some problems with distinguishing red, orange and yellow. This can of course depend on your lighting.
Here you see the code of this project. If you type it over you will learn better how this sketch works.