Arduino library for an AM2315 I2C temperature and humidity sensor.
The AM2315 is a sensor similar to the DHT12 with an I2C interface.
Although in theory this could enable multiple sensors on one bus the AM2315 has a fixed address 0x5C so one need to implement a multiplexing strategy to have multiple sensors in practice. See multiplexing below.
The AM2315 can also be read with the https://github.com/RobTillaart/AM232X library as it uses the same protocol. The AM232X library allows to read some internal registers.
Version 0.2.0 introduced a breaking change. You cannot set the pins in begin() any more. This reduces the dependency of processor dependent Wire implementations. The user has to call Wire.begin() and can optionally set the Wire pins before calling begin().
The AM2315C ( note the C ) is a different sensor with a different protocol. Check - https://github.com/RobTillaart/AM2315C The C-version has a fixed address of 0x38 so easy to detect.
range | accuracy | repeatability | |
---|---|---|---|
Temperature | -40 - 125 | 0.5°C | ±0.2 |
Humidity | 0.0 - 99.9 | ±2% | ±0.1 |
Sample time | 2 seconds |
// AM232X PIN layout AM2315 COLOR
// ============================================
// bottom view DESCRIPTION COLOR
// +---+
// |o | VDD RED
// |o | SDA YELLOW
// |o | GND BLACK
// |o | SCL GREY
// +---+
//
// do not forget pull up resistors between SDA, SCL and VDD.
The AM2315 has a fixed address 0x5C (92).
Multiplexing the AM2315 can be done in several ways. This is not a complete list or tutorial but should get you started.
Drawback of using a multiplexer is that it takes more administration in your code e.g. which device is on which channel. This will slow down the access, which must be taken into account when deciding which devices are on which channel. Also note that switching between channels will slow down other devices too if they are behind the multiplexer.
Which method fit your application depends on your requirements and constraints.
The datasheet states the AM2315 should be used on 100 KHz I2C only. When overclocking I got good readings up to 190 KHz in a test with
I2C clock | timing us | Notes |
---|---|---|
50 KHz | 4570 | under-clocking works (e.g. long wires) |
100 KHz | 3276 | specs default, robust |
150 KHz | 2836 | |
160 KHz | 2792 | |
170 KHz | 2750 | 0.5 ms off, interesting for performance. |
180 KHz | 2700 | near critical. DO NOT USE. |
190 KHz | 2672 | near critical. DO NOT USE. |
200 KHz | crash | sensor needs a power cycle reboot. DO NOT USE. |
If robustness is mandatory stick to the default of 100 KHz. If performance is mandatory do not go beyond 170 KHz.
#include "AM2315.h"
Functions to adjust the communication with the sensor.
name | value | notes |
---|---|---|
AM2315_OK | 0 | |
AM2315_ERROR_CHECKSUM | -10 | I2C problem. |
AM2315_ERROR_CONNECT | -11 | I2C problem. |
AM2315_MISSING_BYTES | -12 | I2C problem. |
AM2315_WAITING_FOR_READ | -50 | called read() too fast, within 2 seconds. |
AM2315_HUMIDITY_OUT_OF_RANGE | -100 | not used by default. |
AM2315_TEMPERATURE_OUT_OF_RANGE | -101 | not used by default. |
AM2315_INVALID_VALUE | -999 | can be suppressed. |
Note: The OUT_OT_RANGE errors are tested on the "raw" readings, excluding the offset. So if the "raw" humidity is 99% and you use an offset of 3% you will get 102% without the error.
See examples
In setup() you have to call the begin() to initialize the Wire library and do an initial read() to fill the variables temperature and humidity. To access these values one must use getTemperature() and getHumidity(). Multiple calls will give the same values until read() is called again.
Note that the AM2315 can go into sleep mode after 3 seconds after last read, so one might need to call wakeUp() before the read().
If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.
Thank you,