Blog

Measuring Soil Moisture with Arduino

11/6/2023 3 min read
Measuring Soil Moisture with Arduino

If you take care of plants in your garden or home, you certainly know how important the right amount of water is for their growth. One method to ensure your plants always have optimal moisture is using an Arduino-based soil moisture sensor. In this blog post, you will learn everything important about the different sensors, how they work, and why calibration is essential. By the way, other microcontrollers can also be used — the code is the same; only the pin designation may need to be adjusted accordingly.


Difference Between Capacitive and Resistive Soil Moisture Sensors

First of all, there are two main types of soil moisture sensors: capacitive and resistive sensors. Both have their advantages and disadvantages.

Resistive Sensors

Resistive sensors measure soil moisture by detecting the electrical resistance of the soil. Water conducts electricity better than dry soil, so a lower resistance indicates higher moisture. These sensors are generally cheaper, but they have some drawbacks:

  • Corrosion of metal probes over time
  • Inaccurate readings in salty or fertilized soil
  • Shorter lifespan

Capacitive Sensors

Capacitive sensors measure soil moisture by detecting changes in the capacitance of a capacitor influenced by the dielectric properties of the surrounding soil. Here are some advantages:

  • No direct electrical connection to the soil
  • Significantly more durable
  • More stable and reproducible measurements
  • Less prone to corrosion

Why is Calibration Necessary?

Regardless of which type of sensor you use, calibration is essential. Every soil is different and contains varying amounts of minerals, organic matter, and water. By calibrating the sensor, you ensure the measurements are accurate and take into account the specific characteristics of your soil. Proper calibration helps you make more precise decisions about watering your plants.


How the Sensor Works

The sensor is inserted into the soil and measures soil moisture at regular intervals. It generates an analog signal transmitted as a voltage to the Arduino. This signal varies depending on the soil's moisture content.

  • Dry soil:
    • High voltage reading for resistive sensors
    • Low voltage reading for capacitive sensors

The Arduino can capture these voltage changes and process them accordingly.


Using Other Microcontrollers

Besides Arduino, you can also use other microcontrollers like the ESP32 to control soil moisture sensors. The ESP32 offers additional advantages such as integrated Wi-Fi and Bluetooth, enabling wireless monitoring and processing of sensor data. The basic operational principle remains the same; only the pin assignments and potentially the libraries need to be adapted.


Why Analog Signals?

Soil moisture sensors output analog signals because they measure continuous changes in moisture. Analog signals can precisely represent these gradual transitions, whereas digital signals can only distinguish between two states.

This allows the microcontroller to very accurately determine how moist the soil is and respond accordingly, for example:

  • Turning on a pump
  • Issuing a warning
  • Adjusting watering intervals

Step-by-Step Implementation Guide

int sensorPin = A0;   // Pin where the sensor is connected
int sensorValue = 0; // Variable to store the sensor value

void setup() {
    Serial.begin(9600); // Start serial communication
}

void loop() {
    sensorValue = analogRead(sensorPin); // Read sensor value
    Serial.print("Soil moisture: ");
    Serial.println(sensorValue); // Output the value
    delay(1000); // Wait 1 second
}

Automatically Calibrating Soil Moisture Sensor and Integrating with Home Assistant

The above script forms the basis to read data from your soil moisture sensor. If you want to automatically calibrate soil moisture values or even record them in Home Assistant, the next step is to extend this code with calibration logic and an appropriate interface for data transfer.

With this guide, you should be able to efficiently monitor your plants' soil moisture and ensure optimal growing conditions. Good luck!