# LTR303 driver for ESP32
A driver for the LITE-ON LTR-303ALS-01 digital light sensor.
[Datasheet](https://optoelectronics.liteon.com/upload/download/DS86-2013-0004/LTR-303ALS-01_DS_V1.pdf)
## Example application
```c
#include "esp_check.h"
#include <freertos/FreeRTOS.h>
#include "ltr303.h"
static const char *TAG = "app_main";
void app_main(void) {
i2c_master_bus_config_t i2c_mst_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_NUM_0,
.scl_io_num = CONFIG_LTR303_I2C_SCL,
.sda_io_num = CONFIG_LTR303_I2C_SDA,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = false,
};
i2c_master_bus_handle_t i2c_hdl;
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &i2c_hdl));
ESP_ERROR_CHECK(i2c_master_bus_reset(i2c_hdl));
ltr303_handle_t *hdl;
ESP_ERROR_CHECK(ltr303_new(&hdl, i2c_hdl));
// Wait fo device to power up
vTaskDelay(pdMS_TO_TICKS(200));
als_contr_gain_t gain = ALS_CONTR_GAIN_1X;
// Activate device
ESP_ERROR_CHECK(ltr303_als_contr_set(hdl, ALS_CONTR_GAIN_1X, ALS_SW_RESET_OFF,
ALS_ACTIVE, 10));
vTaskDelay(pdMS_TO_TICKS(10));
// Set integration time and measurement rate
als_int_time_t time = ALS_INT_TIME_100;
als_meas_rate_t rate = ASL_MEAS_RATE_500;
ESP_ERROR_CHECK(ltr303_als_meas_rate_set(hdl, time, rate, 10));
while (1) {
uint8_t status;
ESP_ERROR_CHECK(ltr303_als_status_get(hdl, &status, 10));
if (als_status_data_valid(status) == ALS_STATUS_DATA_INVALID) {
vTaskDelay(pdMS_TO_TICKS(10));
continue;
}
uint16_t ch0;
uint16_t ch1;
ESP_ERROR_CHECK(ltr303_als_data_get(hdl, &ch0, &ch1, 10));
float lux = ltr303_als_to_lux(gain, time, 1.0, ch0, ch1);
ESP_LOGI(TAG, "CH0 %d, CH1: %d, LUX: %.2f", ch0, ch1, lux);
vTaskDelay(pdMS_TO_TICKS(500));
};
}
```
idf.py add-dependency "halvfigur/ltr303^1.0.1"