First Step - UWRF-DoTS/soil-moisture-monitor GitHub Wiki

Step 1: Setup the Soil Moisture Sensor

DFRobot provides a wiki page, describing how to integrate the sensor with the Arduino, and how to calibrate it. It should work after the calibration is done, and you can then modify the code to suit your preferences. The calibration asks you to establish an AIR_VALUE, as the value of the sensor when left by itself, a WATER_VALUE, when the sensor is submerged in water, and an INTERVALS value for a set of 3 states, WET, DRY, and AIR.

I went the extra step of creating a calculation to measure the percentage of soil moisture content. Since the AIR_VALUE, represented the highest value of the sensor, and the WATER_VALUE represented the lowest, it gives you a set range of [AIR_VALUE to WATER_VALUE]. I then subtracted the sensor’s current value(sensorValue), from the AIR_VALUE, to represent a constant range, so that now, the sensor reads the lowest value when in the air, and the highest when in water. Lastly, I divided by 10 to change the value to a percentage, which makes it easier to read for statistical purposes.

#define AIR_VALUE   508
#define WATER_VALUE 242
#define SENSOR_PIN  A0
       
void setup() {
    pinMode(sensorPin, INPUT);
    Serial.begin(9600);
}

void loop() {
   int sensorValue = analogRead(SENSOR_PIN);
   int soilMoistureContent = (AIR_VALUE - sensorValue) / 10;
   
   Serial.print("Soil Moisture Content at: ");
   Serial.print(soilMoistureContent);
   Serial.println('%');
}