ArduinoIoT HardwaresTutorials/DIY

Smart Irrigation System using Arduino and IoT

Project Overview

This Smart Irrigation System uses soil moisture sensors, a water pump, and IoT connectivity to automate watering based on real-time soil conditions. The system prevents overwatering and optimizes water usage.

1. Components Required

ComponentQuantityDescription
Arduino Uno/Nano1Microcontroller Board
Soil Moisture Sensor1+Detects soil moisture level
Relay Module (5V)1Controls the water pump
Water Pump1Pumps water to the plants
DHT22 Sensor1Measures temperature and humidity
ESP8266/ESP321IoT connectivity for remote monitoring
OLED Display (I2C)1Displays sensor readings
Jumper WiresAs neededFor connections
Breadboard1For prototyping

2. Pin Connections

ComponentPin NameArduino Pin
Soil Moisture SensorVCC5V
GNDGND
Analog OutA0
Relay ModuleVCC5V
GNDGND
SignalD3
DHT22VCC5V
GNDGND
DataD2
ESP8266/ESP32VCC3.3V
GNDGND
TXRX
RXTX
OLED DisplayVCC3.3V
GNDGND
SDAA4 (I2C SDA)
SCLA5 (I2C SCL)

3. Code Implementation

This Arduino code reads soil moisture, temperature, and humidity, controls the water pump, and sends data to a cloud server via ESP8266.

Required Libraries

  1. DHT.h
  2. Wire.h
  3. Adafruit_GFX.h
  4. Adafruit_SSD1306.h
  5. ESP8266WiFi.h
  6. ThingSpeak.h

Install these libraries via the Arduino Library Manager.

C
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

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

#define SOIL_PIN A0
#define RELAY_PIN D3

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
WiFiClient client;
unsigned long myChannelNumber = YOUR_THINGSPEAK_CHANNEL;
const char * myWriteAPIKey = "YOUR_API_KEY";

void setup() {
    Serial.begin(9600);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting...");
    }
    Serial.println("WiFi Connected!");
    ThingSpeak.begin(client);

    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
        Serial.println(F("SSD1306 allocation failed"));
        for (;;);
    }
    display.clearDisplay();
    dht.begin();
    pinMode(RELAY_PIN, OUTPUT);
}

void loop() {
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();
    int soilMoisture = analogRead(SOIL_PIN);
    
    Serial.print("Temp: "); Serial.print(temperature);
    Serial.print("C | Humidity: "); Serial.print(humidity);
    Serial.print("% | Soil: "); Serial.println(soilMoisture);

    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.print("Temp: "); display.print(temperature); display.print(" C");
    display.setCursor(0, 10);
    display.print("Humidity: "); display.print(humidity); display.print(" %");
    display.setCursor(0, 20);
    display.print("Soil Moisture: "); display.print(soilMoisture);
    display.display();

    if (soilMoisture < 400) {
        digitalWrite(RELAY_PIN, HIGH);
        Serial.println("Water Pump ON");
    } else {
        digitalWrite(RELAY_PIN, LOW);
        Serial.println("Water Pump OFF");
    }

    ThingSpeak.setField(1, temperature);
    ThingSpeak.setField(2, humidity);
    ThingSpeak.setField(3, soilMoisture);
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    delay(5000);
}

4. Working Explanation

  1. The Soil Moisture Sensor detects soil moisture level.
  2. The DHT22 Sensor records temperature & humidity.
  3. The ESP8266/ESP32 sends the data to the cloud (ThingSpeak).
  4. The OLED Display shows real-time data.
  5. The Relay Module turns the Water Pump ON when soil moisture is too low.
  6. The system updates every 5 seconds.

Projects for Beginners and Enthusiasts

Click here: https://iotbyhvm.ooo/10-exciting-arduino-projects-for-beginners-and-enthusiasts/

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *