RobTillaart / AM2315

Arduino library for the I2C AM2315 temperature and humidity sensor.
MIT License
3 stars 1 forks source link
arduino humidity i2c temperature

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

AM2315

Arduino library for an AM2315 I2C temperature and humidity sensor.

Description

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.

0.2.0 Breaking change

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().

AM2315C

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.

Typical parameters

range accuracy repeatability
Temperature -40 - 125 0.5°C ±0.2
Humidity 0.0 - 99.9 ±2% ±0.1
Sample time 2 seconds

Hardware connection

//  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.

I2C

The AM2315 has a fixed address 0x5C (92).

Multiplexing

Multiplexing the AM2315 can be done in several ways. 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. You may use a PCF8574 to control the AND gates. https://github.com/RobTillaart/PCF8574
  3. Use a TCA9548A I2C Multiplexer, or similar.

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.

I2C clock speed

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.

Interface

#include "AM2315.h"

Constructor

Core

Offset

Control

Functions to adjust the communication with the sensor.

Error codes

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.

Operation

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().

Future

Must

Should

Could

Wont

Support

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,