Humidity and temperature control system with buttons

Hello everyone, I am a beginner and I recently started a project for a friend.
And I have some problems if someone can give me some advice or help me solve this problem I would be grateful.
Let me explain a little about this project. I want to make a temperature and humidity control system.
What I use for this project are the following:

  1. Arduino uno
  2. Unit and Temperature riser (dht11)
  3. 5V 4-Channel Relay Module
  4. I2C 16x2 LCD (just 4 wires)
  5. 3 Push Button
  6. 3 10k Resistance

How do I want this system to be controlled ?
First of all I want to be able to set the temperature and humidity from the buttons.

example:

set temperature to 30 C
and humidity at 40%
when the temperature drops below 30 C
I want relay 1 to start
When the temperature exceeds 30 C I want relay 2 to start and stop relay 1
When the humidity drops below 40% I want the relay 3 to start
And when the humidity exceeds 40 I want the relay 4 to start and stop relay 3
When one relay starts the other one stops. 3 relays never working at once.
I managed to make the temperature work.
but I can't get it to work at humidity.

What I managed to do is :

If the temperature is below 30 C relay 1 works.
If the temperature exceeds 30 C. relay 1 stops and relay 2 starts.
The temperature part I managed to make it work.
On the humidity side I managed to make it work
When the humidity is over 40%, relay 4 starts.
But when the humidity drops above 40, relay 3 does not start.
Relay 4 works properly only relay 3 does not start.
I've been trying to solve this problem for a whole day now and I haven't been able to do it.

If anyone can help me and explain to me why the code does not work and how it should be written to work, I would be very grateful.

Thank you all in advance.

I leave links to the code and diagram :
code - File on MEGA
diagrama - File on MEGA

I think providing the code and circuit diagram in the post itself would improve your chances of getting response.

Below is an incubator

R1 - Fan
R2 - Heating Element
R3 - Humidifier
R4 - Dehumidifier

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 2

const int ok = A3;
const int UP = A1;
const int DOWN = A2;

const int relay1 = 8;             // relay1 (FUN)
const int relay2 = 9;             // relay2 (HEATING ELEMENT)
const int relay3 = 10;            // relay3 (HUMIDIFIER)
const int relay4 = 11;            // relay4 (DEHUMIDIFIER)


int ack = 0;
int pos = 0;
int sec = 0;
int Min = 0;
int hrs = 0;
int T_threshold = 30;
int H_threshold = 60;

int SET = 0;
boolean T_condition = true;
boolean H_condition = true;

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);

void setup() {

  // Buttons
  pinMode(ok, INPUT);
  pinMode(UP, INPUT);
  pinMode(DOWN, INPUT);

  // Relay
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay3, OUTPUT);

  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay4, LOW);
  digitalWrite(relay3, LOW);

  digitalWrite(ok, HIGH);
  digitalWrite(UP, HIGH);
  digitalWrite(DOWN, HIGH);

  lcd.init();
  lcd.backlight();

  Serial.begin(9600);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temperature &");
  lcd.setCursor(0, 1);
  lcd.print("Humidity ");

  delay (3000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Controller For");
  lcd.setCursor(0, 1);
  lcd.print("Incubator");

  delay (3000);

  lcd.clear();
  Serial.println("Temperature and Humidity Controller");
}

void loop() {

  if (SET == 0) {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set Temperature:");
    lcd.setCursor(0, 1);
    lcd.print(T_threshold);
    lcd.print(" *C");

    while (T_condition) {

      if (digitalRead(UP) == LOW) {
        T_threshold = T_threshold + 1;
        lcd.setCursor(0, 1);
        lcd.print(T_threshold);
        lcd.print(" *C");
        delay(200);

      }
      if (digitalRead(DOWN) == LOW) {
        T_threshold = T_threshold - 1;

        lcd.setCursor(0, 1);
        lcd.print(T_threshold);
        lcd.print(" *C");

        delay(200);

      }

      if (digitalRead(ok) == LOW) {
        delay(200);
        T_condition = false;
      }
    }

    lcd.backlight();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set Humidity:");
    lcd.setCursor(0, 1);
    lcd.print(H_threshold);
    lcd.print("%");

    delay(100);

    while (H_condition) {
      if (digitalRead(UP) == LOW) {
        H_threshold = H_threshold + 1;

        lcd.setCursor(0, 1);
        lcd.print(H_threshold);
        lcd.print("%");

        delay(100);

      }

      if (digitalRead(DOWN) == LOW) {

        H_threshold = H_threshold - 1;

        lcd.setCursor(0, 1);
        lcd.print(H_threshold);
        lcd.print("%");

        delay(200);
      }

      if (digitalRead(ok) == LOW) {
        delay(100);
        H_condition = false;
      }
    }

    SET = 1;
  }
  ack = 0;
  int chk;
  chk = DHT.read(DHT11_PIN);    // READ DATA
  switch (chk)
  {

    case DHTLIB_OK:

      //Serial.print("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:

      //Serial.print("Checksum error,\t");
      ack = 0;
      break;
    case DHTLIB_ERROR_TIMEOUT:

      //Serial.print("Time out error,\t");
      ack = 0;
      break;
    default:
      //Serial.print("Unknown error,\t");
      break;
  }

  // DISPLAT DATA

  Serial.print("DHT11, \t");
  Serial.print(DHT.temperature, 1);
  Serial.print(",\t");
  Serial.println(DHT.humidity, 1);

  delay(100);

  if (ack == 0)
  {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    Temp:");
    lcd.print(DHT.temperature);
    lcd.setCursor(0, 1);
    lcd.print("    Humi:");
    lcd.print(DHT.humidity);

    delay(500);
    if (DHT.temperature > T_threshold) {
      if (DHT.temperature > T_threshold) {
        digitalWrite(relay3, HIGH);
      }
    }

    delay(500);
    if (DHT.temperature < T_threshold) {
      if (DHT.temperature < T_threshold) {
        digitalWrite(relay3, LOW);
      }
    }

    if (DHT.humidity > H_threshold) {
      delay(500);
      if (DHT.humidity > H_threshold) {
        digitalWrite(relay4, HIGH);
      }
    }
    if (DHT.humidity < H_threshold) {
      delay(500);
      if (DHT.humidity < H_threshold) {
        digitalWrite(relay4, LOW);
      }

      if (DHT.temperature < T_threshold) {
        delay(500);
        if (DHT.temperature < T_threshold) {
          digitalWrite(relay1
                       , LOW);
        }
      }

      if (DHT.temperature > T_threshold) {
        delay(500);
        if (DHT.temperature > T_threshold) {
          digitalWrite(relay1, HIGH);
        }
      }

      if (DHT.humidity < H_threshold) {
        delay(500);
        if (DHT.humidity < H_threshold) {
          digitalWrite(relay2, HIGH);
        }
      }

      if (DHT.humidity > H_threshold) {
        delay(500);
        if (DHT.humidity > H_threshold) {
          digitalWrite(relay2, LOW);
        }

        delay(25);

        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("No Sensor data.");
        lcd.setCursor(0, 1);
        lcd.print("System Halted.");
        digitalWrite(relay1, LOW);
        digitalWrite(relay2, LOW);
        digitalWrite(relay4, LOW);
        digitalWrite(relay3, LOW);
      }
      delay(500);
    }
  }
}

Yes 100% Thank you !

You want the heating element (relay 2) to start when temperature > 30 °C and stop the fan (relay 1) ?
Something seems logically wrong here. You probably want to start the fan (relay 1) when temperature > 30 °C and stop heating element (relay 2) or I am not understanding something correctly ?

Hello error403
Provide some desribing comments to the sketch to understand what happens or not.
Have a nice day and enjoy coding in C++.

Hello, I want heating element (relay 2) to start when temperature is lower then 30 °C . And stop fan (relay 1). Why, because if the fan wont stop when the temperature is lower then 30 the heating element will not be able to heat, because if the cold air blows it will cool it continuously.
The Temperature part is working corectly. My problem is with humidity part of the code.
I set humidity to 60 % HUMIDIFIER (relay3) start if the humidity is lower then 60% and stop if the humidity is bigger then 60 % until here it's all ok. But when the humidity is bigger then 60 % the
relay4 (DEHUMIDIFIER) not start. I want the same thing's with relay 1 and 2. With this code my relay 4 not working. So the problem is just with relay 4.

Hi,

 if (DHT.temperature > T_threshold) {
      if (DHT.temperature > T_threshold) {
        digitalWrite(relay3, HIGH);
      }
    }

Why is EVERY comparison, if statement, repeated and nested?

You only have to check once.

Tom... :grinning: :+1: :coffee: :australia:

Hello and thank you. If I remove this part :

if (DHT.humidity > H_threshold) {         
        delay(500);
        if (DHT.humidity > H_threshold) {
          digitalWrite(relay2, LOW);            
        }

I got this error on LCD "No Sensor data. System Halted". Nothing works anymore.

Hi,
A table will help;

Condition RL1 FAN RLY2 HEAT RLY3 HUMIDIFIER RLY4 DEHUM
T > 30 ON OFF
T < 30 OFF ON
H > 60 OFF ON
H < 60 ON OFF

Have you developed your code in stages, or all at once.
If in stages;
You should have some code that reads the humidity and uses one or two if statements to decide if it is HIGH or LOW.
Then you should have some code that uses the HIGH or LOW to switch the relevant relays ON/OFF.

FORGET about the display, use the IDE monitor and get your hardware and logic working.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Thanks Tom. The code is taken from the internet and modified according to my needs.
I specify that I am a beginner and this is my first project.

Hi,
Can I suggest you start from scratch.
Look at the Examples given in the IDE for the DHT11, it is a very good start.

Don't try anything fancy with the display, use Serial.print and the IDE monitor.

Also you will need some hysteresis in your setpoints to stop erratic switch over.

Like this;

Condition RL1 FAN RLY2 HEAT RLY3 HUMIDIFIER RLY4 DEHUM
T > 32 ON OFF
T < 28 OFF ON
H > 62 OFF ON
H < 58 ON OFF

Or closer like +/- 1.

Tom.... :grinning: :+1: :coffee: :australia:

1 Like

Thank you and I appreciate your advice and your time. I will try to write it from the beginning. Thanks again and wish you a special day.

When you do, don't try to write it all at once. Find the smallest possible piece and implement that and test. Once you're happy with it, add a tiny bit more and test again. There will be far less pain and head scratching that way.

Thank you for you advice. This is the steps that I will do.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.