In this article, we are going to interface soil moisture sensor with arduino and measure the volumetric concentration of water inside the soil. This Arduino moisture sensor is designed in a way that it can output data in both digital and analog modes.
Soil Moisture Sensor PinoutThe Soil moisture sensor a.k.a the soil humidity sensor has four pins VCC, GND, Aout, Dout. These four pins can be used to get the soil moisture data from the sensor, The pinout of the Soil Moisture Sensorare as follows:
The working of the soil humidity sensor is pretty simple and straightforward, as you can see in the image below. We just need to stick the fork-shaped conductive probe to the soil, as the probe has two exposed conductive plates that will act as a variable resistor whose resistance will vary depending on the water content in the soil.
Source: Arduino Soil Moisture Sensor
Soil Moisture Sensor - PartsThe entire soil humidity sensor consists of two parts: the first one is the soil moisture sensor probe and the second one is an electronic module. The module processes the incoming data from the probe and that gets processed by a microcontroller like Arduino and we get the final output.
The schematic diagram for the soil moisture sensor module is shown below. The schematic itself is very simple and needs a handful of generic components to build. If you don't have a prebuilt module on hand but still want to test your project, the schematic below will come in handy.
Now that we have a complete understanding of how a Soil Moisture sensor works, we can connect all the required wires to the Arduino UNO board. This section of the article will be divided into two parts, one shows analog output and another one shows the digital output. Let's begin with analog circuitry, the complete circuit to connect a soil humidity sensor with Arduino is shown below
As shown in the above arduino soil moisture sensor circuit diagram we have connected an LED to digital PIN 6 of the Arduino and the analog out pin of the sensor is connected to the A0 pin of the Arduino UNO board, finally, the ground is common between the LED and the sensor. We will program the Arduino so that the brightness of the LED will change depending on the soil moisture data sensed by the probe.
The code for Arduino Based Soil Moisture Sensor is very simple and easy to understand. We are just reading the analog data out of the sensor and changing the brightness of the LED according to the received data. Please do remember that we are only processing the analog data coming out of the sensor for the digital data you can see the onboard LED in the module lights up.
GithubLink: Arduino Soil Moisture Code and Circuit
// Moisture Sensor Arduino Code
//By Circuitdigest
#define ledPin 6
#define sensorPin A0
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
Serial.print("Analog output: ");
Serial.println(readSensor());
delay(500);
}
// This function returns the analog data to calling function
int readSensor() {
int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
int outputValue = map(sensorValue, 0, 1023, 255, 0); // map the 10-bit data to 8-bit data
analogWrite(ledPin, outputValue); // generate PWM signal
return outputValue; // Return analog moisture value
}
// Moisture Sensor Arduino Code
//By Circuitdigest
#define ledPin 6
#define sensorPin A0
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
Serial.print("Analog output: ");
Serial.println(readSensor());
delay(500);
}
// This function returns the analog data to calling function
int readSensor() {
int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
int outputValue = map(sensorValue, 0, 1023, 255, 0); // map the 10-bit data to 8-bit data
analogWrite(ledPin, outputValue); // generate PWM signal
return outputValue; // Return analog moisture value
}
Comments
Please log in or sign up to comment.