Home · Academy · Robotics & Coding · Sensors and Actuators · The Humidity Sensor

The Humidity Sensor

Learn to read a soil/air humidity sensor and make a decision using a threshold.

LESSON COMPASS

What will you use this page for?

Core idea

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."

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

The Servo Motor: We will meet a motor that can turn to a specific angle and give our robot controlled movement.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteThe Motion Sensor
ContentStandard lesson · 1,587 words
Last updated

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:

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

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.

  1. Treat the number from the soil moisture sensor as moisture.
  2. Pick two estimated values for dry and wet soil.
  3. Choose a threshold between them.
  4. 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

Review questions

  1. What quantity can a common humidity sensor estimate?
  2. Why does sensor placement matter in a humidity experiment?
  3. How can temperature influence humidity interpretation?
  4. Why should readings be allowed to settle before comparison?
  5. What is a responsible way to present a low-cost sensor result?
  6. Which controls make a comparison between two locations fairer?

Answers

  1. Many classroom sensors estimate relative humidity, the amount of water vapour relative to the maximum possible at that temperature.
  2. Breath, sunlight, walls, airflow and nearby moisture can create a local condition that does not represent the room.
  3. Relative humidity changes with temperature even when the actual amount of water vapour has not changed.
  4. The sensing material and surrounding air need time to reach a more stable condition after movement.
  5. Report it as an estimate with device type, conditions, repeated readings and known limitations rather than as a laboratory-grade fact.
  6. Use the same sensor, height, waiting time, sampling duration, time of day and distance from heat or moisture sources.

Lesson summary

Check your understanding

  1. How does a soil moisture sensor work, and what is the electrical difference between moist and dry soil?
  2. What does "calibration" mean, and why is it needed?
  3. What is the difference between an analog output and a digital output?
  4. What problem appears in a smart-watering system if we set the threshold too high?
  5. In a humidity sensor activity, what is the most important safety rule about water and electronics?

Answers

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Start QuizBack to Sensors and Actuators
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.