Dallas 18B20 weird values on 2 sensors placed on the same wire

Hello guys,

I tried to make a temperature logger using 2 dallas sensors, a micro SD card for storing values and a RTC for "acurate" date of each row saved in the csv file.

I havent made the PCB yet but I want to start make it now, the wires are all connected to a breadboard now, maybe this was the problem but still it's weird in some case the same issue appeared for multiple seconds .. like 4 - 5 readings, consecutively in 4 - 5 seconds, one reading / second

here are some photos and a graph to see the errors and the breadboard, it seems the problems started only when I placed the breadboard in the car and the temperature started to drop, when the temp dropped from 22 to 14 I placed the sensor inside my stopped car, when it dropped from 14 to -8 I placed the sensor on the hood of my car, with the engine running as I was inside the car flying my QUAD.

I made some protection for the LiPo of my quad and I want to measure the temperature inside the protection and outside of it, to see which material works best.

Thank you and I wish you a Happy New Year to all you guys and girls :slight_smile:

PS. I believe the issue with temperature are because of connection on the bread board, but I wanted to see if other have experienced this before.

Could be your program, a bad void loop or something, could you post it?

Yes, I can post the program but I don't think is the program fault as I already left it overnight and it logged like 70k rows all with correct steady temperature.

I will change the code as for now is only what I got from Examples, I want to save a different log each day or each time the board is power on, also write something to the csv file so I know when the board restarts... if it ever restarts without my intervention.

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

#include <SPI.h>
#include <SD.h>

#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

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

// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;

const int chipSelect = 4;


void setup(void)
{
  // start serial port
   Serial.begin(9600);
  while (!Serial) ; // wait until Arduino Serial Monitor opens
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");  

  sensors.begin();

  // 
  // method 1: by index
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
  if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); 


  // set the resolution to 9 bit
  sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
  sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);

  //!!//Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  //!!//Serial.println("card initialized.");
  
}


void loop(void)
{ 


  sensors.requestTemperatures();

  float tempC = sensors.getTempC(insideThermometer);
  float tempCO = sensors.getTempC(outsideThermometer);



    String curentTime = "";
    curentTime += hour();
    curentTime += ":";
    if(minute()<10)
    {
      curentTime += "0";  
    }
    curentTime += minute();
    curentTime += ":";
    if(second()<10)
    {
      curentTime += "0";  
    }
    curentTime += second();
    curentTime += " ";
    curentTime += day();
    curentTime += ".";
    curentTime += month();
    curentTime += " ";
    curentTime += year();


    String dataString = curentTime;
    dataString += ",";
    dataString += tempC;
    dataString += ",";
    dataString += tempCO;



  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.csv", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.csv");
  }

delay(1000);
  
}

When a DS18B20 is first powered up, its temperature registers are initialized to values corresponding to 85C. Once it has been initialized properly and a conversion completed, this will change to the current temperature. You almost certainly have a problem with the power to the DS18B20 glitching so that it resets the temperature to 85C. Triple check the way that they are powered. I can't tell if you're using parasite power but make sure that both (or all) ds18b20 are wired for the same power.

Your use of the String class will probably ultimately cause problems too. With only 2kB of static ram, there's not much room to keep building and destroying strings. Eventually the code will start printing garbled strings and/or crash mysteriously.

Pete

Thank you, about the Strings , can you tell how to change those lines, how to store the values for saving them all on one line at a time in the csv file ?

About the sensors, maybe the ground wire was moving in the breadboard. I will try to make tomorrow a PCB to solder all the parts and see if it will happen again. At least I know why 85 degrees! I know something was wrong on the sensor side.

Thank you.

Try replacing the code which generates and writes the Strings with this:

  static char report[30];
  static char tempCs[6];
  static char tempC0s[6];
  // create the date/time stamp
  sprintf(report,"%d:%02d:%02d %d.%d %d ",hour(),minute(),second(),day(),month(),year());

  // convert the temperatures to strings
  dtostrf(tempC,5,1,tempCs);
  dtostrf(tempC0,5,1,tempC0s);

  // append the temperatures to the date/time stamp in report
  sprintf(&report[strlen(report)],",%s,%s",tempCs,tempC0s);

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.csv", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(report);
    dataFile.close();
// ... etc.

Will your Excel be able to handle a date string which looks like "hh.mm yyyy"?
I would use a string format like this: YYYY/MM/DD hh:mm:ss

Pete

These days the String class is free from space-leaks I believe, so as long as you don't hold
on to lots of Strings it ought to work ad-infinitum. The wires are all nice and short so
the lack of decoupling on the DS18B20's shouldn't be a big issue. Perhaps worth adding 0.1uF
just in case.

Are these bone-fide DS18B20's and not knock-offs or rejects ?

sibianul:
About the sensors, maybe the ground wire was moving in the breadboard.

I think that is probably right. You don't normally see the 85 at all, and should never see it more than once. If you see it after start up, I believe it is indicative of slack wiring.

If you want to save a different log each day, the simplest approach is to use the date as filename

void getFileName(){
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
}

which means you don't need to add the date in the file, as in

http://homepages.ihug.com.au/~npyner/Arduino/Bluetooth_graphic_5110_with_file_retrieve_DY.ino

which shows the floats going straight from sensors to SD, with a myFile.println for the last number - no strings.

el_supremo:
Try replacing the code which generates and writes the Strings with this:
... code ...

Will your Excel be able to handle a date string which looks like "hh.mm yyyy"?
I would use a string format like this: YYYY/MM/DD hh:mm:ss

Pete

Thank you very much for the code, I will try to understand it in a moment, I havent used that functions in arduino but I'm familiar with the result as I have a few years back as a web developer, but now I try to focus more on aerial photography / videography :slight_smile:

MarkT:
These days the String class is free from space-leaks I believe, so as long as you don't hold
on to lots of Strings it ought to work ad-infinitum. The wires are all nice and short so
the lack of decoupling on the DS18B20's shouldn't be a big issue. Perhaps worth adding 0.1uF
just in case.

Are these bone-fide DS18B20's and not knock-offs or rejects ?

I purchased from a local shop, the sensors should be fine but you never know :slight_smile:

Nick_Pyner:
I think that is probably right. You don't normally see the 85 at all, and should never see it more than once. If you see it after start up, I believe it is indicative of slack wiring.

If you want to save a different log each day, the simplest approach is to use the date as filename

void getFileName(){

sprintf(filename, "%02d%02d%02d.csv", year, month, day);
}




which means you don't need to add the date in the file, as in

http://homepages.ihug.com.au/~npyner/Arduino/Bluetooth_graphic_5110_with_file_retrieve_DY.ino

which shows the floats going straight from sensors to SD, with a myFile.println for the last number - no strings.

Awesome, I'll try your code also. Thank you very much. I just got back from a few hours of flying the UAV in cold weather (-4 degrees C) and now I will send the photos to be processed, if the kids will let me stay more on the PC :slight_smile: I will try to make the PCB in eagle.. I'm a ultra newbie in Eagle.

I will let you guys know if I will still have issues after the boards and sensors are soldered. For one of the sensor I want to use some ~10cm wires, should I add anything for this length ?

10cm seems very short, so no.

Thank you guys, I made the PCB for my test device and it's working great so far, but there is another "issue" related to RTC, the bat pin only have 1.7v is there are volt divider ?

I'm sure the battery was above 3v, and I don't know how to calculate the real value of the battery, I also monitor the main LiPo that it's powering the entire "device" and it's ok, I get a value of 3.53v - 3.58v and my multimeter (not very expensive) is saying the battery is 3.60v .. so the mesurements of the main lipo is ok.

Here's a part of my csv

Time	Sensor 1	Sensor 2	LiPo
2016/1/6 10:16:08	24.5	24.5	3.54
2016/1/6 10:16:25	24.5	24.5	3.56
2016/1/6 10:16:26	24.5	24.5	3.57
2016/1/6 10:16:27	24.5	24.5	3.52
2016/1/6 10:16:28	24.5	24.5	3.52
2016/1/6 10:16:29	24.5	24.5	3.52
2016/1/6 10:16:31	24.5	24.5	3.52
2016/1/6 10:16:32	24.5	24.5	3.52
2016/1/6 10:16:33	24.5	24.5	3.55
2016/1/6 10:16:34	24.5	24.5	3.56
2016/1/6 10:16:35	24.5	24.5	3.57
2016/1/6 10:16:36	24.5	24.5	3.52
2016/1/6 10:16:37	24.5	24.5	3.56
2016/1/6 10:16:38	24.5	24.5	3.53
2016/1/6 10:16:40	24.5	24.5	3.52
2016/1/6 10:16:41	24.5	24.5	3.53
2016/1/6 10:16:42	24.5	24.5	3.52
2016/1/6 10:16:43	24.5	24.5	3.54
2016/1/6 10:16:44	24.5	24.5	3.56
2016/1/6 10:16:45	24.5	24.5	3.56
2016/1/6 10:16:46	24.5	24.5	3.51
2016/1/6 10:16:50	24.5	24.5	3.53
2016/1/6 10:16:51	24.5	24.5	3.53
2016/1/6 10:16:52	24.5	24.5	3.54
2016/1/6 10:16:53	24.5	24.5	3.52
2016/1/6 10:16:54	24.5	24.5	3.54
2016/1/6 10:16:56	24.5	24.5	3.55
2016/1/6 10:16:57	24.5	24.5	3.53
2016/1/6 10:16:58	24.5	24.5	3.52
2016/1/6 10:16:59	24.5	24.5	3.53
2016/1/6 10:17:00	24.5	24.5	3.56

I made a test with the sensor powered only by a small 350mAh LiPo for about 2 hours, seems perfect, now I have to wrap it up and attach it to my quadcopter to start the real tests :slight_smile: