Wiring guide

How to wire a soil moisture sensor to an Arduino Nano

To wire a soil moisture sensor to an Arduino Nano, connect VCC to 5V, GND to GND and AOUT to pin A0. The module runs from 5 V and its AOUT stays inside the converter’s 0 to 5 V range. The sketch below uses no library beyond what the board includes. It reads AOUT and prints a moisture percentage with the word Dry, Moist or Wet.

  • 5 V logic
  • Library: none
  • 3 wires
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.
Pin to pin

Soil moisture sensor to Arduino Nano wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

Soil moisture sensor pinArduino Nano pinWire colorWhy
VCC5VredThe module runs from 3.3 to 5 V; at 5 V the output stays inside the analog pin’s range.
GNDGNDblueCommon ground.
AOUTpin A0greenThe analog output: lower in wet soil, higher in dry air. It goes to an analog input.
Voltage

Power and logic levels for soil moisture sensor and Arduino Nano

Logic level

The Arduino Nano’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The soil moisture sensor takes 5 V from 5V. That is the Arduino Nano’s 5 V rail, so the part shares the current the board can supply.

This pair

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

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.

The sketch

The working code for the Arduino Nano

Library: none, the sketch reads AOUT with analogRead(). This is the sketch the circuit above runs.

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);
}
Output

What you should see

Open the serial monitor at 9600 baud. Each line shows the moisture as a percentage and a word: Dry under 35 percent, Moist under 65 and Wet above. Drag the sensor’s slider from dry to wet and the line follows.

The two calibration numbers in the sketch depend on the module, so read yours in air and in water and put them in.

The serial monitor prints this, recorded from the circuit above with the soil set to 90 percent wet after 300 ms:

Moisture: 30%  Dry
Moisture: 90%  Wet
Pitfalls

Common mistakes wiring a soil moisture sensor to an 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.

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.