AM2315 Temp/Humidity Sensor Raspberry Pi Python Library Released

AM2315 Temp/Humidity Sensor Raspberry Pi Python Library Released
AM2315

SwitchDoc Labs has just released a modified Adafruit library for the AM2315 Encased I2C Temperature and Humidity sensor for the Raspberry.   The previously used tentacle_pi drivers were throwing bad data out occasionally, so we went in and added these drivers to improve the reliability of the AM2315 in GroveWeatherPi.

Download the SDL_Pi_AM2315 libraries here on github.

 

What is the AM2315 Temp/Humidity Sensor?

The AM2315 I2C encased capacitive humidity sensing digital temperature and humidity sensor contains a temperature and humidity combined sensor calibrated digital signal device. It uses special temperature and humidity acquisition technology, to ensure that the sensor has high reliability and excellent long-term stability. The sensor includes a capacitive sensor and an integrated high-precision temperature measurement device.

This probe has been used in numerous SwitchDoc Labs projects including Project CuracaoGroveWeatherPi and the  SunRover solar powered robot.

What is the AM2315?

  • – 3.3V to 5.5V I2C interface and power
  • – 10 mA max current use during conversion
  • – Good for 0-100% humidity readings with minimum 2% accuracy
  • – Good for -20 to 80°C temperature readings ±0.1°C typical accuracy
  • – Updated every 500ms (0.5 Hz)
  • – Body size 98mm x 16mm diameter
  • – 20 inch long – 4 wire cable
  • – This board/chip uses I2C 7-bit address 0x5C

When read, this humidity/temperature sensor returns the humidity, temperature and a CRC check.  A CRC is a “Cyclic Redundancy Check” and detects errors in transmission.  This is very handy to make sure you are getting the right data back.

Issues with the AM2315

The AM2315 has a number of small issues.  The first of all, the AM2315 goes quickly into sleep mode (to avoid self heating – a good idea) and needs to be woken up before data can be acquired.  This is done by writing a “0x00” the read register:

# WAKE UP
self._device.write8(AM2315_READREG,0x00)

Another ramification of this is that the AM2315 (located at I2C address 0x5C) does not show up in a “i2cdetect -y 1” command on the first time you run the command.  Run the command quickly two times in a row and you will see it.  You can also run the program below to see that the device is present.

On occasion on the Raspberry Pi, you will get a bad read from the AM2315.   This is caused by something in the operating system interfering with the I2C read from the AM2315.  In this case you will get a bad CRC and you should re-read the device.    Other computers that are running multi-tasking operating systems, such as the ESP32 and ESP8266 also have this problem.  Check your CRC!

You can find out how to calculate the CRC in Python and compare it by looking in the this tutorial on CRC checks in Python.  You can also look at the code below.

This version of  SDL_Pi_AM2315 will internally check CRCs and return a -1 as the CRC if it fails.   Here is the CRC checking code from SDL_Pi_AM2315 and used in the GroveWeatherPi Code.

    def verify_crc(self, char):
        """Returns the 16-bit CRC of sensor data"""
        crc = 0xFFFF
        for l in char:
                crc = crc ^ l
                for i in range(1,9):
                    if(crc & 0x01):
                         crc = crc >> 1
                         crc = crc ^ 0xA001
                    else:
                         crc = crc >> 1
        return crc

And in the main code, generating and using the CRC

        self.crc = ((tmp[7] << 8) | tmp[6]) 
        # Verify CRC here
        # force CRC error with the next line
        #tmp[0] = tmp[0]+1
        t = bytearray([tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]])
        c = self.verify_crc(t)

        if self.crc != c:
            self.crc = -1

 

 

What is in this Pure Python library?

The SDL_Pi_AM2315 library is simple to use:

#!/usr/bin/env python

import AM2315

while (1):
	thsen = AM2315.AM2315()
	print "T   ", thsen.read_temperature()
	print "H   ", thsen.read_humidity()
	print "H,T ", thsen.read_humidity_temperature()
	print "H,T,C ", thsen.read_humidity_temperature_crc()
        h,t,c = thsen.read_humidity_temperature_crc()
        print "CRC=0x%02x" % c 

And the results of running the program:

pi@RPi3-60:~/SDL_Pi_AM2315 $ sudo python testAM2315.py
T    21.2
H    30.6
H,T  (30.6, 21.2)
H,T,C  (30.6, 21.2, 20868)
CRC=0x5184
T    21.2
H    30.6
H,T  (30.6, 21.2)
H,T,C  (30.6, 21.2, 20868)
CRC=0x5184
T    21.2
H    30.6
H,T  (30.6, 21.2)
H,T,C  (30.6, 21.2, 20868)
CRC=0x5184