-
Notifications
You must be signed in to change notification settings - Fork 52
[Example] Color Detection
This example recognizes objects of different colors using a color sensor. While this is something that might be possible to do with hard-coded values, the use of machine learning and the ESP system offers a few advantages. One advantage is that this example can learn a range of color values for each object, using them to generate a probability distribution of colors that correspond to the object. The ESP system can also help with understanding whether or not two objects are too similar in color to be reliably distinguished.
We're using a TCS34725 color sensor breakout from Adafruit. Connect:
- TCS34725 Vin to Arduino 5V
- TCS34725 GND to Arduino GND
- TCS34725 SDA to Arduino SDA (or A4 on the Arduino Uno)
- TCS34725 SCL to Arduino SCL (or A5 on the Arduino Uno)
Place the Arduino and color sensor in an optically isolated environment. That is, when you place the object to be recognized in front of the color sensor, the color sensor should be completely enclosed so that it is illuminated only by the LED on the color sensor. Otherwise, the color readings will vary based on ambient light levels.
We're using a simple Arduino program that reads the color sensor values and sends them over USB serial to ESP. It requires the Adafruit TCS34725 library. The library is included in the ESP repository. To use it, set your Arduino sketchbook to the ESP/Arduino directory.
Download: ColorSensor.ino
We're using the user_color_sensor.cpp example in the ESP download. For the moment, there's no pre-compiled binary for this example, so you'll need to compile ESP from source. See the installation instructions. Be sure to uncomment the #include "examples/user_color_sensor.cpp"
line in user.cpp (and comment out any other #included example).
To use, record training data for each object you want to recognize. Be sure that the color sensor is completely enclosed when the object is in front of it. You may want to record multiple samples for each object, slightly varying the distance from the object to the color sensor (especially if the object is flexible), the portion of the object over the sensor (if the object varies in color), and with different levels of ambient light / shadow (if the object is partially transparent). That will provide ESP with a range of the color values to expect for your object.
This example includes a number of options, available in the configuration drop-down menu. See the descriptions in the menu for details.