Arduino UNO R4 - Soil Moisture Sensor

In this tutorial, we will learn how to use a moisture sensor with the Arduino UNO R4. Specifically, we will cover:

Arduino UNO R4 soil moisture sensor

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Capacitive Soil Moisture Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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:

  • Capacitive moisture sensor
  • Resistive moisture sensor

Both sensors give information about soil moisture, but they work in different ways. We strongly suggest using the capacitive moisture sensor for this reason:

  • The resistive soil moisture sensor gets damaged gradually. This happens because there is an electrical current running between its probes, leading to a type of damage called electrochemical corrosion.
  • The capacitive soil moisture sensor does not get damaged over time. The reason is that its electrodes are covered and do not corrode easily.

The image below displays a soil moisture sensor made of resistive material that has corroded over time.

resistive soil moisture sensor corroded

The remaining part of this tutorial will focus on using the capacitive soil moisture sensor.

Capacitive Soil Moisture Sensor Pinout

A capacitive soil moisture sensor comes with three pins:

  • GND pin: connect to GND (0V)
  • VCC pin: connect to VCC (5V or 3.3V)
  • AOUT pin: sends an analog signal which changes with soil moisture. Connect to the analog input pin on an Arduino UNO R4.
capacitive soil moisture sensor pinout

How It Works

The higher the water content in the soil, the lower the voltage at the AOUT pin.

Wiring Diagram

The wiring diagram between Arduino UNO R4 soil moisture sensor

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-soil-moisture-sensor */ #define AOUT_PIN A0 // Defines pin A0 to read from the moisture sensor void setup() { Serial.begin(9600); // Initializes serial communication at 9600 bps } void loop() { int value = analogRead(AOUT_PIN); // Reads the moisture level from the sensor Serial.print("Moisture: "); // Sends the text 'Moisture: ' to the serial monitor Serial.println(value); // Prints the moisture level to the serial monitor delay(500); // Pauses the loop for 500 milliseconds }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the soil moisture sensor to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code above and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4.
  • Place the sensor in the soil, then add water to the soil. Alternatively, you can gently place it into a cup of salt water.
  • Check the results on the Serial Monitor. It will display as follows:
COM6
Send
Moisture: 581 Moisture: 581 Moisture: 575 Moisture: 566 Moisture: 556 Moisture: 547 Moisture: 539 Moisture: 530 Moisture: 521 Moisture: 513 Moisture: 506 Moisture: 500 Moisture: 495 Moisture: 492 Moisture: 490 Moisture: 489 Moisture: 488
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

  • Don't use pure water for testing because it doesn't conduct electricity, so it won't affect the sensor readings.
  • Normally, the sensor readings don't fall to zero. They usually stay between 500 and 600. However, this can vary depending on how deep the sensor is in the soil or water, the type of soil or water, and the power supply voltage.
  • Do not put the circuit part of the sensor (located at the top) into the soil or water as it can damage the sensor.

Calibration for Capacitive Soil Moisture Sensor

The reading from the moisture sensor is not fixed; it varies with the type of soil and its water content. To use it correctly, we must calibrate it to find a cutoff point that tells us when the soil is wet or dry.

How to Perform Calibration:

  • Use the Arduino UNO R4 to execute the provided code.
  • Insert the moisture sensor into the soil.
  • Gradually add water to the soil.
  • Observe the Serial Monitor.
  • Record the value when you think the soil shift from dry to wet. This value is known as THRESHOLD.

Determine if the soil is wet or dry

After you calibrate, change the THRESHOLD value you noted to the following code. This code checks if the soil is wet or dry.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-soil-moisture-sensor */ #define AOUT_PIN A0 // Pin on Arduino for moisture sensor output #define THRESHOLD 530 // Set threshold value for moisture level void setup() { Serial.begin(9600); // Initialize serial communication at 9600 bps } void loop() { int value = analog_read(AOUT_PIN); // Read value from moisture sensor if (value > THRESHOLD) // Compare sensor reading to threshold Serial.print("The soil is DRY ("); // Print dry status if above threshold else Serial.print("The soil is WET ("); // Print wet status if below threshold Serial.print(value); // Print the sensor reading Serial.println(")"); // Finish the line delay(500); // Wait for half a second before next read }

The outcome as seen on the Serial Monitor.

COM6
Send
The soil is DRY (581) The soil is DRY (575) The soil is DRY (566) The soil is DRY (547) The soil is DRY (539) The soil is WET (521) The soil is WET (513) The soil is WET (492) The soil is WET (488)
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Function References

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!