Bird
0
0
Arduinoprogramming~5 mins

Soil moisture sensor in Arduino

Choose your learning style9 modes available
Introduction

A soil moisture sensor helps you check how wet or dry the soil is. This is useful to know when plants need water.

To water plants automatically when soil is dry.
To monitor soil moisture in a garden or farm.
To learn about soil conditions for better plant care.
To build a simple weather or environment station.
To save water by watering only when needed.
Syntax
Arduino
int sensorPin = A0;  // Connect sensor to analog pin A0
int sensorValue = 0;   // Variable to store sensor reading

void setup() {
  Serial.begin(9600);  // Start serial communication
}

void loop() {
  sensorValue = analogRead(sensorPin);  // Read sensor value
  Serial.println(sensorValue);          // Print value to serial monitor
  delay(1000);                          // Wait 1 second before next reading
}

analogRead(pin) reads the sensor value from the specified analog pin.

Serial.begin(9600) starts communication to send data to your computer.

Examples
Read the soil moisture sensor value once.
Arduino
int sensorPin = A0;
int sensorValue = analogRead(sensorPin);
Print the sensor value to the serial monitor to see it on your computer.
Arduino
Serial.println(sensorValue);
Pause the program for 1 second before reading the sensor again.
Arduino
delay(1000);
Sample Program

This program reads the soil moisture sensor every second and prints the moisture level to the serial monitor. You can see the numbers change depending on how wet the soil is.

Arduino
int sensorPin = A0;  // Soil moisture sensor connected to analog pin A0
int sensorValue = 0;   // Variable to store sensor reading

void setup() {
  Serial.begin(9600);  // Start serial communication at 9600 baud
}

void loop() {
  sensorValue = analogRead(sensorPin);  // Read the sensor value
  Serial.print("Soil moisture level: ");
  Serial.println(sensorValue);          // Print the value
  delay(1000);                          // Wait 1 second
}
OutputSuccess
Important Notes

The sensor value changes with soil wetness: higher values usually mean drier soil, lower values mean wetter soil.

Make sure to connect the sensor to the correct analog pin and power it properly.

Use the serial monitor in the Arduino IDE to see the sensor readings live.

Summary

A soil moisture sensor measures how wet the soil is.

Use analogRead() to get sensor values from the Arduino.

Print values with Serial.println() to watch changes over time.