Driver for the AM2320 temperature and humidity sensor.
- Documentation
- Repository
- Issues
- Discussions and questions
- Component page at the ESP Component Registry
idf.py add-dependency esp-idf-lib/am2320The AM2320 measures temperature and relative humidity and talks over I2C at
address 0x5C. I2C is the only interface the driver supports; the sensor also
speaks a single-wire protocol on the same pins, which this driver does not use.
The transport is MODBUS-RTU, not plain register access: every transaction is a request frame of three bytes followed by a reply that carries its own CRC16. The stock sensor does not acknowledge its own address until it has been woken up, which the driver does on every read.
| Pin | Connect to |
|---|---|
| VDD | 3.1 … 5.5 V. The module is usually marked 3.3–5.5 V; 5 V works, but the pull-ups then hold SDA/SCL at 5 V, which is too much for a 3.3 V ESP32 without a level shifter. |
| SDA | I2C SDA, pull-up to VDD |
| SCL | I2C SCL, pull-up to VDD |
| GND | GND |
The board has 4.7 kΩ … 10 kΩ pull-ups on SDA and SCL; most breakout boards already include them. The sensor works from 100 kHz; the driver uses 100 kHz.
#include <am2320.h>
i2c_dev_t dev;
ESP_ERROR_CHECK(am2320_init_desc(&dev, 0, CONFIG_EXAMPLE_I2C_MASTER_SDA,
CONFIG_EXAMPLE_I2C_MASTER_SCL));
i2cdev_init();
float temperature, humidity;
esp_err_t err = am2320_get_rht(&dev, &temperature, &humidity);
if (err == ESP_OK)
printf("temperature: %.1f C, humidity: %.1f %%\n", temperature, humidity);
else
printf("read failed: %s\n", esp_err_to_name(err));
i2cdev_done();
am2320_free_desc(&dev);The sensor accepts at most one measurement per two seconds; reading it faster returns the previous value.
Pass NULL for one of the output arguments of am2320_get_rht() to read only
the other value. In that case the driver requests the two bytes of the single
register pair instead of both.
A reading of 0xFFFF in a register means "not measured yet"; the driver then
returns NAN for that value and leaves the other one intact. Check with
isnan() rather than comparing to a number.
- The sensor has no configuration registers: no resolution, no averaging, no
heater, no calibration. There is nothing to set, so the driver has no
_set_functions. - A reply that arrives with a wrong CRC is rejected with
ESP_ERR_INVALID_CRC; a reply that arrives with a wrong function code or length is rejected withESP_ERR_INVALID_RESPONSE. In both cases the caller gets no partial data. am2320_free_desc()only frees the descriptor mutex. The I2C bus belongs to i2cdev and is released byi2cdev_done().am2320_get_model()reads the model ID register (two bytes at0x08), andam2320_get_version()/am2320_get_device_id()read0x0A/0x0B. The values are what the sensor reports; their meaning is not documented by the vendor beyond "model", "version" and "device id".- The driver holds the descriptor mutex for the whole transaction, so a descriptor can be shared between tasks, but a failed transaction costs the wake-up and reply delays of the AM2320 (800 µs + 1.5 ms) plus the I2C transfers.
For questions and discussions about the component, please use Discussions at esp-idf-lib/core.
Please read CONTRIBUTING.md at esp-idf-lib/core.