Arduino Mega - Soil Moisture Sensor

In this guide, we will learn how to use a moisture sensor with the Arduino Mega. We will cover the following:

Arduino Mega soil moisture sensor

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Capacitive Soil Moisture Sensor
1×Jumper Wires

Or you can buy the following 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.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Many soil moisture sensors available in the market are unreliable, regardless of their version. We strongly recommend buying the sensor with TLC555I Chip 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

Moisture sensors come in two kinds:

  • Moisture sensor that uses capacitance
  • Moisture sensor that uses resistance

Both sensors tell you about soil moisture, but they work in different ways. We strongly recommend using the capacitive moisture sensor for this reason:

  • The resistive soil moisture sensor slowly gets damaged. This happens because electricity flows between its probes, causing chemical 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 picture below shows a soil moisture sensor made from a material that has rusted over time.

resistive soil moisture sensor corroded

The rest of this guide will show how to use the capacitive soil moisture sensor.

Capacitive Soil Moisture Sensor Pinout

A capacitive soil moisture sensor has three pins:

  • GND pin: connect to ground (0 volts)
  • VCC pin: connect to power (5 volts or 3.3 volts)
  • AOUT pin: sends an analog signal that changes with soil moisture. Connect to an analog input pin on an Arduino Mega.
capacitive soil moisture sensor pinout

How It Works

More water in the soil means the AOUT pin voltage is lower.

Wiring Diagram

The wiring diagram between Arduino Mega soil moisture sensor

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-soil-moisture-sensor */ #define AOUT_PIN A0 // Pin A0 connected to the soil moisture sensor void setup() { Serial.begin(9600); // Start serial communication at 9600 baud } void loop() { int value = analogRead(AOUT_PIN); // Read the current moisture value from the sensor Serial.print("Moisture: "); // Display the label for the subsequent value Serial.println(value); // Output the raw moisture reading delay(500); // Wait for 500 milliseconds before the next sample }

Detailed Instructions

Follow these steps one by one.

  • Connect the soil moisture sensor to the Arduino Mega as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the right board: Arduino Mega, and the correct COM port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Put the sensor in the soil, then add water to the soil. Or you can gently place it in a cup of salt water.
  • Check the results on the Serial Monitor. It will show like this:
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:

  • Do not test with pure water, because it does not conduct electricity, so it won’t affect the sensor readings.
  • Usually the sensor readings don’t go down to zero. They stay around 500–600, but this can change depending on how deep the sensor is in soil or water, the type of soil or water, and the power supply voltage.
  • Do not insert the top part of the sensor (the circuit) into soil or water, as this can damage the sensor.

Calibration for Capacitive Soil Moisture Sensor

The moisture sensor doesn’t give the same reading every time. It changes with soil type and how wet the soil is. To use it right, we need to adjust it to find the value that shows when the soil is wet or dry.

How to Calibrate

  • Use the Arduino Mega to run the given code.
  • Put the moisture sensor into the soil.
  • Slowly add water to the soil.
  • Watch the Serial Monitor.
  • Note the value when you think the soil changes from dry to wet. This value is called the threshold.

Determine if the soil is wet or dry

After you finish calibrating, replace the THRESHOLD value you wrote with this code. This code checks if the soil is wet or dry.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-soil-moisture-sensor */ #define AOUT_PIN A0 // Analog pin connected to the soil moisture sensor output #define THRESHOLD 530 // Threshold used to categorize moisture level void setup() { Serial.begin(9600); // Start serial communication at 9600 baud } void loop() { int value = analog_read(AOUT_PIN); // Read moisture sensor value from the selected analog pin if (value > THRESHOLD) // Compare the current reading to the dryness threshold Serial.print("The soil is DRY ("); // Output the dry-state label when the reading exceeds the threshold else Serial.print("The soil is WET ("); // Output the wet-state label when the reading does not exceed the threshold Serial.print(value); // Output the numeric sensor reading Serial.println(")"); // End the line with a closing parenthesis delay(500); // Pause briefly before the next reading }

The result you see 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!