Skip to content

[Example] Color Detection

David A. Mellis edited this page Aug 8, 2016 · 7 revisions

Overview

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.

Circuit

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. Otherwise, the color readings will vary based on ambient light levels.

Arduino Code

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

ESP Example

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).