Comparing 3 DS18B20 Temperature sensors.

Hi everybody.
I'm want to compare 3 DS18B20 temp sensors on 1 wire.
How do I do that??
I'm been trying different sketches but I am not getting there.
I want to compare sensor 3 against sensor 1 and get a value, then see if sensor 3 is 5 degrees C higher than sensor 1.
then I want to compare sensor 2 against sensor 1 to see if the temperature is lower than 1 degree.
Does somebody know and could give me a hint how to write this??
Thanks to wildbill I starting to get there, but still having some trouble with the state in the end of sketch.
Status Log


starting one wire sensors
Locating 1-wire devices...
Found 3 devices.
Givarna visar...

Temp solpanel is: C: 28.75

Temp Invatten is: C: 26.00

Temp utvatten is: C: 26.00

Manuellt on = 1, off = 0: 0

Status State1: 0

Status State2: 0

Diff 1 is:2.75

Diff 2 is:0.00


I can't get Diff 2 to change to 1, even moving sensor 2 (utvatten) to same water temp as sensor 3 (solpanel)
Diff 1 works OK
What have I done wrong??

/* This Arduino sketch reads 3 DS18B20 "1-Wire" digital
 temperature sensors. Marcs testing.
*/ 

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

// Data wire is plugged into pin 3 on the Arduino
#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);

//Adjustments

//Set max pool temp
float  poolMax = 31.0;

//if poolMax paus system
float poolCooldown = 1.0;

//Set solpanel diff to start
float setDiff = 5.0;

//Set minimum diff temp
float minDiff = 1.0;

//set state
int state1 = 0;
int state2 = 0;


// Assign the addresses of your 1-Wire temp sensors.
// Solpanel S3, invatten S1, utvatten S2
DeviceAddress solpanel = { 0x28, 0x43, 0x41, 0xD9, 0x04, 0x00, 0x00, 0x1A };
DeviceAddress invatten = { 0x28, 0x10, 0xBB, 0xD9, 0x04, 0x00, 0x00, 0x76 };
DeviceAddress utvatten = { 0x28, 0x53, 0x70, 0xDA, 0x04, 0x00, 0x00, 0x80 };

// Number of temperature devices found
int numberOfDevices;

//variables used for control logic

float   Temp1, Temp2, Temp3;   //  storage for comparing the inlet and outlet temps
const boolean ON = HIGH;        //  makes the code easier to read
const boolean OFF = LOW;        //  makes the code easier to read

//variables used for control logic
float differential1 = 0.0 ;
float differential2 = 0.0 ;
int invalidCount = 0;

//define pin ManPump On
const int manon = 11;
//define pin for pump relay
const int pumpOn = 13;
const int ledPin = 12;      // the number of the green LED pin
// variables will change:
int manonState = 0;         // variable for reading the Manual On status

//this part of the code runs once to get everything setup and started.

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  Serial.println ("starting one wire sensors");
  sensors.begin();
  
  // locate devices on the one wire bus
  Serial.println("");
  Serial.print("Locating 1-wire devices...");
  Serial.println ("");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
  
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(solpanel, 10);
  sensors.setResolution(invatten, 10);
  sensors.setResolution(utvatten, 10);
  

    //set pins to output
   { 
  pinMode (pumpOn, OUTPUT);
  pinMode (manon, INPUT);
  pinMode(ledPin, OUTPUT); // green LED
    }
  //give time for everything to start
  delay (2000);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    }
}
void loop(void)
{
// read the state of the switch value:
  manonState = digitalRead(manon);

  // check if the switch is on.
  // if it is, the buttonState is HIGH:
  if (manonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);
    state1 = 1;
  } 
  if (manonState == LOW) {     
    // turn LED off:    
    digitalWrite(ledPin, LOW);
    //state1 = 0;
  } 
    

{ 
  
  Serial.print("Givarna visar...\n\r");
  sensors.requestTemperatures();
  
  Serial.print("Temp solpanel is: ");
  printTemperature(solpanel);
  Serial.print("\n\r");
  Serial.print("Temp Invatten is: ");
  printTemperature(invatten);
  Serial.print("\n\r");
  Serial.print("Temp utvatten is: ");
  printTemperature(utvatten);
  Serial.print("\n\r");
  Serial.println(""); // radbryt
  Serial.print ("Manuellt paa = 1, av = 0: ");
  Serial.println(manonState);
  Serial.println("");
  Serial.print ("Status State1: ");
  Serial.println(state1);
  Serial.println("");
  Serial.print ("Status State2: ");
  Serial.println(state2);
  Serial.println("");
  delay(1500);
 
 
 //Sensors for logic

Temp1 = sensors.getTempC(invatten);
Temp2 = sensors.getTempC(utvatten); 
Temp3 = sensors.getTempC(solpanel);

     
     //difference between solpanel and invatten gives us our differential
      differential1 = Temp3-Temp1;
      Serial.print("Diff 1 is:");
      Serial.println(differential1);
      Serial.println("");
      differential2 = Temp2-Temp1;
      Serial.print("Diff 2 is:");
      Serial.println(differential2);
      Serial.println("");
      
      //logic1
     {
      if ((differential1 > 5.0) && (Temp1 < poolMax)); { // compare the temps,
      (state1 = 1);      // return 1 if heat is on
           
     if (differential1 < 5.0)
    (state1 = 0);
      }
      //Logic2 ( This doesn't work, Why??) 
      {
    if (differential2 > 1.0 );
   (state2 = 1); 
   
    if (differential2 < 1.0 );
    (state2 = 0);
     }
delay (2000);
}

}
}

How to use this forum

Please post your sketches.

Sounds as if you have solved the tricky part of getting readings from multiple sensors.

To do the comparisons you would save the reading from each sensor into a different variable and then use IF statements to compare them however you want and take the appropriate actions.

Mr_Klatjoff:
I'm getting serial temp readings for sensor 1,2,3 but I'm to new to figure this out:
please help.

Sounds like you are almost there. You should be using a subroutine that returns the variable under the same name, like TempC . You need to store them by assigning new names, like ThisC, ThatC and OtherC. You can then get the differences.

diff=ThatC - ThisC etc.

Nick_Pyner:

Mr_Klatjoff:
I'm getting serial temp readings for sensor 1,2,3 but I'm to new to figure this out:
please help.

Sounds like you are almost there. You should be using a subroutine that returns the variable under the same name, like TempC . You need to store them by assigning new names, like ThisC, ThatC and OtherC. You can then get the differences.

diff=ThatC - ThisC etc.

I been trying that ! Even tried differential =solpanel-invatten; . This code gave me a number 8 and didn't change holding the sensor 3 .

Ah, the poorly named printTemperature function claims another victim.

To get the temp from your sensors, use a construct like this:

Temp1 = sensors.getTempC(invatten);

Mr_Klatjoff:
I been trying that !

Try something like this. Parts from a larger programme but has the essentials.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>                  
#include <Ethernet.h>            // Ethernet 
#include <HttpClient.h>          // Cosm lib
#include <Cosm.h>                // Cosm lib
#include <PCD8544.h>             // Nokia 5110
#include <SD.h>                  // SD card
#include <string.h>              // from Date As Filename 
#include "RTClib.h"              // from Date As Filename
#include "Wire.h"                // LCD disp, SD card, and serial

#define DS1307_ADDRESS 0x68

RTC_DS1307 RTC;
static PCD8544 lcd;


byte InThermo[8] =  {0x28, 0x39, 0xFD, 0x50, 0x04, 0x00, 0x00, 0x69};
byte OutThermo[8] = {0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95};
byte DrainThermo[8] = {0x28, 0x62, 0xA5, 0x2D, 0x04, 0x00, 0x00, 0x21};

#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


int  second, minute, hour, weekDay, monthDay, month, year;

int k=0;                                                                                                
float InTemp, OutTemp, DrainTemp;    




void setup() {
  Wire.begin();
  Serial.begin(9600);

  delay(300);//Wait for newly restarted system to stabilize
  lcd.setCursor (0,0);

  delay(500);
  lcd.setCursor (0,1);

  pinMode(10, OUTPUT);

  

lcd.clear();

  Serial.println("LABEL,Time,InTemp,OutTemp,diff,DrainTemp");

  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);
  sensors.setResolution(DrainThermo, 12);

  Serial.println("Starting multiple datastream upload to Cosm...");
  Serial.println();

  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(10000);
  }
}

void loop() {

 Serial.print("DATA,TIME,       "); 
  int ret=0;
  //get the values from the DS8B20's 
  sensors.requestTemperatures();

  float InTemp = (sensorValue(InThermo));
  float OutTemp = (sensorValue(OutThermo));  
  float DrainTemp = (sensorValue(DrainThermo)); 

  float diff = OutTemp - InTemp;


  Serial.print(InTemp);
  Serial.print(" ,  ");
  Serial.print(OutTemp);
  Serial.print(" ,  ");
  Serial.print(DrainTemp);
  Serial.println(" ,  ");

  lcd.setCursor(49,0);
  lcd.print(InTemp);
  lcd.setCursor(49,1);
  lcd.print (OutTemp);
  lcd.setCursor(49,2);
  lcd.print(DrainTemp);

  k=k+1;  

 
  delay(850);
}  // loop ends here

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  float tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

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

wildbill:
Ah, the poorly named printTemperature function claims another victim.

To get the temp from your sensors, use a construct like this:

Temp1 = sensors.getTempC(invatten);

Thanks wildbill, that was a code I didn't try :roll_eyes:
Is it possible to get the reading in a value of 1.x , now it's only showing me 1 degree C diff?

Mr_Klatjoff:
is it possible to get the reading in a value of 1.x , now it's only showing me 1 degree C diff?

Declare differential as float instead of int.

wildbill:

Mr_Klatjoff:
is it possible to get the reading in a value of 1.x , now it's only showing me 1 degree C diff?

Declare differential as float instead of int.

Thanks wildbill, U are a rock! :wink:
but now I'm having a new trouble.
What am I doing wrong in the end of the new sketch I updated?
Can't get state2 to change, what am I doing wrong??
state1 works.

      //Logic2 ( This doesn't work, Why??) 
      {
    if (differential2 > 1.0 ); // get rid of semicolon
   (state2 = 1); 
   
    if (differential2 < 1.0 ); // and this one
    (state2 = 0);
     }

Those semicolons should not be there - compare the similar code you have above.

wildbill:

      //Logic2 ( This doesn't work, Why??) 

{
    if (differential2 > 1.0 ); // get rid of semicolon
   (state2 = 1);
   
    if (differential2 < 1.0 ); // and this one
    (state2 = 0);
     }




Those semicolons should not be there - compare the similar code you have above.

Thanks wildbill!
Now it works, I'll come back to you when I mess it up again :wink: