HELP! I do not know how to connect the relay module to my arduino board and How can you automatically turn it on and off?

#include <DHT.h>
#include <LiquidCrystal_I2C.h>

#define DHTTYPE DHT11
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);

#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);

#define HUMIDIFIER_PIN 4 // Pin connected to the humidifier
#define RELAY_ON LOW // Define relay logic levels based on your relay module
#define RELAY_OFF HIGH
unsigned long humidifierStartTime = 0;
bool humidifierActive = false;

void setup() {
  dht.begin();
  lcd.init();
  lcd.backlight();
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(humidity);
  lcd.print("%");

  if (humidity > 80 || humidity < 20) {
    // Turn on the humidifier
    digitalWrite(HUMIDIFIER_PIN, RELAY_ON);
    humidifierStartTime = millis();
    humidifierActive = true;
  }

  if (humidifierActive && millis() - humidifierStartTime >= 15000) {
    // Turn off the humidifier after 15 seconds
    digitalWrite(HUMIDIFIER_PIN, RELAY_OFF);
    humidifierActive = false;
  }

  delay(2000); // Delay for 2 seconds before the next reading
}

and this is a picture on how I connected everything.
image

Maybe add some pinmode() calls in setup ?

If I add a pin mode where would that be connected on the mist module?

read about the pinmode() command / function.
You’ll use it a lot in the future.

Sorry to say the Arduino is a dumb machine and cannot think on its own. It only does what it is told. You can tell it the sky is violet and it would not care. The same thing with hardware or software. The core of the processor knows nothing about what is connected to it. It will start at a specific location in program memory, get that instruction and follow those instructions it understands from that point forward. You give it instructions with the code you write. Therefor you need to tell it about the pins etc connected to it. Communication/control of the hardware is via internal registers. To tell it there is an output you do this using the pinMode command. Follow this link, it will explain it better than I can: pinMode() - Arduino Reference

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