Use AM2315 for temperatures below 0°C

am2315-temp-humidity-sensorIn an IoT node I use an AM2315 sensor to measure temperature and humidity. This sensor can be connected via   I2C bus to a NodeMCU module. The results of these periodic measurements are presented here.

On my location these days we had negative temperature on the Celsius scale for the first time in this autumn.

What happened? I had to see wrong temperatures in my graphs.

I looked for the root cause and found that in the used library AM2315.lua the range below 0°C was not handled. The MSB of the temperature value is set in the case of negative temperatures. Therefore I masked this bit and if set the value is multiplied by -1. If you use customized firmware the bit module must be included.

Here are the modified lines for AM2315.lua:

h = string.byte(b[3]) * 256 + string.byte(b[4]) --humidity
h = h / 10
-- MSB is set for negative temperature
t = bit.band(string.byte(b[5]), 127) * 256 + string.byte(b[6]) --temperature
t = t / 10
if bit.isset(string.byte(b[5]), 7) then t = -1 * t end

You will find these modifications on https://github.com/tectiv3/am2315-lua, now.

Finally, here is a temperature graph from today. Winter is coming now 😉

negtemp

Advertisements
Use AM2315 for temperatures below 0°C

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s