Blog/20141113 measuring below zero temperatures with DS18B20
Measuring below zero temperatures with DS18B20
Measuring below zero temperatures with the DS18B20 doesn't seem to be widely discussed in step-by-step documents explaining how to use this sensor with Raspberry Pi (see e.g. ,  and many more documents like that).
The issue is that the "w1_slave" file doesn't report the correct temperature for below zero temperatures in the "t=..." entry. The w1_slave file looks something like this:
AA BB CC DD EE FF 00 11 : crc=... YES AA BB CC DD EE FF 00 11 : t=xxxxx
For below zero temperatures, the t=xxxxx entry is not correct. What I haven't seen discussed elsewhere (though it must be discussed somewhere), is that the first two bytes ("AA BB") are the temperature in hexadecimal.
The most significant bytes are BB, so the full temperature in hexadecimal (for above zero) is
i.e. BBAA is the temperature in 16th of degrees, and BBA.A is the actual temperature. For above zero temperatures, if you conver this to decimal, you'd get the same as the t=xxxxx above (which corresponds to xx.xxx degrees celsius).
For below zero temperatures, you need to decode BBAA, and determine the sign. I'll post a recipe, but the datasheet will tell you:
TEMPERATURE (°C), DIGITAL OUTPUT (BINARY), DIGITAL OUTPUT (HEX) +125 0000 0111 1101 0000 07D0h +85* 0000 0101 0101 0000 0550h +25.0625 0000 0001 1001 0001 0191h +10.125 0000 0000 1010 0010 00A2h +0.5 0000 0000 0000 1000 0008h 0 0000 0000 0000 0000 0000h -0.5 1111 1111 1111 1000 FFF8h -10.125 1111 1111 0101 1110 FF5Eh -25.0625 1111 1110 0110 1111 FE6Fh -55 1111 1100 1001 0000 FC90h
(see datasheet). As you can see, it's the 11th bit (and higher) that determines the sign. For below zero temperatures, you need to take the two complement as well (and add 1/16th of a degree, i.e. add 1).
2014-11-13 | Back to blog|