Arduino - Soil Moisture Sensor

In this tutorial, we are going to learn how to use the moisture sensor with Arduino. In detail, we will learn:

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Capacitive Soil Moisture Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Optional) Screw Terminal Block Shield for Arduino
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Soil Moisture Sensor Sensor

capacitive moisture sensor vs resistive moisture sensor

There are two kinds of moisture sensors:

  • Resistive moisture sensor
  • Capacitive moisture sensor.

Both sensors provide soil moisture information. However, their working principles are different. We highly recommend using the capacitive moisture sensor, because of the following reason:

  • The resistive soil moisture sensor corrodes over time. That is because the electrical current flows between its probes. This causes electrochemical corrosion.
  • The capacitive soil moisture sensor does NOT corrode over time. That is because its electrodes are not exposed electrodes and is comparatively corrosion-free.

The below image shows a resistive soil moisture sensor that corroded over time.

resistive soil moisture sensor corroded

The rest of this tutorial will use the capacitive soil moisture sensor.

Capacitive Soil Moisture Sensor Pinout

A capacitive soil moisture sensor has three pins:

  • GND pin: needs to be connected to GND (0V)
  • VCC pin: needs to be connected to VCC (5V or 3.3v)
  • AOUT pin: Analog signal output pin outputs the voltage in direct proportion to the soil moisture level. Connect this pin to an Arduino's analog input pin.
capacitive soil moisture sensor pinout

How It Works

The more the water in the soil, the higher the voltage in the AOUT pin is

Wiring Diagram

Arduino soil moisture sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-soil-moisture-sensor */ #define AOUT_PIN A0 // Arduino pin that connects to AOUT pin of moisture sensor void setup() { Serial.begin(9600); } void loop() { int value = analogRead(AOUT_PIN); // read the analog value from sensor Serial.print("Moisture: "); Serial.println(value); delay(500); }

Quick Steps

  • Copy the above code and open it with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Embed the moisture sensor into the soil
  • Pour water into the soil slowly
  • See the result on Serial Monitor.
COM6
Send
Moisture: 2 Moisture: 15 Moisture: 108 Moisture: 284 Moisture: 298 Moisture: 313
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Calibration for Capacitive Soil Moisture Sensor

The measured value from the moisture sensor is relative. It depends on the soil's composition and water. In practice, we need to do calibration to determine a threshold that is a border between wet and dry.

How to do calibration:

  • Run the above code on Arduino
  • Embed the moisture sensor into the soil
  • Embed the moisture sensor into the soil
  • Pour water into the soil slowly
  • Watch Serial Monitor.
  • Write down a value at the time you feel that the soil changes its moisture from dry to wet. This value is called THRESHOLD.

Determine if the soil is wet or dry

After the calibration, update the THRESHOLD value you wrote down to the below code. The below code determines if the soil is wet or dry

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-soil-moisture-sensor */ #define AOUT_PIN A0 // Arduino pin that connects to AOUT pin of moisture sensor #define THRESHOLD 100 // CHANGE YOUR THRESHOLD HERE void setup() { Serial.begin(9600); } void loop() { int value = analogRead(AOUT_PIN); // read the analog value from sensor if (value < THRESHOLD) Serial.print("The soil is DRY ("); else Serial.print("The soil is WET ("); Serial.print(value); Serial.println(")"); delay(500); }

The result on Serial Monitor.

COM6
Send
The soil is DRY (31) The soil is DRY (48) The soil is DRY (76) The soil is DRY (74) The soil is DRY (89) The soil is WET (158) The soil is WET (192) The soil is WET (256) The soil is WET (261)
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The Best Arduino Starter Kit

※ OUR MESSAGES