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

The Motion Sensor

Learn to detect movement with a PIR motion sensor and respond to it.

LESSON COMPASS

What will you use this page for?

Core idea

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

Evidence to produce

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

Control trap

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…

Next connection

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.

Module sources: Python Tutorial · Arduino Learn

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

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:

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:

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.

  1. Before connecting the sensor to the board, write down on paper which pin the output leg will go to.
  2. Upload the "turn on a light when there is motion" code above to the board.
  3. 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.
  4. Wave your hand in front of the sensor. Watch the LED turn on.
  5. Stay still again and watch the LED turn off a few seconds later.

Ask yourself:

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

Lesson summary

Check questions

  1. What does a PIR sensor detect, and why is it called "passive"?
  2. What is the main difference between the output of a distance sensor and the output of a PIR sensor?
  3. What does "digital output" mean, and which two values does a PIR sensor give?
  4. If you move a cool object (such as a pencil) in front of the sensor, why does it usually not react?
  5. What privacy rule must we follow when using a motion sensor?

Answers

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

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.