Example Code for Arduino-Ambient Light (UART Mode)

Last revision 2025/12/08

The article offers a detailed guide on using Arduino and the LTR390-UV sensor to measure ambient light in UART mode, including hardware and software setup, wiring instructions, and sample code for obtaining accurate light intensity readings.

Hardware Preparation

Software Preparation

Wiring Diagram

UART Connection Diagram

SEN0540-UART 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 5
UV Sensor D/T DFRduino UNO 4

Other Preparation Work

Note: Select I2C or UART mode by DIP switch.

Sample Code

UART Mode

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

#include "DFRobot_LTR390UV.h"
#include <SoftwareSerial.h>
#define UARTMODE //UART mode

SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
DFRobot_LTR390UV ltr390(/*addr =*/LTR390UV_DEVICE_ADDR, /*s =*/&mySerial);

void setup()
{
  #define UARTMODE
  mySerial.begin(9600);
  Serial.begin(115200);
  while(ltr390.begin() != 0){
    Serial.println(" Sensor initialize failed!!");
    delay(1000);
  }![SEN0540-UART mode obtains ambient light results](https://dfimg.dfrobot.com/62fba4cb025a892c98c6da64/wikien/e032889330d221bc2c3afb65d48f9bc9.png "SEN0540-UART mode obtains ambient light results")
  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 the 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(100);
}

Result

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

SEN0540-UART 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