Driver for the BH1750 ambient light sensor.
- Documentation
- Repository
- Issues
- Discussions and questions
- Component page at the ESP Component Registry
The BH1750FVI is a digital ambient light sensor with a 16-bit ADC and a built-in photodiode. It reports illuminance in lux over I2C; the spectral response is close to the human eye, so the value is good for "how bright is the room" questions and useless for measuring irradiance or colour.
The chip has no registers to read back: commands are single bytes, and a measurement is read as two bytes from the same address. Everything the driver does is one of those single-byte commands or a two-byte read.
idf.py add-dependency esp-idf-lib/bh1750| Pin | Connect to |
|---|---|
| VCC | 2.4 … 3.6 V (the sensor is 3.3 V only; the module may have a regulator and level shifters, check the board) |
| GND | GND |
| SDA | I2C SDA, pull-up to VCC |
| SCL | I2C SCL, pull-up to VCC |
| ADDR | Leave floating or tie low for address 0x23 (BH1750_ADDR_LO), tie high for 0x5C (BH1750_ADDR_HI) |
The bus runs at up to 400 kHz; the driver configures 400 kHz. The address is
part of the chip, not of the module, so two sensors on one bus are possible
only if their ADDR pins differ. bh1750_init_desc() rejects any other address
with ESP_ERR_INVALID_ARG.
#include <bh1750.h>
i2c_dev_t dev;
ESP_ERROR_CHECK(bh1750_init_desc(&dev, BH1750_ADDR_LO, 0,
CONFIG_EXAMPLE_I2C_MASTER_SDA,
CONFIG_EXAMPLE_I2C_MASTER_SCL));
i2cdev_init();
ESP_ERROR_CHECK(bh1750_setup(&dev, BH1750_MODE_CONTINUOUS, BH1750_RES_HIGH));
uint16_t lux;
if (bh1750_read(&dev, &lux) == ESP_OK)
printf("Lux: %u\n", lux);
i2cdev_done();
bh1750_free_desc(&dev);Modes and resolutions:
| Mode | Resolution | Measurement time | Step |
|---|---|---|---|
BH1750_MODE_CONTINUOUS |
BH1750_RES_LOW |
~16 ms | 4 lx |
BH1750_MODE_CONTINUOUS |
BH1750_RES_HIGH |
~120 ms | 1 lx |
BH1750_MODE_CONTINUOUS |
BH1750_RES_HIGH2 |
~120 ms | 0.5 lx |
BH1750_MODE_ONE_TIME |
any of the above | the same | the same |
In continuous mode the chip measures again immediately after every read. In
one-time mode it performs a single measurement and then goes to power down by
itself: bh1750_setup() must be called again before the next
bh1750_read(), and the caller has to wait for the measurement time before
reading, because the driver does not sleep for you. Continuous mode is the
simpler option for periodic sampling.
bh1750_power_down() and bh1750_power_on() send the corresponding commands;
after power down the chip ignores measurements until it is powered on again.
bh1750_set_measurement_time() writes the internal measurement-time register
(0 … 254, default 69). It changes the integration time, so the conversion factor
in bh1750_read() no longer matches and the reported lux is off by the ratio of
the new time to 69. The driver always applies the factor for the default time.
bh1750_read()converts the raw value with the fixed factor10 / 12(1.2 counts per lux), which is correct for the default measurement time of 69. Ifbh1750_set_measurement_time()was used, scale the result yourself.bh1750_read()does not wait in one-time mode. Reading too early returns the previous or a zero value; the datasheet asks for at least the measurement time afterbh1750_setup().- The driver takes the descriptor mutex for each command and read, so the descriptor can be shared between tasks. It does not serialise "setup, wait, read" as a sequence: two tasks doing one-time measurements on the same descriptor will interleave.
- There is no interrupt or threshold support: the chip has none, and polling is the only way to notice a change.
- Saturation: in
BH1750_RES_LOWthe result saturates around 54612 lx; inBH1750_RES_HIGHthe raw value is 16-bit, so the reported maximum is about 54612 lx as well.
For questions and discussions about the component, please use Discussions at esp-idf-lib/core.
Please read CONTRIBUTING.md at esp-idf-lib/core.