bh1900nux: Driver for BH1900NUX temperature sensor
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
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: |
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
#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 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 byi2cdev_done().
API Reference
Defines
- BH1900NUX_I2C_ADDR_BASE
See full list in datasheet.
Enums
- enum bh1900nux_fault_queue_t
Fault queue size.
Values:
- enumerator BH1900NUX_FAULTS_1
1 fault to trigger ALERT pin
- enumerator BH1900NUX_FAULTS_2
2 faults to trigger ALERT pin
- enumerator BH1900NUX_FAULTS_4
4 faults to trigger ALERT pin
- enumerator BH1900NUX_FAULTS_6
6 faults to trigger ALERT pin
- enum bh1900nux_alert_polarity_t
ALERT pin polarity.
Values:
- enumerator BH1900NUX_ALERT_LOW
ALERT active low (default)
- enumerator BH1900NUX_ALERT_HIGH
ALERT active high.
- enum bh1900nux_mode_t
Device operating mode.
Values:
- enumerator BH1900NUX_MODE_CONTINUOUS
Continuous measurement mode (default)
- enumerator BH1900NUX_MODE_SHUTDOWN
Shutdown mode.
- enum bh1900nux_wait_time_t
Delay between measurements in continuous mode.
Values:
- enumerator BH1900NUX_WT_0
186240 * 16 / 450000 ~ 6.622s (Fosc = 450kHz)
- enumerator BH1900NUX_WT_1
186240 * 4 / 450000 ~ 1.655s (Fosc = 450kHz)
- enumerator BH1900NUX_WT_2
186240 / 450000 ~ 0.414s (Fosc = 450kHz)
- enumerator BH1900NUX_WT_3
93120 / 450000 ~ 0.207s (Fosc = 450kHz)
Functions
- esp_err_t bh1900nux_init_desc(bh1900nux_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
Initialize device descriptor.
- Parameters:
dev – Device descriptor
port – I2C port number
addr – I2C address
sda_gpio – SDA GPIO
scl_gpio – SCL GPIO
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_free_desc(bh1900nux_t *dev)
Free device descriptor.
- Parameters:
dev – Device descriptor
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_reset(bh1900nux_t *dev)
Software reset.
- Parameters:
dev – Device descriptor
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_set_mode(bh1900nux_t *dev, bh1900nux_mode_t mode)
Set device operating mode.
- Parameters:
dev – Device descriptor
mode – Operating mode
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_get_config(bh1900nux_t *dev, bh1900nux_wait_time_t *wt, bh1900nux_fault_queue_t *fq, bh1900nux_alert_polarity_t *ap)
Read current device config.
- Parameters:
dev – Device descriptor
wt – [out] Delay between measurements
fq – [out] Fault queue size
ap – [out] ALERT pin polarity
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_set_config(bh1900nux_t *dev, bh1900nux_wait_time_t wt, bh1900nux_fault_queue_t fq, bh1900nux_alert_polarity_t ap)
Configure device.
- Parameters:
dev – Device descriptor
wt – Delay between measurements
fq – Fault queue size
ap – ALERT pin polarity
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_one_shot(bh1900nux_t *dev, float *temp)
Made a single-shot measurement.
Works only when device is in shutdown mode.
- Parameters:
dev – Device descriptor
temp – [out] Temperature, deg.C
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_get_temperature(bh1900nux_t *dev, float *temp)
Read temperature register.
- Parameters:
dev – Device descriptor
temp – [out] Temperature, deg.C
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_get_t_low(bh1900nux_t *dev, float *temp)
Read lower temperature limit register.
- Parameters:
dev – Device descriptor
temp – [out] Temperature, deg.C
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_set_t_low(bh1900nux_t *dev, float temp)
Write lower temperature limit register.
- Parameters:
dev – Device descriptor
temp – Temperature, deg.C
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_get_t_high(bh1900nux_t *dev, float *temp)
Read higher temperature limit register.
- Parameters:
dev – Device descriptor
temp – [out] Temperature, deg.C
- Returns:
ESP_OKon success
- esp_err_t bh1900nux_set_t_high(bh1900nux_t *dev, float temp)
Write higher temperature limit register.
- Parameters:
dev – Device descriptor
temp – Temperature, deg.C
- Returns:
ESP_OKon success
- struct bh1900nux_t
#include <bh1900nux.h>Device descriptor.