Arduino Nano - Soil Moisture Sensor

This tutorial instructs you how to use a moisture sensor with Arduino Nano. Specifically, we will look at:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Capacitive Soil Moisture Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Buy Note: Many soil moisture sensors available in the market are unreliable, regardless of their version. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of Soil Moisture Sensor Sensor

capacitive moisture sensor vs resistive moisture sensor

There are two types of moisture sensors:

  • The resistive moisture sensor
  • The capacitive moisture sensor.

Both sensors offer soil moisture information. However, their working principles are different. We strongly suggest utilizing the capacitive moisture sensor due to the following:

  • The resistive soil moisture sensor is prone to corrosion over time. This is because electrical current passes between its probes, leading to electrochemical corrosion.
  • The capacitive soil moisture sensor corrodes with time much slower than the resistive soil moisture sensor. This is because its electrodes are not exposed and are comparatively more resistant to corrosion.

This is an illustration of a resistive soil moisture sensor that has been damaged due to corrosion over time.

resistive soil moisture sensor corroded

The remainder of this tutorial will utilize the capacitive soil moisture sensor.

Capacitive Soil Moisture Sensor Pinout

The capacitive soil moisture sensor has three pins:

  • GND pin: This should be connected to the GND (0V)
  • VCC pin: This should be connected to VCC (5V or 3.3v)
  • AOUT pin: This analog signal output pin will produce a voltage that is proportional to the soil moisture level. This should be connected to an Arduino's analog input pin.
capacitive soil moisture sensor pinout

How It Works

The AOUT pin has a higher voltage when there is more water in the soil.

Wiring Diagram

The wiring diagram between Arduino Nano and soil moisture sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code for reading value from soil moisture sensor

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-soil-moisture-sensor */ #define AOUT_PIN A6 // The Arduino Nano 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); }

Detailed Instructions

  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Insert the moisture sensor into the soil.
  • Gently pour water into the soil.
  • Check out the result on the 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 value obtained from the moisture sensor is not absolute. It depends on the soil's composition and amount of water present. In order to accurately determine a boundary between wet and dry, calibration must be done.

Instructions for calibration:

  • Execute the code on Arduino Nano
  • Place the moisture sensor in the soil
  • Gradually add water to the soil
  • Check out the Serial Monitor
  • Record the value when the soil changes from dry to wet. This is referred to as the THRESHOLD.

Arduino Nano determines if the soil is wet or dry

Once the calibration is complete, update the THRESHOLD value that you noted down into the code below. This Arduino Nano code will decide whether the soil is wet or dry.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-soil-moisture-sensor */ #define AOUT_PIN A6 // The Arduino Nano 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 output displayed on the 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

Function References