Example Code for Arduino-Ambient Light (I2C Mode)

Last revision 2025/12/08

This article explains how to use the LTR390UV sensor with Arduino to measure ambient light intensity in I2C mode, including hardware setup, software installation, wiring instructions, and a sample code for accurately capturing light data.

Hardware Preparation

Software Preparation

Wiring Diagram

I2C Connection Diagram

SEN0540-I2C Connection Diagram

Sensor Pin name Controller board Pin name
UV Sensor + DFRduino UNO 5V
UV Sensor - DFRduino UNO GND
UV Sensor C/R DFRduino UNO SCL
UV Sensor D/T DFRduino UNO SDA

Other Preparation Work

Note: Select I2C or UART mode by DIP switch.

Sample Code

I2C Mode

The function of the following code is to obtain the value of ambient light for this sensor in I2C communication mode.

#include "DFRobot_LTR390UV.h"
DFRobot_LTR390UV ltr390(/*addr = */LTR390UV_DEVICE_ADDR, /*pWire = */&Wire);

void setup()
{
  Serial.begin(115200);
  while(ltr390.begin() != 0){
    Serial.println(" Sensor initialize failed!!");
    delay(1000);
  }
  Serial.println(" Sensor  initialize success!!");
  ltr390.setALSOrUVSMeasRate(ltr390.e18bit,ltr390.e100ms);//18-bit data, sampling time of 100ms 
  ltr390.setALSOrUVSGain(ltr390.eGain3);//Gain of 3
  ltr390.setMode(ltr390.eALSMode);//Set ambient light mode 
}
void loop()
{
  float als = 0;
  als = ltr390.readALSTransformData();//Get data converted from ambient light intensity, which can only be used in ambient light mode
  Serial.print("ALS:");
  Serial.print(als);
  Serial.println("Lux");
  delay(1000);
}

Result

Serial print ambient light data (in an office with strong light).

SEN0540-I2C mode obtains ambient light results

Additional Information

Note: Ambient Light Reference Range

Place/Environment Illuminance
Sunny day 30000-300000 lux
Indoors on sunny day 100-1000 lux
Cloudy day 3000-10000 lux
Outdoors on cloudy day 50-500 lux
Indoors on cloudy day 5-50 lux
Indoors at dusk 10 lux
At sunrise and sunset 300 lux
At night 0.001-0.02 lux
Moonlit evening 0.02-0.3 lux
Lightless office 30-50 lux
Exposure to flashlight light 8000-15000 lux

Was this article helpful?

TOP