Turn pin LOW until value is reached

Hi again,

I´m checking the water temperature with a sensor and then use this to start the heater for the water. 3 years ago when I first built this thing I tried to get this to work as I wanted it to be but then I just left it like it was. Now I´m rebuilding and want it to work :slight_smile:

I use a relay board that activates relays with LOW.

The code:

startTemp = presetTemp-0.5

if (waterTemp < startTemp){
  digitalWrite(heater, LOW);
}

if (waterTemp > presetTemp {
  digitalWrite(heater, HIGH);
}

Problem here is that it takes some time for waterTemp to be stable. If presetTemp is 38 and the water temperature drops to 37.5 it can shift between lets say 37.25 and 37.5 and the heater turns on and off for a while. The same with the upper limit when the waterTemp reach presetTemp

I want it to work like this.

waterTemp drops to startTemp and then the heater turns on until waterTemp = presetTemp

waterTemp reach presetTemp and the heater turns off until waterTemp = startTemp

Thanks in advance

How can I write this?

Perhaps there is delay() in your code that causes your problem.

arduino_new:
Perhaps there is delay() in your code that causes your problem.

His problem is he is heating fluid. The heated water rises and cooler water takes it's place. For his scheme to work, he must stir the water.

Paul

How much water does your tank hold? What is the wattage of your heating element and where is it located, bottom, top or externally with a circulating pump? What kind of temperature sensor and where is it located?

Hi,
Is there a reason for only 0.5C hysteresis.
What range can the tank temperature be acceptable.

I agree with @outsider, sounds like you need a stirrer and possibly reposition your sensor.
If the sensor is positioned over the heater then it will not represent the tank temperature as the heater operates.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you post a diagram of the tank, sensor, heater, so we can see the relative positioning of the components?

Problem here is that it takes some time for waterTemp to be stable. If presetTemp is 38 and the water temperature drops to 37.5 it can shift between lets say 37.25 and 37.5 and the heater turns on and off for a while. The same with the upper limit when the waterTemp reach presetTemp

Then your hysteresis is not working.
You may have declare your temperature values as int.
Or make your 0.5 a whole number like 1, 2, 3.

The problem with your code is you are mixing integer and float values.
How do you input your preset temperture.

Please post the complete code.

Thanks.. Tom... :slight_smile:
PS. Is this to do with this
http://forum.arduino.cc/index.php?topic=553238.0
A HotTub?

Hi all,

I´ve been reading what you are writing and I changed startTemp from int to float. And now it seems to work like I want it to? is it correct to use float? Im not very good at all with variables. Don´t bother the airTemp things since I need a new sensor for that. Just testing with the same adress as waterTemp.

I´m running everything just on the table now. I will connect everything today and see how it works with the real things around it.

It´s for a hot tub I built by myself. Its around 3000 litres and the heater is 3kW. There is an external circulation pump. The sensor is placed inside the tub and the presetTemp is controlled via a potentiometer.

Attached a picture of the connections. I use 24vdc to control the contactors. The big unit is for 12vdc that is used for arduino and LED-lights in the tub.

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

const int heater = 2;
const int circulation = 3;
const int drain = 4;

int potPin = 1;
int tempPot = 0;
int presetTemp = 0;
float startTemp;

//Temperatursensorer
#define ONE_WIRE_BUS 10
#define TEMPERATURE_PRECISION 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress airTemp = { 0x28, 0xFF, 0x62, 0x89, 0x53, 0x15, 0x01, 0x42 };
DeviceAddress waterTemp = { 0x28, 0xFF, 0x62, 0x89, 0x53, 0x15, 0x01, 0x42 };

void setup()
{
  pinMode(heater, OUTPUT);
  pinMode(circulation, OUTPUT);
  pinMode(drain, OUTPUT);

  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(airTemp, 10);
  sensors.setResolution(waterTemp, 10);

  digitalWrite(heater, HIGH);
  digitalWrite(drain, HIGH);
  digitalWrite(circulation,LOW);

}

void printTemperature(DeviceAddress deviceAddress)
{
 float tempC = sensors.getTempC(deviceAddress);
 Serial.print("Temp C: ");
 Serial.print(tempC);
}

void loop()
{

sensors.requestTemperatures();
//Serial.print("Lufttemperaturen ar: ");
//printTemperature(lufttemp);
//Serial.print("\n\r");
Serial.print("Vattentemperaturen ar: ");
printTemperature(waterTemp);
Serial.print("\n\r");


tempPot = analogRead(potPin);
presetTemp = map(tempPot, 0, 1023, 5, 42);
Serial.print("Installd temp ar:");
Serial.print(presetTemp);
Serial.print("\n\r");
  
float airTemperature;
float waterTemperature;
airTemperature = sensors.getTempC(airTemp);
waterTemperature = sensors.getTempC(waterTemp);

startTemp = presetTemp-0.5;

if (waterTemperature <= startTemp){
  digitalWrite(heater, LOW);
}
if (waterTemperature > presetTemp) {
  digitalWrite(heater, HIGH);
}


}

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Ops pic.


How are you controlling the relays?

Is that a relay bank under the UNO?

This may help with respect to int and float maths.

Tom... :slight_smile:

DeviceAddress airTemp = { 0x28, 0xFF, 0x62, 0x89, 0x53, 0x15, 0x01, 0x42 };
DeviceAddress waterTemp = { 0x28, 0xFF, 0x62, 0x89, 0x53, 0x15, 0x01, 0x42 };

You have two DS18B20 temperature sensors for monitoring the air-temperature and the water-temperature. Why are they having the same Address (64-bit ROM Code)?

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Ops pic.
![|500x281](Turn pin LOW until value is reached - Programming Questions - Arduino Forum
How are you controlling the relays?

Is that a relay bank under the UNO?

This may help with respect to int and float maths.
https://www.baldengineer.com/5-common-arduino-programming-mistakes.html

Tom... :slight_smile:
[/quote]

Yes its a relay board under the arduino. I runt 24vdc through them to control the contactors.

Im taking notes of everything while building and will make a circuit diagram when I´m finnished and everything is fixed and working. Also channels to hide all cabling will be installed :slight_smile:

I´ll chekc that link out! Thanks!

GolamMostafa:
You have two DS18B20 temperature sensors for monitoring the air-temperature and the water-temperature. Why are they having the same Address (64-bit ROM Code)?

As I wrote in my post it was just for testing now since one of my sensor broke and I haven´t got a new one yet. So just for testing I used the same adress.)

1 kW per ton of water, I can imagine response time is a bit long, 4 hours per degree? C or F?

outsider:
1 kW per ton of water, I can imagine response time is a bit long, 4 hours per degree? C or F?

The response is not important since I normally just use 37C in the winters and some periods in the summer I keep it at 28C. So there is never any quick changes needed.

Yup, you need to build some hysteresis into your code.

loop() {
if I am currently heating, and the temp is above the upper limit
stop heating
else if I am currently not heating, and the temp is below the lower limit
start heating
end
}

This means that you need to keep a global variable named "I am currently heating", or something, to keep track of the state.