Try Embedded

Plant watering alarm (soil moisture)

Measure soil moisture as a percentage and switch a pump relay on when the plant is dry.

Sensors · Beginner

In a real product: Smart plant pot (Agriculture): Waters the plant when the soil gets dry.

A soil moisture probe is two metal prongs: wet soil conducts better than dry soil. Its board turns that into a voltage, and the sketch decides when the plant needs water.

What you'll learn

  • Turning a raw ADC reading into a percentage with two calibration points
  • Switching a pump with hysteresis, so it does not turn on and off around one value
  • Powering a probe only when reading it

How it works

The analog output is high in dry soil and falls as the soil gets wetter. The sketch maps the reading between "dry" (1023) and "in water" (about 256 here) onto 0–100 %. On real hardware you measure those two points for your probe.

The pump starts below 30 % and stops above 60 %. The gap between the two (hysteresis) means one watering takes the soil well past the threshold instead of switching the pump every few seconds. The probe is powered from pin 7 only for the moment of each reading, which slows the corrosion of its prongs (the probe draws a few milliamps, which a pin can give).

The pump relay is on a relay module set to high-level trigger, so pin 8 HIGH turns the pump on. A relay coil needs far more current than a pin gives, so the module powers it from its own VCC and switches it with a transistor.

Try this

  • Click the probe while it runs and slide the moisture down: the relay switches on below 30 %.
  • Move the DO threshold slider and read DO on a pin instead of AO: what is lost?
  • Water for at most 10 seconds at a time, so a failed probe cannot flood the pot.

sketch.ino

// Soil moisture probe (AO on A0, powered from pin 7) and a pump relay module on pin 8 (high-level trigger).
const int PROBE = A0, PROBE_POWER = 7, PUMP = 8;
const int DRY = 1023, WET = 256;   // calibration: readings in air and in water

bool pumping = false;

int moisturePercent() {
  digitalWrite(PROBE_POWER, HIGH);
  delay(10);
  int raw = analogRead(PROBE);
  digitalWrite(PROBE_POWER, LOW);
  return constrain(map(raw, DRY, WET, 0, 100), 0, 100);
}

void setup() {
  Serial.begin(9600);
  pinMode(PROBE_POWER, OUTPUT);
  pinMode(PUMP, OUTPUT);
}

void loop() {
  int m = moisturePercent();
  if (!pumping && m < 30) pumping = true;
  if (pumping && m > 60) pumping = false;
  digitalWrite(PUMP, pumping);
  Serial.print("Moisture ");
  Serial.print(m);
  Serial.println(pumping ? " %  pump ON" : " %");
  delay(500);
}

Lessons behind it