Soil Moisture Sensor Arduino — Wiring, Code & Simulation

A Soil Moisture Sensor Arduino setup lets an Arduino Uno read how wet the soil is around a plant and switch a pump or relay automatically. In this tutorial we cover wiring, calibration and the full soil moisture sensor Arduino code so you can build a reliable auto-watering system that doesn't over-water your plants.
Welcome to this beginner-friendly Arduino tutorial on the soil moisture. By the end of the guide, you'll wire the module to an Arduino UNO, flash a short sketch, and read live values on the Serial Monitor — no prior electronics experience required.
Need this built for you? I do custom Arduino, ESP32 and Proteus projects from $15.
What you'll learn
- How the module works in plain language
- The exact parts you need and how to wire them safely
- The full Arduino IDE sketch with comments
- Common issues and how to fix them


Arduino code
Open the Arduino IDE, paste the sketch below into a new file, install any libraries the sketch #includes (Tools → Manage Libraries), select your board and COM port, then click Upload.
// Sensor pins pin D6 LED output, pin A0 analog Input
#define ledPin 6
#define sensorPin A0
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
Serial.print("Analog output: ");
Serial.println(readSensor());
delay(500);
}
// This function returns the analog data to calling function
int readSensor() {
int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
int outputValue = map(sensorValue, 0, 1023, 255, 0); // map the 10-bit data to 8-bit data
analogWrite(ledPin, outputValue); // generate PWM signal
return outputValue; // Return analog moisture value
}




How it works
The sketch initialises serial communication and the soil moisture driver in setup(), then in loop() it samples the sensor at a regular interval and prints the result to the Serial Monitor at 9600 baud. Open the Serial Monitor (Ctrl+Shift+M) after upload to see live readings.
Troubleshooting checklist
- No readings: verify the baud rate in Serial Monitor matches the sketch (usually 9600).
- Garbage characters: wrong baud rate or loose GND wire.
- Library not found: install the exact library referenced in the
#includeline via Library Manager. - Sensor not detected (I²C): run an I²C scanner sketch to confirm the address.
What to build next
Once the basic readout works, try logging values to an SD card, sending them over Wi-Fi with an ESP32, or pushing them to a Blynk IoT dashboard. Pair this module with our simulator round-up to prototype the circuit before soldering.
Stuck on your own build? Send me your Arduino / ESP32 / Proteus project on Fiverr — from $15, you don't pay if it doesn't work.
You may also like
Frequently Asked Questions
Q.How to interface a soil moisture sensor with Arduino?
Connect the sensor's VCC to Arduino 5V, GND to GND, and the analog AO output to A0. Use analogRead(A0) to get a value between 0 (very wet) and 1023 (dry air).
Q.What is the ideal soil moisture reading for plants?
For most houseplants a reading between 300 and 700 on a 10-bit Arduino ADC is comfortable. Below ~700 the soil is drying out, so that's a good threshold to trigger a pump.
Q.Capacitive vs resistive soil moisture sensor — which is better?
Capacitive sensors last much longer because they don't corrode in wet soil like resistive probes do. For any project running longer than a few weeks, use the capacitive version.
Q.Can I power a water pump directly from Arduino?
No — even small 5V pumps draw more current than an Arduino pin can supply. Drive the pump through a transistor, MOSFET or relay module, and give the pump its own power supply.
The Engineer Post
Embedded systems engineer and educator. Writes weekly tutorials at EmbedLab to help beginners ship real hardware.
Related projects

Water Level Sensor with Arduino
Step-by-step Arduino tutorial: wire the water level to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Rain Detection Sensor with Arduino
Step-by-step Arduino tutorial: wire the rain detection to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Pulse Sensor with Arduino
Step-by-step Arduino tutorial: wire the pulse to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

MQ2 Combustible Gas Sensor with Arduino
Step-by-step Arduino tutorial: wire the mq2 combustible gas to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.
 — Arduino UNO tutorial cover image](/uploads/arduino-covers/interfacing-the-sr04t-sr04m-waterproof-ultrasonic-sensor-with-arduino.webp)
Interfacing SR04T/SR04M Waterproof UltraSonic Sensor with Arduino Uno
Step-by-step Arduino tutorial: wire the the sr04t, sr04m waterproof ultrasonic to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.



