0
0
AutocadHow-ToBeginner · 4 min read

How to Use Soil Moisture Sensor with Arduino: Simple Guide

To use a soil moisture sensor with Arduino, connect the sensor's analog output to an Arduino analog pin and read its value using analogRead(). Then, use this value to determine soil moisture level and trigger actions like turning on a water pump.
📐

Syntax

Here is the basic syntax to read a soil moisture sensor value in Arduino:

  • analogRead(pin): Reads the analog voltage from the sensor connected to the specified pin.
  • pinMode(pin, mode): Sets the pin mode, usually INPUT for sensors.
  • Serial.begin(baud_rate): Starts serial communication to print sensor values.
arduino
const int sensorPin = A0;  // Analog pin connected to sensor

void setup() {
  Serial.begin(9600);      // Start serial communication
  pinMode(sensorPin, INPUT); // Set sensor pin as input
}

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

Example

This example reads the soil moisture sensor value and turns on an LED if the soil is dry (sensor value below a threshold).

arduino
const int sensorPin = A0;    // Soil moisture sensor connected to analog pin A0
const int ledPin = 13;       // Onboard LED pin
const int dryThreshold = 400; // Threshold for dry soil (adjust as needed)

void setup() {
  Serial.begin(9600);        // Start serial communication
  pinMode(sensorPin, INPUT); // Sensor pin as input
  pinMode(ledPin, OUTPUT);   // LED pin as output
}

void loop() {
  int sensorValue = analogRead(sensorPin);  // Read sensor value
  Serial.print("Soil Moisture Value: ");
  Serial.println(sensorValue);

  if (sensorValue < dryThreshold) {
    digitalWrite(ledPin, HIGH);  // Turn LED on if soil is dry
  } else {
    digitalWrite(ledPin, LOW);   // Turn LED off if soil is wet
  }

  delay(1000);  // Wait 1 second before next reading
}
Output
Soil Moisture Value: 350 Soil Moisture Value: 360 Soil Moisture Value: 420 Soil Moisture Value: 390 ...
⚠️

Common Pitfalls

  • Incorrect wiring: Make sure the sensor's VCC and GND are connected properly to 5V and GND on Arduino.
  • Using digital pin instead of analog: Soil moisture sensors output analog signals; connect to analog pins (A0, A1, etc.).
  • Not calibrating threshold: Sensor values vary; test your soil and adjust the dry/wet threshold accordingly.
  • Leaving sensor in soil continuously: Some sensors corrode if powered continuously; consider powering sensor only during readings.
arduino
/* Wrong way: Using digitalRead on analog sensor pin */
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() {
  int value = digitalRead(sensorPin);  // WRONG: sensor outputs analog signal
  Serial.println(value);
  delay(1000);
}

/* Right way: Use analogRead */

void loop() {
  int value = analogRead(sensorPin);  // Correct
  Serial.println(value);
  delay(1000);
}
📊

Quick Reference

Here is a quick cheat sheet for using a soil moisture sensor with Arduino:

StepDescription
Connect sensor VCCTo Arduino 5V pin
Connect sensor GNDTo Arduino GND pin
Connect sensor outputTo Arduino analog pin (e.g., A0)
Read sensor valueUse analogRead(pin)
Set thresholdDecide value for dry/wet soil
Control deviceUse if-else to act on sensor value

Key Takeaways

Connect the soil moisture sensor output to an Arduino analog pin and read it with analogRead().
Calibrate your sensor by testing soil and setting a proper threshold for dry and wet conditions.
Use serial output to monitor sensor values and debug your setup.
Avoid using digitalRead() on analog sensors; always use analogRead() for accurate readings.
Ensure correct wiring and consider powering the sensor only during readings to prevent corrosion.