Sunlight Sensor
#Description
This sensor is able to detect the sunlight and measure its UV, infrared, and visible light value.
#Links
#Units
- UV IndexUVI
- Lumenlm
#Aliases
- UV Sensor
#Functions
#JavaScript / Node.js
SunlightSensorextends I2cComponentBaseSunlightSensor component for reading UV index, infrared, and visible light from SI1145 sensor. This is an I2C component that emits change events when light levels change.
#Value type
interface SunlightSensorValue {
uv: number;
ir: number;
vis: number;
}#Constructor options
| Option | Type | Required | Description | Inherited from |
|---|---|---|---|---|
frequency | number | optional | — | |
pin | I2CPinID | required | — | I2cComponentBase |
#Events
| Event | Parameters | Description |
|---|---|---|
change | value: SunlightSensorValue | — |
#Methods
get uv→ numberGet current UV index.
get ir→ numberGet current infrared light level.
get vis→ numberGet current visible light level.
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, SunlightSensor } from "@blokdots/components";
const board = new Board("/dev/ttyACM0");
await board.open();
const sunlightSensor = new SunlightSensor({ board, pin: "example", frequency: 100 });
sunlightSensor.on("change", (value) => {
console.log("SunlightSensor changed:", value);
});#C++ (Arduino)
UV, Infrared, and Visible light sensor using Adafruit SI1145 Measures: - UV Index (0-11+) - Infrared light intensity (lux/arbitrary units) - Visible light intensity (lux/arbitrary units) Emits ONE event when any of the three values change significantly (atomic snapshot) Usage: - uvIndex() / emittedUV() - UV index value - infrared() / emittedIR() - Infrared light level - visible() / emittedVIS() - Visible light level - rawUV() / rawIR() / rawVIS() - Most recent samples (may be newer than last event)
#Required libraries
#include <Wire.h>— Wire#include <Adafruit_Sensor.h>— Adafruit Unified Sensor v1.1.15#include <Adafruit_SI1145.h>— Adafruit SI1145 Library v1.2.1
#Setup
#include "blokdots.h"
SunlightSensor myComponent(eventBus, MY_EVENT);#Constructor
SunlightSensor(bus, evtChange, uvThreshold, irThreshold, visThreshold, sampleIntervalMs, minEmitIntervalMs)| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
evtChange | uint8_t | required |
uvThreshold | float | 0.1f |
irThreshold | float | 10.0f |
visThreshold | float | 10.0f |
sampleIntervalMs | uint16_t | 100 |
minEmitIntervalMs | uint16_t | 200 |
#Methods
uvIndex()→ floatinfrared()→ floatvisible()→ floatemittedUV()→ floatAliases for consistency with JS API
emittedIR()→ floatemittedVIS()→ floatrawUV()→ floatOptional: most recent sample (may be newer than last event)
rawIR()→ floatrawVIS()→ floatemittedValue()→ const char*Format: "uv:ir:vis" (e.g., "1.50:200.00:300.00")
setUVThreshold(v)Optional: update thresholds at runtime
| Parameter | Type | Default |
|---|---|---|
v | float | required |
setIRThreshold(v)| Parameter | Type | Default |
|---|---|---|
v | float | required |
setVISThreshold(v)| Parameter | Type | Default |
|---|---|---|
v | float | required |