Comparing two ds20b18 values

Hello forum users! I am very new to home automation and I am currently working on my first project. ofcourse any of you will know that it will not go as hoped before XD. I have my code up and running but now I use a set value to compare my sensors against. What i want to achieve now is comparing two sensor values, and with that comparison i want to switch a relay either on or off.
I have tried to get it working but I am stuck, is someone able to help me?

Thanks in advance!

//hereby the code
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged TO GPIO 4
#define ONE_WIRE_BUS 4

// 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);

// Number of temperature devices found
int numberOfDevices;
const int relay = 26;

// We'll use this variable to store a found device address
DeviceAddress tempDeviceAddress;

void setup(){
// start serial port
pinMode(relay, OUTPUT);
// Start up the library

// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();

// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");

// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++){
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");

void loop(){
sensors.requestTemperatures(); // Send the command to get temperatures
//int arrayOne[2]={ sensors.getTempC(tempDeviceAddress), sensors.getTempC(tempDeviceAddress)};
//werkt nog niet
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++){
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
// Output the device ID
Serial.print("Temperature for device: ");
// Print the data
float tempC = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp C: ");

 if( sensors.getTempC(tempDeviceAddress) > 24)

digitalWrite(relay, LOW);
Serial.println("Current Flowing");
if( sensors.getTempC(tempDeviceAddress) < 24)
digitalWrite(relay, HIGH);
Serial.println("Current not Flowing");


// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++){
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}Vooraf opgemaakte tekst

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

1 Like