Boot time and DS18B20 problem

I am measuring temperature with DS18B20 and write it on LCD display. I also included max and min temperature function. The problem is that when i turn the circuit on for the first time, temperature shows 85 degrees but just for a second. Because of that maximum temperature is set to 85. If i reset the whole circuit it's ok but i wonder why this is happening and how to solve this problem. I really don't want to reset the whole arduino because of this every time.

      float temperature1 = getTemp(ds1);
      float temperature2 = getTemp(ds2);
      
      int temp = ((temperature1 + temperature2) / 2);
      
      
      // MAX/MIN TEMP
      if(temp > maxi) {maxi = temp;}
      if(temp < mini) {mini = temp;}

Really need to see all the code to diagnose problem but maybe you need to read the sensor a couple of times in setup() and discard the result before entering loop() where I assume your extract code came from.

85 is a special result: IIRC, it's what the device returns by default when it hasn't read the temperature. As Riva says, just read the temp a few times to start with and ignore the results. Having said that, I haven't seen inappropriate 85s since I stopped using parasitic power on the device.

wildbill:
85 is a special result: IIRC, it's what the device returns by default when it hasn't read the temperature. As Riva says, just read the temp a few times to start with and ignore the results. Having said that, I haven't seen inappropriate 85s since I stopped using parasitic power on the device.

Thanks wildbill for the heads up (I never looked at the datasheet), a quick look at datasheet page 7 http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf shows 85c is default loaded from eeprom on powerup into scratch registers. So you just need to read and discard first value when doing setup() and you should be fixed. Probably when you was using parasitic power the device was resetting causing the defaults to be loaded.

temperature shows 85 degrees but just for a second.

You may want to read the datasheet about that.

I think you might be printing the temperature before you are actually reading it. I had the same thing and fixed it by rearranging the code rather than altering it.

luxy:
The problem is that when i turn the circuit on for the first time, temperature shows 85 degrees but just for a second.

I tried to put the whole function for reading temperature before loop (where i am reading current, min and max temp) but it didn't help.

Here's my whole code for reading temp (now is putted betwen void setup and void loop:

      float getTemp(OneWire sensor){
      byte data[12];
      byte addr[8];
  
      if ( !sensor.search(addr)) {
  
         sensor.reset_search();
         return -1000;
      }
  
      if ( OneWire::crc8( addr, 7) != addr[7]) {
         Serial.println("Bad CRC!");
         return -1000;
      }
  
      if ( addr[0] != 0x10 && addr[0] != 0x28) {
         Serial.print("Unknown device!");
         return -1000;
      }
  
      sensor.reset();
      sensor.select(addr);
      sensor.write(0x44,1);
  
      byte present = sensor.reset();
      sensor.select(addr); 
      sensor.write(0xBE);
  
  
      for (int i = 0; i < 9; i++) { 
        data[i] = sensor.read();
      }
  
      sensor.reset_search();
  
      byte MSB = data[1];
      byte LSB = data[0];
  
      float tempRead = ((MSB << 8) | LSB);
      float TemperatureSum = tempRead / 16;
  
      return TemperatureSum;
  
      }

NickPyner:
I think you might be printing the temperature before you are actually reading it. I had the same thing and fixed it by rearranging the code rather than altering it.

luxy:
The problem is that when i turn the circuit on for the first time, temperature shows 85 degrees but just for a second.

How exactly did you rearrange the code?

I think i just solve the problem. I'm calling temperature in void setup and now it works, without 85 XD

   // VOID
    void setup() {        
    
    float temperature1 = getTemp(ds1);
    float temperature2 = getTemp(ds2);
    
    delay (1000);
    
    }

luxy:
I think i just solve the problem. I'm calling temperature in void setup and now it works, without 85 XD

I had a similar problem with DS18B20 sensors where they would return a spurious value (around 85 C) at the first read. I just did a range check on the result. Given that it seems to be a one-off thing, doing a dummy read in setup() is probably as good a way of dealing with it as any other.

luxy:
How exactly did you rearrange the code?

I am modifying my shield at the moment and I can't remember what I had originally, but I'm pretty sure this is OK. Note that I am not actually reading the temps in the setup section, but I am initialising them.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8,9,16,5,6,7); // patchwire is D4 to A0 (pin14) on this proto
int flag;
// Data wire is on pin 3 
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress InThermo = {  
  0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0X9F };
DeviceAddress OutThermo = { 
  0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F };

//temperature variables
float InTemp, OutTemp, diff, drain, flow, power, tempC;

void setup(void)
{
  // start serial port
  Serial.begin(9600);

  // set up the LCD,s number of columns and rows: 
  lcd.begin(16, 2);
  Serial.println("LABEL,Time,TempIn,TempOut,diff");
  // Print a message to the LCD.
  lcd.print("temp in     out");    

  // Start up the library
  sensors.begin();
  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);
}

void loop(void)
{ 
  delay(1000);
    Serial.print("DATA,TIME,       "); 
  flag = 0;
  //Get the sensor readings. There are two of them
  sensors.requestTemperatures();

  GetandPrint(InThermo);
  InTemp=tempC;
  flag = 1;

  GetandPrint(OutThermo); 
  diff = tempC - InTemp;
  Serial.print (diff); 
  Serial.println(" ,  ");

} 

void GetandPrint(DeviceAddress deviceAddress)
{
  tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } 
  else {
    Serial.print(tempC);
    Serial.print(" ,  ");
  }
  lcd.setCursor (1,1);
  if (flag==1)
    (
    lcd.setCursor (11,1)
      );
  lcd.print (tempC); 
}