Skip to main content

Color Sensor

I2C
input
Medium RAM
2.2.0

#Description

This sensor can detect the color of what is in front of it.

The Grove component has a little switch that needs to be toggled manually to control the LED.

For options change to take effect, please unplug and replug your board.

#Aliases

  • TCS3472

#Functions



#JavaScript / Node.js

ClassColorSensorextends I2cComponentBase

ColorSensor component for reading RGB color values from TCS34725/TCS3472 sensor. This is an I2C component that emits change events when the detected color changes.

#Value type

TypeScript
interface ColorSensorValue {
  r: number;
  g: number;
  b: number;
}

#Constructor options

OptionTypeRequiredDescriptionInherited from
gainnumberoptional
integrationTimenumberoptional
sampleIntervalnumberoptional
pinI2CPinIDrequiredI2cComponentBase

#Events

EventParametersDescription
changevalue: ColorSensorValue

#Methods

get rnumber

Get current red channel (0–255).

get gnumber

Get current green channel (0–255).

get bnumber

Get current blue channel (0–255).

get i2cIdI2CPinIDfrom I2cComponentBase

The resolved I2C pin ID (e.g. "i2c-0"). Useful for logging or passing back to the Arduino sketch.

#Example

JavaScript
import { Board, ColorSensor } from "@blokdots/components";

const board = new Board("/dev/ttyACM0");
await board.open();

const colorSensor = new ColorSensor({ board, pin: "example" });

colorSensor.on("change", (value) => {
  console.log("ColorSensor changed:", value);
});


#C++ (Arduino)

ClassColorSensorextends ComponentBase

Grove I2C Color Sensor V2.x (TCS3472 / TCS34725 family) Emits ONE event when RGB changes beyond threshold (atomic snapshot)

#Required libraries

  • #include <Wire.h>Wire
  • #include <Adafruit_TCS34725.h>Adafruit TCS34725 v1.4.4

#Setup

C++
#include "blokdots.h"

ColorSensor myComponent();

#Constructor

ColorSensor(integrationTimeMs, gainMultiplier, sampleIntervalMs, minEmitIntervalMs, rgbThreshold, wire)

Accepts plain numbers: ColorSensor sensor(700, 16, 200)

ParameterTypeDefault
integrationTimeMsfloat50
gainMultiplieruint8_t16
sampleIntervalMsuint16_t50
minEmitIntervalMsuint16_t100
rgbThresholduint16_t8
wireTwoWireWire
ColorSensor(bus, evtChanged, integrationTimeMs, gainMultiplier, sampleIntervalMs, minEmitIntervalMs, rgbThreshold, wire)
ParameterTypeDefault
busEventBusrequired
evtChangeduint8_trequired
integrationTimeMsfloat50
gainMultiplieruint8_t16
sampleIntervalMsuint16_t50
minEmitIntervalMsuint16_t100
rgbThresholduint16_t8
wireTwoWireWire

#Methods

integrationTimeFromMs(ms)static IntegrationTime

ColorSensor::gainFromMultiplier(16) → TCS34725_GAIN_16X

ParameterTypeDefault
msfloatrequired
gainFromMultiplier(x)static Gain
ParameterTypeDefault
xuint8_trequired
r()uint16_t
g()uint16_t
b()uint16_t
c()uint16_t
rawR()uint16_t
rawG()uint16_t
rawB()uint16_t
rawC()uint16_t
r8()uint8_t
g8()uint8_t
b8()uint8_t
isReady()bool