esp-idf-lib/bh1750

1.1.9

Latest
uploaded 5 days ago
Driver for BH1750 light sensor

Readme

# esp-idf-lib/bh1750

[![Build examples](https://github.com/esp-idf-lib/bh1750/actions/workflows/build.yml/badge.svg)](https://github.com/esp-idf-lib/bh1750/actions/workflows/build.yml)
[![Build docs](https://github.com/esp-idf-lib/bh1750/actions/workflows/build-docs.yml/badge.svg)](https://github.com/esp-idf-lib/bh1750/actions/workflows/build-docs.yml)
[![Validation](https://github.com/esp-idf-lib/bh1750/actions/workflows/validate-component.yml/badge.svg)](https://github.com/esp-idf-lib/bh1750/actions/workflows/validate-component.yml)

Driver for the BH1750 ambient light sensor.

* [Documentation](https://esp-idf-lib.github.io/bh1750/)
* [Repository](https://github.com/esp-idf-lib/bh1750)
* [Issues](https://github.com/esp-idf-lib/bh1750/issues)
* [Discussions and questions](https://github.com/esp-idf-lib/core/discussions)
* [Component page at the ESP Component Registry](https://components.espressif.com/components/esp-idf-lib/bh1750)

## Overview

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.

## Installation

```sh
idf.py add-dependency esp-idf-lib/bh1750
```

## Hardware

| 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`.

## Usage

```c
#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.

## Limitations

- `bh1750_read()` converts the raw value with the fixed factor `10 / 12`
  (1.2 counts per lux), which is correct for the default measurement time of
  69. If `bh1750_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 after `bh1750_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_LOW` the result saturates around 54612 lx; in
  `BH1750_RES_HIGH` the raw value is 16-bit, so the reported maximum is about
  54612 lx as well.

## Support

For questions and discussions about the component, please use
[Discussions](https://github.com/esp-idf-lib/core/discussions)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).

## Contributing

Please read [CONTRIBUTING.md](https://github.com/esp-idf-lib/core/blob/main/CONTRIBUTING.md)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).

Links

Supports all targets

Maintainer

  • Ruslan V. Uss (@UncleRus) <unclerus@gmail.com>
To add this component to your project, run:

idf.py add-dependency "esp-idf-lib/bh1750^1.1.9"

download archive

Stats

  • Archive size
    Archive size ~ 16.75 KB
  • Downloaded in total
    Downloaded in total 6.6k times
  • Weekly Downloads Weekly Downloads (All Versions)
  • Downloaded this version
    This version: 4 times

Badge

esp-idf-lib/bh1750 version: 1.1.9
|