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&hi ... 20#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
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();
}
jgeist