81cf96aa16b4e27a895d221916935ef41edcf038
ESP32 ESP-IDF component for BH1750 ambient light sensor
Features
- I2C interface with configurable frequency up to 400 kHz (Fast Mode).
- Support for two I2C addresses: 0x23 (ADDR low) and 0x5C (ADDR high).
- One-time H-resolution measurement with 1 lux step.
- Built-in error statistics tracking and reset for diagnostics.
- Automatic lux conversion from raw sensor data.
- Sensor presence verification during initialization via I2C probe.
- Support multiple sensors on one device with zh_pca9548a.
Using
In an existing project, run the following command to install the component:
cd ../your_project/components
git clone https://github.com/aZholtikov/zh_bh1750.git
In the application, add the component:
#include "zh_bh1750.h"
Example
#include "zh_bh1750.h"
#define I2C_PORT (I2C_NUM_MAX - 1)
zh_bh1750_handle_t *bh1750_handle = NULL;
void app_main(void)
{
esp_log_level_set("zh_bh1750", ESP_LOG_ERROR);
i2c_master_bus_config_t i2c_bus_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_PORT,
.scl_io_num = GPIO_NUM_22,
.sda_io_num = GPIO_NUM_21,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t i2c_bus_handle;
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
zh_bh1750_init_config_t config = ZH_BH1750_INIT_CONFIG_DEFAULT();
config.i2c_handle = i2c_bus_handle;
zh_bh1750_init(&config, &bh1750_handle);
float lux = 0.0;
for (;;)
{
zh_bh1750_read(&bh1750_handle, &lux);
printf("Lux %0.2f\n", lux);
const zh_bh1750_stats_t *stats = zh_bh1750_get_stats();
printf("Number of i2c driver error: %ld.\n", stats->i2c_driver_error);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
Description
ESP32 ESP-IDF component for BH1750 ambient light sensor.
617 KiB
0 Stars
1 Watchers
0 Forks
Languages
C
98.3%
CMake
1.7%