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).
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.
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
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:
- 1x YL-69 moisture sensorÂ
- Arduino UNO – read Best Arduino Starter Kits
- 1x Breadboard
- 2x 220 Ohm Resistors
- 1x Red LED
- 1x Green LED
- Jumper wires
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:
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);
}
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
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.
Hi Phil.
Thanks for your suggestion. 🙂
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
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
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.
Thanks for your suggestion.
Regards 🙂
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
Awesome! I’m glad it worked Jean.
Thanks for reading,
Rui
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
You need the ADC, because this sensor only has an Analog out pin
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?
I don’t remember the exact link… But search for the sensor name + fritzing part
Thanks, will do!
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
Hei Rui,
Can i get the conversion factor from volt to moisture, because i want to get the output as moisture forms
Regards
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.
Hello,Did you have the C source code about STC89C52,if you have ,can you give me to learn ,I will be great appreciate。
Hi.
Unfortunately, we don’t have any content on that subject.
I tested the tutorial. It works flawlessly, I m going to do a 2-week test using a mini Arduino.
Hi.
We’re glad it works 🙂
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?
Hi Alexis.
You can create your own app using MIT App Inventor to communicate with your Arduino.
You can send the values to your smartphone via Bluetooth using the HC-05 bluetooth module.
You can read our guide on Getting Started with MIT App Inventor 2 and Arduino.
Alternatively, there’s an app called Blynk that is an app for IOT and works perfectly with Arduino.
Thanks.
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.
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.
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?
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
Hi.
I don’t have the datasheet for this specific module.
Regards,
Sara 🙂
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.
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
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?
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
Any experience with the digital out pin. I can not deduce the logic of it’s values (1, 0)?
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
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.
Hi.
I haven’t tried it. Do you?
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?
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
Thank you Sara.
I appreciate your kind help and advice
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