In this article, we’ll walk you through building an AI-Based Water Plant System using Arduino. This project will automate the watering of your plants based on their moisture levels, leveraging sensors, Arduino, and a bit of AI. This guide covers all the necessary components, the step-by-step assembly process, and the code required to get your system up and running.

Disclaimer:

Advertisements

Those who love plants and have plants on the balcony or rooftop, always face difficulties watering the plants when we are on a tour or staying a few days outside frequently. Sometimes our lovely plants die due to lack of water.

In this era of technology, we can do something for this task. Normally we can make automatic water planting systems using any microcontroller such as Arduino or even simple analog circuits with moisture sensors. But we want to use AI (Artificial Intelligence) with this. So first let’s see what we need to do.

So the first stage of Building an AI-Based Water Plant System is to make a system which can gather data over a long time and log into memory.

What you need to implement AI:

  1. Arduino Uno – The brain of our system.
  2. Soil Moisture Sensor – To measure the moisture level of the soil.
  3. Water Pump – To water the plants.
  4. Relay Module – To control the water pump.
  5. SD card module
  6. DHT11 sensor
  7. Jumper Wires – For connections.
  8. Breadboard – For prototyping.
  9. Power Supply – For the Arduino and water pump.
  10. AI Platform (TensorFlow, Edge Impulse, etc.) – To create and deploy the AI model.

These are the common materials you may need. If you already have some, it’s okay to use them. Below is a list with links to get them directly if required.

Anyway, we need to set up our hardware.

Circuit diagram:

You can follow this simple circuit diagram in the beginning, which is a basic representation of the setup.

Building an AI-Based Water Plant System

Then first you need to make the automatic plant watering system. Then you can do the next stages.

Arduino Code:

#define SOIL_MOISTURE_PIN A0
#define RELAY_PIN 2

int moistureLevel = 0;
int threshold = 500;  // Adjust this threshold based on your soil and sensor

void setup() {
  Serial.begin(9600);
  pinMode(SOIL_MOISTURE_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH);  // Ensure the relay is off initially
}

void loop() {
  moistureLevel = analogRead(SOIL_MOISTURE_PIN);
  Serial.print("Soil Moisture Level: ");
  Serial.println(moistureLevel);
  
  if (moistureLevel < threshold) {
    digitalWrite(RELAY_PIN, LOW);  // Turn on the water pump
    Serial.println("Watering the plant...");
  } else {
    digitalWrite(RELAY_PIN, HIGH);  // Turn off the water pump
    Serial.println("Soil moisture is adequate.");
  }
  
  delay(2000);  // Wait for 2 seconds before next reading
}

You can use this basic code for basic operation. It will automate the watering process. However, for AI integration, we need a substantial amount of data under various conditions such as different weather, times of day, and seasons. Gathering this data takes considerable time. If you can find relevant existing data, you can use it for the AI component. If not, you’ll need to collect data over an extended period.

So to log the data we need an SD card adapter for our Arduino to store the data in an SD card. And a sensor that can give us temperature and humidity to understand the environment such as DHT11.

Modified diagram:

Building an AI-Based Water Plant System

Modified code:

#include <DHT.h>
#include <SPI.h>
#include <SD.h>

#define SOIL_MOISTURE_PIN A0
#define RELAY_PIN 2
#define DHT_PIN 3
#define DHT_TYPE DHT11
#define SD_CS_PIN 10  // Adjust this pin based on your setup

int moistureLevel = 0;
int threshold = 500;  // Adjust this threshold based on your soil and sensor
DHT dht(DHT_PIN, DHT_TYPE);
File logFile;

void setup() {
  Serial.begin(9600);
  pinMode(SOIL_MOISTURE_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH);  // Ensure the relay is off initially

  dht.begin();

  // Initialize the SD card
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("SD card initialization failed!");
    return;
  }
  Serial.println("SD card initialized.");

  // Create or open the log file
  logFile = SD.open("datalog.txt", FILE_WRITE);
  if (!logFile) {
    Serial.println("Failed to open datalog.txt");
    return;
  }
  // Write the header to the log file
  logFile.println("Temperature,Humidity,Moisture,Pump Status");
  logFile.close();
}

void loop() {
  // Read soil moisture level
  moistureLevel = analogRead(SOIL_MOISTURE_PIN);
  Serial.print("Soil Moisture Level: ");
  Serial.println(moistureLevel);

  // Read temperature and humidity
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Check if readings are valid
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Determine pump status
  String pumpStatus;
  if (moistureLevel < threshold) {
    digitalWrite(RELAY_PIN, HIGH);  // Turn on the water pump
    pumpStatus = "ON";
    Serial.println("Watering the plant...");
  } else {
    digitalWrite(RELAY_PIN, LOW);  // Turn off the water pump
    pumpStatus = "OFF";
    Serial.println("Soil moisture is adequate.");
  }

  // Log data to SD card
  logFile = SD.open("datalog.txt", FILE_WRITE);
  if (logFile) {
    logFile.print(temperature);
    logFile.print(",");
    logFile.print(humidity);
    logFile.print(",");
    logFile.print(moistureLevel);
    logFile.print(",");
    logFile.println(pumpStatus);
    logFile.close();
    Serial.println("Data logged to SD card.");
  } else {
    Serial.println("Failed to open datalog.txt for writing.");
  }

  delay(20000);  // Wait for 20 seconds before next reading
}

Now run this system for many days to get all the weather conditions and let the data log in the SD card.

Read more: Arduino based egg incubator.

Integrating AI for Smarter Watering:

To make the system smarter, we can use AI to predict the optimal watering schedule. This involves training a model on data collected from the soil moisture sensor over time.

  1. Collect Data:
    • Run the above code and log the soil moisture readings along with timestamps.
    • Collect data under different conditions (e.g., different weather, and soil types).
  2. Train an AI Model:
    • Use a platform like TensorFlow or Edge Impulse to train a regression model.
    • The model should predict the soil moisture level based on past data and environmental conditions (temperature, humidity, etc.).
  3. Deploy the Model:
    • Export the trained model to a format compatible with Arduino (e.g., TensorFlow Lite).
    • Integrate the model into your Arduino code. This will require using a microcontroller with sufficient computational power to run the model, like an Arduino Nano 33 BLE Sense.

Updating the Arduino Code with AI:

Here’s a simplified example of how to integrate the AI model (assuming you have already trained and converted your model to TensorFlow Lite).

#include <TensorFlowLite.h>
#include "model.h"  // Your trained model

#define SOIL_MOISTURE_PIN A0
#define RELAY_PIN 2

int moistureLevel = 0;
int predictedMoisture = 0;

void setup() {
  Serial.begin(9600);
  pinMode(SOIL_MOISTURE_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH);  // Ensure the relay is off initially
  // Initialize the TensorFlow Lite model here
}

void loop() {
  moistureLevel = analogRead(SOIL_MOISTURE_PIN);
  predictedMoisture = predictMoistureLevel(moistureLevel);
  
  Serial.print("Predicted Soil Moisture Level: ");
  Serial.println(predictedMoisture);
  
  if (predictedMoisture < threshold) {
    digitalWrite(RELAY_PIN, LOW);  // Turn on the water pump
    Serial.println("Watering the plant...");
  } else {
    digitalWrite(RELAY_PIN, HIGH);  // Turn off the water pump
    Serial.println("Soil moisture is adequate.");
  }
  
  delay(2000);  // Wait for 2 seconds before next reading
}

int predictMoistureLevel(int currentMoisture) {
  // Implement the inference logic using TensorFlow Lite
  // This is a placeholder for actual AI model inference
  return currentMoisture + 10;  // Dummy prediction logic
}

Conclusion:

By following this guide, you have created a basic AI-based water plant system using Arduino. This project not only automates plant watering but also demonstrates how AI can enhance simple IoT applications. Continue experimenting with more sensors and advanced models to further improve your system. Happy gardening!

Practically I’m making this hardware and setting it with one of my soil pots. Then will run this device for a long time then will gather the data and run the AI model to get the result. So stay connected to get the final feedback.

For Professional Designs or Help:

Loading

Categories: ArduinoArtificial Intelligence

MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.