Guide for Soil Moisture Sensor YL-69 or HL-69 with Arduino

The soil moisture sensor or the hygrometer is usually used to detect the humidity of the soil. So, it is perfect to build an automatic watering system or to monitor the soil moisture of your plants.

The sensor is set up by two pieces: the electronic board (at the right), and the probe with two pads, that detects the water content (at the left).

moisture sensor

The sensor has a built-in potentiometer for sensitivity adjustment of the digital output (D0), a power LED and a digital output LED, as you can see in the following figure.

labeled sensor

You can also read this guide for the Rain Sensor FC-37 or YL-83 with Arduino.

How does it work?

The voltage that the sensor outputs changes accordingly to the water content in the soil.

When the soil is:

  • Wet: the output voltage decreases
  • Dry: the output voltage increases

soil moisture sensor

The output can be a digital signal (D0) LOW or HIGH, depending on the water content. If the soil humidity exceeds a certain predefined threshold value, the modules outputs LOW, otherwise it outputs HIGH. The threshold value for the digital signal can be adjusted using the potentiometer.

The output can be a analog signal and so you’ll get a value between 0 and 1023.

Example: Soil Moisture Sensor with the Arduino

This is a simple example for you to understand how you can use the soil moisture sensor in your projects with Arduino.

In this example, you’ll read the analog sensor output values using the Arduino and print those readings in the Arduino IDE serial monitor.

Parts required

For this example, you’ll need the following components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Pin Wiring

Wiring your sensor to the Arduino is pretty simple:

Pin Wiring to Arduino Uno
A0 Analog Pins
D0 Digital Pins
GND GND
VCC 5V

Schematics

To complete the project, follow these schematics:

moisture sensor fritzing

Code

Upload the following sketch to your Arduino board:

/*
 
 All the resources for this project:
 https://randomnerdtutorials.com/
 
*/

int rainPin = A0;
int greenLED = 6;
int redLED = 7;
// you can adjust the threshold value
int thresholdValue = 800;

void setup(){
  pinMode(rainPin, INPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  digitalWrite(greenLED, LOW);
  digitalWrite(redLED, LOW);
  Serial.begin(9600);
}

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(rainPin);
  Serial.print(sensorValue);
  if(sensorValue < thresholdValue){
    Serial.println(" - Doesn't need watering");
    digitalWrite(redLED, LOW);
    digitalWrite(greenLED, HIGH);
  }
  else {
    Serial.println(" - Time to water your plant");
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
  }
  delay(500);
}

View raw code

Open the Arduino IDE serial monitor to see the values. Then, try your sensor in a wet and in a dry soil and see what happens.

When the analog value goes above a certain threshold, a red LED will turn on (indicates that the plant needs watering), and when the value goes below a certain threshold, a green LED will turn on (indicates that the plant is ok).

Wrapping Up

The moisture sensor allows to monitor the water content in the soil. This is useful if you want to build an automatic watering system. You can also use it to just monitor your plants soil moisture.

I hope you’ve found this guide useful. Have you used this sensor in any of your projects?

Let me know by leaving a comment below. Share this post with a friend that also likes electronics.

Thanks for reading,

Rui



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

39 thoughts on “Guide for Soil Moisture Sensor YL-69 or HL-69 with Arduino”

  1. I found that if you have the moisture sensors powered up all the time the probe will eventually dissolve due to electrolysis. Better to switch on via a relay, wait, then take a reading.

    Reply
    • Phil, I am new to this.
      Can we add 5Volt mini water pump?
      When dry – it turns pump on UNTIL sensor detects water then it turns off water pump?

      If so, is there a video to show EVERY STEP building it and how to add code to Arduino?
      Also list of every item needed and website to order it?

      I am DISABLED BUT CAN FOLLOW GOOD VIDEO OF EACH STEP.

      OR I AM WILLING TO PAY SOMEONE TO SET UP SOME FOR MY NEEDS.
      EMAIL: altonddavisjr(at)yahoo com.
      Most Humble Appreciation
      Alton

      Reply
  2. Rui,
    I tried those type of sensors and after a while (1-2 weeks) they was damage due corrosion problems. Today I’m trying the capacitive ones.
    Regards,
    AH

    Reply
  3. Very nice project. It is very useful. One suggestion from me. You can also use a three pin soil moisture sensor which will not require any extra circuit board. It will minimize the code too.

    Reply
  4. Hi,
    We tested the tutorial. It works flawlessly even with beginners as we are.
    It is a very good first step for beginners. The total cost is less than 5 € !!!
    Thanks a lot
    Jean Pierre

    Reply
  5. If i use the same set up but, with Raspberry pi3 model B….do i need to use adc mcp3008??or it can be done without it??
    regards

    Reply
  6. Great tutorial, I’m using the exact same moisture sensor. For a school project I’m working on I’ll be required to present schematics of my project. I was wondering, where did you get the Fritzing schematics for the moisture sensor?

    Reply
  7. We are beginners and therefore like a lot simple projects to learn our way. Sine the last comment, we integratded in the project a peristaltic pump wich deliver 50 ml water to an indoor plant when the analog reading is less than 450. Then we added a RTC DS 1307 to control daily at the same time if the plant need water. The total cost is less than 20 usd. We are now learning the home automation tutorial on the NodeCmu Lua ESP8266-12E. to replace the clock and the arduino (uno or nano) card while making our project part of IoT for a cost of around 15 usd per plant.
    Thanks a lot for the very helpfull tutorials (Home Automation)
    Jean Pierre

    Reply
    • Hi.
      You have to do that yourself.
      You need to have known humidity values and then check what analog values your Arduino is reading.
      Do this for three humidity values and build a calibration curve.
      Hope this helps.

      Reply
  8. Hi,

    I’m using the YL-69 and the same arduino, about the reading values, is there a form to send those value to a smarthphone? I had read that using a bluetooth module (HC-05) would help, and for the same case, what app would you recommend?

    Reply
      • Hello,

        Thank you for the first response. I have another question, do you know any error range for the sensor YL-69, i mean when is reading the moisture from the plant.

        Reply
  9. Sir, I m darshan jain working in karnataka enginnering research station, Mandya, karnataka, India. i m doing project on soil moisture sensur. the sensor is displaying only two values i.e, 33 and 1023. however, the sensor is not showing the intermittent values. Please help me out in this issue.

    Reply
    • Hi.
      Please confirm that you’re using the analog output pin (A0) of the sensor and you’re using the analogRead() function to read the values from the sensor.
      Additionally, the sensor might not be working properly. Are you using the same model as shown in this tutorial, or are you using a different one?

      Reply
  10. i tried v hard but not able to find the data sheet for soil moisture seensor if any one has link to the data sheet please help me

    Reply
  11. Hey, thinking of desoldering the potentiometer. Could i do that? Would the circuit still work? would i have to solder a wire to reconnect the pins. Any guidance would be appreciated.

    Reply
    • Hi Jack.
      I’ve never tried doing that. You need to check the connections with the multimeter.
      The potentiometer is used to set up the threshold value for the digital output.
      If you just use the analog output, maybe you won’t need the potentiometer or that connection. But you need to check with a multimeter and see if that doesn’t affect the analog output.
      Regards,
      Sara

      Reply
  12. Hi 🙂 I want to do the calibration curve but I do not know if it is a straight line or not. I can take 2 measurements, is that enough?

    Reply
    • Hi.
      Two measurements won’t give you an accurate calibration curve.
      I would recommend that you take more measurements. However, the number of points will depend on the desired accuracy.
      Regards,
      Sara

      Reply
    • Hi Terry.
      That module has a potentiometer that sets a threshold. The module will output a HIGH signal (1) if the readings are above the threshold set with the potentiometer. It will output 0 if the readings are below the threshold. However, you can’t “see” what is the threshold. You have to rotate the potentiometer and experiment with several types of soil humidity values to check the best threshold.
      Alternatively, you can use the analog readings and set your own threshold value on the code.
      I hope this helps.
      Regards,
      Sara

      Reply
  13. As the analog output of the Soil Moisture Sensor YL-69 or HL-69 is directly connected to the probe and a 10k Ohm pull-up resistor (i.e. to Vcc, see e.g. medium.com/@chirag.parmar/know-your-sensor-yl38-soil-hygrometer-fceca860faac), it should be possible to replace the whole YL-69 or HL-69 by just a 10k resistor. Have you tried that? To avoid corrosion I intend to use stainless steel screws as electrodes.

    Reply
  14. Hi Sara,

    I am a newbie on the Arduino stuff. I have seen that for other sensors you are required to upload a library; I did not see any link anywhere to a library for this sensor, is it not required for this one?

    Reply
    • Hi.
      You don’t need a library because this is an analog sensor.
      So, you just need to read the data pin using the analogRead() function as shown in the code.
      Regards,
      Sara

      Reply
  15. Hey , the operating range of the sensor is 3.3 V to 5V

    i wanted to know if there will be any changes in the values if the input voltages differ

    Reply

Leave a Reply to Jack Cancel reply

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.