Board and part

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

This is a soil moisture sensor wired to an Arduino Nano 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 Nano · 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 Nano

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 Nano 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. Ten bits against 5 V, 0 to 1023, the same as an Uno. A6 and A7 are extra analog inputs that cannot be digital pins at all.

Pins to leave alone on the Arduino Nano

A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.

On a real bench

The Nano plugs into the breadboard itself, straddling the center channel, so the Soil Moisture Sensor goes in a few columns along and every connection is a short jumper. The silkscreen labels its pins D2 to D13 and A0 to A7; in code D2 is simply 2. A Nano clone with a CH340 USB chip needs that driver on a real computer.

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 Nano 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 Nano

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 Nano?

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.