Example Code for Arduino-UV Light (I2C Mode)
This article provides a comprehensive guide on setting up and coding the Gravity: LTR390-UV Sensor with an Arduino in I2C mode, covering hardware and software preparation, wiring diagrams, and sample code execution to measure ultraviolet radiation effectively.
Hardware Preparation
- DFRduino UNO R3 (SKU: DFR0216) ×1
- Gravity: LTR390-UV Sensor(SKU: SEN0540) ×1
- Gravity-4P I2C/UART Sensor Cable(included with SEN0540) × 1
Software Preparation
- Download Arduino IDE: Click to Download Arduino IDE
- Download the DFRobot_LTR390UV Library: DFRobot_LTR390UV Library
- For Arduino IDE V1.8.19 (or earlier), install the library manually: How to Add a Library
- For Arduino IDE V2.0.0 (or later), directly search for the "DFRobot_LTR390UV" library in the Library Manager and install it.
Wiring Diagram
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 ultraviolet radiation 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.eUVSMode);//Set UV mode
}
void loop()
{
uint32_t data = 0;
data = ltr390.readOriginalData();//Get UV raw data
Serial.print("data:");
Serial.println(data);
delay(1000);
}
Result
Serial print UV raw data (in the environment without direct sunlight on sunny day).

Was this article helpful?
