Skip to main content

Sunlight Sensor

I2C
input
Medium RAM
0.4.5

#Description

This sensor is able to detect the sunlight and measure its UV, infrared, and visible light value.

#Units

  • UV IndexUVI
  • Lumenlm

#Aliases

  • UV Sensor

#Functions



#JavaScript / Node.js

ClassSunlightSensorextends I2cComponentBase

SunlightSensor 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

TypeScript
interface SunlightSensorValue {
  uv: number;
  ir: number;
  vis: number;
}

#Constructor options

OptionTypeRequiredDescriptionInherited from
frequencynumberoptional
pinI2CPinIDrequiredI2cComponentBase

#Events

EventParametersDescription
changevalue: SunlightSensorValue

#Methods

get uvnumber

Get current UV index.

get irnumber

Get current infrared light level.

get visnumber

Get current visible light level.

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, 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)

ClassSunlightSensorextends ComponentBase

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

#Setup

C++
#include "blokdots.h"

SunlightSensor myComponent(eventBus, MY_EVENT);

#Constructor

SunlightSensor(bus, evtChange, uvThreshold, irThreshold, visThreshold, sampleIntervalMs, minEmitIntervalMs)
ParameterTypeDefault
busEventBusrequired
evtChangeuint8_trequired
uvThresholdfloat0.1f
irThresholdfloat10.0f
visThresholdfloat10.0f
sampleIntervalMsuint16_t100
minEmitIntervalMsuint16_t200

#Methods

uvIndex()float
infrared()float
visible()float
emittedUV()float

Aliases for consistency with JS API

emittedIR()float
emittedVIS()float
rawUV()float

Optional: most recent sample (may be newer than last event)

rawIR()float
rawVIS()float
emittedValue()const char*

Format: "uv:ir:vis" (e.g., "1.50:200.00:300.00")

setUVThreshold(v)

Optional: update thresholds at runtime

ParameterTypeDefault
vfloatrequired
setIRThreshold(v)
ParameterTypeDefault
vfloatrequired
setVISThreshold(v)
ParameterTypeDefault
vfloatrequired