How to wire a soil moisture sensor to an Arduino Uno
To wire a soil moisture sensor to an Arduino Uno, 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
Soil moisture sensor to Arduino Uno 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 pin | Arduino Uno pin | Wire color | Why |
|---|---|---|---|
| VCC | 5V | red | The module runs from 3.3 to 5 V; at 5 V the output stays inside the analog pin’s range. |
| GND | GND | blue | Common ground. |
| AOUT | pin A0 | green | The analog output: lower in wet soil, higher in dry air. It goes to an analog input. |
Power and logic levels for soil moisture sensor and Arduino Uno
Logic level
The Arduino Uno’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 Uno’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. The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.
Pins to leave alone
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.
The working code for the Arduino Uno
Library: none, the sketch reads AOUT with analogRead(). This is the sketch the circuit above runs.
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);
}
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% WetCommon mistakes wiring a soil moisture sensor to an 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.
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.