Home > Blockchain >  How to keep the temperature constant by relay
How to keep the temperature constant by relay

Time:01-01

I started a tutorial project to work with the Arduino Uno board. The summary of this project is that with the help of a heater and cooler we have to keep the temperature of a chamber constant.

The project is almost ready, but the problem is that after reaching a temperature higher than the desired temperature, the cooler starts working and lowers the temperature below the desired temperature, and then the heater starts working to raise the temperature. You know that after the chamber is heated, its temperature is constant for some time and there is no need to cool it down to a temperature lower than the desired temperature.

The problem is precisely here that I can't write a code to turn on the cooling and bring it from a temperature above 25 degrees to 25 degrees so that when it reaches 25 degrees, the cooling turns off, and if the temperature of the chamber without any cooling drops below 25 degrees, the heater starts to work

**** Meanwhile, I have a 2 relay module in my project ****

Complete code :

//********************Temp Control Relay
int PinRelay1 = 5;
int PinRelay2 = 6;
//********************Display
int PinClkLcd = 2;
int PinDioLcd = 3;
#include <TM1637.h>
#define CLK PinClkLcd
#define DIO PinDioLcd
TM1637 Display1(CLK, DIO);
//********************Temp
int PinVccTemp = 8;
int PinDataTemp = 9;
int PinGndTemp = 10;
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS PinDataTemp
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature Temp(&oneWire);
int8_t Digitos[] = {8, 8, 8, 8};
int Temp_IN;
//********************Area Tempreture
float TempSettings = 25.5;
//********************Switch
//int PinSW = 12;
int TimeSW;
int TimeDisplay;
boolean Key;

void setup() {
  Temp.begin();
  Display1.set(7);
  Display1.init();
  Display1.display(Digitos);
  Display1.point(POINT_OFF);
  //pinMode(PinSW, INPUT_PULLUP);
  pinMode(PinRelay1, OUTPUT);
  pinMode(PinRelay2, OUTPUT);
  pinMode(PinVccTemp, OUTPUT);
  pinMode(PinGndTemp, OUTPUT);
  digitalWrite(PinRelay1, HIGH);
  digitalWrite(PinRelay2, LOW);
  digitalWrite(PinVccTemp, HIGH);
  Temp.requestTemperatures();
  delay(500);
}

void loop() {
  Temp.requestTemperatures();
  Temp_IN = Temp.getTempCByIndex(0);
  int8_t Digit2 = Temp_IN % 10 ;
  int8_t Digit1 = (Temp_IN % 100) / 10 ;
  Digitos[3] = 12 ;
  Digitos[2] = Digit2 ;
  Digitos[1] = Digit1 ;
  Digitos[0] = 19 ;
  Display1.display(Digitos);
  if (Temp_IN > TempSettings)
    digitalWrite(PinRelay, LOW);
  if (Temp_IN < TempSettings)
    digitalWrite(PinRelay, HIGH);
}

}

CodePudding user response:

You can't keep the temperature at exactly 25°C with a simple on-off system; such a system will usually oscillate.

You could try to add some hysteresis; for example, don't start cooling until the temperature reaches 25.5°C, and don't start heating until the temperature drops to below 24.5°C.

Another option would be to model the chamber's thermal properties and the heating and cooling systems and then design and calibrate a (PID) control system based on that model.

BTW I think there is one too many curly brackets at the end.

CodePudding user response:

Unfortunately, with this training structure of the board, we cannot professionally keep the temperature constant, but it can be improved to some extent by using conditions and loops.

However, in the final code, a question arises regarding eliminating the delay in the loop. Because if there is no delay, the cooling does not work, and despite this delay, the cooling always turns on and off. I will be happy if someone can solve this problem and leave the answer here so that other friends can also use it.

Modified part :

    delay(50);
   if (Temp_IN  < (TempSettings - 0.2) ){
     //delay(500);
     digitalWrite(PinRelay1, HIGH);
     Serial.println("Heater on");
     digitalWrite(PinRelay2, LOW);
   } 
   if (Temp_IN  > (TempSettings   0.9) ){
    do {       
         digitalWrite(PinRelay1, LOW);
         //This delay should be removed, but I don't know how to solve 
         the problem after removal
         delay(800); 
         digitalWrite(PinRelay2, HIGH); 
         Serial.println("fan on");

       } while (Temp_IN  == (TempSettings ) );
    digitalWrite(PinRelay2, HIGH);
    digitalWrite(PinRelay1, HIGH);
    Serial.println("BOTH OFF");
  } 
  • Related