2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
[![Arduino CI](https://github.com/RobTillaart/AM232X/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
2021-10-19 05:51:07 -04:00
|
|
|
[![Arduino-lint](https://github.com/RobTillaart/AM232X/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/AM232X/actions/workflows/arduino-lint.yml)
|
|
|
|
[![JSON check](https://github.com/RobTillaart/AM232X/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/AM232X/actions/workflows/jsoncheck.yml)
|
2021-01-29 06:31:58 -05:00
|
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/AM232X/blob/master/LICENSE)
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/AM232X.svg?maxAge=3600)](https://github.com/RobTillaart/AM232X/releases)
|
|
|
|
|
2021-10-19 05:51:07 -04:00
|
|
|
|
2020-05-07 03:53:25 -04:00
|
|
|
# AM232X
|
|
|
|
|
2021-10-19 05:51:07 -04:00
|
|
|
Arduino library for AM2320 AM2321 and AM2322 I2C temperature and humidity sensor.
|
2020-05-07 03:53:25 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-05-07 03:53:25 -04:00
|
|
|
## Description
|
|
|
|
|
|
|
|
AM232X is a sensor similar to the DHT12 with an I2C interface.
|
|
|
|
Although in theory this could enable multiple sensors on one bus
|
2021-04-07 07:31:22 -04:00
|
|
|
the AM232X has a fixed address **0x5C** so one need to implement a
|
|
|
|
multiplexing strategy to have multiple sensors in practice.
|
|
|
|
See multiplexing below.
|
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
This library works also for the **AM2315** which has a library of its own - https://github.com/RobTillaart/AM2315
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
|
|
|
|
#### Typical parameters
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-10-19 05:51:07 -04:00
|
|
|
| | range | accuracy | repeatability |
|
|
|
|
|:------------|:----------:|:--------:|:-------------:|
|
2022-06-17 06:53:44 -04:00
|
|
|
| Temperature | -40 - 80 | 0.5°C | ±0.1 |
|
2022-06-18 03:51:07 -04:00
|
|
|
| Humidity | 0.0 - 99.9 | ±3% | ±0.1 |
|
2021-10-19 05:51:07 -04:00
|
|
|
| Sample time | 2 seconds | | |
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
#### Hardware connection
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
```
|
2022-01-06 08:05:38 -05:00
|
|
|
// AM232X PIN layout AM2315 COLOR
|
|
|
|
// ============================================
|
|
|
|
// bottom view DESCRIPTION COLOR
|
2021-01-29 06:31:58 -05:00
|
|
|
// +---+
|
2022-01-06 08:05:38 -05:00
|
|
|
// |o | VDD RED
|
|
|
|
// |o | SDA YELLOW
|
|
|
|
// |o | GND BLACK
|
|
|
|
// |o | SCL GREY
|
2021-01-29 06:31:58 -05:00
|
|
|
// +---+
|
2022-01-06 08:05:38 -05:00
|
|
|
//
|
|
|
|
// do not forget pull up resistors between SDA, SCL and VDD.
|
2021-01-29 06:31:58 -05:00
|
|
|
```
|
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### I2C clock speed
|
2022-06-18 03:51:07 -04:00
|
|
|
|
|
|
|
The datasheet states the AM2320 should be used on 100 KHz I2C only.
|
|
|
|
|
|
|
|
TODO measure and verify (see AM2315)
|
2022-06-17 06:53:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
## One Wire Protocol
|
|
|
|
|
|
|
|
The AM2320, AM2321 and AM2322 can also be read by using a "OneWire" protocol.
|
|
|
|
This protocol is identical to the DHT22 and will only provide temperature,
|
|
|
|
humidity and a CRC. See issue #26.
|
|
|
|
|
|
|
|
If one wants to use this protocol, one can use https://github.com/RobTillaart/DHTNew
|
|
|
|
and optionally force the type to 22.
|
|
|
|
|
|
|
|
If problems arise with the "OneWire" protocol, please let me know and
|
|
|
|
file the issues under DHTNew.
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
## Interface
|
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
Since 0.4.2 the library provides specific classes for the AM2320, AM2321 and AM2322 which have the same interface.
|
|
|
|
|
2021-10-19 05:51:07 -04:00
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### Constructor
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
- **AM232X(TwoWire \*wire = &Wire)** constructor, default using Wire (I2C bus), optionally set to Wire0 .. WireN.
|
|
|
|
- **bool begin(uint8_t dataPin, uint8_t clockPin)** begin for ESP32 et al, to set I2C bus pins.
|
|
|
|
Returns true if device address 0x5C is connected.
|
|
|
|
- **bool begin()** initializer for non ESP32 e.g. AVR.
|
|
|
|
Returns true if device address 0x5C is connected.
|
|
|
|
- **bool isConnected(uint16_t timeout = 3000)** returns true if the device address 0x5C is found on I2C bus.
|
2021-10-19 05:51:07 -04:00
|
|
|
As the device can be in sleep modus it will retry for the defined timeout (in micros) with a minimum of 1 try.
|
|
|
|
minimum = 800 us and maximum = 3000 us according to datasheet.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### Core
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
- **int read()** read the sensor and store the values internally.
|
|
|
|
Returns the status of the read which should be **AM232X_OK** == 0.
|
|
|
|
- **float getHumidity()** returns the last read humidity + optional offset,
|
|
|
|
or **AM232X_INVALID_VALUE** == -999 in case of error.
|
|
|
|
This error can be suppressed, see below.
|
|
|
|
- **float getTemperature()** returns the last read temperature + optional offset,
|
|
|
|
or **AM232X_INVALID_VALUE** == -999 in case of error.
|
|
|
|
This error can be suppressed, see below.
|
|
|
|
- **uint32_t lastRead()** returns the timestamp in milliseconds since startup of the last successful read.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### Offset
|
2022-01-06 08:05:38 -05:00
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
- **void setHumOffset(float offset = 0)** set an offset for humidity to calibrate (1st order) the sensor.
|
|
|
|
Default offset = 0, so no parameter will reset the offset.
|
|
|
|
- **float getHumOffset()** return current humidity offset, default 0.
|
|
|
|
- **void setTempOffset(float offset = 0)** set an offset for temperature to calibrate (1st order) the sensor.
|
|
|
|
Default offset = 0, so no parameter will reset the offset.
|
|
|
|
- **float getTempOffset()** return current temperature offset, default 0.
|
2022-01-06 08:05:38 -05:00
|
|
|
|
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### Control
|
2022-01-06 08:05:38 -05:00
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
Functions to adjust the communication with the sensor.
|
2022-01-06 08:05:38 -05:00
|
|
|
|
|
|
|
- **void setReadDelay(uint16_t rd = 0)** Tunes the time it waits before actual read can be done.
|
2022-06-18 03:51:07 -04:00
|
|
|
Set readDelay to 0 will reset it to 2000 ms effective the next **read()**.
|
2022-06-17 06:53:44 -04:00
|
|
|
- **uint16_t getReadDelay()** returns the above setting.
|
|
|
|
Note that a value of zero (reset) will return 0 before the call and 2000 after the call to **read()**.
|
|
|
|
- **bool wakeUp()** function that will try for 3 milliseconds to wake up the sensor.
|
|
|
|
This can be done before an actual read to minimize the **read()** call.
|
2022-06-18 03:51:07 -04:00
|
|
|
- **void setSuppressError(bool b)** suppress error values of **AM232X_INVALID_VALUE** == -999 => you need to check the return value of read() instead.
|
|
|
|
This can be used to keep spikes out of your graphs / logs.
|
|
|
|
- **bool getSuppressError()** returns the above setting.
|
2022-01-06 08:05:38 -05:00
|
|
|
|
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### Metadata
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
Check datasheet for details.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
- **int getModel()** idem
|
|
|
|
- **int getVersion()** idem
|
|
|
|
- **uint32_t getDeviceID()** idem
|
|
|
|
- **int setStatus(uint8_t value)**
|
2022-06-17 06:53:44 -04:00
|
|
|
- **int getStatus()**
|
|
|
|
|
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### User registers
|
2022-06-17 06:53:44 -04:00
|
|
|
|
|
|
|
Check datasheet for details.
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
- **int setUserRegisterA(int value)**
|
|
|
|
- **int setUserRegisterB(int value)**
|
2022-06-17 06:53:44 -04:00
|
|
|
- **int getUserRegisterA()**
|
|
|
|
- **int getUserRegisterB()**
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-05-07 03:53:25 -04:00
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### Error codes
|
2022-06-18 03:51:07 -04:00
|
|
|
|
|
|
|
| name | value | notes |
|
|
|
|
|:----------------------------------|------:|:------------|
|
|
|
|
| AM232X_OK | 0 |
|
|
|
|
| AM232X_ERROR_UNKNOWN | -10 |
|
|
|
|
| AM232X_ERROR_CONNECT | -11 |
|
|
|
|
| AM232X_ERROR_FUNCTION | -12 |
|
|
|
|
| AM232X_ERROR_ADDRESS | -13 |
|
|
|
|
| AM232X_ERROR_REGISTER | -14 |
|
|
|
|
| AM232X_ERROR_CRC_1 | -15 |
|
|
|
|
| AM232X_ERROR_CRC_2 | -16 |
|
|
|
|
| AM232X_ERROR_WRITE_DISABLED | -17 |
|
|
|
|
| AM232X_ERROR_WRITE_COUNT | -18 |
|
|
|
|
| AM232X_MISSING_BYTES | -19 |
|
|
|
|
| AM232X_READ_TOO_FAST | -20 |
|
|
|
|
| AM232X_HUMIDITY_OUT_OF_RANGE | -100 | not used by default.
|
|
|
|
| AM232X_TEMPERATURE_OUT_OF_RANGE | -101 | not used by default.
|
|
|
|
| AM232X_INVALID_VALUE | -999 | can be suppressed.
|
|
|
|
|
|
|
|
|
2020-05-07 03:53:25 -04:00
|
|
|
## Operation
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
See examples
|
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
In **setup()** you have to call the **begin()** to initialize
|
2021-10-19 05:51:07 -04:00
|
|
|
the Wire library and do an initial **read()** to fill the variables temperature and humidity.
|
2021-01-29 06:31:58 -05:00
|
|
|
To access these values one must use **getTemperature()** and **getHumidity()**.
|
2022-06-17 06:53:44 -04:00
|
|
|
Multiple calls will give the same values until **read()** is called again.
|
2020-05-07 03:53:25 -04:00
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
Note that the sensor can go into sleep mode after 3 seconds after last read,
|
|
|
|
so one might need to call **wakeUp()** before the **read()**.
|
2021-04-07 07:31:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
## Multiplexing
|
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
Multiplexing the **AM232X** can be done in several ways.
|
2021-04-07 07:31:22 -04:00
|
|
|
This is not a complete list or tutorial but should get you started.
|
|
|
|
|
|
|
|
1. Control the power line by means of an extra pin (+ transistor).
|
|
|
|
Only switch on the sensor you want to use. Drawback might be time
|
|
|
|
the sensor takes to boot and to be ready for the first measurement.
|
|
|
|
2. Use an AND gate between the I2C SCL (clock) line and the I2C SCL
|
|
|
|
pin of the sensors. This way one can enable / disable communication
|
|
|
|
per sensor. This will still need an IO pin per sensor but does not
|
|
|
|
have the "boot time" constraint mentioned above.
|
2022-06-18 03:51:07 -04:00
|
|
|
you may use a **PCF8574** to control the AND gates.
|
|
|
|
https://github.com/RobTillaart/PCF8574
|
2022-06-17 06:53:44 -04:00
|
|
|
3. Use a **TCA9548A** I2C Multiplexer, or similar. https://github.com/RobTillaart/TCA9548
|
2021-04-07 07:31:22 -04:00
|
|
|
|
|
|
|
Which method fit your application depends on your requirements and constraints.
|
|
|
|
|
|
|
|
|
2022-01-06 08:05:38 -05:00
|
|
|
## Future
|
|
|
|
|
2023-01-12 11:33:55 -05:00
|
|
|
#### Must
|
|
|
|
|
2022-01-06 08:05:38 -05:00
|
|
|
- update documentation
|
2022-06-17 06:53:44 -04:00
|
|
|
- test more (other platforms)
|
2023-01-12 11:33:55 -05:00
|
|
|
|
|
|
|
#### Should
|
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
- keep in sync with AM2315 class
|
2022-06-18 03:51:07 -04:00
|
|
|
- merge in a far future.
|
|
|
|
- update unit test
|
|
|
|
- add examples
|
2023-01-12 11:33:55 -05:00
|
|
|
|
|
|
|
#### Could
|
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
- I2C performance measurements
|
|
|
|
- clock speed > 170 - see AM2315
|
|
|
|
|
|
|
|
|