How to Test Soil Moisture Using Arduino and a Sensor

Published

Introduction: How to Test Soil Moisture Using Arduino and a Sensor

The goal of this test plan is to see if we can measure soil moisture. There are instructions below on how to build a simple moisture sensor, program it, and then use it to see if you can detect any moisture within the various soil samples.

(https://i.stack.imgur.com/J64vr.jpg)

Step 1: Gather Materials

1. Arduino Kit (Breadboard, wires, USB cable, LEDs, etc.)

2. Soil Moisture Sensor

3. Computer with Arduino Program installed

4. Multiple soil samples with different levels of moisture (water) Completely dry Slightly moist Completely saturated

5. Laptop

Step 2: Connect Your Circuit

Soil Moisture Sensor: Between the Breadboard and Arduino

Ground goes to Ground

VCC goes to -7

SIG goes to AO

Source: https://learn.sparkfun.com/tutorials/soil-moistur...

Between LEDs, Breadboard, and Arduino:

Connect the 2 LED lights from the breadboard to arduino with the wires.

From each LED light output on the breadboard (3) connect

Source: (https://i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2016/07/moisture-sensor-fritzing.jpg?resize=834%2C393&ssl=1)

Step 3: Arduino Program

/* Soil Moisture Basic Example

This sketch was written by SparkFun Electronics Joel Bartlett August 31, 2015

Basic skecth to print out soil moisture values to the Serial Monitor

Released under the MIT License(http://opensource.org/licenses/MIT)

*/

int val = 0; //value for storing moisture value

int soilPin = A0;//Declare a variable for the soil moisture sensor

int soilPower = 7;//Variable for Soil moisture Power

//Rather than powering the sensor through the 3.3V or 5V pins,

//we'll use a digital pin to power the sensor. This will

//prevent corrosion of the sensor as it sits in the soil.

void setup()

{

Serial.begin(9600); // open serial over USB

pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT

digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing

through the sensor pinMode (10,OUTPUT);

pinMode (8, OUTPUT);

pinMode (9, OUTPUT);

}

void loop()

{

if (readSoil() < 500){

Serial.println(readSoil());

Serial.print("not in moist environment");

digitalWrite (10,HIGH);

digitalWrite (8, LOW);

digitalWrite (9, LOW);

delay(1000);

}

else if ((readSoil() > 550) && (readSoil()< 750)) {

Serial.println(readSoil());

Serial.print("dry soil");

digitalWrite (10,HIGH);

digitalWrite (8, LOW);

digitalWrite (9, LOW);

delay(1000);

}

else if ((readSoil() > 800) && (readSoil() < 890)) {

Serial.println(readSoil());

Serial.print("wet soil");

digitalWrite (8, HIGH);

digitalWrite (10, LOW);

digitalWrite (9, LOW);

delay(1000);

}

else {

Serial.println(readSoil());

Serial.println("water");

//delay (1000);

digitalWrite (9, HIGH);

digitalWrite (8, LOW);

digitalWrite (10, LOW);

delay(1000); }

//Serial.print("Soil Moisture = ");

//get soil moisture value from the function below and print it

// second timefrme is used so you can test the sensor and see it change in real-time.

//For in-plant applications, you will want to take readings much less frequently. delay(1000);

//take a reading every second }

//This is a function used to get the soil moisture content int readSoil()

{

digitalWrite(soilPower, HIGH);//turn D7 "On"

delay(10);//wait 10 milliseconds

val = analogRead(soilPin);//Read the SIG value form sensor

digitalWrite(soilPower, LOW);//turn D7 "Off"

return val;//send current moisture value

Serial.println(readSoil());

//delay(1000);

//Serial.println(readSoil);

}

Step 4: Calibrate Data

We determined the ranges by opening the serial monitor and noticing the pattern in the ranges spit out by the moisture sensor when it was placed in dry soil, wet soil, or pure water.

It was determined that when the sensor was neither in soil nor in water, the values that the monitor returned never exceeded 500. When it was placed in dry soil, its values ranged between 550 and 750. In wet soil, the ranges varied between 800 and 890. Lastly, when it was in the pure water, it exceeded all of those values. These ranges were then implemented into the code so that the computer could eventually predict when the soil moisture sensor was placed in a moist environment. This prediction would later be used to determine when it had rained.

Step 5: Test Device

Now that the sensor can measure soil moisture from the coding input, we have to test it. In order to test the device, set up the arduino, sensor, and different soil samples. Once everything is connected and in sync with the program on the computer, place the sensor into a sample of soil. In no moisture soil, the program should output a value of zero. When the sensor comes into contact with moisture, the output values should increase into the hundred values and then decrease once the sensor is not in contact with moisture.

(https://www.instructables.com/id/Soil-Moisture-Sensor-Test-Plan/)

The code was later altered so that the serial monitor could predict whether the moisture sensor was in dry soil, wet soil, or pure water. The code was altered to include this portion:
void loop() {

if (readSoil() < 500){

Serial.println(readSoil());

Serial.print("not in moist environment");

digitalWrite (10,HIGH);

digitalWrite (8, LOW);

digitalWrite (9, LOW);

delay(1000);

}

else if ((readSoil() > 550) && (readSoil()< 750)) {

Serial.println(readSoil());

Serial.print("dry soil");

digitalWrite (10,HIGH);

digitalWrite (8, LOW);

digitalWrite (9, LOW);

delay(1000);

}

else if ((readSoil() > 800) && (readSoil() < 890)) {

Serial.println(readSoil());

Serial.print("wet soil");

digitalWrite (8, HIGH);

digitalWrite (10, LOW);

digitalWrite (9, LOW);

delay(1000);

}

else {

Serial.println(readSoil());

Serial.println("water");

//delay (1000);

digitalWrite (9, HIGH);

digitalWrite (8, LOW);

digitalWrite (10, LOW);

delay(1000); }

And thus when the serial monitor was opened, the computer was able to correctly identify the surrounding environment of the moisture sensor. Since the computer’s predictions were, in fact, correct, the device’s test proved correct.

Step 6: Calculate Device’s Accuracy

How do we test the device's accuracy?

To test the Soil Moisture Sensor’s accuracy, we changed the code and added LED lights to inform us of what type of environment the sensor was in. Depending on the environment, a different light would turn on.
When the white light turned on this signaled that the sensor was in the water. When the blue light turned on this signaled that the sensor was in the wet soil. When the green light turned on this signaled that it was dry soil or that it isn’t in a moist environment.

(https://i.stack.imgur.com/rXWPi.png)

Share

    Recommendations

    • Creative Misuse Contest

      Creative Misuse Contest
    • Water Contest

      Water Contest
    • Oil Contest

      Oil Contest

    Discussions