Know Your Sensor — YL38 Soil Hygrometer
By: Chirag Parmar
Tags: Moisture, Plants, Arduino, Sensor, IoT

Wikipedia defines Hygrometers as, instruments used for measuring the amount of humidity and water vapor in the atmosphere, in the soil, or in confined spaces. So, it pretty much follows that the YL38 measures the moisture content in the soil. Does it though? Let’s find out.

Please read through all the links attached in between sentences to gain a broader understanding of the subject.

Understanding the Concept

To measure the moisture content of soil we must first quantify it. Most amateurs quantify moisture content in terms of percentage but fail to provide information about the relative quantity. Moisture content represented in percentage could be relative to the weight of soil without any water, the weight of the soil including water or volume of soil. The point is that such representations of moisture content are ambiguous and far from universally accepted standards.

The center for agriculture, food and the environment at the University of Massachusetts, Amherst defines three ways by which soil moisture can be quantified.

Soil water content is a measurement of the amount of water in a known amount of soil; it can be expressed as % water by weight or volume of soil, or inches of water per foot of soil. Soil water potential or soil moisture tension is a measurement of how tightly water clings to the soil and is expressed in units of pressure called bars (one bar is equal in strength to the pressure of one atmosphere). Generally, the drier the soil, the greater the soil water potential and the harder a plant must work to draw water from the soil. Plant available water (PAW) is the amount of water in the soil between the soil’s field capacity (soil water content after gravity has removed any freely draining, excess water) and its permanent wilting point (soil water content at which most plants can not recover from wilting). It is expressed as inches of available water per foot of soil.

Shockingly, the YL38 measures none of the above, not directly. So what does the YL38 measure? The sensor is designed to measure the electrical resistance of soil between the two conductors of the probe. Remember, the soil is a mixture of various things some of them happen to be salts and minerals. On addition of water, these salts and minerals act as electrolytes capable of conducting electricity. Therefore, by adding water we increase the conductivity or reduce the resistivity of soil.

To accurately convert the electrical resistance of the soil to its moisture content an initial calibration procedure must be completed. Unfortunately, there is no direct formula or “plug and play” method to do that. For example, assume that we want to measure the soil moisture tension. In this case, we must simultaneously record the soil moisture tension(using an external device)and measure the electrical resistance. The obtained data would then be used to plot a curve, known as the calibration curve, which maps electrical resistance to soil moisture tension. Since this curve directly maps the electrical resistance to soil moisture tension, the procedure must be repeated if any of the parameters defining soil moisture tension are changed. For example, if you change the soil or add more fertilizer to the soil you will have to recalibrate.

Typical calibration curves for two commercial gypsum blocks. The vertical axis shows the resistance in ohms as either measured by the meter and the horizontal axis shows the tension at which water is held in the soil. For example, if the resistance is 10,000 ohms for an SEC (Soil Moisture Equipment Corporation) block, the soil water tension is 345 kPa ( or 3.45 bars). <source>

This article illustrates an experiment to plot the calibration curve for mapping electrical resistance to moisture % by weight (“Soil Water Content”).

The method illustrated in the above article is a clever trick of measuring the soil water content by weight without even measuring the weight of the soil(Measuring the weight of the soil is not possible when your plant is not in a pot). The method exploits the porous nature of gypsum to establish an equilibrium of moisture level inside and outside the sensor. Therefore, measurements taken inside the sensor can be regarded as the moisture level outside the sensor(in other words, the moisture content of soil). Interestingly, it is independent of the type and amount of soil used because the calibration curve is derived from the weight of the sensor and not the soil.

Unfortunately, this cannot be applied to YL38 because it does not confine any volume or weight when buried in soil. Therefore, YL38 cannot be used for accurate or quantifiable measurements of soil water content.

So, why even bother using YL38? The YL38 might not accurately measure the moisture content but it is still dependent on it. In other words, the voltage on the analog output of the sensor would change if the moisture content changes. These changes can be mapped to a roughly defined moisture content scale (slightly wet, wet, completely dry etc). Another reason for using the YL38 is its digital output(more on that in the following sections). Furthermore, the YL38 cannot be used for soil water content readings but it can be used for measuring soil moisture tension but you would require an external device for plotting the calibration curve.

Understanding the Design

The sensor has two components, a probe, and a signal processing circuit. The signal processing circuit contains a voltage comparator (LM393), a sensitivity knob, power indicating LED, moisture indicating LED and supporting resistor and capacitor circuits for the comparator. On the other hand, the probe, effectively, is just a pair of metal conductors buried in soil.

Sidenote: The designers were smart enough to break the sensor into two parts. Considering an extended period of use the probes would wear out much faster than the circuit and since they are separate you would only have to change the probes.

Prerequisites — Inputs, Outputs, and Resistance Measurement Techniques

Before we move on to analyze the sensor let’s brush up our knowledge on inputs and outputs, here. The probe has one of its terminals connected to GND so it is a sinking input. Similarly, The LM393 is an open-collector voltage comparator i.e. it either pulls down the output to GND or it leaves it floating. Hence it is a sinking output. Now that we have defined the inputs and outputs let’s dive into measuring resistances.

Never betray the law.

It is known that all electronic devices work on voltages or currents. This implies that an unknown resistance cannot be measured directly and has to be converted to a voltage or current signal. There are two well-known techniques to measure resistances, voltage dividers and constant current sources(this forum briefly explains the two methods). The underlying theory of both techniques is sourced from Ohm’s Law(if you are dealing with resistances you can never overlook ohms law). Each method has its own advantages and disadvantages in terms of power loss and design complexity but the YL38 uses the voltage divider method.

Circuit Diagram

This circuit diagram is reverse engineered

Let’s start our analysis with the analog circuit. The “reverse engineered” circuit diagram shows us that one pin of the probe is directly connected to the pin A0 and the other pin is connected to GND. The AO pin is pulled up through a 10K resistor and pulled down through a 10nF capacitor(the purpose of the capacitor is unknown). The 10K resistor serves the purpose of creating a voltage divider setup so that a micro-controller can be used to read the voltages and calculate the resistance between the probes. Once we have the resistance values we can map them to a rough scale of moisture content(slightly wet, wet, dry etc as mentioned earlier).

The digital circuit uses a voltage comparator(LM393, the triangle in the circuit diagram) to convert the analog output(A0) into a digital output. To know how this is achieved requires basic knowledge of voltage comparators (read this before continuing). Basically, there are two things you must know.

The inverting input(-) is, in most cases, used to set the reference voltage. Whenever the non-inverting input(+) voltage exceeds the inverting input(-) voltage the output is pulled up to VCC(HIGH) otherwise it is pulled down to GND(LOW).

From the circuit diagram, we can interpret that the inverting input(-) is connected to the variable pin of a potentiometer. Simply put, a potentiometer is a voltage divider with a knob to change resistances. So, by changing the position of the knob you can change the output voltage of the divider and thereby, indirectly, changing the reference voltage(sensitivity) of the comparator. Although it is important to note that the output voltage of the potentiometer can vary only in the range of VCC to GND.

The analog output(A0) is directly connected to the non-inverting(+) input of the comparator. So, whenever the voltage of the analog output(A0) goes higher than the reference voltage set by the potentiometer the output is pulled up to VCC(HIGH), otherwise, it is pulled down to GND(LOW). But, the LM393 is an open-collector voltage comparator (sinking output) it cannot pull-up the output voltage to VCC by itself. Hence, the second 10K resistor is used as a pull-up resistor for the output.

Apart from all the components discussed above, the circuit also contains two LEDs one connected to the to the power rail to indicate power and the other connected to the output of LM393 to indicate above-threshold moisture content. Both LEDs draw current through current limiting resistors with resistances of 1K each.

The last component in the circuit is a 10nF ceramic capacitor connected across the power rails. This capacitor is generally referred to as a “de-spiking” capacitor because it removes undesirable spikes on power rails. Capacitors follow a simple rule, “Block all DC and let the AC pass”. Therefore, a capacitor across the power rails will let the AC(spikes, ripples, jitter etc.) pass to the ground and block all DC preventing a “massive” short circuit.

In a real-life scenario, when would the analog voltage go above the threshold voltage? Consider this example.

Assume the potentiometer’s knob is in the middle resulting in a reference voltage of VCC/2. When the plant is watered the soil resistance is very low and the non-inverting input is pulled to GND(LOW) because current always chooses a low resistance path. In this case, the analog voltage would be lesser than the reference voltage( GND < VCC/2) and the output would be pulled to GND(LOW).

Watered soil → GND(LOW) → LED glows

The soil between the probes acts as a resistor after all.

When the soil is dry the soil resistance is at the maximum(~100K to 1M). In this case, the soil resistance is very very big compared to 10K pull-up due to this the non-inverting input is pulled up very close to VCC(HIGH). Since the analog voltage is greater than the reference voltage(VCC > VCC/2) the output is also pulled up to VCC(HIGH).

Dried soil → VCC(HIGH) → LED doesn’t glow

Under an extremely rare case when the inverting input and non-inverting input have the same voltage(to the last decimal point the comparator can read) the voltage comparator will lose stability and start oscillating. The article referred above for “comparator basics” explains this concept under the topic of hysteresis. The article also suggests a method for preventing this problem.

Usage

After understanding the concept of soil moisture measurement and working of the YL38 sensor it should be easy to decide when to use/not use this sensor. If still confused, let’s take a bird’s eye view of the entire design.

The sensor, undoubtedly, has heavier pre-processing circuitry for digital output compared to analog output. It uses a voltage comparator to set a threshold resistance(or voltage) and provide a clean digital output when the analog resistance(or voltage) is higher than the threshold. The analog output circuit, entirely, is made up of just two conductors, one pull-up resistor and one pull-down capacitor.

Therefore, a clean and strong digital output makes the sensor ideal for control purposes(turn a light on, set a buzzer on, open curtains etc). In such cases, you wouldn’t require a microcontroller to read the digital input and present the same digital output on another pin. You could directly wire the digital output to a control circuit. The digital output, for example, can directly drive a relay switch which could then be used to control electrical appliances.

Although if you are using the sensor for recording data on a server(IoT) you would prefer analog readings over digital. The sensor is just overkill for this use case. You would be wasting money and battery power on the extra comparator. Furthermore, the probe design does not provide accurate measurements of soil moisture content(as explained under “Understanding the Concept”).

Sidenote: DIY: cheap soil moisture sensor, in case you want to make one of your own.

Side Effects of using the sensor

If you are sticking an electronic component in your potted plant then you want to be sure that it has minimal side effects. So, let’s analyze the possible effects of the sensor on your plant’s growth.

As we discussed earlier, the sensor exploits the presence of salts and minerals in the soil to measure its moisture content. Under the subject of chemistry, this process is known as electrolysis and can result in increased soil temperature and residual waste(corrosion). Suat Irmak from the University of Nebraska — Lincoln shows us through his research the negative impacts of increased soil temperature on a plant's growth.

On the other hand, the residual waste of an electrolytic reaction would affect the pH value of the soil. Considering a plant’s growth, the pH value of the soil plays as important a role as the moisture content of the soil(read this article to know more). Therefore, if not for the increased soil temperature the residual waste might finish the job.

To conclude, the YL38 soil hygrometer would not be a personal preference considering its effects on the plant’s health and its inability to provide accurate measurements of moisture content.

Hope this article helps you plot the pros and cons of the sensor for your desired application. And if you liked this article hit the clap button.