Detecting Colors with the SparkFun Tristimulus Color Sensor

Pages
Contributors: -KJB-
Favorited Favorite 4

Read Measurements with a Serial Monitor

Now that we've installed the Arduino library, it's time to upload our first sketch to make sure everything is working properly and you are able to read basic measurements with your Serial Monitor in the Arduino IDE.

For this example you will need the SparkFun Tristimulus Color Sensor - OPT4048DTSR (Qwiic), a SparkFun RedBoard Qwiic, a Qwiic Cable, and a USB Micro-B Cable.

Using the Qwiic system, assembling the hardware is simple. Connect the RedBoard to one of the SparkFun Tristimulus Color Sensor Qwiic ports using your Qwiic cables (please remember to insert this cable in the correct orientation). Then connect the RedBoard to your computer via the MicroUSB cable and voila! You're ready to rock!

SparkFun Tristimulus Color Sensor to RedBoard

SparkFun Tristimulus Color Sensor to RedBoard

 

Alternatively, you can copy and paste the code below into a shiny new Arduino sketch:

#include "SparkFun_OPT4048.h"  // Include the SparkFun OPT4048 library
#include <Wire.h>  // Include the Wire library for I2C communication

SparkFun_OPT4048 colorSensor;  // Create an instance of the OPT4048 color sensor

void setup() {
 Serial.begin(115200);  // Initialize serial communication at 115200 baud
 Serial.println("OPT4048 Color Sensing Example");  // Print a message to the serial monitor

 Wire.begin();  // Initialize the I2C bus

 if (!colorSensor.begin()) {  // Try to initialize the color sensor
   Serial.println("OPT4048 not detected - check wiring or I2C address!"); 
   while (1);  // Enter an infinite loop to halt the program
 }

 colorSensor.setBasicSetup();  // Apply basic setup configuration to the sensor

 Serial.println("Sensor initialized and ready!");  // Print a success message
}

void loop() {
  float x = colorSensor.getCIEx();  // Get the CIE X value from the sensor
  float y = colorSensor.getCIEy();  // Get the CIE Y value from the sensor
  float z = 1 - x - y;  // Approximate the CIE Z value (this is a simplification)

  Serial.print("CIEx: ");  // Print label for CIE X value
  Serial.print(x, 4);  // Print CIE X value with 4 decimal places
  Serial.print(" CIEy: ");  // Print label for CIE Y value
  Serial.print(y, 4);  // Print CIE Y value with 4 decimal places
  Serial.print(" CIEz: ");  // Print label for CIE Z value
  Serial.print(z, 4);  // Print approximated CIE Z value with 4 decimal places

  String detectedColor = classifyColor(x, y, z);  // Classify the color based on CIE values
  Serial.print(" Detected color: ");  // Print label for detected color
  Serial.println(detectedColor);  // Print the classified color

  delay(600);  // Wait for 600ms before the next reading (3 channels * 200ms conversion time)
}

String classifyColor(float x, float y, float z) {
  float total = x + y + z;  // Calculate the sum of x, y, and z
  float r = x / total;  // Calculate the relative red component
  float g = y / total;  // Calculate the relative green component
  float b = z / total;  // Calculate the relative blue component

  // The following if statements check the RGB ratios to classify the color
  if (r > 0.4 && g < 0.3 && b < 0.3) return "Red";
  if (r < 0.3 && g > 0.4 && b < 0.3) return "Green";
  if (r < 0.3 && g < 0.3 && b > 0.4) return "Blue";
  if (r > 0.3 && g > 0.3 && b < 0.3) return "Yellow";
  if (r > 0.3 && g < 0.3 && b > 0.3) return "Purple";
  if (r < 0.3 && g > 0.3 && b > 0.3) return "Cyan";
  if (r > 0.3 && g > 0.3 && b > 0.3) return "White";
  if (r < 0.1 && g < 0.1 && b < 0.1) return "Black";

  return "Unknown";  // If no color matches, return "Unknown"
}

Make sure you've selected the correct board and port in the Tools menu and then hit the upload button. Once the code has finished uploading, go ahead and open a Serial Monitor. Set the baud rate to 115200. You should see output like this:

Color Readings