Example Code for Arduino-UV Light (UART Mode)

Last revision 2025/12/08

This article offers a comprehensive guide on using the LTR390-UV sensor with an Arduino UNO R3 in UART mode. It includes hardware and software setup, connection diagrams, sample code, and expected results for UV light measurement, providing readers with the necessary tools to implement accurate UV sensing in their projects.

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 ultraviolet radiation 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);
  }
  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.eUVSMode);//Set UV mode 
}

void loop()
{
  uint32_t data = 0;
  data = ltr390.readOriginalData();//Get UV raw data
  Serial.print("data:");
  Serial.println(data);
  delay(100);
}

Result

Serial print UV raw data (in the environment without direct sunlight on sunny day).

SEN0540-UART mode obtains ultraviolet radiation results

Was this article helpful?

TOP