#include <Wire.h> // Include the Wire library for I2C communication
#include <Adafruit_Sensor.h> // Include the Adafruit Sensor library for the DHT22 sensor
#include <DHT.h> // Include the DHT library for the DHT22 sensor
#include <LiquidCrystal_I2C.h> // Include the Liquid Crystal I2C library for the LCD display
#define DHTPIN 7 // Define the pin that the DHT22 sensor is connected to
#define DHTTYPE DHT22 // Define the type of the DHT22 sensor
#define RELAY_PIN 4 // Define the pin that the relay is connected to
#define BUTTON_UP_PIN 2 // Define the pin for the "UP" button
#define BUTTON_DOWN_PIN 3 // Define the pin for the "DOWN" button
#define MIN_SETPOINT 10.0 // Define the minimum allowed temperature setpoint in degrees Celsius
#define MAX_SETPOINT 50.0 // Define the maximum allowed temperature setpoint in degrees Celsius
#define HYSTERESIS 0.1 // Define the temperature hysteresis in degrees Celsius
#define HUMIDIFIER_PIN 13 // Define the pin that the humidifier is connected to
#define HUMIDITY_SETPOINT 50.0 // Define the desired humidity level
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object with the defined pin and type
LiquidCrystal_I2C lcd(0x27,16,2); // Initialize the LCD with the I2C address and dimensions
float temperature_setpoint = 37.0; // Set the initial temperature setpoint in degrees Celsius
void setup() {
// Initialize I2C communication
Wire.begin();
// Initialize the DHT22 sensor
dht.begin();
// Set the relay and humidifier pins as outputs
pinMode(RELAY_PIN, OUTPUT);
pinMode(HUMIDIFIER_PIN, OUTPUT);
// Set the button pins as inputs with pull-up resistors enabled
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
// Initialize the LCD display
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.setCursor(0,1);
lcd.print("Setpoint: ");
lcd.print(temperature_setpoint);
lcd.print(" C");
}
void loop() {
// Read the temperature and humidity from the DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// If the DHT22 sensor failed to read data, print an error message to the LCD display
if (isnan(temperature) || isnan(humidity)) {
lcd.setCursor(0,0);
lcd.print("Error: DHT22 fail");
}
// Otherwise, print the temperature and relay status to the LCD display
else {
lcd.setCursor(6,0);
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Relay: ");
if (digitalRead(RELAY_PIN) == HIGH) {
lcd.print("ON ");
}
else {
lcd.print("OFF");
}
}
// Control the relay based on the temperature reading and setpoint
if (temperature < (temperature_setpoint - HYSTERESIS)) {
digitalWrite(RELAY_PIN, HIGH); // Turn on the relay
}
else if (temperature > (temperature_setpoint + HYSTERESIS)) {
digitalWrite(RELAY_PIN, LOW); // Turn off the relay
}
// Check if the "UP" button is pressed and increase the setpoint if it is
if (digitalRead(BUTTON_UP_PIN) == LOW && temperature_setpoint < MAX_SETPOINT) {
temperature_setpoint += 0.1; // Increase the setpoint by 0.1 degrees Celsius
lcd.setCursor(11,1);
lcd.print(temperature_setpoint);
lcd.print(" C");
delay(100); // Add a small delay to debounce the button
}
// Check if the "DOWN" button is pressed and decrease the setpoint if it is
if (digitalRead(BUTTON_DOWN_PIN) == LOW && temperature_setpoint > MIN_SETPOINT) {
temperature_setpoint -= 0.1; // Decrease the setpoint by 0.1 degrees Celsius
lcd.setCursor(11,1);
lcd.print(temperature_setpoint);
lcd.print(" C");
delay(100); // Add a small delay to debounce the button
}
// Control the humidifier based on the humidity reading and setpoint
if (humidity < HUMIDITY_SETPOINT) {
digitalWrite(HUMIDIFIER_PIN, HIGH); // Turn on the humidifier
}
else {
digitalWrite(HUMIDIFIER_PIN, LOW); // Turn off the humidifier
}
delay(100); // Wait for 0.1 second before repeating the loop
}