DS18B20 sensor showing +200F and -200F alternately

I am using the DS18B20 sensor for measuring water temperatures. My sensor was showing readings between 70 and 200 for a day, and then its showing +200 to -200.

Looking at the data in a chart seems like sine waves with values -200 to +200.
It should be 70 to 200, (all temperaures in Farenheit)

Is it possible that when it goes high, its going negative, or is it possible my sensor is going bad?
Interestingly it keeps showing the high number correctly (i assume) but the low number goes way low , instead of about 70, shows -200.

Could be a problem with your code that you forgot to post.

1 Like

Ok here is my code. I dont think its the code as it worked well for 2 days, but here is the code anyway just in case I am doing something wrong.

#include "thingProperties.h"
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 14
#define LED_BUILTIN 2 
#define RELAY1PIN 5

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress Thermometer, sensor1, sensor2, sensor3;

int deviceCount = 0;
int counter;
float mytemp1, mytemp2,  mytemp3;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(RELAY1PIN, OUTPUT);
  
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  delay(1500); 
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
  sensors.begin();
  Serial.println("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
  
  if (deviceCount < 0) {
    inputTemp = -999;
    outputTemp = deviceCount;
    exit;
  }
  
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    
    if (i==0) { 
      sensors.getAddress(sensor1, i); 
      addr1=printAddress(sensor1);
    }
     if (i==1) { sensors.getAddress(sensor2, i); 
      addr2=printAddress(sensor2);
    }
  if (i==2) { sensors.getAddress(sensor3, i); 
      addr3=printAddress(sensor3);
     }
   }
}

void onStatus82Change()  {
  Serial.println("Status was changed ");
  if (status82) {
      Serial.println("Status was changed to ON");
      ledColor=true;
      digitalWrite(RELAY1PIN, HIGH);
      delay(500);
  } else {
      Serial.println("Status was changed to OFF");
      ledColor=false;
      digitalWrite(RELAY1PIN, LOW);
      delay(500);
  }
}

void onLedColorChange()  {
}
void onLastUpdateChange()  {
}
void onLastUpdate2Change()  {
}
void onLastUpdateTChange()  {
}
String printAddress(DeviceAddress deviceAddress)
{ 
  String addr="";
  for (uint8_t i = 0; i < 8; i++)
  {
    Serial.print("0x");   addr+="0x";
    if (deviceAddress[i] < 0x10) {Serial.print("0"); addr+="0"; }
    Serial.print(deviceAddress[i], HEX); addr+=deviceAddress[i]; 
    if (i < 7) { Serial.print(", "); addr+=", "; }
  }
  Serial.println(""); addr+="";
  return addr;
}
float  printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  tempC = (tempC * 9 / 5) + 32;  
  mytemp1 = tempC;
  
  Serial.print(" Temperature is: " + String(tempC));
  Serial.print((char)176);
  Serial.println("F");
  return tempC;
}
void loop() {
  String str;
  counter++;
  ArduinoCloud.update();
  sensors.requestTemperatures();
  Serial.print("Sensor 1: ");
  inputTemp = printTemperature(sensor1);
  Serial.print("Sensor 2: ");
  outputTemp=printTemperature(sensor2);
  Serial.print("Sensor 3: ");
  circulationTemp = printTemperature(sensor3);
  Serial.println();
  if (inputTemp < 100 ||  outputTemp > 80 || circulationTemp > 80) {
     status82=false;
  } else {
     status82=true;
  }
  digitalWrite(LED_BUILTIN, LOW);
  delay(50);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  lastUpdate = ArduinoCloud.getLocalTime(); - 32400;
  lastUpdateT = ArduinoCloud.getLocalTime(); - 32400; 
  
  if (counter = 1) {
    counter=0;
    if (ledColor) {
      ledColor=false;
    } else {
      ledColor=true;
    }
  }
}
void onAddr1Change()  {
  // Add your code here to act upon Addr1 change
}

void onAddr2Change()  {
}

void onAddr3Change()  {
}
void onOutputTempChange()  {
}

if (counter = 1) {

= is not the same as ==


lastUpdate = ArduinoCloud.getLocalTime(); - 32400;

Did you mean ?

lastUpdate = ArduinoCloud.getLocalTime(); 
- 32400;

So what does this do ? :thinking:


Place each line of code on a line by itself.

Other than Larry's comments, what does this do?

AKA

ledColor = ! ledColor;

Thanks for pointing it out.
I was so used to programming in other languages whereby I used = on a daily basis, but thanks for pointing out that = is not same as ==. I need to switch gears when writing code in sketch mode and totally my fault.
The first line "- 32400" was to change to my time zone, but I overlooked the semicolon, my mistake; Yes I totally agree to put each statement on a separate line, thats my preferred writing style anyway and I totally agree. At rare times I like to put in 1 line when its a very small fragment that otherwise goes to separate lines. Thats the best practice for best readability as well.

I would think it stops processing and forces execution to get out of the block/function. Is that not correct?

thanks for the shortcut. I am fairly new to arduino so am not yet resorting to shortcuts but soon I will, once I get used to sketch writing. But thanks so much for pointing it out.

Adafruit tells me that the range is -67°F to +257°F. So if you're getting -200°F, something is wrong. I suggest you write a much simpler program, grab a thermometer and prove that you can read a sensor (approximately) correctly.

Did you mean "break"?

exit appears a function, so would read "exit();" (or similar - you'd probably want to supply an error code), and returns control to the operating system.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.