The ESP32-S3 AI camera from DFRobot has onboard Lux sensor for measuring the Light intensity. This sensor can also be used to control the IR LEDs that are there on the device.
As I am used to the ESP-IDF Framework for my development, I found that there was not a device driver for this particular sensor. In ESP-IDF it is called a component. This component is ready made code that can be added to any project and can be used inside the application code.
There are different ways to add components in ESP-IDF project. One of the simple ways is by adding idf_component.yml file inside a main folder. In this guide I will use this method. But you can also add component using add-component command.
The first step is to download the component from the following url.
ujur007/LTR-308ALS: LTR-308ALS Light intensity sensor ESP-IDF component
Once the component is downloaded put it in a separate components folder or another folder of your choice.
Then inside the .yml file put the following lines.
dependencies:
ltr308:
version: "*"
path: \components\ltr308
Inside your esp-idf project you can now add a few lines of code to get the Light intensity data.
Inside your app_main() function add the following lines of code. This will initialize the I2C interface and also initialize the sensor for Light intensity.
i2c_master_bus_config_t i2c_mst_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = -1,
.scl_io_num = 9,
.sda_io_num = 8,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t bus_handle;
i2c_new_master_bus(&i2c_mst_config, &bus_handle);
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = 0x53,
.scl_speed_hz = 400000,
};
i2c_master_dev_handle_t dev_handle;
i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle);
dev_handle = ltr308_device_create(bus_handle, LTR308_I2C_ADDR, 400000);
ltr308_enable(dev_handle);
ltr308_set_gain(dev_handle, gain_3x);
ltr308_set_resolution(dev_handle, e20_bit_400ms);
ltr308_set_rate(dev_handle, eRate_500ms);
float lux;
bool level = 0;
while (1) {
lux = ltr308_read_lux(dev_handle);
printf(" Lux: %.2f\n", lux);
vTaskDelay(2000 / portTICK_PERIOD_MS);
gpio_set_level(IR_LED_GPIO, level);
level = !level;
}
Once you have add this, you can now flash the code with flash command.
You can see the light intensity value changing on the command line.
If you find any issue you can drop the comments below.











