Board and part

Arduino Uno Soil Moisture Sensor Simulator: Wiring, Code and a Live Circuit

This is a soil moisture sensor wired to an Arduino Uno and read live in your browser. The sketch turns the analog output into a percentage and prints Dry, Moist or Wet.

  • 5 V logic
  • No library: analogRead()
  • AOUT on pin A0
  • Powered from 5V
Arduino Uno · Soil Moisture Sensorlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the Soil Moisture Sensor to the Arduino Uno

Three wires: power from 5V, ground, and AOUT to pin A0. In the editor, drag the sensor’s slider from dry to wet.

Every connection in the circuit above, with the board’s own pin names.

Soil Moisture Sensor pinArduino Uno pinWhy
VCC5VThe module runs from 3.3 to 5 V; at 5 V the output stays inside the analog pin’s range.
GNDGNDCommon ground.
AOUTpin A0The analog output: lower in wet soil, higher in dry air. It goes to an analog input.

Power

The module takes its power from 5V. Its output stays within 0 to 5 V, the converter’s range. The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.

Pins to leave alone on the Arduino Uno

Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.

On a real bench

On a real bench the Uno stands beside the breadboard: the Soil Moisture Sensor goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

Soil moisture on Arduino Uno pin A0
const int SOIL_PIN = A0;   // AOUT, pin A0
const int DRY_RAW = 522;   // the reading in air
const int WET_RAW = 260;   // the reading in water

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

void loop() {
  int raw = analogRead(SOIL_PIN);
  int percent = map(raw, DRY_RAW, WET_RAW, 0, 100);
  percent = constrain(percent, 0, 100);
  Serial.print("Moisture: ");
  Serial.print(percent);
  Serial.print("%  ");
  if (percent < 35) {
    Serial.println("Dry");
  } else if (percent < 65) {
    Serial.println("Moist");
  } else {
    Serial.println("Wet");
  }
  delay(250);
}

analogRead() converts the output voltage to a number. The module gives a lower voltage the wetter the soil is, so DRY_RAW is the larger reading, taken in air, and WET_RAW the smaller one, taken in water.

map() turns that span into 0 to 100 percent and constrain() keeps stray readings inside it. Dry, Moist and Wet are just thresholds on the percentage, at 35 and 65.

The two constants depend on the module and on the supply, so calibrate them: read the sensor in air and in a glass of water, and put those two numbers in.

Pitfalls

Common mistakes with the Soil Moisture Sensor on the Arduino Uno

Using the digital output for a level

The DO pin on the module is only a comparator with a trimmer: it says wet or dry and nothing in between. Use AOUT on an analog pin for a real reading.

Leaving the probe powered all the time

A resistive probe corrodes when current flows through it for months. Power it from a pin and switch it on only while reading, or use a capacitive sensor, which has no exposed metal.

Skipping calibration

Every module and every supply gives different readings in air and in water. Measure both and set DRY_RAW and WET_RAW to them, or the percentage is off.

FAQ

Questions and answers

How do I connect a soil moisture sensor to an Arduino Uno?

Power the module from 5V, join its GND to GND, and wire AOUT to pin A0, an analog input. Read it with analogRead().

Why does the reading fall when the soil gets wetter?

Water makes the soil conduct, which pulls the module’s output voltage down. Dry air gives the highest reading, so a percentage needs map() with the dry value first.

Resistive or capacitive: which should I buy?

Capacitive. A resistive probe is cheap but its bare metal corrodes in the soil; a capacitive one is sealed and lasts. Both wire the same way.

How often should I read it?

Soil changes over minutes, so once every few seconds is plenty, and for a watering project once a minute is enough.

Build your own Soil Moisture Sensor project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.