How would you modify this heating system example code

Hi,

I have two squares side-by-side and I would heat a square next to a reference square. I would use the same sensor type DS18B20 for the reference square and heated square.

How would you change the example code below, that constants (in example 20 and 15) could be replaced with natural temp square sensor temp+4 (turn off) and natural temp square sensor temp+2 (turn on).

Do I just add #define SENSOR_PIN 3 when I try to activate the second sensor and how do the sensors separate in code?
Can I share the same 3.3 V source for two (or even 5) sensors? Google gave 1 mA current for the sensor and Arduino Mega has max 50 mA current rating(?), but is the math as straight forward?

I hope you get the idea from this! I'm just about finishing my Hello World exercise, this is a bit too demanding task.


/*

 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-heating-system
 */

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

#define SENSOR_PIN  2   // Arduino pin connected to DS18B20 sensor's DQ pin
#define RELAY_PIN   A5  // Arduino pin connected to relay which connected to heating element

const int TEMP_THRESHOLD_UPPER = 20; // upper threshold of temperature, change to your desire value
const int TEMP_THRESHOLD_LOWER = 15; // lower threshold of temperature, change to your desire value

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library

float temperature;    // temperature in Celsius

void setup() {
  Serial.begin(9600); // initialize serial
  sensors.begin();    // initialize the sensor
  pinMode(RELAY_PIN, OUTPUT); // initialize digital pin as an output
}

void loop() {
  sensors.requestTemperatures();             // send the command to get temperatures
  temperature = sensors.getTempCByIndex(0);  // read temperature in Celsius

  if(temperature > TEMP_THRESHOLD_UPPER) {
    Serial.println("The heating element is turned off");
    digitalWrite(RELAY_PIN, LOW); // turn off
  } else if(temperature < TEMP_THRESHOLD_LOWER){
    Serial.println("The heating element is turned on");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }

  delay(500);
}

You can have several DS18B20 sensors on the same pin. So you do not need to set another pin for another sensor. 5 sensors on one pin should be no trouble. You do need one pullup resistor (4.7K) for any number of sensors on the one pin.

Each sensor will have an index. Use the getTempCByIndex(index); Function to get the reading from a particular sensor using that sensors index. You can also get the temperature of a particular sensor by its address.

The Arduino will happily power 5 DS18B20 sensors. If you are using a Mega, why power the sensors with 3.3V? They work fine at 5V and you have more current available on the 5V.

Hi, thank you very much!

Does the code go like this after that:

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

#define SENSOR_PIN  2   // Arduino pin connected to DS18B20 sensor's DQ pin
#define RELAY_PIN   A5  // Arduino pin connected to relay which connected to heating element

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library

float temperature;    // temperature in Celsius

void setup() {
  Serial.begin(9600); // initialize serial
  sensors.begin();    // initialize the sensor
  pinMode(RELAY_PIN, OUTPUT); // initialize digital pin as an output
}

void loop() {
  sensors.requestTemperatures();             // send the command to get temperatures
  temperature = sensors.getTempCByIndex(0);  // read temperature in Celsius
  temperature2 = sensors.getTempCByIndex(1);  // read temperature in Celsius from unheated soil (how do I get the index right?)

  if(temperature > (temperature2 + 4)) {
    Serial.println("The heating element is turned off");
    digitalWrite(RELAY_PIN, LOW); // turn off
  } else if(temperature < (temperature2 + 2)){
    Serial.println("The heating element is turned on");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }

  delay(500);
}

The Arduino will happily power 5 DS18B20 sensors. If you are using a Mega, why power the sensors with 3.3V? They work fine at 5V and you have more current available on the 5V.

I was leaving the 5 V for the 230 V relay/relays (max 8 relays), but should I get an external power source for them?

Yes, the Arduino is not a power supply. It is fine for powering low current devices like sensors, but use an external supply to provide the higher current to the relay coils and to isolate the Arduino from the noise that relays can put on a power supply.

Are you going to do anything with the second DS18B20? You read it but do nothing with the reading.

It's used here

if(temperature > (temperature2 + 4)) {
...
} else if(temperature < (temperature2 + 2)){

Are these 5V relays? If so get a 5V, 1A PSU. This PSU can power the relays and the Arduino. Power the Arduino through its 5V pin, not the Vin pin or barrel socket.

Need to get my eyes checked, I guess.

Hi, thank you for the advice! And even more questions...

Is this the right kind of power supply?: DC POWER SUPPLY 5V 1A 5W - PARTCO

Just cut the head and divide for the relays and arduino? Is there any other downside than the need of two 230 V sockets if I still power Arduino with the barrel socket?

Is this suitable relay module or does the code change? those 4 extra relays could be useful in future. https://www.velleman.eu/downloads/29/vma436_a4v02.pdf
And what about this for temp sensor? Does these need the 4,7 kohm resistor (brochure says requires no external components...). https://www.elfadistrelec.fi/Web/Downloads/_m/an/WPSE324_eng_man.pdf

If i start to heat four squares, how do I address four relays:

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

#define SENSOR_PIN  2   // Arduino pin connected to DS18B20 sensor's DQ pin
#define RELAY_PIN   A5  // Arduino pin connected to relay which connected to heating element

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library

float temperature;    // temperature in Celsius

void setup() {
  Serial.begin(9600); // initialize serial
  sensors.begin();    // initialize the sensor
  pinMode(RELAY_PIN, OUTPUT); // initialize digital pin as an output
}

void loop() {
  sensors.requestTemperatures();             // send the command to get temperatures
  temperature = sensors.getTempCByIndex(0);  // read temperature in Celsius heated soil
  temperature2 = sensors.getTempCByIndex(1);  
  temperature3 = sensors.getTempCByIndex(2);
  temperature4 = sensors.getTempCByIndex(3);
  temp.reference = sensors.getTempCByIndex(4);  // read temperature in Celsius from unheated soil (how do I get the index right?)


  if(temperature > (temp.reference + 4)) {
    Serial.println("The heating element 1 is turned off");
    digitalWrite(RELAY_PIN, LOW); // turn off
  } else if(temperature < (temp.reference + 2)){
    Serial.println("The heating element 1 is turned on");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }

  if(temperature2 > (temp.reference + 4)) {
    Serial.println("The heating element 2 is turned off");
    digitalWrite(RELAY_PIN, LOW); // turn off
  } else if(temperature2 < (temp.reference + 2)){
    Serial.println("The heating element 2 is turned on");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }

  if(temperature3 > (temp.reference + 4)) {
    Serial.println("The heating element 3 is turned off");
    digitalWrite(RELAY_PIN, LOW); // turn off
  } else if(temperature3 < (temp.reference + 2)){
    Serial.println("The heating element 3 is turned on");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }

  if(temperature4 > (temp.reference + 4)) {
    Serial.println("The heating element 4 is turned off");
    digitalWrite(RELAY_PIN, LOW); // turn off
  } else if(temperature4 < (temp.reference + 2)){
    Serial.println("The heating element 4 is turned on");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }

  delay(500);
}

Sorry for the basic questions, but this is a much faster way to learn!

If the VMA324 requires no external components then the, required, pullup resistor is on the board. That is fine with one sensor but if you add more VMA324 sensors the pullups are then in parallel and it doesn't take many till the total pullup resistance is too low. You only need 1 pullup resistor (4.7K recommended) on the one wire bus so you may have to remove the pullups from all but one sensor board.

You will need 1 OUTPUT pin for each relay that you want to control. You could put the pin numbers in an array so that each relay index corresponds to a temperature sensor index (if that is what you want).

Thanks, have to find other sensor. I read somewhere that these DS18B20 sensors are safe to extend? Longest distance is 14 m.
Are the analog IN pins ok to use, as in this example they used A5? There seems to be also PWM pins in use for relays.

Is this closer :smiley: :

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

#define SENSOR_PIN  2   // Arduino pin connected to DS18B20 sensor's DQ pin
#define RELAY_PIN1   A1  // Arduino pin connected to relay which connected to heating element
#define RELAY_PIN2   A2  // Arduino pin connected to relay which connected to heating element
#define RELAY_PIN3   A3  // Arduino pin connected to relay which connected to heating element
#define RELAY_PIN4   A4  // Arduino pin connected to relay which connected to heating element


OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library

float temperature;    // temperature in Celsius

void setup() {
  Serial.begin(9600); // initialize serial
  sensors.begin();    // initialize the sensor
  pinMode(RELAY_PIN1, OUTPUT); // initialize digital pin as an output
  pinMode(RELAY_PIN2, OUTPUT); // initialize digital pin as an output
  pinMode(RELAY_PIN3, OUTPUT); // initialize digital pin as an output
  pinMode(RELAY_PIN4, OUTPUT); // initialize digital pin as an output
}

void loop() {
  sensors.requestTemperatures();             // send the command to get temperatures
  temperature = sensors.getTempCByIndex(0);  // read temperature in Celsius heated soil
  temperature2 = sensors.getTempCByIndex(1);  
  temperature3 = sensors.getTempCByIndex(2);
  temperature4 = sensors.getTempCByIndex(3);
  temp.reference = sensors.getTempCByIndex(4);  // read temperature in Celsius from unheated soil (how do I get the index right?)


  if(temperature > (temp.reference + 4)) {
    Serial.println("The heating element 1 is turned off");
    digitalWrite(RELAY_PIN1, LOW); // turn off
  } else if(temperature < (temp.reference + 2)){
    Serial.println("The heating element 1 is turned on");
    digitalWrite(RELAY_PIN1, HIGH); // turn on
  }

  if(temperature2 > (temp.reference + 4)) {
    Serial.println("The heating element 2 is turned off");
    digitalWrite(RELAY_PIN2, LOW); // turn off
  } else if(temperature2 < (temp.reference + 2)){
    Serial.println("The heating element 2 is turned on");
    digitalWrite(RELAY_PIN2, HIGH); // turn on
  }

  if(temperature3 > (temp.reference + 4)) {
    Serial.println("The heating element 3 is turned off");
    digitalWrite(RELAY_PIN3, LOW); // turn off
  } else if(temperature3 < (temp.reference + 2)){
    Serial.println("The heating element 3 is turned on");
    digitalWrite(RELAY_PIN3, HIGH); // turn on
  }

  if(temperature4 > (temp.reference + 4)) {
    Serial.println("The heating element 4 is turned off");
    digitalWrite(RELAY_PIN4, LOW); // turn off
  } else if(temperature4 < (temp.reference + 2)){
    Serial.println("The heating element 4 is turned on");
    digitalWrite(RELAY_PIN4, HIGH); // turn on
  }

  delay(500);
}

That code looks OK. Does it do what you want?

Yes, the analog inputs are really digital pins with analog input as a special function.

Not sure what that means. The PWM capable pins are digital pins so work fine controlling relays. There is no reason that I know of to use PWM on a relay. In fact PWMing a relay is probably a good way to kill it.

Why?

Here is a Maxim tutorial on how to use a long one wire bus.

See also Guidelines for Reliable one wire bus.

Hi, I meant to a type that doesn't have the resistor already.

Yes, the analog inputs are really digital pins with analog input as a special function.

Not sure what that means. The PWM capable pins are digital pins so work fine controlling relays. There is no reason that I know of to use PWM on a relay. In fact PWMing a relay is probably a good way to kill it.

Thanks!

Thanks for these. So better use the 5V pin as voltage source. I assume with weight of 46 m and radius of 14 m I'm good?

1-Wire networks that use Category 5e, twisted-pair copper wire and have 5V bus power supplied by the master.

So I can use Cat 5/Cat 6 Ethernet cable in extensions? We have this cable available, which isn't probably the same where you are, but it's twisted pair as the minimum in the guidelines: NEXANS TELEKAAPELI KANAVA/MAA NEXANS VMOHBU-TL 3x2x1,0 K1000 | Telekuparikaapelit | Onninen

I hope so! Thanks for all the help. Do I give feedback here after trials? :slight_smile:

It should not be difficult to unsolder the pullups on all but one of the VMA324 boards. If I were using those sensors that is what I would do. Or use one VMA324 board (with the pullup intact) and the rest just plain DS18B20 sensors.

That is something that you will have to find out. There are too many factors that I know nothing about for me to say.

Please do. It is always appreciated and future members that may come across this thread can learn from it.

Problem:
https://www.partco.fi/en/electromechanics/relays/relay-modules/17531-relaymod-8-iso.html

All the 5V relays I'm finding have warning "for max 50 VAC devices". But their specifications say 10 A 230 V. My heating cables are 230 V grid voltage ones.

What kind of relay would be suitable?

Looks ok. If you don't want to cut the cable, you could buy an in-line barrel socket and solder some cables/PCB header connectors to it. If you want to update to 8 relays, it's probably ok but maybe look for a 1.5A or 2A PSU in case you want to make more upgrades in future.

I can't think of one, but isn't that downside enough to make you want to avoid it?

You're right, what a stupid vendor to not correct their page!

Do not use this device to operate mains voltage devices. Only for voltages under 50VAC/DC.
...can control the load with the maximum current consumption of 10A (at 250V AC)

You need to look for relays which can handle the load of your heaters.

Thanks! I have four 700 W heating cables so total 2800 W consumption and <12.5 A current. So it was already too small even if it could handle 230 V. My bad.
What 5 V relays are available for these kinds of applications or are there any.

Thanks for confirmation for the psu.

They are separa

four 700 W heating cables so total 2800 W consumption and <12.5 A current.

they are separate so one relay needs to handle 3.04 A current.

Most relay boards offered for use with Arduino can handle mains voltages and more than 3A. Keep looking.