The OPT4048 sensor outputs color as true "CIE" XYZ output so you can match it to your existing color space. It's also excellent for calculating color temperature, which is based on CIE, and as a bonus also has Lux calculation, up to 144 kLux for light measurement even in the brightest situations.
This example will visualize what the "CIE" XYZ data coming out the sensor actually means. Data will be plotted onto a chart depicting the full range of colors.
The example works with the following web page:
The WebSerial APIs used by the OPT4048 CIE Color Plotter page are currently only supported in Chrome based browsers.
Microcontroller Wiring
First wire up a OPT4048 to your board exactly as shown below. Here's an example of wiring a Feather RP2040 to the sensor with I2C using one of the handy STEMMA QT connectors:
-
Board STEMMA 3V to sensor VCC (red wire)
-
Board STEMMA GND to sensor GND (black wire)
-
Board STEMMA SCL to sensor SCL (yellow wire)
- Board STEMMA SDA to sensor SDA (blue wire)
The following is the OPT4048 wired to a Feather RP2040 using a solderless breadboard:
-
Board 3V to sensor VCC (red wire)
-
Board GND to sensor GND (black wire)
-
Board SCL to sensor SCL (yellow wire)
- Board SDA to sensor SDA (blue wire)
Following the instructions from the CircuitPython guide page, download the project bundle and copy the libraries and code file to your device.
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim C for Adafruit Industries # SPDX-License-Identifier: MIT """ This example reads color data from the OPT4048 sensor and outputs it in a format suitable for displaying on a web page using Web Serial API. It continuously measures CIE x,y coordinates, lux, and color temperature. This example works with the web interface in the /webserial directory of the gh-pages branch of the Arduino driver repo: https://github.com/adafruit/Adafruit_OPT4048/tree/gh-pages, which can be accessed at: https://adafruit.github.io/Adafruit_OPT4048/webserial/ """ import time from time import sleep import board from adafruit_opt4048 import OPT4048, ConversionTime, Mode, Range READ_INTERVAL = 0.1 # seconds i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller sensor = OPT4048(i2c) sensor.range = Range.AUTO sensor.conversion_time = ConversionTime.TIME_100MS sensor.mode = Mode.CONTINUOUS last_read_time = 0 while True: if time.monotonic() > last_read_time + READ_INTERVAL: try: last_read_time = time.monotonic() x, y, lux = sensor.cie print("---CIE Data---") print(f"CIE x: {x}") print(f"CIE y: {y}") print(f"Lux: {lux}") print(f"Color Temperature: {sensor.calculate_color_temperature(x,y)} K") print("-------------") except RuntimeError: # CRC check failed while reading data pass
Follow the instructions from the Arduino guide page to install the required libraries, then run the following code on your device.
/*! * @file opt4048_webserial.ino * * This example reads color data from the OPT4048 sensor and outputs it * in a format suitable for displaying on a web page using Web Serial API. * * It continuously measures CIE x,y coordinates, lux, and color temperature. * * This sketch works with the web interface in the /webserial directory, * which can be accessed at: https://adafruit.github.io/Adafruit_OPT4048/webserial/ */ #include <Wire.h> #include "Adafruit_OPT4048.h" // Create sensor object Adafruit_OPT4048 sensor; // Set how often to read data (in milliseconds) const unsigned long READ_INTERVAL = 100; unsigned long lastReadTime = 0; void setup() { // Initialize serial communication at 115200 baud Serial.begin(115200); // Wait briefly for serial to connect (not needed for all boards) delay(100); Serial.println(F("Adafruit OPT4048 WebSerial Example")); Serial.println(F("This sketch works with the OPT4048 CIE Color Plotter web page")); // Initialize the sensor if (!sensor.begin()) { Serial.println(F("Failed to find OPT4048 chip")); while (1) { delay(10); } } Serial.println(F("OPT4048 sensor found!")); // Set sensor configuration sensor.setRange(OPT4048_RANGE_AUTO); // Auto-range for best results across lighting conditions sensor.setConversionTime(OPT4048_CONVERSION_TIME_100MS); // 100ms conversion time sensor.setMode(OPT4048_MODE_CONTINUOUS); // Continuous mode } void loop() { // Only read at the specified interval unsigned long currentTime = millis(); if (currentTime - lastReadTime >= READ_INTERVAL) { lastReadTime = currentTime; // Calculate and display CIE chromaticity coordinates and lux double CIEx, CIEy, lux; if (sensor.getCIE(&CIEx, &CIEy, &lux)) { // Print the values in a format that can be easily parsed by the web page Serial.println(F("---CIE Data---")); Serial.print(F("CIE x: ")); Serial.println(CIEx, 8); Serial.print(F("CIE y: ")); Serial.println(CIEy, 8); Serial.print(F("Lux: ")); Serial.println(lux, 4); // Calculate and display color temperature double colorTemp = sensor.calculateColorTemperature(CIEx, CIEy); Serial.print(F("Color Temperature: ")); Serial.print(colorTemp, 2); Serial.println(F(" K")); Serial.println(F("-------------")); } else { Serial.println(F("Error reading sensor data")); } } // Check for any incoming serial commands if (Serial.available() > 0) { String command = Serial.readStringUntil('\n'); command.trim(); // Process any commands here if needed } }
Plotter Usage
Once the demo code is loaded onto the microcontroller, then open the OPT4048 CIE Color Plotter Page on your computer.
- Click on the Connect to Microcontroller button.
- Click on the Feather RP2040 or whichever microcontroller is connected via USB to the computer. Then click the Connect button.
The page will read data sent to it by the microcontroller and plot the CIE x/y values onto the CIE 1931 Chromaticity Diagram.
Page last edited June 03, 2025
Text editor powered by tinymce.