Color Sensor
#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.
#Links
#Units
- Percent%
#Aliases
- TCS3472
#Functions
#JavaScript / Node.js
ColorSensorextends I2cComponentBaseColorSensor 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
interface ColorSensorValue {
r: number;
g: number;
b: number;
}#Constructor options
| Option | Type | Required | Description | Inherited from |
|---|---|---|---|---|
gain | number | optional | — | |
integrationTime | number | optional | — | |
sampleInterval | number | optional | — | |
pin | I2CPinID | required | — | I2cComponentBase |
#Events
| Event | Parameters | Description |
|---|---|---|
change | value: ColorSensorValue | — |
#Methods
get r→ numberGet current red channel (0–255).
get g→ numberGet current green channel (0–255).
get b→ numberGet current blue channel (0–255).
get i2cId→ I2CPinIDfrom I2cComponentBaseThe resolved I2C pin ID (e.g. "i2c-0"). Useful for logging or passing back to the Arduino sketch.
#Example
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)
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
#include "blokdots.h"
ColorSensor myComponent();#Constructor
ColorSensor(integrationTimeMs, gainMultiplier, sampleIntervalMs, minEmitIntervalMs, rgbThreshold, wire)Accepts plain numbers: ColorSensor sensor(700, 16, 200)
| Parameter | Type | Default |
|---|---|---|
integrationTimeMs | float | 50 |
gainMultiplier | uint8_t | 16 |
sampleIntervalMs | uint16_t | 50 |
minEmitIntervalMs | uint16_t | 100 |
rgbThreshold | uint16_t | 8 |
wire | TwoWire | Wire |
ColorSensor(bus, evtChanged, integrationTimeMs, gainMultiplier, sampleIntervalMs, minEmitIntervalMs, rgbThreshold, wire)| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
evtChanged | uint8_t | required |
integrationTimeMs | float | 50 |
gainMultiplier | uint8_t | 16 |
sampleIntervalMs | uint16_t | 50 |
minEmitIntervalMs | uint16_t | 100 |
rgbThreshold | uint16_t | 8 |
wire | TwoWire | Wire |
#Methods
integrationTimeFromMs(ms)→ static IntegrationTimeColorSensor::gainFromMultiplier(16) → TCS34725_GAIN_16X
| Parameter | Type | Default |
|---|---|---|
ms | float | required |
gainFromMultiplier(x)→ static Gain| Parameter | Type | Default |
|---|---|---|
x | uint8_t | required |
r()→ uint16_tg()→ uint16_tb()→ uint16_tc()→ uint16_trawR()→ uint16_trawG()→ uint16_trawB()→ uint16_trawC()→ uint16_tr8()→ uint8_tg8()→ uint8_tb8()→ uint8_tisReady()→ bool