Skip to content

Soil moisture sensor

Monitoring the moisture content of soil is a common requirement in horticultural and agricultural projects. There are several types of sensor that can be used; here we discuss a simple resistive sensor and provide links to other kinds.

Soil moisture sensor Figure 1: Soil moisture sensor

This type of sensor relies on the fact that moist soil has a lower resistance than dry soil. Because it relies on a change in resistance, it needs a voltage divider to translate the signal into a change in voltage that can be detected by the microprocessor. Some models of this type of sensor come with an external module that includes the appropriate components, and some come with the resistors embedded in the sensor itself. This is the case with the DFRobot sensor shown in Fig. 1. The resistors that make up the voltage dividers a labelled (1) and (2) in Fig 2.

Voltage divider Figure 2: Voltage divider

This means that you can connect the output from the sensor directly to an analogue input pin on the Particle Argon and get a reading.

Hardware

The hardware setup is shown in Fig. 3. Note that the power and ground connections correspond to the Fritzing image provided - i.e. ground is in the middle. Not all soil moisture sensors are set up in the same way. The one shown in Fig. 1, for example has the power connection in the middle. Make sure to which configuration your sensor requires.

Breadboard layout Figure 3: Breadboard layout

The layout in Fig. 3 assumes that the output from the sensor is a voltage event though the image shown does not have an on-board voltage divider as discussed above.

Software

The code below will work with the layout in Fig. 3. As well as the simple sensor connection, it illustrates the use of

  • millis() to control the sampling interval
  • the Particle Time class to generate a timestamp
  • the map() function to change the output range from 0-4095 to 0-100
  • string formatting using Serial.printf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "Particle.h"

const int soilSensor = A0;
const unsigned long sensorReadInterval = 3000;

int moisture = 0;
int percent = 0;
unsigned long lastInterval;
String dateTime;


void setup() {
    Serial.begin(9600);

}

void loop() {
    if((millis() - lastInterval) > sensorReadInterval) {
        dateTime = Time.timeStr();
        moisture = analogRead(soilSensor);
        percent = map(moisture, 0, 4095, 0, 100);
        Serial.print(dateTime);
        Serial.printf(": %lu\t%lu%%\n", moisture, percent);

        lastInterval = millis();
    }

}

Arduino tutorial Arduino tutorial

Capacitive moisture sensor Capacitive moisture sensor

Industrial sensor types Industrial sensor types