Measure soil moisture with Arduino

Components Required

  1. Arduino Uno (or any other compatible board)
  2. Soil moisture sensor (capacitive is recommended over resistive for durability)
  3. I2C LCD module (16×2)
  4. Breadboard and jumper wires
  5. Power source (USB or 9V battery with adapter)
  6. Different soil samples (e.g., clay, loam, sandy soil)

Wiring Connections

Soil Moisture Sensor:

  • VCC → 5V on Arduino
  • GND → GND on Arduino
  • A0 → A0 on Arduino (Analog input)

I2C LCD Module:

  • VCC → 5V on Arduino
  • GND → GND on Arduino
  • SDA → A4 on Arduino
  • SCL → A5 on Arduino

For Arduino Mega: SDA = Pin 20, SCL = Pin 21
Ensure your I2C LCD uses the 0x27 or 0x3F I2C address (you can scan with I2C scanner sketch if needed)

Arduino Code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Use the correct I2C address: 0x27 or 0x3F
LiquidCrystal_I2C lcd(0x27, 16, 2);  

const int soilPin = A0;

void setup() {
  lcd.init();            // Correct for libraries using lcd.init()
  lcd.backlight();       
  lcd.setCursor(0, 0);
  lcd.print("Soil Moisture");
  delay(2000);
}

void loop() {
  int moistureValue = analogRead(soilPin);

  // Adjust these values based on your sensor calibration
  int dryValue = 1023;   // Value when sensor is dry
  int wetValue = 300;    // Value when sensor is in water

  int moisturePercent = map(moistureValue, dryValue, wetValue, 0, 100);
  moisturePercent = constrain(moisturePercent, 0, 100);  // Ensure within bounds

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Moisture:");
  lcd.setCursor(0, 1);
  lcd.print(moisturePercent);
  lcd.print(" %");

  delay(2000);
}

Top 3 Enhancements for Better Accuracy

  1. Use a Capacitive Soil Moisture Sensor
  • More accurate and less prone to corrosion than resistive sensors.
  1. Calibrate Your Sensor Properly
  • Measure values in dry soil, moist soil, and wet soil to improve reading accuracy.
  1. Take Multiple Readings and Average Them
  • Smooth out noise by averaging several sensor readings before displaying.