Multiple sensor thermostat

Update.

the issue is fixed. Cause was the relay board being a type where wiring the coils separately was not possible.
thank you for all the help.

Hi all,

I hope this is the correct category to get some support. I have been working on a multisensor thermostat, using DS18B20 temp sensors and Dallastemperature library and arduino nano. I have the temp sensor codes and they work fine. the problem is that the relays are always in HIGH position, no matter what temperature the sensors measure. According to the data on serial monitor, the temperature informaiton is fine. Any ideas why he relays stuck in High? the relay board and the nano are powered separately.

/* Read multiple DS18B20 1-Wire digital temperature sensors by address. More info: https://www.makerguides.com */

// Include the required Arduino libraries:
#include <OneWire.h>
#include <DallasTemperature.h>

// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 4

// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);

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

// Addresses of DS18B20 sensors connected to the 1-Wire bus
byte sensor1[8] = {0x28, 0x00, 0x57, 0x1D, 0x50, 0x50, 0x09, 0xEF};
byte sensor2[8] = {0x28, 0xFF, 0x64, 0x06, 0xF8, 0x24, 0x9D, 0xF1};
byte sensor3[8] = {0x28, 0xFF, 0x64, 0x06, 0xF8, 0x7C, 0xBE, 0x31};
byte sensor4[8] = {0x28, 0xFF, 0x64, 0x06, 0xF8, 0x63, 0x1B, 0x55};

const int relay1 = 5;
const int relay2 = 6; //ujabb
const int relay3 = 7; //ujabb
const int relay4 = 8; //ujabb

const float onTemp = 53.0;
const float offTemp = 58.0;

// Keep track of whether the relay is energised/closed (1) or open (0). Start with closed (1).
int relay1Status = 1;
int relay2Status = 1;
int relay3Status = 1;
int relay4Status = 1;



void setup() {
  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
  // Start up the library:
  sensors.begin();
  sensors.setResolution(10);

// Set up the relay output for the arduino, and start with it high
 // since the thermostat only turns off at >offTemp degrees.
 pinMode(relay1, OUTPUT);
 digitalWrite(relay1, HIGH);
 pinMode(relay2, OUTPUT); //uj
 digitalWrite(relay2, HIGH); //uj
 pinMode(relay3, OUTPUT); //uj
 digitalWrite(relay3, HIGH); //uj
 pinMode(relay4, OUTPUT); //uj
 digitalWrite(relay4, HIGH); //uj
}

void loop() {
  // Send the command for all devices on the bus to perform a temperature conversion:
  //sensors.requestTemperatures();

sensors.requestTemperatures(); // Read temperature of sensor1
 float sensorOneTemperature = sensors.getTempCByIndex(1);
 float sensorTwoTemperature = sensors.getTempCByIndex(2); //uj
 float sensorThreeTemperature = sensors.getTempCByIndex(3); //uj
 float sensorFourTemperature = sensors.getTempCByIndex(4); //uj 

if(relay1Status == 1 and sensorOneTemperature >= offTemp-0.00001){
 // Relay is currently closed, but the temperature exceeds offTemp - therefore open the relay
 digitalWrite(relay1, LOW);
 // Remember the new status
 relay1Status = 0;
 }

if(relay1Status == 0 and sensorOneTemperature <= onTemp+0.00001){
 // Relay is currently open from a previous high temperature event, but now the temperature 
 // is below the threshold so close it again
 digitalWrite(relay1, HIGH);
 // Remember the new status
 relay1Status = 1;
 }

//uj2

if(relay2Status == 1 and sensorTwoTemperature >= offTemp-0.00001){                                       
 // Relay is currently closed, but the temperature exceeds offTemp - therefore open the relay
 digitalWrite(relay2, LOW);
 // Remember the new status
 relay2Status = 0;
 }

if(relay2Status == 0 and sensorTwoTemperature <= onTemp+0.00001){
 // Relay is currently open from a previous high temperature event, but now the temperature 
 // is below the threshold so close it again
 digitalWrite(relay2, HIGH);
 // Remember the new status
 relay2Status = 1;
 }

// uj3

if(relay3Status == 1 and sensorThreeTemperature >= offTemp-0.00001){                                       
 // Relay is currently closed, but the temperature exceeds offTemp - therefore open the relay
 digitalWrite(relay3, LOW);
 // Remember the new status
 relay3Status = 0;
 }

if(relay3Status == 0 and sensorThreeTemperature <= onTemp+0.00001){
 // Relay is currently open from a previous high temperature event, but now the temperature 
 // is below the threshold so close it again
 digitalWrite(relay3, HIGH);
 // Remember the new status
 relay3Status = 1;
 }

// uj4

if(relay4Status == 1 and sensorFourTemperature >= offTemp-0.00001){                                       
 // Relay is currently closed, but the temperature exceeds offTemp - therefore open the relay
 digitalWrite(relay4, LOW);
 // Remember the new status
 relay4Status = 0;
 }

if(relay4Status == 0 and sensorFourTemperature <= onTemp+0.00001){
 // Relay is currently open from a previous high temperature event, but now the temperature 
 // is below the threshold so close it again
 digitalWrite(relay4, HIGH);
 // Remember the new status
 relay4Status = 1;
}

  Serial.print("Sensor B ajto: ");
  Serial.print(sensorOneTemperature); // mod
  Serial.print(" C   ");
 // printTemperature(sensor1); // call the printTemperature function with the address of sensor1 as input
  Serial.print("Sensor J ajto: ");
  printTemperature(sensor2);
  Serial.print("Sensor szelvedo ");
  printTemperature(sensor3);
  Serial.print("Sensor padlo: ");
  printTemperature(sensor4);

  Serial.println(); // prints an empty line
  delay(500);
}

void printTemperature(DeviceAddress address) {
  // Fetch the temperature in degrees Celsius for device address:
  float tempC = sensors.getTempC(address);
  Serial.print(tempC);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.print("C    ");

}

Can you provide a link to the relays? and a drawing of how the relays are connected.G

One of the best ways to find problems like this is to simplify the code to the most basic that will do the job. Write some simple code that deals only with 1 DS18B20 and 1 relay and test it.

Put
Serial.print(sensorOneTemperature) // Or whichever sensor you are reading
Right after you read the sensor and see if it prints what you expect.

Your temperatures for switching are a bit in the high side, if it were me I'd set the a few degrees above room temperature then heat the sensors with my hand to test.

Once you get simple code working then add in the extra code for other sensors and relays and anything else you need.

That said, the problem could be in the wiring, no way to know unless you share more information, such as a schematic, details of the parts you have and some photos.

here is a link: https://www.diyelectronics.co.za/store/electro-mechanical-relays/267-4-channel-5v-relay-module-10amp-250v.html

the connecion - I do not have a drawing, so here is a descripion:

5v power supply (max 5A) connected to the ground and vcc pins on the relay board. the in1,2,3,4 pins are connected to d5,6,7,8 on he nano.

5v (max5A) power supply is also connected o 5v and gnd on the nano (they are in fron of each other.
the temp sensors are connected in parallel connection to gnd and 3.3v on the nano, plus to d4 pin. there is a 4.7k resistor between d2 and 3.3v pin.

I have to hold myself back when someone says that, very tempting to make a sarcastic remark about pencils and paper...

Please read How to get the best out of this forum, there's information there about creating a schematic. A list of this connected to that is close to useless. Photos help too.

1 Like

thank you. I did test it closer to room temperature - but no reaction other then switching the relays HIGH at the start.
can I connect he nano to the laptop while powering it via powersupply? that way I could add the relay state on the monitor as you suggested.

Do the relays have a separate power supply?
The Nano will, possibly , have insufficient power for the relays.
G

Yes, should be OK, there is a diode between the 5V from the USB connection and the 5V from everywhere else. Ideally feed 5V into the 5V pin. Even better connect the external PSU to the relays and connect the Nano to the USB, don't forget the common ground between the different power supplies.

Trying to write code without having serial print available is like pulling your own teeth without being drunk.

I don't know if this has anything to do with your problem, but at the top of your loop(), you refer to sensor indices of 1, 2, 3 and 4, but they should be 0, 1, 2 and 3.

Maybe that could help?

Best analogy ever.

1 Like


here is a drawing...

'thank you. I will give i't a try.

'thanx. I used serial print to check if the relayOnetemperaure data is correct or not.

yes.

How many grounds do you have?
3.3 V and 5v ground should be joined together.
G

They are- look at the schematic.

Hi @Tiborx8

I tested your sketch with a DS18B20 of mine.
I modified the values of:
const float onTemp = 23.0;
const float offTemp = 30.0; (greater than room temperature here and now).
to facilitate my testing.
I commented on the print lines at the end of the sketch.
I read the temperature and status value of relay 1
And see the result on the serial monitor:
It worked correctly.

RV mineirin

sensorOneTemperature  28.75
relay1  1
sensorOneTemperature  28.75
relay1  1
sensorOneTemperature  28.50
relay1  1
sensorOneTemperature  28.50
relay1  1
sensorOneTemperature  28.25
relay1  1
sensorOneTemperature  28.75
relay1  1
sensorOneTemperature  29.25
relay1  1
sensorOneTemperature  29.50
relay1  1
sensorOneTemperature  29.75
relay1  1
sensorOneTemperature  30.25
relay1  1
sensorOneTemperature  30.50
relay1  0
sensorOneTemperature  30.75
relay1  0
sensorOneTemperature  30.75
relay1  0
sensorOneTemperature  31.00
relay1  0
sensorOneTemperature  31.25
relay1  0
sensorOneTemperature  31.25

Yes, what?

There are two grounds drawn on the Nano. Are they joined together or is that an error.?
G

They are the same ground:
nano