esp-idf-lib/bh1900nux

1.0.9

Latest
uploaded 5 days ago
Driver for BH1900NUX temperature sensor

Readme

# esp-idf-lib/bh1900nux

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

Driver for the BH1900NUX temperature sensor.

* [Documentation](https://esp-idf-lib.github.io/bh1900nux/)
* [Repository](https://github.com/esp-idf-lib/bh1900nux)
* [Issues](https://github.com/esp-idf-lib/bh1900nux/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/bh1900nux)

## Overview

The BH1900NUX is a digital temperature sensor in a TMP102-compatible register
layout: a 16-bit temperature register, a configuration register with the same
fields, and a pair of threshold registers for the ALERT output. It measures
from -40 to +125 °C with a 0.0625 °C step (12 bits) and talks over I2C at up to
400 kHz.

The chip can run in continuous mode, where it measures by itself and the newest
value is always in the temperature register, or in shutdown mode, where a single
one-shot conversion is triggered by writing a bit and the chip returns to
sleeping afterwards.

## Installation

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

## Hardware

| Pin | Connect to |
| --- | --- |
| VCC | 1.6 … 3.6 V |
| GND | GND |
| SDA | I2C SDA, pull-up to VCC |
| SCL | I2C SCL, pull-up to VCC |
| ALERT | Optional, open-drain output for the T_LOW/T_HIGH comparison |
| A0, A1 | Address select: `0x48` … `0x4F` (`BH1900NUX_I2C_ADDR_BASE` is `0x48`) |

The driver configures 400 kHz. `bh1900nux_init_desc()` rejects an address
outside `0x48` … `0x4F` with `ESP_ERR_INVALID_ARG`; the address pins are the only
way to put several sensors on one bus.

## Example

```c
#include <bh1900nux.h>

bh1900nux_t dev = { 0 };
ESP_ERROR_CHECK(bh1900nux_init_desc(&dev, BH1900NUX_I2C_ADDR_BASE, 0,
                                    CONFIG_EXAMPLE_I2C_MASTER_SDA,
                                    CONFIG_EXAMPLE_I2C_MASTER_SCL));
i2cdev_init();

ESP_ERROR_CHECK(bh1900nux_set_mode(&dev, BH1900NUX_MODE_CONTINUOUS));
ESP_ERROR_CHECK(bh1900nux_set_config(&dev, BH1900NUX_WT_2,
                                     BH1900NUX_FAULTS_1, BH1900NUX_ALERT_LOW));

float temp;
if (bh1900nux_get_temperature(&dev, &temp) == ESP_OK)
    printf("Temperature: %.02f °C\n", temp);

i2cdev_done();
bh1900nux_free_desc(&dev);
```

Modes:

- `BH1900NUX_MODE_CONTINUOUS` — the chip converts on its own; the interval is
  set by the wait time in the configuration register (`BH1900NUX_WT_0` …
  `BH1900NUX_WT_3`: about 6.622, 1.655, 0.414 and 0.207 s at the nominal
  450 kHz oscillator). `bh1900nux_get_temperature()` just reads the register.
- `BH1900NUX_MODE_SHUTDOWN` — the chip sleeps until `bh1900nux_one_shot()`
  sets the one-shot bit, waits 35 ms (`CONV_TIME_MS`) and reads the result.
  Calling `bh1900nux_one_shot()` while the descriptor is in continuous mode
  returns `ESP_ERR_INVALID_STATE`; the mode is remembered in
  `bh1900nux_t.mode`, not read back from the chip.

Thresholds for the ALERT output are written with `bh1900nux_set_t_low()` and
`bh1900nux_set_t_high()` and read back with the matching getters; the fault
queue (`BH1900NUX_FAULTS_1/2/4/6`) and the output polarity
(`BH1900NUX_ALERT_LOW/HIGH`) are part of `bh1900nux_set_config()` together with
the wait time. `bh1900nux_get_config()` returns those three fields; the mode is
not among them.

`bh1900nux_reset()` writes the reset register, waits 250 ms and reapplies the
mode that the descriptor remembers.

### The ALERT output

The chip drives ALERT when the measured temperature leaves the window set by
`bh1900nux_set_t_low()` and `bh1900nux_set_t_high()`: it needs
`BH1900NUX_FAULTS_1/2/4/6` consecutive out-of-window conversions to assert, and
the polarity is chosen by `BH1900NUX_ALERT_LOW` or `BH1900NUX_ALERT_HIGH`. The
pin is open-drain, so it needs a pull-up.

ALERT is an ordinary GPIO-level signal, and connecting it to a pin, an interrupt
or a logic analyser is the application's business, not the driver's: any GPIO
driver does it, and the sensor does not care who listens. The driver therefore
leaves the pin alone; it only configures the comparison on the chip.

## Limitations

- The TM bit, which selects comparator or interrupt mode, and the ALERT latch bit
  are not exposed by `bh1900nux_set_config()`. It rewrites only the wait time,
  the fault queue and the polarity, so those bits keep whatever the chip has,
  which is the power-on default unless something else changed them.
- The resolution is fixed at 12 bits (0.0625 °C) and the conversion takes up to
  35 ms, which is what `bh1900nux_one_shot()` waits for. There is no way to ask
  the chip for a faster, coarser result.
- The driver takes the descriptor mutex per transaction, so one descriptor can
  be shared between tasks, but a sequence such as "set mode, wait, read" is not
  serialised as a whole.
- `bh1900nux_free_desc()` only deletes the mutex; the I2C bus itself is managed
  by i2cdev and is released by `i2cdev_done()`.

## 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/bh1900nux^1.0.9"

download archive

Stats

  • Archive size
    Archive size ~ 18.05 KB
  • Downloaded in total
    Downloaded in total 109 times
  • Weekly Downloads Weekly Downloads (All Versions)
  • Downloaded this version
    This version: 1 time

Badge

esp-idf-lib/bh1900nux version: 1.0.9
|