One-sentence summary
A motion sensor (PIR) is a part that detects whether a warm object is moving in front of it by sensing infrared light, and tells us either "motion detected" or "no motion."
Why it matters
In the previous lesson, the distance sensor measured how far away an object was. But sometimes we do not need to know the distance; we only want the answer to one question: "did someone arrive?"
Many everyday systems answer exactly that question. A shop door opening by itself, a light turning on when you step into a dark hallway, or a public tap releasing water when you reach out your hand all rely on this idea. In most of these, the working part is a motion sensor.
Once you understand the motion sensor, your robot can react to people around it. You can build systems that greet someone, turn on a light, or sound an alert when a person comes near. This is one of the basic skills that make a robot "aware of its surroundings."
What is a motion sensor?
Every warm object around us gives off a kind of light we cannot see with our eyes. It is called infrared light. The human body is warm, so it constantly gives off infrared. This light is invisible to us, but a special part can detect it.
The most common motion sensor is called a PIR sensor. The letters stand for "Passive Infrared." The word "passive" means the sensor itself does not send out any light; it only listens to the infrared already in the room. A distance sensor sends out a sound wave and waits, but a PIR sensor only watches.
A PIR sensor does not actually measure distance. Its only job is this: is the amount of infrared in front of it changing suddenly? When a warm object (a person, a cat) moves, the infrared pattern the sensor sees changes. The sensor reads this change as "motion."
Two everyday examples:
- Automatic hallway light: The part that turns on the light when you enter an apartment hallway is usually a PIR sensor. As you move, the infrared pattern changes and the light comes on. If nobody stirs for a while, the light turns off by itself.
- Doorbell camera: Smart doorbells that start recording when someone passes the door first use a motion sensor to learn "someone is here," and only then switch on the camera.
Digital output: yes or no?
The distance sensor gave us a number (for example, 30 centimetres). A PIR sensor does not give a number. It reports only one of two states:
- Motion present → the sensor sets its output to 1 (HIGH).
- No motion → the sensor sets its output to 0 (LOW).
Because it gives only two values, this is called a digital output. It works just like a button we saw earlier being pressed or not pressed. This makes a PIR sensor very easy to program: all you have to do is ask, "is the value 1?"
How do we read a motion sensor?
When we connect the sensor to a board such as a micro:bit or Arduino, we read its output pin. If the value is 1, there is motion; if it is 0, there is none.
First, let us write the logic in pseudocode:
Start
Repeat forever
Read the value from the sensor
If the value is 1
say "Motion detected"
Otherwise
say "Quiet"
If we write this idea with micro:bit-style code:
# PIR sensor connected to pin P0
while True:
motion = pin0.read_digital()
if motion == 1:
display.show(Image.YES) # motion present
else:
display.clear() # no motion
The code looks different, but the logic is the same: read the value, and if it is 1, respond.
Example: turn on a light when there is motion
Now let us connect this logic to a real goal. Our goal is to turn on an LED when someone comes near, and turn it off when nobody is there.
Start
Repeat forever
Read the value from the sensor
If the value is 1
turn the LED on
Otherwise
turn the LED off
With Arduino-style code, the same idea looks like this:
// PIR sensor on pin 2, LED on pin 8
void loop() {
int motion = digitalRead(2); // read the sensor
if (motion == 1) {
digitalWrite(8, HIGH); // turn the LED on
} else {
digitalWrite(8, LOW); // turn the LED off
}
}
A real PIR sensor keeps its output at 1 for a few seconds after it sees motion. So the light does not switch off the instant you stop; it stays on for a short while. This is actually useful, because we do not want a hallway light flickering on and off every moment.
Mini practice
Do this practice together with an adult. You will need: a micro:bit or Arduino board, a PIR motion sensor, an LED (or the light already built into the board), connecting wires, and USB power.
- Before connecting the sensor to the board, write down on paper which pin the output leg will go to.
- Upload the "turn on a light when there is motion" code above to the board.
- Stand completely still in front of the sensor and wait for the LED to stay off. After it is switched on, a PIR sensor spends the first 30–60 seconds "learning" the room; wait a little during this time.
- Wave your hand in front of the sensor. Watch the LED turn on.
- Stay still again and watch the LED turn off a few seconds later.
Ask yourself:
- How slowly can you move your hand and still have the sensor see it?
- How many seconds after the motion stops does the sensor turn the light off?
- If you move a cool object (such as a pencil), does the sensor react? Why?
Common mistakes
Expecting an instant response
When a PIR sensor is switched on, it needs a short time to learn the room. During these first 30–60 seconds it may give false signals. Wait a little before you decide the code is not working.
Treating the digital output as a number
A PIR sensor does not give a distance like "3 metres." It gives only 1 or 0. If you try to read a distance from it, you will always be confused; a distance sensor is used for that.
Pointing the sensor at something hot or moving
If you point the sensor at a radiator, a sunny window, or a fan, it will mistake the heat changes for "motion" and keep triggering. Place the sensor facing a calm direction.
Connecting the pins the wrong way
Most PIR sensors have three legs: plus (+), minus (−), and output (OUT). If you mix them up, the sensor will not work at all. Check the labels on the legs before connecting.
Safety note
- Use only low-voltage, educational power sources in this circuit: USB, batteries, or boards such as the micro:bit or Arduino. Never work with wall sockets, mains electricity, or household wiring.
- Have an adult with you while you work with the sensor, LED, and wires.
- Connect the polarity (+ and −) correctly, and never let the plus and minus wires touch each other, which causes a short circuit. A short circuit can heat up the wires and the board.
- Privacy note: Do not use a motion sensor to watch other people secretly. It is not right to place a motion sensor in someone's room, on their bed, or anywhere they do not know about. Use these sensors in your own projects, in places that are clearly known and allowed.
- If a part heats up, gives off a strange smell, or you see smoke, disconnect the circuit at once and tell an adult.
Lesson summary
- A motion sensor (PIR) notices the movement of warm objects in front of it by sensing infrared light.
- Being "passive" means the sensor does not send out any light itself; it only listens to the infrared already in the room.
- A PIR sensor does not give a distance; it gives only a digital output: 1 (HIGH) if there is motion, 0 (LOW) if there is none.
- In code, the only job is to read the value and respond (for example, turn on a light) when it is 1.
- These sensors are used only with low-voltage sources, under adult supervision, and never to watch anyone secretly.
Check questions
- What does a PIR sensor detect, and why is it called "passive"?
- What is the main difference between the output of a distance sensor and the output of a PIR sensor?
- What does "digital output" mean, and which two values does a PIR sensor give?
- If you move a cool object (such as a pencil) in front of the sensor, why does it usually not react?
- What privacy rule must we follow when using a motion sensor?
Answers
- A PIR sensor detects changes in the infrared light given off by warm objects (people, animals) in front of it. It is called "passive" because it does not send out any light of its own; it only listens to the infrared already in the room.
- A distance sensor gives a number (a distance); a PIR sensor gives no number, only the information of motion or no motion (1 or 0).
- A digital output is one that takes only one of two values. A PIR sensor gives 1 (HIGH) when there is motion and 0 (LOW) when there is none.
- A PIR sensor looks for changes in heat (infrared). A cool object like a pencil is at the same temperature as its surroundings, so it does not clearly change the infrared pattern, and the sensor usually does not react.
- We must not use a motion sensor to watch other people secretly; we use it only in places that are clearly known and allowed, in our own projects.
Source and verification note
For “The Motion Sensor”, verification focuses on whether the relationship between What is a motion sensor? and How do we read a motion 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 Humidity Sensor: You will meet a sensor that measures the moisture in the air or soil, and learn to build a circuit that understands when a plant needs watering.