# esp-idf-lib/am2320
[](https://github.com/esp-idf-lib/am2320/actions/workflows/build.yml)
[](https://github.com/esp-idf-lib/am2320/actions/workflows/build-docs.yml)
[](https://github.com/esp-idf-lib/am2320/actions/workflows/validate-component.yml)
Driver for the AM2320 temperature and humidity sensor.
* [Documentation](https://esp-idf-lib.github.io/am2320/)
* [Repository](https://github.com/esp-idf-lib/am2320)
* [Issues](https://github.com/esp-idf-lib/am2320/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/am2320)
## Installation
```sh
idf.py add-dependency esp-idf-lib/am2320
```
## Overview
The 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.
## Hardware
| 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.
## Usage
```c
#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.
## Limitations
- 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 with
`ESP_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 by `i2cdev_done()`.
- `am2320_get_model()` reads the model ID register (two bytes at `0x08`), and
`am2320_get_version()` / `am2320_get_device_id()` read `0x0A` / `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.
## 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).
ccb7ca97e8a22bde8a85ac4869f7b1af2767e59c
idf.py add-dependency "esp-idf-lib/am2320^1.0.9"