Measuring soil moisture using resistive or capacitive [sensors](https://www.ampheo.com/c/sensors) is a common task in agriculture, gardening, and IoT projects. Below is a detailed guide on how to use both types of sensors with an [Arduino](https://www.ampheo.com/c/development-board-arduino) (or similar [microcontroller](https://www.ampheo.com/c/microcontrollers)).

**1. Resistive Soil Moisture Sensors**
**How They Work**
* Measure the electrical resistance between two probes.
* Higher moisture = lower resistance (conducts more current).
* Disadvantages: Prone to corrosion over time due to electrolysis.
**Wiring (Arduino Example)**

**Example Code**
```
cpp
const int sensorPin = A0; // Analog pin
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin); // Read analog value (0-1023)
Serial.print("Moisture Level: ");
Serial.println(sensorValue);
delay(1000);
}
```
**Interpreting Values**
* Dry soil: Higher value (e.g., 800-1023).
* Wet soil: Lower value (e.g., 300-500).
* Calibrate by testing in dry vs. wet conditions.
**2. Capacitive Soil Moisture Sensors**
**How They Work**
* Measure dielectric permittivity (changes with moisture).
* No corrosion (no direct metal contact with soil).
* More accurate and durable than resistive sensors.
**Wiring (Arduino Example)**

**Example Code**
```
cpp
const int sensorPin = A0; // Analog pin
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
Serial.print("Moisture Level: ");
Serial.println(sensorValue);
delay(1000);
}
```
**Interpreting Values**
1. Dry soil: Lower value (e.g., 0-300).
2. Wet soil: Higher value (e.g., 500-900).
3. Calibrate based on your soil type.
**3. Improving Accuracy & Reliability**
**Avoid Sensor Corrosion (Resistive Sensors)**
* Use galvanic isolation (pulse power instead of constant DC).
* Apply a protective coating (e.g., nail polish, conformal coating).
**Calibration & Mapping to Percentage**
```
cpp
int dryValue = 1023; // Value in dry soil
int wetValue = 300; // Value in wet soil
void loop() {
int sensorValue = analogRead(sensorPin);
int moisturePercent = map(sensorValue, dryValue, wetValue, 0, 100);
moisturePercent = constrain(moisturePercent, 0, 100); // Clamp to 0-100%
Serial.print("Moisture: ");
Serial.print(moisturePercent);
Serial.println("%");
delay(1000);
}
```
**Power Saving (For Battery-Powered Projects)**
Only power the sensor when reading (use a [transistor](https://www.onzuu.com/category/transistors)/[MOSFET](https://www.onzuu.com/category/fets-mosfets) to switch VCC).
**4. Recommended Sensors**

**5. Advanced Options**
* I2C/SPI Sensors (e.g., Adafruit STEMMA Soil Sensor) for better noise immunity.
* Wireless Monitoring (ESP8266/ESP32 + MQTT for IoT).
* Automated Irrigation (Trigger a relay/pump when moisture is low).
**Final Tips**
* Capacitive [sensors](https://www.ampheoelec.de/c/sensors) are better for long-term use (no corrosion).
* Calibrate your sensor in your specific soil type.
* Avoid constant power to resistive sensors to reduce corrosion.