Driver for the BH1900NUX temperature sensor.
- Documentation
- Repository
- Issues
- Discussions and questions
- Component page at the ESP Component Registry
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.
idf.py add-dependency esp-idf-lib/bh1900nux| 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.
#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 untilbh1900nux_one_shot()sets the one-shot bit, waits 35 ms (CONV_TIME_MS) and reads the result. Callingbh1900nux_one_shot()while the descriptor is in continuous mode returnsESP_ERR_INVALID_STATE; the mode is remembered inbh1900nux_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 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.
- 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 byi2cdev_done().
For questions and discussions about the component, please use Discussions at esp-idf-lib/core.
Please read CONTRIBUTING.md at esp-idf-lib/core.