Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP8266 NodeMCU Controlled Smart Irrigation and Security System

View and Edit Project In Cirkit DesignerImage of ESP8266 NodeMCU Controlled Smart Irrigation and Security System

Circuit Documentation

Summary

This circuit is designed to interface various sensors and actuators with an ESP8266 NodeMCU microcontroller. The system is capable of reading environmental data, detecting motion, and controlling a relay which in turn can control a water pump. The ESP8266 NodeMCU is also connected to an LCD display for real-time feedback of sensor data and system status. The circuit is powered by a 7V battery set.

Component List

1-Channel Relay (5V 10A)

Set battery (7V)

PIR Sensor

Mini Water Pump

DHT11 Humidity and Temperature Sensor

LCD Display 16x4 I2C

Humidity YL-69

Pushbutton

ESP8266 NodeMCU

Wiring Details

1-Channel Relay (5V 10A)

Set battery (7V)

PIR Sensor

Mini Water Pump

DHT11 Humidity and Temperature Sensor

LCD Display 16x4 I2C

Humidity YL-69

Pushbutton

ESP8266 NodeMCU

Documented Code

ESP8266 NodeMCU (Main Controller)

// Pin Definitions
#define RELAY_PIN_1       D3   // Relay
#define PUSH_BUTTON_1     D7   // Button
#define SOIL_SENSOR_PIN   A0   // Soil Moisture Sensor
#define PIR_SENSOR_PIN    D5   // PIR Motion Sensor
#define DHT_SENSOR_PIN    D4   // DHT11 Temperature Sensor

// Include the library files
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// Initialize the LCD display
LiquidCrystal_I2C lcd(0x3F, 16, 2);

// Blynk credentials and WiFi credentials
char auth[] = "YourAuthToken";  // Enter your Blynk Auth token
char ssid[] = "YourSSID";       // Enter your WIFI SSID
char pass[] = "YourPassword";   // Enter your WIFI Password

// Initialize DHT sensor
DHT dht(DHT_SENSOR_PIN, DHT11);

// Blynk timer for scheduling
BlynkTimer timer;

// Variables to store sensor data
int relay1State = LOW;
int pushButton1State = HIGH;
int PIR_ToggleValue;

// Function prototypes
void checkPhysicalButton();
void DHT11sensor();
void soilMoistureSensor();
void PIRsensor();

void setup() {
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  pinMode(PIR_SENSOR_PIN, INPUT);
  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, LOW);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  dht.begin();
  lcd.setCursor(0, 0);
  lcd.print("  Initializing  ");
  // Initialization sequence
  for (int a = 5; a <= 10; a++) {
    lcd.setCursor(a, 1);
    lcd.print(".");
    delay(500);
  }
  lcd.clear();
  lcd.setCursor(11, 1);
  lcd.print("W:OFF");
  // Schedule functions
  timer.setInterval(100L, soilMoistureSensor);
  timer.setInterval(100L, DHT11sensor);
  timer.setInterval(500L, checkPhysicalButton);
}

void loop() {
  if (PIR_ToggleValue == 1) {
    lcd.setCursor(5, 1);
    lcd.print("M:ON ");
    PIRsensor();
  } else {
    lcd.setCursor(5, 1);
    lcd.print("M:OFF");
    WidgetLED LED(V5);
    LED.off();
  }

  if (relay1State == HIGH) {
    lcd.setCursor(11, 1);
    lcd.print("W:ON ");
  } else {
    lcd.setCursor(11, 1);
    lcd.print("W:OFF");
  }

  Blynk.run();  // Run the Blynk library
  timer.run();  // Run the Blynk timer
}

// Function to read and display DHT11 sensor values
void DHT11sensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.print(t);
  lcd.setCursor(8, 0);
  lcd.print("H:");
  lcd.print(h);
}

// Function to read and display soil moisture values
void soilMoistureSensor() {
  int value = analogRead(SOIL_SENSOR_PIN);
  value = map(value, 0, 1024, 0, 100);
  value = (value - 100) * -1;
  Blynk.virtualWrite(V3, value);
  lcd.setCursor(0, 1);
  lcd.print("S:");
  lcd.print(value);
  lcd.print(" ");
}

// Function to read PIR sensor values and trigger events
void PIRsensor() {
  bool value = digitalRead(PIR_SENSOR_PIN);
  if (value) {
    Blynk.logEvent("pirmotion", "WARNING! Motion Detected!");
    WidgetLED LED(V5);
    LED.on();
  } else {
    WidgetLED LED(V5);
    LED.off();
  }
}

// Blynk function to handle virtual pin writes
BLYNK_WRITE(V6) {
  PIR_ToggleValue = param.asInt();
}

// Sync virtual pin state with