Example Code for Arduino-I2C Communication

Last revision 2026/02/05

This article details Arduino-I2C communication setup using FireBeetle ESP32-E and LTR390 UV sensor, including hardware preparation, wiring, and sample code for light intensity measurement.

Hardware Preparation

Wiring Diagram

DFR0654-I2C wiring diagram

Other Preparation Work

The FireBeetle 2 ESP32-E's I2C can be configured to any I/O port by passing relevant parameters. For ease of use, we have already provided default configuration for I2C which is fully compatible with Arduino. The default configured pins can be seen in the pinout diagram.

In this section, we will obtain the ambient light intensity by driving LTR390-UV light sensor based on the default I2C configuration. About how to use the LTR390-UV light sensor, please refer to the LTR390-UV Ultraviolet Light Sensor Wiki.

Sample Code

Program function: Read the light intensity in the current environment.

#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 rate 100ms 
  ltr390.setALSOrUVSGain(ltr390.eGain3);//3x Gain 
  ltr390.setMode(ltr390.eALSMode);//Set ambient light mode 
}
void loop()
{
  float als = 0;
  als = ltr390.readALSTransformData();//Read the converted data of ambient light, can only be used in ambient light mode
  Serial.print("ALS:");
  Serial.print(als);
  Serial.println("Lux");
  delay(1000);
}

Result

When the program is uploaded, open serial monitor and you can see that the UV sensor is constantly collecting the current light intensity value.

DFR0654-I2C result

Was this article helpful?

TOP