Jobin J Jose
Published

Portable Arduino-Based Soil Moisture Detection System

Build a compact and reliable soil moisture detector using an Arduino and a moisture sensor—perfect for gardening, small-scale agriculture.

BeginnerShowcase (no instructions)4 hours83
Portable Arduino-Based Soil Moisture Detection System

Things used in this project

Story

Read more

Schematics

Circuit Diagram

Code

Soil Moisture code

C/C++
// Pin definitions
#define MOISTURE_PIN A1     // Analog pin for soil moisture sensor
#define LED_R 6             // Red channel of RGB LED
#define LED_G 5             // Green channel of RGB LED
#define LED_B 3             // Blue channel of RGB LED

void setup() {
  // Configure RGB pins as outputs
  pinMode(LED_R, OUTPUT);
  pinMode(LED_G, OUTPUT);
  pinMode(LED_B, OUTPUT);

  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  // Read raw analog value from soil sensor
  int rawValue = analogRead(MOISTURE_PIN);
  Serial.print("Raw Moisture Value: ");
  Serial.println(rawValue);

  // Convert raw sensor data to percentage (0 = dry, 100 = wet)
  int percentMoisture = map(rawValue, 1023, 0, 0, 100);

  // Control RGB LED colors based on soil condition
  if (percentMoisture < 35) {
    setRGB(255, 0, 0);   // Red → Soil is dry
  } 
  else if (percentMoisture >= 35 && percentMoisture <= 70) {
    setRGB(0, 255, 0);   // Green → Soil is healthy
  } 
  else {
    setRGB(0, 0, 255);   // Blue → Soil is too wet
  }

  delay(1000); // Delay for stability
}

// Function to update RGB LED using PWM values
void setRGB(int redVal, int greenVal, int blueVal) {
  analogWrite(LED_R, redVal);
  analogWrite(LED_G, greenVal);
  analogWrite(LED_B, blueVal);
}

Credits

Jobin J Jose
11 projects • 1 follower

Comments