Posts: 3
Joined: Sun Jun 18, 2017 8:51 pm

DS18B20: error reading negative temperatures

Tue Mar 02, 2021 5:16 pm

Problem with negative Centigrade readings on DS18B20 with Raspbian as of 2021-01-11.
Negative temperatures show as positive with values around 4095.xx. Examples: -0.1C gives 4095.9; -1.0C gives 4095.0; -6.0C gives 4090.0... etc. Note that rounding of floating point numbers mais give very slightly different results from these examples.
1) go back to "w1_therm" version of december Raspbian release (2020-12-01)
2) add a test "post reading of sensor" in your code such as (python example):
if temp_read > 4000.0 : temp_read -= 4096.0
# This test works as long as measured temperatures are above -95.999C.
# This is true on Earth and in Pfizer freezers .... but NOT on Mars
# This test will remain valid even with the proper (or future correction) of "w1_therm"
Linux kernel 5.9 and prior is correct, but since 5.10-rc1 up to 5.11 inclusive, the code in "w1_therm.c" contains a "correction" that yields invalid readings for negative Celcius temperatures. The GitHub Raspbian (Buster) repository information suggests that Raspbian uses Linux 5.10 since the the january 2021. The 5.12-rc1 kernel version of Linux contains an adequate correction but that is not yet in the next Buster release or the Raspbian release.

Posts: 7
Joined: Fri Mar 05, 2021 3:00 pm

Re: DS18B20: error reading negative temperatures

Fri Mar 05, 2021 3:16 pm

Thanks for posting details on this.

I had spent a little time re-wiring an existing long time running ds18b20 temperature reading installation - thinking I had some hardware issues. Then after the re-wiring didn't fix ... upon further investigation determined that garbage data on sensor only appeared as it crossed below 0deg C / 32 degF. Inspecting the data further I had determined software workaround required..

However was puzzled as to why things went bad all of a sudden. Glad to see that you have related the cause back to recent kernel "update". I hadn't gotten that far.

Return to “Raspberry Pi OS”