Soil Moisture Sensor with Arduino: Wiring, Calibration, Code
Share
Soil Moisture Sensor with Arduino: Wiring, Calibration, Code
Quick answer: Wire the sensor's AOUT to A0, GND to GND, and VCC to a digital pin rather than the 5V rail — both probes draw 5mA, well inside the Uno's 20mA-per-pin limit, so a GPIO can power them only while you take a reading. Then calibrate two points: dry air and water. Sensors from Rs.35.
Written and fact-checked by Compoden's engineering team, India. Every price, pin and current figure below is read from our live catalogue record for the exact part named. The sketch was written for this guide and has not been compiled by us — it uses no external library, so you can read every line of it. Published 18 August 2026 · Last updated 18 August 2026.
A soil moisture sensor is one of the few components where the wiring is trivial and everything else is not. Three wires, one analog pin, one line of code to read it — and then a reading that means nothing until you calibrate it, that moves the wrong way on the sensor most people buy, and that comes from a probe which will corrode away if you leave it powered. This guide covers all three problems in the order you will hit them.
| Spec | Capacitive V2.0 | Resistive (LM393 / YL-69 / FC-28) |
|---|---|---|
| Price (India, COD) | Rs.55 | Rs.35 |
| Pins | VCC, GND, AOUT | VCC, GND, AOUT, DOUT |
| Supply voltage (V) | 3.3 – 5.5 | 3.3 – 5 |
| Current draw (mA) | 5 | 5 |
| Library needed | None — analogRead()
|
None — analogRead() / digitalRead()
|
| Reading interval | 200 ms minimum | No stated minimum |
| Calibration required | Yes, per unit | Yes, per unit |
Verdict in one line: for anything staying in soil, buy the capacitive probe — and wire either of them the same way.
How do you wire a soil moisture sensor to an Arduino Uno?
Three wires, with one deliberate change from every tutorial you have read: the sensor's VCC goes to a digital pin, not the 5V rail. Both probes draw 5mA according to our records, and the Uno's recommended per-pin limit is about 20mA, so a digital pin can supply the sensor comfortably — which lets your code switch the probe on only while it takes a reading. On a resistive probe that single change is the difference between weeks and months of life, because corrosion needs current and time.
| Sensor pin | Arduino Uno pin | Why |
|---|---|---|
| VCC | D7 | Any plain digital pin. D7 has no PWM, bus or LED duty — nothing else wants it. |
| GND | GND | Common ground. |
| AOUT | A0 | The 10-bit analog input that reads the moisture level. |
| DOUT (resistive only) | Leave unconnected | The comparator output only gives you a yes/no at a trimpot threshold. Use AOUT instead. |
What code reads a soil moisture sensor?
No library at all — both sensors are plain analog parts, and our records list no driver for either. The sketch below reads the probe, applies your two calibration points, and powers the sensor down between readings. The two calibration constants are placeholders: they are not our measurements and they will not be right for your probe. Replace them using the next section.
// Soil moisture with two-point calibration and GPIO-gated power.
// Board: Arduino Uno R3 (5V). No library required - analogRead() only.
// Wiring: AOUT -> A0, GND -> GND, VCC -> D7.
const int SENSOR_PIN = A0;
const int POWER_PIN = 7;
// PLACEHOLDERS - replace with YOUR two calibration readings.
const int RAW_DRY = 620; // raw reading in dry air
const int RAW_WET = 310; // raw reading in water or soaked soil
void setup() {
Serial.begin(9600);
pinMode(POWER_PIN, OUTPUT);
digitalWrite(POWER_PIN, LOW); // sensor off until we read it
}
int readRaw() {
digitalWrite(POWER_PIN, HIGH);
delay(500); // let the probe settle
int raw = analogRead(SENSOR_PIN);
digitalWrite(POWER_PIN, LOW);
return raw;
}
void loop() {
int raw = readRaw();
int pct = constrain(map(raw, RAW_DRY, RAW_WET, 0, 100), 0, 100);
Serial.print(raw);
Serial.print(" raw -> ");
Serial.print(pct);
Serial.println("% moisture");
delay(2000);
}
How do you calibrate a soil moisture sensor?
Two readings, both taken with your actual probe on your actual board. Upload the sketch, open the Serial Monitor at 9600 baud, and note the raw number with the probe held in dry air — that is your RAW_DRY. Then put the probe in a glass of water up to its marked line (capacitive) or into thoroughly soaked soil (resistive) and note the raw number again — that is your RAW_WET. Put both into the sketch and re-upload. This is not optional and it is not transferable: neither module has an official manufacturer datasheet, and our records state the output span varies per unit and with supply voltage. A threshold copied from a tutorial was calibrated on someone else's probe at someone else's rail voltage.
Why does the reading go down when the soil gets wetter?
On the capacitive probe, that is correct behaviour, not a fault. Its output voltage decreases as moisture rises — our record puts the span at roughly 3.0V in dry air falling to about 1.2-1.4V fully wet on a 3.3V supply, and notes the span is different again at 5V, which is what an Uno gives it. That is why the sketch above maps RAW_DRY to 0 and RAW_WET to 100 rather than the other way round: map() handles a descending input range perfectly well, so calibrating in that order makes the inversion disappear from the rest of your code. If your watering logic runs backwards, check this before you suspect the sensor.
Why do the readings get erratic after a few weeks?
Because the probe is corroding, if it is the resistive one. Our record for the resistive module lists "erratic readings after a few weeks" and "probe discoloration or rust" as its expected failure symptoms in continuous use — the fork passes DC through wet soil to measure it, and DC through an electrolyte strips metal. That is the measurement principle running its course, not a bad unit. The GPIO-gated power in the sketch above is the cheap fix; buying the capacitive probe is the real one, though note that our record for the capacitive part carries its own slow failure mode: "PCB coating may degrade over time if constantly wet". Seal the header end with hot glue or heat-shrink before burying it.
What else moves the reading besides water?
Temperature, salt and nearby objects. Both records list output drift with temperature; the resistive one adds salt buildup from fertiliser, and the capacitive one adds sensitivity to stray capacitance from anything close to the probe, including a metal pot rim or your hand. Neither sensor reports an absolute moisture percentage — both records describe their range as relative with no absolute units, which is why the sketch prints a calibrated percentage rather than pretending to a scientific measurement. Write your logic as "drier than my calibrated dry point" rather than "below 30% soil moisture".
Which sensor should you buy?
For anything that stays in soil — a plant monitor, an automatic watering system — the Capacitive Soil Moisture Sensor V2.0 (Rs.55). For a demo, a classroom set, or when you specifically want the trimpot digital threshold, the Soil Moisture Sensor Module (LM393) (Rs.35). The board this guide is wired for is the Arduino Uno R3 CH340G (Rs.230). All available cash on delivery across India.
What should you read next?
The full comparison of the two probes, including why the cheaper one is a false economy, is in our capacitive vs resistive guide. To turn this into a finished build with a display, follow the soil moisture monitor with OLED, and check the Arduino Uno pinout reference before you claim D7 for something else.
Want the wiring and code generated for your exact parts? Open Soldr, describe your build in one sentence, and it will produce the pin plan and the sketch.
Frequently asked questions
Soil moisture sensor ko Arduino se kaise connect karein?
Teen taar: AOUT ko A0 par, GND ko GND par, aur VCC ko 5V rail ke bajaye kisi digital pin par (jaise D7). Dono sensors sirf 5mA lete hain aur Uno ka per-pin limit ~20mA hai, isliye digital pin se power dena safe hai - aur iska fayda yeh hai ki sensor sirf reading ke waqt on rehta hai, jisse resistive probe bahut kam corrode hota hai. Iske baad do point calibration zaroori hai: sookhi hawa aur paani.
Do I need a library for a soil moisture sensor?
No. Both the capacitive and the resistive modules are plain analog parts — our catalogue records list no driver library for either. You read them with analogRead() on the analog pin, and the resistive module's optional digital output with digitalRead(). Everything else is your own calibration arithmetic.
Can I power a soil moisture sensor from an Arduino digital pin?
Yes, and you should. Both modules draw 5mA according to our catalogue records, well inside the Arduino Uno's recommended limit of about 20mA per pin. Driving VCC from a digital pin lets your sketch power the probe only while it takes a reading, which dramatically slows the electrolysis that destroys resistive probes. Keep in mind the Uno's whole-board budget is about 200mA across all pins together, so this trick scales to several sensors but not to motors or LED strips.
What raw value means dry soil?
Whatever your own probe reads in dry air — there is no universal number. Neither module has an official manufacturer datasheet, and our records state the output span varies between units and with supply voltage. Take a raw reading in dry air and another in water or soaked soil, put those two numbers into your sketch, and map between them. Any specific threshold you find in a tutorial was calibrated on a different probe and rail voltage.
What is the price of a soil moisture sensor in India?
At Compoden, the Capacitive Soil Moisture Sensor V2.0 is Rs.55 and the resistive Soil Moisture Sensor Module (LM393, also sold as YL-69 or FC-28) is Rs.35. The Arduino Uno R3 CH340G board this guide wires them to is Rs.230. All available with cash on delivery across India. Prices checked August 2026.