0

AM2320 Temp and Humidity Sensor "Hack"
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

AM2320 Temp and Humidity Sensor "Hack"

by jgeist on Thu Aug 05, 2021 7:05 pm

Hello:

Thought I would share this as I have been having this sensor fail occasionally, typically declaring the temperature is NaN (not a number) and the humidity is pegged at 255. I have everything wired up correctly (don't forget your pull up resistors on the i2c lines). You can read more about the sensor's peculiarities here (viewtopic.php?f=19&t=178326&p=868306&hilit=am2320#p868306) and try a few of those things. The following works for me in my use case so I though I would share.

1. The sensor has a query threshold of about 1 or 2 seconds, so don't request a reading too often. As the temperature and humidity don't vary frequently, I just check the sensor every 5 minutes or so using a timer, like in the Blink Without Delay scenario (see examples in the IDE).

2. Now here is the rub, the sensor's i2c compliance is kinda irregular, so it doesn't always 'wake up' and do it's thing. So I used a little while loop to keep waking the thing up (am2320.begin()) until the sensor actually reports a valid number. Again, when the sensor fails to wake to the i2c call, it returns a NaN (not a number) for the temperature. So, I keep .begin()-ing until it provides something that is not NaN.

Code: Select all | TOGGLE FULL SIZE
  if (millis() - lastCheck > checkInterval) {
    while (isnan(temp)) { //ensure temp is a valid number, due to i2c am2320 issue, keep doing this until you get a reading that is valid
      am2320.begin(); //wake sensor up
      temp = am2320.readTemperature(); //get temp reading
      humid = am2320.readHumidity(); //get humidity reading
    }
    //    Serial.println(Data.t);
    lastCheck = millis();
  }

As to the accuracy of the sensor, I don't think it is too accurate, but I only need ballpark readings. Works for me so far, maybe it will help someone else on their journey . . . happy coding!

jgeist

jgeist
 
Posts: 12
Joined: Tue Aug 18, 2015 11:01 am

Re: AM2320 Temp and Humidity Sensor "Hack"

by adafruit_support_bill on Thu Aug 05, 2021 7:25 pm

Yea, most sensors have their quirks. But some are quirkier than others. Thanks for sharing your results.

adafruit_support_bill
 
Posts: 81910
Joined: Sat Feb 07, 2009 10:11 am

Re: AM2320 Temp and Humidity Sensor "Hack"

by jgeist on Thu Aug 05, 2021 9:24 pm

Just an update, the while() does pose the issue of an infinite loop if the sensor were to really go down. In my use case I am installing out of doors and this needs to run remotely for at least 6 months. To make things somewhat fail safe, I test by taking the circuit apart and rebuilding it live. If I can remove a sensor and it can recover, then things are pretty hardened. When I remove the sensor it goes into an infinite loop with the code from the previous post. Try this:

Code: Select all | TOGGLE FULL SIZE
if (millis() - lastCheck > checkInterval) {
    int counter = 0; //count how many time we try to reboot the sensor
    while (isnan(temp)) { //ensure temp is a value, due to i2c am2320 issue
      am2320.begin();
      counter++;
      if (counter > 9) { //we have tried to boot the sensor 10 times, guess it's really down : (
        break; //leave the while()
      }
    }

    temp = am2320.readTemperature() * 1.8 + 32; //make reading
    humidity = am2320.readHumidity(); //make reading

    //    Serial.println(temp);
    lastCheck = millis();
  }

Later on in the code, I send a -99 to a server for temp if the sensor insists on sending a NaN value. That way I can weed it out of my data, but still be notified that particular sensor is down.

Enjoy your Arduino . . .

jgeist

jgeist
 
Posts: 12
Joined: Tue Aug 18, 2015 11:01 am

Re: AM2320 Temp and Humidity Sensor "Hack"

by jgeist on Fri Aug 06, 2021 11:15 am

Had to go back to the drawing board on this. Eventually, this library did the trick:

https://github.com/RobTillaart/AM232X

You can read about the 'sleep after reading feature' here and see that this library has a .wakeUp() function which seems to fix the issue. Just call .wakeUp() before you do any readings. There are some extended functionalities in this library as well that may be of service to your project. Running well for many hours. Read more here;

https://github.com/RobTillaart/AM232X/issues

jgeist

jgeist
 
Posts: 12
Joined: Tue Aug 18, 2015 11:01 am

Please be positive and constructive with your questions and comments.