Go Down

Topic: DS18B20 problem bad reading with two or more sensors (Read 1 time) previous topic - next topic


Jan 25, 2013, 03:18 am Last Edit: Jan 25, 2013, 03:29 am by Nick_Pyner Reason: 1
Well, nobody has seen the code, but the fact that you get some sort of reading, and it is fine with one sensor, rather  implies it is kosher.  I guess there is the faintest possibility that your code is improperly addressing multiple sensors but I have never heard of such a thing giving a result like yours. Similarly, your description of the events clearly implies that your hardware items are equally kosher.

This pretty well means that the wiring is the only area left that merits your attention.  Wiring your probes in the manner recommended is a recognised method that, by separating the probes, may even offer a better means of identifying the problem.

Suboptimal is a relative term. The best description of suboptimal wiring I can think of, is wiring that doesn't work.


There may also be a problem of cable length, compounded when you add more DS18B20, or bad home connection.

Here is a version of the Hacktronics code

Code: [Select]
//  This Arduino sketch reads DS18B20 "1-Wire" digital
//  temperature sensors.
//  Copyright (c) 2010 Mark McComb, hacktronics LLC
//  License: http://www.opensource.org/licenses/mit-license.php (Go crazy)
//  Tutorial:
//  http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
//  modernised, compacted, and metricated by Nick Pyner
//  code uses Arduino LCD stuff, for shield on Freetronics EtherTen.
//  Serial print commands are for PLX-DAQ
//  Research your own pins!

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

//16x2 LCD
#include <LiquidCrystal.h>

#include <SD.h>
#include "Wire.h"
File myFile;
char filename[] = "00000000.CSV";

// clock
#include <string.h>
#include "RTClib.h"
#include "Wire.h"
#define DS1307_ADDRESS 0x68

LiquidCrystal lcd(8,9,56,5,6,7); // patchwire is D4 to A2 (pin56) for Mega with  this proto
int flag;
int second, minute, hour;
// 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

    delay(300);//Wait for newly restarted system to stabilize
    Serial.print("Temperature measurement, two sensors:\n\n");
    Serial.print("Initializing SD card...");
     pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    Serial.println("initialization OK.");
    FileName;   // first file named as today's date
  // if the file opened okay, write to it: This is an IF-THEN-ELSE  on validity of myFile
  if (myFile) {
    Serial.print("Writing to file...");
    myFile.println(" date       time      temp.  IN      OUT      Diff");
     Serial.println(" date        time       temp.    IN       OUT        DIFF");
  } else {
    Serial.println("error opening test.txt");
  // set up the LCD,s number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("temp in     out");   

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

void loop(void)

     if (hour == 0 && minute == 0 && second == 0)

  flag = 0;
        myFile = SD.open("test.txt", FILE_WRITE); //+++++++++++++OPEN
  //Get the sensor readings. There are two of them

  flag = 1;

  diff = tempC - InTemp;
  Serial.print (diff);
  Serial.println(" ,  ");
    myFile.close();//++++++++++++++++++++++++ CLOSE

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


void printDate(){

  // Reset the register pointer

  byte zero = 0x00;

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print("     ");
      if (second < 10)
  Serial.print("         ");

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );

void FileName(){

DateTime now = RTC.now();

filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
//filename[8] = ".csv";

myFile = SD.open(filename, FILE_WRITE);


Well if you had a sensor which created an analog voltage,     you can easily have effects which change that voltage by the time it gets to your analog-to-digital converter.    It is not hard to corrupt a low-voltage analog signal,  with the result that you get a temperature different.

But a digital communication either works or it doesn't.   It is quite hard to corrupt the I2C signal in such a way,  to add 0.8 degrees to the reported temperature,   and not have the communication completely fail.

It's the same principle with digital vs. analog  tv or radio.

0.8 degrees isn't much, anyway


Bear in mind that these sensors generate some heat themselves - if they're in close proximity they may affect each other.

Go Up