2 x ds18b20 to control 2 x relay

Hi there I’m trying to develop a system of 2 ds18b20 sensors to control a relay each, this would be done by using their temperature reading but I am struggling with getting the relays to click in and out I don’t get any change when it reaches the set temperature, I get the temperature readings fine but I’ve been going round in circles trying different methods to get the relays to function but can’t seem to get it working, I am also going to be using a clamp meter to measure current and control a relay as well but I haven’t got that far yet. I have attached my code below if anyone can help or come up with another way to do it that would be fantastic

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);  

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
int relPin1 =6 ;
int relPin2 =5 ;
int relPin3 =4 ;
int relPin1In = 3;
int relPin2In = 4;
int relPin3In = 5;
int Sensor1 =2;
float tempC;

int relPin1Val;
int relPin2Val;
int relPin3Val;
void setup()
{
  
  pinMode(relPin1, OUTPUT);
  pinMode(relPin2, OUTPUT);// declaring pins to be an output:
  pinMode(relPin3, OUTPUT);
  
  sensors.begin();  // Start up the library
  Serial.begin(9600);
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
  int reading = digitalRead(Sensor1); 

}

void loop()
{ 
  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures(); 
  float temperature1 = sensors.getTempCByIndex(0);
  float temperature2 = sensors.getTempCByIndex(1);
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor");
    Serial.print(i+1);
    Serial.print(" : ");
    tempC = sensors.getTempCByIndex(i);
    Serial.println(tempC);
    Serial.print((char)176);//shows degrees character
    delay (2000); // change reading times
   
    if (temperature1 <= 25) {
    relPin1Val=LOW;
    }
     else if (temperature1 > 25) {
      relPin1Val=HIGH;
    }
    
    if (temperature2 <= 25) {
    relPin1Val=LOW;
    }
     else if (temperature2 > 25) {
      relPin1Val=HIGH;
    }
  {    digitalWrite(relPin1, relPin1Val);
  digitalWrite(relPin2, relPin2Val);
  digitalWrite(relPin3, relPin3Val);
  }  } }

Hi…

Could you share your serial output … i dont have a ds18b20 so i can’t replicate your setup…

This is the onewire library you using?

Your temperature reading variables are float:

float temperature1 = sensors.getTempCByIndex(0);

Compare against a float not an integer. Change likes like:

if (temperature1 <= 25) {

to

if (temperature1 <= 25.0) {

IndianaTux:
Your temperature reading variables are float:

float temperature1 = sensors.getTempCByIndex(0);

Compare against a float not an integer. Change likes like:

if (temperature1 <= 25) {

to

if (temperature1 <= 25.0) {

It should not matter unless temp1 == 25 which is highly unlikely. For details on C hence Arduino float comparison read this Question 14.5

Relevant Discussion on this forum is here
Arduino forum discussion on float comparison

Write something simpler that just turns a single relay on and off. Blink in other words. Forget reading temperatures until you have that working.

mugambi: Hi..

Could you share your serial output .. i dont have a ds18b20 so i can't replicate your setup..

This is the onewire library you using?

this is my serial output

Locating devices...Found 2 devices.

Sensor1 : 23.81 ⸮Sensor2 : 23.31 ⸮Sensor1 : 23.87 ⸮Sensor2 : 23.31 ⸮Sensor1 : 24.87 ⸮Sensor2 : 23.31 ⸮Sensor1 : 26.19 ⸮Sensor2 : 23.31 ⸮Sensor1 : 26.31 ⸮Sensor2 : 23.31 ⸮Sensor1 : 26.19 ⸮Sensor2 : 23.31 ⸮Sensor1 : 26.12 ⸮Sensor2 : 23.31 ⸮Sensor1 : 26.00 ⸮Sensor2 : 23.31 ⸮Sensor1 : 25.87 ⸮Sensor2 : 23.31

and I believe so

rhysajones: this is my serial output

Locating devices...Found 2 devices.

Output looks good. The sensors reading seem ok. The logic seems correct too..

Please share your relay wiring pics and details and code if you have changed it from above to my email or whatsapp .. details are in your PM