Soil Moisture Sensor Arduino Complete Setup Guide

Author

Reads 1.2K

Close-up of autumn leaves on wet soil in İzmir, Türkiye.
Credit: pexels.com, Close-up of autumn leaves on wet soil in İzmir, Türkiye.

To set up a soil moisture sensor with Arduino, you'll need a few key components, including the sensor itself, an Arduino board, and a breadboard.

The soil moisture sensor typically consists of two probes that insert into the soil, allowing it to measure the moisture levels.

Connect the sensor's VCC pin to the Arduino's 5V pin, the GND pin to the Arduino's GND pin, and the OUT pin to a digital pin on the Arduino.

Make sure to connect the sensor's VCC pin to a power source that can supply up to 20mA of current.

Hardware

To build a soil moisture sensor Arduino project, you'll need the following hardware. A soil moisture sensor consists of two main parts: the sensor probes and the LM393 comparator module.

You can choose between an Official Arduino Uno or a DIYables STEM V3, which is fully compatible with the Arduino Uno R3. Alternatively, you can opt for a DIYables STEM V3 Starter Kit that includes the Arduino Uno.

Credit: youtube.com, SoilWatch 10 - Arduino - Capacitive soil moisture sensor

Here's a list of the required hardware:

You can also purchase a DIYables Sensor Kit, which includes 30 or 18 sensors/displays, depending on the version.

Hardware Overview

A soil moisture sensor is a clever device that helps you measure the moisture levels in the soil. It's made up of two main parts: the sensor probes and the LM393 comparator module.

The sensor probes are the parts that actually touch the soil and measure its moisture levels. The LM393 comparator module is what processes the data from the sensor probes and sends it to your Arduino board.

You'll need to connect the sensor probes to the LM393 comparator module, and then connect that to your Arduino board. This is where the magic happens, and you can start monitoring the soil moisture levels.

The sensor probes are usually connected to the AOUT pin, which is where the voltage data is sent to. The more water in the soil, the lower the voltage in the AOUT pin will be.

Credit: youtube.com, Arduino Hardware Overview | The First Impressions | Microcontroller and Single Board Computer

Here's a list of the required hardware for this project:

If you're not feeling like building everything from scratch, you can also buy a starter kit that includes everything you need. Some popular options include the DIYables STEM V3 Starter Kit and the DIYables Sensor Kit.

Specifications

The Arduino Soil Moisture Sensor is a reliable tool for monitoring soil moisture levels. It operates within a voltage range of 3.3V to 5V, with 5V being the recommended voltage for use with an Arduino UNO.

The sensor has dual output capabilities, providing both analog and digital output options. This allows you to choose the output type that best suits your project's needs.

Analog output from the sensor ranges from 0 to 1023, which maps to a 0-5V range. This means you can easily read the sensor's output using an analog-to-digital converter (ADC).

The digital output is a simple LOW/HIGH signal, which can be configured to a specific threshold value. This flexibility makes it easy to integrate the sensor into your project.

Green Leaf on Brown Soil
Credit: pexels.com, Green Leaf on Brown Soil

The probe material is made of nickel-plated material, which is corrosion-resistant and durable. This ensures that the sensor can withstand the harsh conditions of outdoor use.

The sensor's response time is less than 1 second, allowing for real-time monitoring of soil moisture levels. This is particularly useful for applications where rapid feedback is essential.

Working Principle

The working principle of a soil moisture sensor is based on the simple yet effective concept of measuring the electrical resistance between two metal probes placed in the soil.

In dry soil, the high resistance makes it difficult for electricity to flow between the probes, while in wet soil, the presence of more water allows electricity to flow more easily, resulting in low resistance.

The sensor detects this change in resistance and converts it into an electrical signal, which is then processed by the module in two ways: analog and digital output.

The analog output provides a continuous voltage that corresponds to the moisture level, while the digital output compares the analog output to a threshold set by a potentiometer.

Credit: youtube.com, interfacing Soil/Moisture Sensor arduino| Moisture Sensor with Arduino | Code for the Soil Moisture

This threshold determines whether the soil moisture is lower (dry) or higher (wet) than the set value, resulting in a HIGH or LOW digital output respectively.

Here's a breakdown of the sensor's working principle:

  • In dry soil, high resistance makes it difficult for electricity to flow.
  • In wet soil, low resistance allows electricity to flow more easily.

The sensor's resistance is inversely proportional to the soil moisture, meaning that the more water in the soil, the lower the resistance, and the less water, the higher the resistance.

The sensor's analog output voltage varies inversely with soil moisture content, making it perfect for Arduino soil moisture sensor programming applications.

Setup and Wiring

To set up your soil moisture sensor with Arduino, start by connecting the sensor to the board. Connect the VCC pin on the sensor module to the 5V pin on the Arduino, and connect the GND pin on the sensor to one of the GND pins on the Arduino.

The AO pin of the sensor should be connected to the A0 analog input pin on the Arduino. This is a crucial connection to get accurate readings. You can refer to the quick reference table below for the pin connections.

Remember to power the sensor only when you need to take a reading, and use quality jumper wires to avoid intermittent readings.

Wiring

Credit: youtube.com, Never Solder Again - How to Connect Wires the EASY Way

To connect your soil moisture sensor to your Arduino, start by connecting the VCC pin on the sensor module to the 5V pin on the Arduino. This will provide the necessary power to the sensor.

The GND pin on the sensor should be connected to one of the GND pins on the Arduino. Make sure all components share the same ground connection to avoid erratic behavior.

To avoid powering the sensor when not needed, connect the VCC pin to a digital output pin on the Arduino, such as digital pin #7, and control it with your code.

Finally, connect the AO pin of the sensor to the A0 analog input pin on the Arduino. This will allow you to read the sensor's output voltage and determine the soil moisture level.

Here's a quick reference table for the pin connections:

Remember to double-check your pin connections to ensure they match your code and avoid code errors.

LM393 Comparator Module

Credit: youtube.com, LM393 how to step by step wired as inverting comparator circuit beginning learning electronics

The LM393 Comparator Module is a crucial part of our soil moisture sensor setup, and it's surprisingly easy to use.

This module provides both digital and analog outputs, which can be used to trigger actions or monitor soil moisture levels.

The analog output comes directly from the changing resistance of the probes, which decreases as the soil gets wetter.

The digital output is what really makes this module useful, as it can be used to trigger actions like turning on a relay to water plants when the soil gets too dry.

Here are the possible digital output states:

  • If the soil is drier than the set level, the comparator gives a HIGH digital output signal at the digital pin (DO).
  • If the soil is wetter than the set level, it outputs a LOW digital signal.

You can adjust the moisture level threshold by turning the potentiometer, which is a great feature to have.

Introduction: LCD

The LCD screen is a crucial part of our project, and it's what will help us visualize the data from our Arduino moisture sensor.

We're using the YL-69 sensor, which works based on a resistance between two "blades".

The sensor will give us values between 450-1023, but we need to map these values to get a percentage reading.

We'll get to that later, but for now, let's focus on setting up our LCD screen.

Experiment 1

Credit: youtube.com, Arduino Soil Moisture Sensor (Lesson #14)

Experiment 1 is all about measuring soil moisture using an analog output from the sensor. This experiment will give you a good understanding of how to read the sensor's output and get a sense of the moisture levels in the soil.

A common issue with these sensors is that they don't last very long because they're always in contact with moisture. This can be a problem if you're planning to use the sensor for an extended period.

If the sensor is constantly powered while inserted in the soil, it tends to corrode more quickly, which can further reduce its lifespan.

Experiment 2

Experiment 2 is where things get really interesting. We're going to use the digital output from the soil moisture sensor to check if the soil is within an acceptable range.

The circuit setup remains the same as in Experiment 1, but we need to make a small adjustment. We'll disconnect the wire going to the analog input pin (A0) and connect the DO (digital output) pin on the sensor module to digital pin 8 on the Arduino.

Here's a quick rundown of the connections:

Now, upload the sketch to your Arduino, and this program will check whether the soil is dry or not using the sensor's digital output.

Code and Calibration

Credit: youtube.com, How to Use a Soil Moisture Sensor with Arduino - Step-by-Step Tutorial

To get started with your soil moisture sensor project, you'll need to write some code and calibrate your sensor. The code for the Arduino-based soil moisture sensor is relatively simple and easy to understand.

You'll need to initialize your code by declaring two macros, one for the LED and one for the sensor pin. The setup() function should initialize the serial with 9600 baud and set the LED pin as output, making it LOW to prevent floating.

The loop() function will print "Analog output:" to the serial monitor window and then call the readSensor() function to get the analog value from the A0 pin. The readSensor() function returns the analog value, which is then mapped to 8-bit data using the map function.

Here are the threshold values you can use to determine the soil moisture level:

  • If the reading is less than 500, the soil is too wet.
  • If the reading is between 500 and 750, the soil is in the ideal range.
  • If the reading is more than 750, the soil is dry enough that it probably needs watering.

To calibrate your sensor, run the code on Arduino, embed the moisture sensor in the soil, and pour water into the soil slowly while watching the Serial Monitor. Write down the value at the time you feel the soil changes its moisture from dry to wet – this value is called the THRESHOLD.

Code

Small Mushroom with a Brown Cap Growing Out of Wet Soil
Credit: pexels.com, Small Mushroom with a Brown Cap Growing Out of Wet Soil

Writing code for your Arduino-based soil moisture sensor is a relatively straightforward process. You'll need to use the threshold values you found during calibration to determine if the soil is too dry, too wet, or just right.

The code for the Arduino-based soil moisture sensor is very simple and easy to understand. You're just reading the analog data out of the sensor and changing the brightness of the LED according to the received data.

To get started, you'll need to declare two macros: one for the LED and one for the sensor pin. This is where you'll connect an LED and read the data coming out of the sensor.

You'll also need to initialise your code by declaring the serial connection at 9600 baud and setting the LED pin as an output.

In the loop function, you'll print "Analog output:" as text on the serial monitor window and then call the readSensor() function to get the analog value.

Red earthworm crawling on grassy soil
Credit: pexels.com, Red earthworm crawling on grassy soil

The readSensor() function returns the analog value that's read through the A0 pin of the Arduino. This data is in 10-bit format, so you'll need to convert it to 8-bit data using the map function.

Here's a summary of the threshold values you can use to determine the soil moisture level:

  • If the reading is less than 500, the soil is too wet.
  • If the reading is between 500 and 750, it’s in the ideal range.
  • If it’s more than 750, the soil is dry enough that it probably needs watering.

Remember to use the threshold values you found during calibration to determine the soil moisture level.

Calibration

Calibration is a crucial step in getting your soil moisture sensor working accurately. It's essential to understand that the measured value from the moisture sensor is relative and depends on the soil's composition and water.

To calibrate your sensor, you'll need to run the code on your Arduino and embed the moisture sensor into the soil. Then, pour water into the soil slowly and watch the Serial Monitor for the reading.

The calibration process involves finding the threshold value that marks the border between wet and dry soil. This is done by writing down a value at the time you feel that the soil changes its moisture from dry to wet.

Credit: youtube.com, Corghi Master Code Calibration

Here are the steps to follow:

  • Run the code on your Arduino
  • Embed the moisture sensor into the soil
  • Pour water into the soil slowly
  • Watch the Serial Monitor for the reading
  • Write down the value at the point where the soil changes its moisture

The threshold value you find will help your Arduino decide when the soil needs water. You might see a number around 500 when the soil is wet, 750 when it's in the ideal range, and 850 when it's dry. These numbers give you a rough idea of the sensor's behavior and can help you set up your threshold values.

Simulation and Diagrams

Before diving into the nitty-gritty of connecting a soil moisture sensor to your Arduino, it's a good idea to simulate the circuit on Thinker CAD. This will give you a chance to test the circuit and see how the sensor value affects the output.

You can adjust the sensor value by tweaking the slider on the Thinker CAD simulation, and the code will show the humidity level using 5 LEDs connected to your Arduino.

To get a better understanding of the circuit, it's also helpful to refer to the Arduino Soil Moisture Sensor Circuit connection diagram.

ThinkerCad Simulation

Credit: youtube.com, Tinkercad Electronic Circuit Simulation Introduction

ThinkerCad Simulation is a great way to test and visualize a circuit before building it.

Clicking the start simulation button allows you to test the circuit in a virtual environment.

You can adjust the sensor value by sliding a slider that appears when clicking on the sensor.

The Tinkercad code for Arduino Soil moisture sensor will show the humidity level using 5 LEDs connected to Arduino.

Circuit Diagram

The circuit diagram for a soil moisture sensor connected to an Arduino is a crucial part of the setup. The sensor is powered using the 5V and GND pins of the Arduino UNO Board.

To connect the sensor, you'll need to attach the analog out pin to the A0 pin of the Arduino UNO board. The ground is common between the LED and the sensor.

The digital interface part of the circuit uses the +5V and Ground from the Arduino to power the sensor module. This is a pretty simple process, as the sensor outputs both analog and digital signals.

The soil moisture sensor circuit diagram shows the internal workings, including an LED connected to digital PIN 6 of the Arduino. This LED will change brightness depending on the soil moisture data sensed by the probe.

Troubleshooting and Tips

Credit: youtube.com, Capacitive Soil Moisture Sensors don't work correctly + Fix for v2.0 v1.2 Arduino ESP32 Raspberry Pi

Soil moisture sensors can be finicky, but with a few simple checks, you can avoid common mistakes and get accurate readings. Always use 5V for optimal performance.

Incorrect voltage can lead to reduced analog resolution, so make sure to use the right power source. Loose jumper wires can cause intermittent readings, so use quality jumper wires and double-check your connections.

A missing common ground can result in erratic behavior, so ensure all components share the Arduino GND. Wrong pin assignments can lead to code errors, so double-check your pin connections match your code.

Here are some key things to check before uploading your code:

  • The power LED on the sensor module lights up when the Arduino is powered
  • All wire connections are secure and match your circuit diagram
  • No short circuits between VCC and GND
  • The sensor probe is clean and dry for initial testing

By following these simple tips, you can troubleshoot common issues and get your soil moisture sensor up and running smoothly.

Documents

If you're looking for more information on building your own soil moisture sensor for Arduino, you'll want to check out the available documents.

The Gravity: Analog Soil Moisture Sensor For Arduino Wiki is a great resource to start with. It provides a comprehensive guide on how to use the sensor.

You can also find the moisture sensor schematic, which is a detailed diagram of the sensor's circuitry.

In addition, the moisture sensor sample code is available for download. This code can be used as a starting point for your own projects.

Frequently Asked Questions

What is the range of soil moisture sensor in Arduino?

The soil moisture sensor's range on an Arduino is theoretically 0 to 1023, but in practice, it's more like 923 due to lost range. For a percentage output, aim for a 100-point difference between wet and dry readings.

Amy Martin

Senior Writer

Amy Martin is a seasoned writer with over a decade of experience in various industries. She has a passion for creativity and enjoys exploring different perspectives on life. Amy's work often inspires readers to think outside the box and embrace new ideas.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.