One-sentence summary
A humidity sensor measures how moist the soil or air is; by reading that value and comparing it to a threshold, we can make smart decisions such as "water the plant when the soil is dry."
Why it matters
Think about a potted plant. If you water it every day without checking the soil, the roots may rot; if you never water it, the plant dries out. A person guesses by looking at the soil and asking, "Is it dry yet?" A robot, though, wants to decide by measuring rather than guessing.
This is exactly where a humidity sensor helps. It turns the moisture in the soil into a number. Our program can then say, "When this number drops below a certain limit, water the plant." That idea is the foundation of smart irrigation systems, greenhouses and even garden robots.
Measuring moisture is not only for plants. Sensors that measure air humidity are used in greenhouses, museums, server rooms and weather forecasting. Learning to measure is an important part of helping a robot "feel" its surroundings.
What a humidity sensor is and how it works
Humidity is the amount of water in a place. A soil moisture sensor measures the water inside the soil, while an air humidity sensor measures the water vapour in the air.
The soil moisture sensor
A soil moisture sensor has two metal ends (probes) that go into the soil. Water conducts electricity. When the soil is moist, electricity passes between the two ends more easily; when the soil is dry, it becomes harder.
The sensor turns this difference into an analog value. That means it does not give a simple "yes/no" but a range:
- High moisture → a value near one end
- Low moisture → a value near the other end
Our board (a micro:bit or Arduino) reads this value as a number, for example between 0 and 1023. The exact meaning of the number depends on the sensor, so before real use we calibrate it: we check which numbers it gives in dry soil and in wet soil.
The air humidity sensor
An air humidity sensor (such as a DHT11) measures the water vapour in the air and usually gives it as a percentage. 30% means dry air, while 80% means quite humid air. This kind of sensor often measures humidity and temperature together.
Two everyday examples
- Potted plant: A soil moisture sensor is buried in the soil. As the value drops, we learn that the soil is drying out.
- Greenhouse: If an air humidity sensor sees that the air is very dry, the system can turn on a humidifier or the water supply.
Reading the humidity value
Reading the sensor really means receiving a number. The pseudocode below reads the soil moisture over and over and shows it on the screen.
Start
Repeat forever
moisture = read the value from the soil
display moisture
wait 1 second
In a micro:bit-style program, the same idea looks like this:
# Soil moisture sensor connected to pin P0
while True:
moisture = pin0.read_analog() # a number from 0 to 1023
display.scroll(moisture) # scroll the value on screen
sleep(1000) # wait 1 second
There is no decision here yet; we are only measuring. Observing how the number changes first is a good habit. Wet your finger and touch the probes, and watch the number change.
Making a decision with a threshold: the smart-watering idea
Measuring alone is not enough; we need to decide. For that we set a threshold (a limit value). We say, "If the moisture drops below this threshold, the soil is dry."
Suppose that after calibration we saw this: wet soil gives about 700 and dry soil gives about 300. Then we can put the threshold somewhere in between, for example 400.
Start
Repeat forever
moisture = read the value from the soil
If moisture < 400
display "Soil is dry - watering needed"
Otherwise
display "Soil is moist - standing by"
wait 10 seconds
A real irrigation system could turn on a water pump here. But be careful: a pump is also a motor. Motors are not powered directly from the board; they need a separate power source and a driver (a relay or a motor driver). We will look at motors in more detail in the coming modules. In this lesson our goal is to make the decision correctly.
Why the threshold must be chosen carefully
If you set the threshold too high, the system calls the soil "dry" while it is still moist and waters it needlessly. If you set it too low, the plant may go thirsty. That is why measuring and observing for a few days is the most reliable way to choose a good threshold.
Mini practice
Design a "smart pot reminder." Even without real hardware, you can do this with pseudocode or on paper.
- Treat the number from the soil moisture sensor as
moisture. - Pick two estimated values for dry and wet soil.
- Choose a threshold between them.
- Write this rule: if the moisture is below the threshold, show a yellow warning instead of a green light; if it is above, say "all is well."
Example solution:
dry_value = 300
wet_value = 700
threshold = 450
moisture = read the sensor
If moisture < threshold
show the message "Please water me!"
Otherwise
show the message "Thanks, I am fine."
Questions: What happens if you set the threshold to 600? Does the reading change if you push the sensor deeper? Write your own guess, then test it with a real sensor if you can.
Common mistakes
Setting a threshold without calibrating
Every soil and every sensor is different. Choosing a threshold before measuring the values in dry and wet soil usually leads to wrong decisions.
Confusing analog and digital reading
A moisture sensor often has two outputs: analog (a range) and digital (only 0/1, based on its own adjustment screw). If you want the range value, you must use the analog output and the read analog command.
Reading too often
Soil moisture does not change quickly. Reading the value many times per second and trying to water immediately is unnecessary. Leave enough time between measurements.
Mixing water and electronics
Only the metal probes of the sensor go into the soil. The circuit board, the wires and the battery must stay dry. Pouring water directly onto electronic parts causes damage.
Safety note
- Use only low-voltage, educational sources: a battery, USB, a micro:bit or an Arduino. Never work with mains electricity (a wall socket).
- Keep water and electronics apart. Only the sensor's probes touch the soil or water; the board and wires must stay dry. Do not touch the circuit with wet hands.
- If you add a pump or motor for watering, do not power it directly from the board; use a separate power source and a driver. Keep your fingers and hair away from moving parts.
- The sensor's metal probes can rust in the soil over time; dry them after use. Do the activity together with an adult.
Review questions
- What quantity can a common humidity sensor estimate?
- Why does sensor placement matter in a humidity experiment?
- How can temperature influence humidity interpretation?
- Why should readings be allowed to settle before comparison?
- What is a responsible way to present a low-cost sensor result?
- Which controls make a comparison between two locations fairer?
Answers
- Many classroom sensors estimate relative humidity, the amount of water vapour relative to the maximum possible at that temperature.
- Breath, sunlight, walls, airflow and nearby moisture can create a local condition that does not represent the room.
- Relative humidity changes with temperature even when the actual amount of water vapour has not changed.
- The sensing material and surrounding air need time to reach a more stable condition after movement.
- Report it as an estimate with device type, conditions, repeated readings and known limitations rather than as a laboratory-grade fact.
- Use the same sensor, height, waiting time, sampling duration, time of day and distance from heat or moisture sources.
Lesson summary
- A humidity sensor measures the amount of water in soil or air and turns it into a readable number.
- A soil moisture sensor works on the idea that water conducts electricity; moist soil conducts better.
- The meaning of the reading depends on the sensor, so we calibrate it in dry and wet soil.
- We can set a threshold and make the decision "water when moisture is below the threshold," which is the basis of smart irrigation.
- Water and electronics must be kept apart, and only low-voltage sources should be used.
Check your understanding
- How does a soil moisture sensor work, and what is the electrical difference between moist and dry soil?
- What does "calibration" mean, and why is it needed?
- What is the difference between an analog output and a digital output?
- What problem appears in a smart-watering system if we set the threshold too high?
- In a humidity sensor activity, what is the most important safety rule about water and electronics?
Answers
- A soil moisture sensor has two probes. Because water conducts electricity, current passes between the probes more easily in moist soil, so the sensor gives a high moisture value; in dry soil the passage is harder and the value drops.
- Calibration means measuring beforehand which numbers the sensor gives in dry and wet soil. Because every soil and sensor is different, choosing a correct threshold is only possible with these measurements.
- The analog output gives a range (for example 0–1023) and shows how much moisture there is. The digital output gives only 0 or 1 and only tells you whether the value is below or above a threshold.
- If the threshold is too high, the system treats the soil as "dry" while it is still moist and waters it needlessly. This can harm the plant's roots and waste water.
- Water and electronics must be kept apart: only the sensor's metal probes go into the soil or water, while the board, wires and battery stay dry. In addition, only low-voltage sources should be used.
Source and verification note
For “The Humidity Sensor”, verification focuses on whether the relationship between What a humidity sensor is and how it works and The air humidity sensor remains consistent across examples. Sensor readings can change with the model, supply voltage and environment. Thresholds in the lessons are therefore examples; a real project should use a measurement table and calibration.
Next lesson
The Servo Motor: We will meet a motor that can turn to a specific angle and give our robot controlled movement.