Arduino Uno Aquarium heater/cooler

Hy!
Time ago I start maked a project for my aquarium. I want to create system with automatic cooling and heating.
The component setup is ok, technicaly everything is working, but I want a little different result.
I Use : Arduino UNO, ds18b20 temp sensor, LCD and 2 relay block. The problem is when the heater starts then the heating relay works to the cooling temperature.
I want get that result Heating 23-25C temperature- heating relay off, starts under 23C degrees, stops 25C degrees, but when the air temperature reach 26C degrees then turn on cooling (second) relay for cooler.

I Add a code from project. Maybe some one can help? I’ll think that the problem is in the code.
Thanks!

used.ino (1.58 KB)

Please read the "how to use this forum-please read" stickies to see how to post code and some hints on what we need to know in order to help you. See #7 & 11. You will get more help if people don't have to download your code to help.

Thanks for answer. I understand my mistake. Here is a code:

#include <LiquidCrystal_I2C.h>

/*
Temperature Controller

Made by Dainis
*/

#include <Wire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define RELAY1  4  // Relay heating
#define RELAY2  6  // Relay cooling

int red = 8; // red LED heating
int blue = 2; // blue LED cooling

#define BACKLIGHT_PIN 13
#define ONE_WIRE_BUS 7
LiquidCrystal_I2C lcd(0x3F,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {

  Serial.begin(9600);
  sensors.begin();
  lcd.begin(20,4);
  lcd.init();                      // initialize the lcd 
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();


pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);

    }

void loop() {
lcd.setCursor(0, 0);
  lcd.print("    Temperature");
  
  sensors.requestTemperatures();
  float temperature = sensors.getTempCByIndex(0);
  lcd.setCursor(0, 2);
  lcd.print("      ");
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("\337C");
  
    if(temperature < 25)
    {
digitalWrite(red,HIGH);
digitalWrite(blue,LOW);
 
      lcd.setCursor(0, 3);
      lcd.print("       Heating");
 
digitalWrite(RELAY1,0); 
digitalWrite(RELAY2,1);   
           }
    else if(temperature > 25)
    {
digitalWrite(RELAY1,1);
digitalWrite(RELAY2,0); 
digitalWrite(red,LOW);
digitalWrite(blue,HIGH);

 lcd.setCursor(0, 3);
      lcd.print("       Cooling");
      lcd.setCursor(0, 3);
      
}                 
}

is this what you are attempting?

if(temperature < 23) {
    digitalWrite(RELAY1,0);
    digitalWrite(RELAY2,1);   
 }
 if(temperature == 25) {
    digitalWrite(RELAY1,1);
    digitalWrite(RELAY2,1);  
 }
 if(temperature >= 26)
   {
   digitalWrite(RELAY1,1);
   digitalWrite(RELAY2,0);
}
float temperature = sensors.getTempCByIndex(0);

if(temperature == 25) {

I think that you need to be careful using equality with float data types.

Maybe?

if(temperature > 25.0 && temperature < 26.0) {

You need to include the OneWire library.

Thanks for answers. After work i"ll make some tests, then tell about the results! Thanks!

Thanks for helping! After few hours in internet I found answers and now I writing working code for my controller. Big thanks for all, who answer to my problem! :slight_smile:

Ill post the finished code:

#include <LiquidCrystal_I2C.h>

/*
  Temperature Controller

  Made by Dainis
*/

#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define Rel 4  // Relay heating
#define Rel 6  // Relay cooling

int red = 8; // red LED heating
int blue = 2; // blue LED cooling

#define BACKLIGHT_PIN 13
#define ONE_WIRE_BUS 7
LiquidCrystal_I2C lcd(0x3F, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {

  Serial.begin(9600);
  sensors.begin();
  lcd.begin(20, 4);
  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();


  pinMode(4, OUTPUT);
  pinMode(6, OUTPUT);

}

void loop() {
  lcd.setCursor(0, 0);
  lcd.print("    Temperatura");

  sensors.requestTemperatures();
  float temperature = sensors.getTempCByIndex(0);
  lcd.setCursor(0, 2);
  lcd.print("      ");
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("\337C");

  if(temperature > 27)  {
    digitalWrite(4, HIGH);
    pinMode(4, OUTPUT);
    digitalWrite(6, LOW);
    pinMode(4, OUTPUT);
    digitalWrite(red,HIGH);
    digitalWrite(blue,LOW);
    lcd.setCursor(0, 3);
      lcd.print("       Dzese");
        
  }
  else if(temperature > 25 && temperature > 23) {
    digitalWrite(4, HIGH);
    pinMode(4, OUTPUT);
    digitalWrite(6, HIGH);
    pinMode(6, OUTPUT);
    digitalWrite(red,LOW);
    digitalWrite(blue,HIGH);
    lcd.setCursor(0, 3);
       lcd.print("        Off");
  }
  else if(temperature <=23) {
    digitalWrite(4, LOW);
    pinMode(4, OUTPUT);
    digitalWrite(6, HIGH);
    pinMode(4, OUTPUT);
    digitalWrite(red,LOW);
    digitalWrite(blue,HIGH);
    lcd.setCursor(0, 3);
       lcd.print("       Silda");
  }
  }

in the statement

  else if(temperature > 25 && temperature > 23) {

l if temperature is greater than 25 it must be greater than 23 ??

When temperature is 23-25 degrees heater and cooler is off When temperature is under 23 degrees heater is on. Heater works from 23 degrees to 25 degrees When temperature is higer than 27 degrees starts cooler, when drops down cooler off.

When temperature is under 23 degrees heater is on. Heater works from 23 degrees to 25 degrees

In other words, when the temperature is greater than 23 and the temperature is less than 25, turn the heater on.

Pay attention to two things in that statement. The first is the order of tests required. The second is the operator to use in each each part of the test.

Yeah, but I upload this code in arduino, add a wires, relays, display and temperature sensor and it works.
Maybe there is some mistake in wiring? But i dont think so.
I test schematic. Heater starts at 23 degrees, stops on 25 degrees, when the temperature drops down again to 23 heater starts again.

If I use this code:

if(temperature < 23)
    {
digitalWrite(red,HIGH);
digitalWrite(blue,LOW);
 
      lcd.setCursor(0, 3);
      lcd.print("       Silda");
 
digitalWrite(RELAY1,0); 
digitalWrite(RELAY2,1);   
           }
    else if(temperature > 27)
    {
digitalWrite(RELAY1,1);
digitalWrite(RELAY2,0); 
digitalWrite(red,LOW);
digitalWrite(blue,HIGH);

 lcd.setCursor(0, 3);
      lcd.print("       Dzese");
      lcd.setCursor(0, 3);
      
}                 
}

Then heater starts at 23 degrees and stops only in 27 degrees, when cooler starts. Every time the heater or cooler is on. Thats the problem. In other words The RELAY1 or RELAY2 is every time on, but I dont need that. In need when the RELAY1 or RELAY2 starts only at the temperature,
which one I set in code.

vortexxx12:
When temperature is 23-25 degrees heater and cooler is off
When temperature is under 23 degrees heater is on. Heater works from 23 degrees to 25 degrees
When temperature is higer than 27 degrees starts cooler, when drops down cooler off.

is this what you require

  else if(temperature < 25 && temperature > 23) {

if the temperature is less than 25 and greater than 23 …

Please re-read your last post. I think that it should be obvious that RELAY1 and RELAY2 are terrible names. HeaterRelayPin and CoolerRelayPin would tell us EXACTLY what is being switched. 0 and 1 are useless values. #define ON x and #define OFF x, and turning a relay pin ON would tell us EXACTLY what you expect to happen (with appropriate values for x in each case).

Look at your code. It looks like it was typed by a drunken monkey. Use Tools + Auto Format, and get that monkey into treatment.

Finally, that snippet of code only deals with temperatures less than 23 or greater than 27. It makes NO changes to the state of the heater and cooler relay pins when the temperature is 23, 24, 25, 26, or 27 degrees. That is probably not what you really want.

is this what you require

Except that most people don’t think that way. You are weird. Most people think in terms of “if the temperature is above 23 and less than 25, do…”.

   else if(temperature > 23 && temperature < 25)

In post 7 Ill posted my new code (without terrible names), wich works.