We’ve learned how to measure the temperature and humidity of the air around us. But what if we want to monitor the health of our indoor garden? Whether you are notorious for under-watering your ferns or over-watering your succulents, a Soil Moisture Sensor is exactly what your Arduino needs to keep your plants alive.
Soil moisture sensors act like two giant probes that pass an electrical current through the dirt to measure resistance.
A standard resistive soil moisture sensor consists of two exposed conductive prongs. When you stick these prongs into the dirt, the sensor acts like a variable resistor (similar to the potentiometer we used in Lesson 116).
Water is a good conductor of electricity. Dry soil is a very poor conductor.
The sensor module reads this resistance and outputs an analog voltage that the Arduino can measure.
A typical soil moisture sensor kit comes in two parts: the two-pronged probe, and a small amplifier board with a potentiometer on it.
VCC, GND, DO (Digital Out), and AO (Analog Out).VCC to the Arduino’s 5V pin.GND to the Arduino’s GND pin.AO to the Arduino’s Analog Pin A0.Note: The DO pin acts as a simple threshold switch. You can tune the little blue potentiometer on the board so DO goes HIGH when the soil hits a specific dryness, but using the AO pin gives us much more precise data.
Because we are reading an analog voltage, the code is incredibly simple. We use analogRead(), which will give us a value between 0 and 1023.
int sensorPin = A0;
int sensorValue = 0;
void setup() {
Serial.begin(9600); // Start the serial monitor
}
void loop() {
// Read the analog value from the sensor
sensorValue = analogRead(sensorPin);
// Print the raw value to the Serial Monitor
Serial.print("Soil Moisture Value: ");
Serial.println(sensorValue);
delay(1000); // Wait 1 second before reading again
}
Upload the code and open your Serial Monitor.
By testing your specific soil, you can determine the perfect threshold for your plant.
If you leave a resistive soil moisture sensor powered on in wet soil 24/7, the electrical current passing through the metal prongs will cause them to rust and corrode rapidly (a process called electrolysis).
Pro Tip: To make your sensor last years instead of weeks, connect the sensor’s VCC pin to a Digital Pin on the Arduino instead of the 5V pin. In your code, set that pin HIGH, wait 10 milliseconds, take your analogRead(), and immediately set it LOW again. This way, electricity only flows through the soil for a fraction of a second when taking a measurement!