Introduction
In this experiment, we will learn how to measure soil moisture using an Arduino and a soil moisture sensor. This sensor will help us monitor soil hydration levels, which is ideal for automated watering systems in gardens or farms.
Components Needed
- Arduino (e.g., Uno, Nano)
- Soil moisture sensor
- Breadboard and jumper wires
- USB cable for programming
Circuit Setup
- Connect the VCC pin of the soil moisture sensor to the 5V pin on the Arduino.
- Connect the GND pin of the soil moisture sensor to the GND pin on the Arduino.
- Connect the analog output pin of the sensor to an analog input pin on the Arduino (e.g., A0).
Ensure that the soil moisture sensor is properly inserted into the soil before testing.
Code for Soil Moisture Sensor
Upload the following code to your Arduino to read soil moisture levels:
// Define the analog input pin for the sensor
int sensorPin = A0;
int sensorValue = 0;
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
sensorValue = analogRead(sensorPin); // Read the value from the sensor
Serial.println(sensorValue); // Print the value to the serial monitor
delay(1000); // Wait for a second
}
Explanation
The soil moisture sensor measures the electrical resistance between two probes. The Arduino reads this resistance as an analog value, which indicates the moisture level in the soil:
analogRead(pin)
: Reads the voltage from the sensor and converts it to a value between 0 and 1023.Serial.println()
: Sends the moisture level to the serial monitor for display.- The lower the sensor value, the drier the soil is. Higher values indicate moister soil.
Troubleshooting
- If the sensor values are inconsistent, ensure the sensor is securely placed in the soil.
- Check the sensor wiring to ensure it's connected correctly to the Arduino.
- If the sensor reads high values constantly, the sensor may be too wet or malfunctioning.