This circuit integrates an Arduino UNO microcontroller with a SparkFun Soil Moisture Sensor to measure the moisture content of the soil. The sensor's output is read by the Arduino through an analog input. The Arduino is programmed to power the sensor using a digital pin to prevent corrosion, read the moisture level, and output the reading to the serial monitor.
/**
* This example collects soil moisture measurements from the Sparkfun soil
* moisture sensor and prints them out to the serial monitor.
*
* - When you open the serial monitor, make sure that you select 9600 baud.
*
* This example was originally developed by Sparkfun.
*/
// Value for storing moisture value
int val = 0;
// Declare a variable for the soil moisture sensor pin
int soilPin = A0;
// Variable for Soil moisture Power
int soilPower = 7;
// 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);
// Set D7 as an OUTPUT
pinMode(soilPower, OUTPUT);
// Set to LOW so no power is flowing through the sensor
digitalWrite(soilPower, LOW);
}
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.
// take a reading every second
delay(1000);
}
// This is a function used to get the soil moisture content
int readSoil()
{
// turn D7 "On"
digitalWrite(soilPower, HIGH);
// wait 10 milliseconds
delay(10);
// Read the SIG value from sensor
val = analogRead(soilPin);
// turn D7 "Off"
digitalWrite(soilPower, LOW);
// return current moisture value
return val;
}
The code is designed to minimize corrosion of the soil moisture sensor by controlling the power supply through a digital pin (D7). The sensor is powered on only during the moisture reading and then immediately powered off. The soil moisture value is read from the analog pin A0 and printed to the serial monitor every second.