Setting Temperature Differential Between 2 x DS18B20's

Hi All,

Im making a type of temprature controller using two DS18B20 temperature sensors.
I would like to activate relays and modes based on the difference between the two temperatures.
I know i can use greater than or less than but I dont know how to code so that I can set a value of difference.

EG; When sensorA is 5 degrees lower than sensorB

if (sensorsA.getTempCByIndex(0) < sensorsB.getTempCByIndex(0)) {

Serial.println("Cooling Mode Enabled");

1 Like

if( (sensorA-5) < sensorB ) )

I think that should be:

if (sensorA < sensorB-5)

Thanks for the reply, I think I need to do it in this code somehow though

if (sensorsA.getTempCByIndex(0) < sensorsB.getTempCByIndex(0)) {

Serial.println("Cooling Mode Enabled");

if ( sensorsA.getTempCByIndex(0) < (sensorsB.getTempCByIndex(0)-5) )

Legend! thank youi!

Hi Guys,

Im currently working on a type of temperature controller.
Most of which does seem to worl besides a couple of little things.
On the heating side, everything stages up fine but as it drops back to relay 1 it stays on even though the temperature is below the 28 degrees.

Would anyone have any idea as to why after the relay1 is activated above 28 degrees it does not go off again when it falls below the 28 degrees?

Sorry Im new to all this and trying to learn.

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

#define RELAY_ON 1
#define RELAY_OFF 0
#define RELAY_1 14 //Relay 1 pin
#define RELAY_2 15 //Relay 2 pin
#define sensorsA supplyAir //sensorA defined as supplyAir
#define sensorsB returnAir //sensorB defined as returnAir
const byte oneWireAPin = 18;
const byte oneWireBPin = 19;

const int diffCool = 2; //differentail to determine cooling mode
const int diffHeat = 2; //differentail to determine cooling mode

const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

OneWire oneWireA (oneWireAPin);
OneWire oneWireB (oneWireBPin);

DallasTemperature supplyAir (&oneWireA) ;
DallasTemperature returnAir (&oneWireB) ;

void setup()
{
  pinMode(RELAY_1,OUTPUT); // relay one pin
  pinMode(RELAY_2,OUTPUT); // realy two pin
  digitalWrite(RELAY_1,RELAY_OFF);
  digitalWrite(RELAY_2,RELAY_OFF);
  supplyAir.begin();
  returnAir.begin();

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("AC DRED Control");
  lcd.setCursor(0, 1);
  lcd.print("Starting FW:1.0");
  delay(5000);
  lcd.clear();
  lcd.setCursor(2, 2);
  lcd.print("Please Wait");
  delay(5000);
  lcd.clear();
}

void loop()
{
supplyAir.requestTemperatures();
returnAir.requestTemperatures();

float tempC1 = supplyAir.getTempCByIndex(0);
    lcd.setCursor(0, 0);
    lcd.print("Supply Air:");
    lcd.println(tempC1);
  
    

float tempC2 = returnAir.getTempCByIndex(0);
    lcd.setCursor(0, 1);
    lcd.print("Return Air:");
    lcd.println(tempC2);
    delay(2000); //delay 2000ms

//if sensorA is less than sensorB by (diffCool = 5)
if (supplyAir.getTempCByIndex(0) < (returnAir.getTempCByIndex(0)-diffCool)) { 
    lcd.setCursor(0, 1); // Cursor position
    lcd.print("Cooling Mode   "); // Display Cooling Mode on LCD
    delay(2000); //delay 2000ms
}

if (supplyAir.getTempCByIndex(0) <15) {
    digitalWrite(RELAY_1,RELAY_ON);
    digitalWrite(RELAY_2,RELAY_OFF);
    delay(2000); //delay 2000ms
}

if (supplyAir.getTempCByIndex(0) <10) {
    digitalWrite(RELAY_1,RELAY_OFF);
    digitalWrite(RELAY_2,RELAY_ON);
    delay(2000); //delay 2000ms
}
//if sensorA is greater than sensorB by (diffHeat = 5)
if (supplyAir.getTempCByIndex(0) > (returnAir.getTempCByIndex(0)+diffHeat)) { 
    lcd.setCursor(0, 1);
    lcd.print("Heating Mode   "); // Display heating mode on LCD
    delay(2000); //delay 2000ms
}
//if sensorA is greater than or equal to 28 degrees and below 33 degrees, turn on relay 1 and turn off relay 2
if (supplyAir.getTempCByIndex(0) >=28 && (returnAir.getTempCByIndex(0) <33)) {
    digitalWrite(RELAY_1,RELAY_ON); // Turn on relay 1
    digitalWrite(RELAY_2,RELAY_OFF); // Turn off relay 2
    delay(2000); //delay 2000ms
}


else{  //if temperatures are below 28 degrees then no relays should be activated.
    digitalWrite(RELAY_1,RELAY_OFF); // Turn relay off
    digitalWrite(RELAY_2,RELAY_OFF); // Turn relay off
}
//if sensorA is greater than 33 degrees, turn off relay 1 and turn on relay 2
if (supplyAir.getTempCByIndex(0) >=33) {
    digitalWrite(RELAY_1,RELAY_OFF); //Turn off relay 1
    digitalWrite(RELAY_2,RELAY_ON); // Turn on relay 2
    delay(2000); // delay 2000ms
}

lcd.clear(); //Clear display before restrating loop
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hi and welcome to the forum. Please read the forum guide in the sticky post and then correct how you posted your code above and anything else you forgot to include. Thanks.

Because there is no code to make that happen. Relay1 won't get switched off until the temp drops below 10 degrees.

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Thank you.

thanks for the reply, where are you getting the 10 degrees from?
what would be the best code to make this happen correctly?

The only way I can get it to go off properly is by adding a else statment after that 28 degrees section of code.

understood, they are totally different questions though.

They are the same topic, your code in the second question clearly (to me anyway) is just a simple follow on from the first question, so they are highly related. The information in the first question explains much about the second.

As to your question, I am going to reverse what @PaulRB said and ask you where in your code you think it turns relay1 off: Where in your code do you think it turns relay1 off?

And add the code tags please!

From your code.

EDIT: Oh, it will also switch relay1 off if the temp is above 33.

Please fix your post as requested, then ask your questions.

It would help a lot of people if you use named constants and variables - as well as adding copious comments throughout your code…

Code Formatting has already been mentioned.

all good

1 Like

sorry, i have now edited and tried to add in as best possible with the little I know

thanks, I have tried editing as best that I know now

Thanks, that is much better :grinning: