UV Sensor

The Adafruit LTR390 measures ambient light (ALS) and UV index (UVS) in two selectable modes. It has a fixed I2C address. See the Adafruit learn guide for wiring and hardware details.

Connection

Call startUVSensor() inside preload(). The device is ready to use by the time setup() runs.

let sensor;

function preload() {
  sensor = startUVSensor(name?);
}

name string | false | null "uv"

Controls automatic event callback registration. With the default name:

function uvChanged(event) { /* new reading */ }

Pass false or null to disable and use addEventListener instead.

Methods

getGain() UVSensorGain

Returns the current sensor gain enum value.

getMode() UVSensorMode

Returns the current measurement mode: UV_SENSOR_MODE_ALS (ambient light) or UV_SENSOR_MODE_UVS (UV index).

getResolution() UVSensorResolution

Returns the current ADC resolution enum value.

getValue() number

Returns the last reading — an ALS lux count or UVS count depending on the current mode.

setGain(value) Promise<boolean>

Sets the sensor gain. Available constants:

ConstantGain
UV_SENSOR_GAIN_1×1
UV_SENSOR_GAIN_3×3 (default)
UV_SENSOR_GAIN_6×6
UV_SENSOR_GAIN_9×9
UV_SENSOR_GAIN_18×18

setMode(value) Promise<boolean>

Sets the measurement mode. The sensor can only operate in one mode at a time; switching modes takes effect on the next reading.

ConstantMode
UV_SENSOR_MODE_ALSAmbient light sensor (default)
UV_SENSOR_MODE_UVSUV index sensor

setResolution(value) Promise<boolean>

Sets the ADC resolution. Higher resolution increases integration time. Available constants:

ConstantResolution
UV_SENSOR_RESOLUTION_20_BIT20-bit
UV_SENSOR_RESOLUTION_19_BIT19-bit
UV_SENSOR_RESOLUTION_18_BIT18-bit (default)
UV_SENSOR_RESOLUTION_17_BIT17-bit
UV_SENSOR_RESOLUTION_16_BIT16-bit
UV_SENSOR_RESOLUTION_13_BIT13-bit

Methods (BaseDevice)

connect() Promise<Response>

Opens the USB connection and sends the start command to the firmware. Calling it multiple times is safe — subsequent calls return the same promise. In p5.js you do not need to call this manually; startXxx() calls it for you inside preload().

Properties

Properties (BaseDevice)

id { type, address }

Identifies the device. type is the DeviceType enum value, address is the I2C address the device was started with.

isConnected boolean

true once the USB connection has been established and the firmware has acknowledged the device. In p5.js this is always true by the time setup() runs, because startXxx() waits for the connection inside preload().

Events

changed(event) changed

Fires when a new reading is available. The value field reflects the current mode (ALS or UVS).

Event Argument

All event callbacks receive a single event argument. The event detail is available at event.detail and contains:

event.detail.value number — The latest sensor reading. Interpretation depends on the current mode: ALS count (lux) or UVS count (UV index).