Introduction: Soil Moisture Senor : on or Off?

A guide to the Soil Moisture Sensor. Code details, calibration, and helpful tips. Lets get started with the basic code used for this specific sensor.

Step 1: The Soil Moisture Code

Soil Mositure Basic Example

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

(This sketch was written by SparkFun Electronics

Joel Bartlett August 31, 2015)

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

}

void loop() {

Serial.print("Soil Moisture = "); //get soil moisture value from the function below and print it

Serial.println(readSoil()); //This 1 second timeframe 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

}

Step 2: Connect to the Arduino

There are only three pins to connect: VCC, GND, and SIG.

The VCC pin should connect to digital pin 7.

The GND pin should connect to GND on the Arduino.

The SIG pin should connect to the Analog 0 (A0).

The pins must be connected properly to avoid crazy numbers in serial monitor.

Step 3: Calibrate !!

For useful data one should always calibrate the instruments. To begin you should test the numbers the serial number prints out when the sensor is completely dry vs when the sensor is completely submerged in a cup of water. Now these numbers will slightly vary when the sensor is placed in the soil. The numbers will vary between 0-880 depending on the voltage of the Arduino. Closer to 0 when the soil is dry and closer to 880 when the soil is saturated with moisture (wet). This requires patience as you begin to test the difference of the sensor in just water and the sensor in soil (try to get your soil as dry as possible first).

Step 4: Happy Coding

You should run multiple test, with multiple types of soils and different amounts of water in order to avoid outlier data. Work with whats best for you and just be patient. The numbers should fall into place as long as you have Arduino board connected properly to the soil moisture sensor. Its also useful to not place wires fully underwater and avoid touching the jars/pots the soil or water is in to avoid getting wrong numbers because of sensor sensibility.