Arduino
Using the OPT4048 breakout with Arduino involves wiring up the breakout to your Arduino-compatible microcontroller, installing the Adafruit_OPT4048 library, and running the provided example code.
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the sensor VIN.
Here is an Adafruit Metro wired up to the sensor using the STEMMA QT connector:
-
Board 5V 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)
Here is an Adafruit Metro wired up using a solderless breadboard:
-
Board 5V 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)
Library Installation
You can install the Adafruit_OPT4048 library for Arduino using the Library Manager in the Arduino IDE.
Search for Adafruit_OPT4048, and select the Adafruit_OPT4048 library, then click the Install button.
If asked about dependencies, click "Install all".
If the "Dependencies" window does not come up, then you already have the dependencies installed.
/*!
* @file test_opt4048.ino
*
* A basic demo for using the OPT4048 tristimulus XYZ color sensor
*
* This example reads the sensor values from all four channels (X, Y, Z, W),
* demonstrates setting and getting threshold values, and displays the results.
*/
#include <Wire.h>
#include "Adafruit_OPT4048.h"
// Create sensor object
Adafruit_OPT4048 sensor;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Wait for serial monitor to open
while (!Serial) {
delay(10);
}
Serial.println(F("Adafruit OPT4048 Tristimulus XYZ Color Sensor Test"));
// Initialize the sensor
if (!sensor.begin()) {
Serial.println(F("Failed to find OPT4048 chip"));
while (1) {
delay(10);
}
}
Serial.println(F("OPT4048 sensor found!"));
sensor.setRange(OPT4048_RANGE_AUTO); // Set range to auto
sensor.setConversionTime(OPT4048_CONVERSION_TIME_100MS); // Set conversion time to 100ms
sensor.setMode(OPT4048_MODE_CONTINUOUS); // Set operating mode to continuous
}
void loop() {
// Calculate and display CIE chromaticity coordinates and lux
double CIEx, CIEy, lux;
if (sensor.getCIE(&CIEx, &CIEy, &lux)) {
Serial.println(F("\nCIE Coordinates:"));
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"));
} else {
Serial.println(F("Error reading sensor data"));
}
delay(1000); // Read once per second
}
Page last edited May 28, 2025
Text editor powered by tinymce.