Monitoring soil moisture is crucial for the health of plants. Integrating a Soil Moisture Sensor with an Arduino allows you to measure moisture levels in real time such as in automatic Irrigation System.
In this tutorial, we will explore the Soil Moisture Sensor and how to interface it with an Arduino Uno and measure the volumetric content of water in the soil.
How Does a Soil Moisture Sensor Work?A soil moisture sensor measures the water content in soil using electrical conductivity. The sensor has two exposed conductors that are inserted into the soil. These probes measure the soil’s resistance;
- When water content is high, the resistance is low, and
- When the soil is dry, resistance is higher.
For beginners, understanding that the sensor outputs an analog signal corresponding to the moisture level is essential. This signal is then interpreted by the Arduino to produce a readable value.
Soil Moisture Sensor Hardware OverviewThere are two main components in a soil moisture sensor:
The Sensing Probe
The probe is a simple two-terminal device that measures the resistance between two points. If the soil has high moisture content, the resistance will be lower, resulting in higher conductivity.
Sensor Module
The sensor module takes the raw data from the probe and translates it into an analog or digital output. It also includes a potentiometer for calibration, allowing you to adjust the sensitivity of the sensor to different moisture levels.
Soil Moisture Sensor PinoutVCC: This is the power input pin. It connects to the 3.3V or 5V pin of the Arduino to power the soil moisture sensor module.
GND: This is the ground pin. It should be connected to one of the GND pins of the Arduino.
A0 (Analog Output): The analog output pin provides a continuous analog voltage based on the moisture level detected by the probe.
D0 (Digital Output): This is the digital output pin, which gives a HIGH or LOW signal depending on the moisture threshold set using the potentiometer on the sensor module.
Interfacing Soil Moisture Sensor with Arduino Uno in Analog ModeIn the wiring diagram, the Arduino UNO is connected to a soil moisture sensor as follows: The VCC and GND pins of the sensor are connected to the 5V and GND pins of the Arduino, respectively. The analog output (A0) pin of the sensor is connected to the A0 analog input pin on the Arduino. Additionally, a 16×2 I2C LCD is connected to the Arduino, with the SCL pin of the LCD connected to the SCL pin of the Arduino and the SDA pin connected to the SDA pin of the Arduino.
Code
// Library to Run I2C LCD
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the analog pin for the soil moisture sensor
const int soilMoisturePin = A0;
// Variables to store sensor values
int sensorValue = 0;
int moisturePercent = 0;
void setup()
{
// initialize the lcd
lcd.init();
// Turn on the Backlight
lcd.backlight();
// Clear the display buffer
lcd.clear();
// Print a message to the LCD
lcd.setCursor(0, 0);
lcd.print("Soil Moisture:");
}
void loop()
{
// Read the value from the soil moisture sensor
sensorValue = analogRead(soilMoisturePin);
// Convert the sensor value to percentage
moisturePercent = map(sensorValue, 0, 1020, 100, 0);
// Display the moisture percentage on the LCD
lcd.setCursor(0, 1);
lcd.print(moisturePercent);
lcd.print(" % "); // Clear any extra characters
// Wait for 1 second
delay(1000);
}
Interfacing Soil Moisture Sensor with Arduino Uno in Digital ModeHere is the schematic diagram. Here we are using the digital output pin of the Sensor.
Code
#include <LiquidCrystal_I2C.h>
// Library to Run I2C LCD
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the digital pin for the soil moisture sensor
const int soilMoisturePin = 2;
// Variable to store Output
int sensorOutput = 0;
void setup() {
// initialize the lcd
lcd.init();
// Turn on the Backlight
lcd.backlight();
// Clear the display buffer
lcd.clear();
// Initialize the digital pin as an input
pinMode(soilMoisturePin, INPUT);
// Print a message to the LCD
lcd.setCursor(0, 0);
lcd.print("Soil Moisture:");
}
void loop() {
// Read the value from the soil moisture sensor
sensorOutput = digitalRead(soilMoisturePin);
// Display the moisture status on the LCD
lcd.setCursor(0, 1);
if (sensorOutput == HIGH) {
lcd.print("Dry");
} else {
lcd.print("Wet");
}
// Wait for 1 second
delay(1000);
To learn more checkout the complete article here: https://playwithcircuit.com/soil-moisture-sensor-arduino-tutorial/
Comments
Please log in or sign up to comment.