DS18B20 is throwing -127C from time to time



Any ideas why my DS18B20 is giving -127C from time to time - this is completely messes with the temperature graph over time.


Photon Maker Kit - DS18B20 - temperature sensing issues/questions

Like the other thread…gotta catch CRC failures and throw those results out.

 if (OneWire::crc8(data, 9)) {
      Serial.println("CRC Failed");
    //do something here


Sorry about that, I thought you were part of another thread about this. I have just published a lib that includes an easy to use CRC check that you can use to throw away bad results. Look at the example app and you will see what I mean.

DS18B20 0.1.00
DSB18XX Lib on Particle Photon


I have a compile error when I try to compile the code below:

sensors.requestTemperatures(); tempc= sensors.getTempCByIndex(0); if (OneWire::crc8(data, 9)) { { Serial.println("CRC Failed"); } else { Particle.publish("temperature_Hot_Water_Tank", String(tempc) + "°C"); }

as ‘data’ is not defined. I am using slightly different method than in the example.


Post your gettempbycbindex function. You need to compare the calculated CRC with the CRC in the 9th bit of the sensor returned data.


I found the lib you are using. A few things…
The -127 is a defined error value that is thrown when the device is considered disconnected. In DallasTemperature.h

    // Error Codes

-127 is outside the range of the device so you can just toss that value like so…untested.

if (tempc == -127)
Serial.println("Bad temp read");
Particle.publish("temperature_Hot_Water_Tank", String(tempc) + "°C");

The getTemp() function in DallasTemperature.cpp is what the getTempCByIndex() leads to. There you can see in the comments that if sensor.isConnected(DeviceID); is false OR !sensor.isConnected(DeviceID); it will return the DEVICE_DISCONNECTED value that is defined in DallasTemperature.h.


Thanks @LukeUSMC

I have just implemented similar code before seeing your post given this is an internal temp sensor.
We are thinking in a similar way I guess

if (tempc < 0) { Serial.println("Temp read failed"); } else { Particle.publish("temperature_Hot_Water_Tank", String(tempc) + "°C"); }