Digital ChallengeWorkshop · Arduino × Processing
L05Workshop lab

Soil Moisture

Ask the pot if it is thirsty.

Read a soil probe on A0, remember that wet usually means a **lower** number, calibrate three cups, and build a live moisture bar — the last workshop system.

2 × 45 min difficultyBiologyPhysicsComputer Science
Soil Moisture
Analog sensorsCalibrationInverted map()Alerts
01Before you start

What you’ll learn

By the end of this lab you can…
  • 1Wire a 3-pin soil probe (VCC, GND, AOUT→A0) and read it.
  • 2Calibrate dry / damp / wet for the probe on your desk — do not trust a datasheet.
  • 3Invert map() so a falling analog value still grows a “wetter” bar.
  • 4Design an alert that would later drive a pump or a buzzer.
Key vocabulary
Capacitive probe
Measures soil as a dielectric. The electronics stay out of the water, so it lasts longer.
Resistive probe
Two exposed legs. Current flows through damp soil. Cheap, rusts, the handbook figure looks like this.
Calibration
Recording real readings in known states (dry cup, damp cup, wet cup) so 0–1023 becomes a meaning.
Hysteresis
Two thresholds — on and off — so a pump does not chatter at one magic number.
02Kit list

Required equipment

ComponentQtyPurpose
Arduino Uno1ADC
Soil moisture probe1Capacitive v1.2 or resistive fork
Three cups of soil / kitchen towel3Dry, damp, wet calibration
USB + both IDEs1Display
03Theory

Wet is usually a smaller number

Most classroom probes output a higher voltage in dry media and a lower voltage when wet. analogRead of ~800–950 is “leave it”, ~500 is “fine”, ~300 is “soaked”. Your board will differ. That is why this lab starts with three cups, not with a formula.

Figure vs text
The handbook text says “capacitive v1.2”. The photo is a two-prong resistive fork. Both plug in the same way (VCC, GND, AOUT→A0). Capacitive probes survive wet soil longer. Resistive probes are fine for a two-hour workshop if you dry them after.
Soil probe on 5 V, GND and A0 — workshop figure.
Soil probe on 5 V, GND and A0 — workshop figure.
InteractiveMoisture benchSlide from dry to soaked. The bar grows even though the raw ADC value is falling.

DRY = 880, WET = 340. The raw number falls as the soil gets wetter — the bar still grows.

Raw ADC
610
Calibrated
50%
Moisture OK
04Firmware

Arduino — send the raw reading

Probe pinArduino
VCC5V (or 3.3 V if the module insists)
GNDGND
AOUTA0
Soil_Serial.inoArduino · C++
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int moisture = analogRead(sensorPin); // often 1023 dry → ~300 wet
  Serial.println(moisture);
  delay(300);
}
Tip
Do not leave a resistive probe sitting in a cup overnight — the legs corrode and the reading crawls. Rinse and dry after the lesson.
05Visualise

Processing — invert the bar

map(moisture, 1023, 300, 0, 360) reads backwards on purpose: a falling ADC still grows a “wetter” bar. Replace 1023 and 300 with your dry and wet averages.

MoistureBar.pdeProcessing · Java
import processing.serial.*;

Serial myPort;
int moisture = 0;
int DRY = 900;  // replace after calibration
int WET = 320;

void setup() {
  size(500, 240);
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\\n');
}

void draw() {
  background(18);
  float pct = constrain(map(moisture, DRY, WET, 0, 100), 0, 100);
  fill(230);
  textSize(20);
  text("Soil moisture  " + moisture + "   " + int(pct) + " %", 32, 48);

  fill(pct < 25 ? color(255, 92, 77) : color(124, 255, 178));
  rect(32, 90, map(pct, 0, 100, 0, 420), 40, 4);
  if (pct < 25) {
    fill(255, 92, 77);
    text("DRY — water the plant", 32, 170);
  }
}

void serialEvent(Serial p) {
  String val = trim(p.readStringUntil('\\n'));
  if (val.matches("\\\\d+")) moisture = int(val);
}
06From the workshop

Student tasks

Basic
Read dry air, then dip the probe. Write both numbers in your notebook.
You know which way “wet” moves.
Basic
Draw an inverted bar that grows when you water.
The UI matches intuition.
Intermediate
Calibrate three cups and put the numbers in `DRY` / `WET`.
0 % and 100 % match the cups, not the datasheet.
Advanced
Log a CSV and graph it after you “water” once.
A spike down (wetter) then a slow climb as it dries.
Challenge
Design hysteresis for a future pump: two thresholds, not one.
A short written spec + a sketch of the state machine.
07When it doesn’t work

Troubleshooting

ProblemLikely causeFix
Reading never changes.You are holding the PCB, not the legs, or AOUT is not on A0.Only the exposed probe goes in the media. Check the three wires.
Bar is backwards.map used 300→1023 instead of 1023→300.Invert the from-range, or map to 100−pct.
Numbers wander in the same cup.Uneven moisture, air pockets, corroded legs.Stir, wait 10 s, average 10 samples.
Golden rule
Check the physical connections first, then the code. If nothing works, unplug, close both programs, reconnect and try again.
08Student edition

Exercises

Basic · 1Intermediate · 2Advanced · 1Challenge · 1
L05-E1BasicPredict 5 min

Which way does the number go?

You dip a typical classroom probe from dry potting mix into a wet cup. Does analogRead rise or fall? Why did the Processing bar still get longer in the starter sketch?

The raw reading falls. map(moisture, DRY, WET, 0, 100) has DRY > WET, so a smaller input maps to a larger output. The bar follows wetness, not voltage.
L05-E2IntermediateCalculate 8 min

Percent wet

Your calibration: DRY = 880, WET = 340. A reading is 610. What % wet is constrain(map(610, 880, 340, 0, 100), 0, 100)?

(880−610) / (880−340) × 100.
270 / 540 × 100 = 50 %.
L05-E3IntermediateBuild 15 min

Three-cup lab sheet

Fill in a table: medium (dry soil, damp soil, wet soil, cup of water, open air) × 10 readings × average. Then choose DRY and WET for your Processing sketch. Photograph the table or type it into the handbook.

  1. 01Wipe the probe between cups.
  2. 02Wait 8 seconds after dipping before you record.
  3. 03Average 10 samples — do not pick the prettiest one.
  4. 04Open air is not the same as dry soil. Use dry soil as DRY if the plant will live in soil.
There is no universal answer. A good sheet shows a clear gap between the three soil averages and a note of which two numbers you put in the sketch.
L05-E4AdvancedCode 10 min

Smooth the jitter

Average the last 8 analog readings on Arduino before printing. Keep the serial contract as one integer per line.

A small array + an index, or a running sum.
average 8Arduino · C++
const int N = 8;
int buf[N];
int i = 0;

void loop() {
  buf[i] = analogRead(A0);
  i = (i + 1) % N;
  long sum = 0;
  for (int k = 0; k < N; k++) sum += buf[k];
  Serial.println(sum / N);
  delay(50);
}
L05-E5ChallengeDesign 15 min

Don’t chatter the pump

A single threshold if (reading > 700) pumpOn(); else pumpOff(); will flip the relay every few hundred milliseconds as the reading hovers. Design hysteresis: pick two numbers and write the if logic. Explain why plants (and relays) prefer this.

Example: turn on only if reading > 750 (drier than dry), turn off only if reading < 550 (wet enough). In between, keep the last state. Relays live longer; roots are not pulsed with 5 Hz floods. This is the same idea as a thermostat.
09Check yourself

Self-check quiz

Progress
0/5 answered · 0 correct
  1. Q01

    On most classroom probes, wet soil makes analogRead…

  2. Q02

    You should calibrate because…

  3. Q03

    map(v, 900, 300, 0, 100) when v = 900 returns…

  4. Q04

    A resistive probe left in water for a week…

  5. Q05

    Hysteresis uses…

10Beyond the classroom

Real world & extensions

Where this is used
Houseplant reminderGreenhouse rowSchool gardenSmart irrigation (next project)
Extension ideas
  • +Add a buzzer on D8 when pct < 20.
  • +Two probes (A0, A1) for two pots — a competitive “who forgot to water?” dashboard.
  • +Save a 10-minute CSV and fit a drying curve.
Cross-curricular connections
SubjectConnection
BiologyWilting point vs field capacity — plants care about water in the root zone, not a raw ADC.
PhysicsDielectric constant of water vs air (capacitive); ionic conduction (resistive).
CSCalibration constants belong in named variables, not magic numbers.
11Think about it

Reflection & conclusion

?1

Which cup surprised you — was “damp” closer to dry or to wet?

?2

If you built a pump next week, would you switch on raw ADC or on calibrated %? Why?

?3

A farmer waters at dawn. How would you log 24 h of moisture with the same serial line?

Conclusion

You closed the workshop the way you opened it: a sensor, a one-line protocol, a picture. The only new skill was calibration — deciding what the numbers mean in the real world.