Ugenz Academy/IoT Tutorial

Motion And Distance Sensors

11 min read2,179 words

Reading progress

Keep learning

Motion and Distance Sensors

Motion and distance sensors are used to detect movement, presence, proximity, or the distance between an object and the sensor.

They are extremely common in:

  • Robotics
  • Smart homes
  • Security systems
  • Automatic doors
  • Parking systems
  • Obstacle-avoiding robots
  • Industrial automation
  • IoT
  • Drones
  • People counting
  • Navigation systems

A basic system works like:

Object / Person ↓ Motion / Distance Sensor ↓ Electrical / Digital Signal ↓ Arduino / ESP32 ↓ Processing ↓ Action

1. Motion vs Distance

These two concepts are related but different.

Motion Detection

Answers:

"Is something moving?"

Example:

Person enters room ↓ PIR detects movement ↓ Light ON

Distance Measurement

Answers:

"How far away is the object?"

Example:

Object ↓ Ultrasonic Sensor ↓ Distance = 25 cm

Therefore:

Motion Sensor → Detects movement/presence Distance Sensor → Measures distance

Some sensors can provide information useful for both.


2. PIR Sensor

PIR = Passive Infrared

A PIR sensor detects changes in infrared radiation, commonly caused by moving warm objects such as people.

A typical PIR module contains:

PIR Sensor ┌──────────┐ │ Fresnel │ │ Lens │ └──────────┘

It detects changes in infrared radiation entering its field of view.


3. How PIR Works

Humans emit infrared radiation because of their body temperature.

When a person moves across the sensor's detection zones:

Person moves ↓ IR pattern changes ↓ PIR sensing element responds ↓ Electronics process signal ↓ Digital output changes

Typical module behavior:

No motion → LOW Motion → HIGH

The exact behavior depends on the module and its configuration.


4. PIR Sensor Pins

A common PIR module has:

VCC GND OUT

Typical connection to ESP32:

PIR ESP32 VCC ────────── 3.3V GND ────────── GND OUT ────────── GPIO

Some PIR modules are designed for different supply voltages, so check the specific module.


5. PIR with ESP32

Example:

#define PIR_PIN 27 #define LED_PIN 2 void setup() { Serial.begin(115200); pinMode(PIR_PIN, INPUT); pinMode(LED_PIN, OUTPUT); } void loop() { int motion = digitalRead(PIR_PIN); if (motion == HIGH) { Serial.println("Motion Detected!"); digitalWrite(LED_PIN, HIGH); } else { Serial.println("No Motion"); digitalWrite(LED_PIN, LOW); } delay(200); }

6. PIR Applications

PIR sensors are commonly used for:

Automatic Lights

Person enters ↓ PIR ↓ ESP32 ↓ Relay ↓ Light ON

Security Systems

Motion ↓ PIR ↓ ESP32 ↓ Alarm

Smart Home

Lights, fans, and other devices can be controlled based on occupancy.


7. Limitations of PIR

PIR sensors do not measure distance.

They primarily detect changes in infrared radiation.

They can also have limitations such as:

  • Cannot reliably identify exact object distance
  • Can be affected by environmental temperature
  • Stationary people may eventually stop triggering motion detection
  • Detection depends on sensor placement and field of view

Therefore:

PIR ≠ Distance Sensor

8. Ultrasonic Sensor

An ultrasonic sensor measures distance using sound waves above the human hearing range.

A popular beginner sensor is:

HC-SR04

It typically has:

VCC TRIG ECHO GND

9. How Ultrasonic Distance Measurement Works

The sensor sends an ultrasonic pulse.

Sensor ↓ Ultrasonic wave ↓ Object ↓ Reflection ↓ Sensor receives echo

The sensor measures the time between sending the pulse and receiving the echo.

Then:

Distance ∝ Time of Flight

More specifically:

Distance = (Speed of Sound × Time) / 2

We divide by 2 because the sound travels:

Sensor → Object Object → Sensor

10. HC-SR04

The HC-SR04 is widely used for educational projects.

Typical measurement range is approximately:

2 cm → 400 cm

Actual performance depends on conditions and the particular module.


11. HC-SR04 Pins

HC-SR04 VCC TRIG ECHO GND

Typical Arduino connection:

HC-SR04 Arduino VCC ──────── 5V GND ──────── GND TRIG ──────── Digital Pin ECHO ──────── Digital Pin

12. Important HC-SR04 + ESP32 Warning

This is very important.

Many HC-SR04 modules operate at 5 V, and their ECHO output can be around 5 V.

ESP32 GPIOs are generally 3.3 V logic.

Therefore:

HC-SR04 ECHO ↓ Voltage Divider / Level Shifter ↓ ESP32 GPIO

Do not blindly connect a 5 V ECHO signal directly to an ESP32 GPIO.

A suitable voltage divider can be used, for example:

ECHO │ [Voltage Divider] │ ↓ ESP32 GPIO

13. HC-SR04 with ESP32

Conceptually:

#define TRIG_PIN 5 #define ECHO_PIN 18 void setup() { Serial.begin(115200); pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); } void loop() { digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); long duration = pulseIn(ECHO_PIN, HIGH); float distance = duration * 0.0343 / 2; Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(500); }

The 0.0343 value is approximately the speed of sound in cm/µs at room temperature. Actual speed varies with environmental conditions.


14. Why Temperature Matters

The speed of sound changes with temperature.

Therefore:

Temperature changes ↓ Speed of sound changes ↓ Ultrasonic calculation changes ↓ Distance error

For high-precision systems, temperature compensation can improve measurements.


15. Ultrasonic Applications

Obstacle Avoiding Robot

Ultrasonic ↓ Distance ↓ ESP32 / Arduino ↓ Motor Driver ↓ Motors

Logic:

Distance < 20 cm ↓ Obstacle ↓ Stop / Turn

Parking Sensor

Vehicle ↓ Ultrasonic Sensor ↓ Distance ↓ Controller ↓ Buzzer

Example:

100 cm → Slow beep 50 cm → Faster beep 20 cm → Continuous alarm

16. IR Proximity Sensor

An IR proximity sensor uses infrared light to detect nearby objects.

A typical module contains:

IR LED + Photodetector

Basic principle:

IR LED ↓ Object ↓ Reflected IR ↓ Photodetector

The module determines whether an object is present based on the reflected infrared light.


17. IR Sensor vs Ultrasonic

FeatureIR ProximityUltrasonic
MediumInfrared lightSound
OutputOften digital/analogTime-of-flight
Measures distanceUsually approximateYes
Affected by lightingCan beUsually much less
Transparent objectsCan be problematicCan also be problematic depending on surface
Common useShort-range detectionDistance measurement

18. ToF Sensors

ToF = Time of Flight

ToF sensors measure how long light takes to travel to an object and return.

Unlike ultrasonic sensors:

Ultrasonic → Sound waves

ToF sensors generally use:

ToF → Light, commonly infrared

19. VL53L0X

The VL53L0X is a popular ToF distance sensor.

It uses infrared light to measure distance.

Basic operation:

IR Light ↓ Object ↓ Reflected signal ↓ Time-of-flight measurement ↓ Distance

It commonly communicates through:

I²C

20. VL53L0X vs HC-SR04

FeatureHC-SR04VL53L0X
TechnologyUltrasonicOptical ToF
SignalSoundInfrared light
InterfaceTrigger/EchoI²C
SizeLargerVery small
Typical useRobotics/educationCompact distance sensing

21. Radar / Microwave Motion Sensors

Some motion sensors use microwaves/radio waves rather than PIR.

One common type is a Doppler radar sensor.

Example:

RCWL-0516

Basic concept:

Microwave signal ↓ Object movement ↓ Reflected signal changes ↓ Sensor detects movement

These sensors can detect movement differently from PIR sensors.


22. PIR vs Radar

FeaturePIRRadar
TechnologyInfrared radiationRadio/microwave
DetectsChanges in thermal IRMovement-related RF changes
Requires body heatTypically yesNo
Can detect through some materialsGenerally noSome RF-transparent materials may allow it
Common useRoom motionMotion/occupancy detection

The exact behavior depends on the sensor design and environment.


23. Distance Sensors in Robotics

Robots need sensors to understand their environment.

Example:

ROBOT │ ┌────────┼────────┐ ↓ ↓ ↓ Ultrasonic IR ToF │ │ │ └────────┼────────┘ ↓ ESP32 ↓ Motor Driver ↓ Motors

This gives the robot information about nearby objects.


24. Multiple Ultrasonic Sensors

A robot may use several distance sensors:

FRONT ↑ HC-SR04 │ LEFT ← ROBOT → RIGHT Sensors

The controller can compare:

Left Distance Front Distance Right Distance

Then choose the safest direction.

Example:

Front = 15 cm Left = 50 cm Right = 80 cm → Turn Right

25. People Counting

Motion/distance sensors can also be used for people counting.

For example, two sensors can be placed at a doorway:

Outside Sensor A ↓ 🚶 Sensor B ↓ Inside

The sequence can determine direction:

A → B = Enter B → A = Exit

Then:

People Count + Enter/Exit ↓ ESP32 ↓ Display

This is more reliable than simply using one PIR sensor for counting.


26. Automatic Door

A distance or presence sensor can trigger an automatic door.

Person approaches ↓ Distance Sensor ↓ ESP32 ↓ Servo / Motor ↓ Door OPEN

After the person moves away:

No person ↓ Delay ↓ Door CLOSE

Safety systems should use appropriate sensors and fail-safe mechanisms rather than relying on a simple hobby sensor alone.


27. Water Tank Level

An ultrasonic sensor can measure the distance from the top of a tank to the water surface.

Tank ┌──────────────────┐ │ Ultrasonic │ │ ↓↓↓ │ │ │ │ │ │~~~~~~~~~~~~~~~~~~│ ← Water └──────────────────┘

If the tank height is known:

Water Level = Tank Height - Measured Distance

Then:

Water level low ↓ ESP32 ↓ Pump ON

28. Security System

Motion sensors can be combined:

PIR + Door Sensor + Radar ↓ ESP32 ↓ Security Logic ↓ Alarm / Notification

For example:

Door opens + Motion detected ↓ Potential intrusion ↓ Alarm

Using multiple sensor types can reduce false alarms.


29. Common Problems

PIR Problems

  • False triggers
  • Warm environmental objects
  • Poor placement
  • Incorrect sensitivity settings

Ultrasonic Problems

  • Soft materials absorb sound
  • Angled surfaces reflect sound away
  • Temperature affects speed of sound
  • Very small or irregular objects can be difficult to measure

IR Problems

  • Ambient sunlight can interfere
  • Dark/reflective surfaces can behave differently
  • Detection range is usually limited

ToF Problems

  • Strong ambient light can affect some sensors
  • Reflectivity and surface properties matter
  • Range varies by sensor

30. Motion and Distance Sensor Comparison

SensorDetects MotionMeasures DistanceTechnology
PIR✅❌Passive IR
IR ProximityPresence/objectApproximateInfrared reflection
HC-SR04❌✅Ultrasonic
VL53L0X❌✅Optical ToF
Radar✅Some modelsRF/microwave

31. Choosing the Right Sensor

Use a PIR when:

You need: "Is someone moving?"

Use HC-SR04 when:

You need: "How far is the object?"

Use VL53L0X when:

You need: Compact optical distance measurement

Use an IR proximity sensor when:

You need: Simple short-range object detection

Use radar when:

You need: RF-based motion/presence detection

32. Complete Smart Room Example

A smart room can combine multiple sensors:

SMART ROOM │ ┌──────────┼──────────┐ ↓ ↓ ↓ PIR ToF Door Sensor │ │ │ └──────────┼──────────┘ ↓ ESP32 │ ┌──────────┼──────────┐ ↓ ↓ ↓ Light Fan Security

Example:

Person enters ↓ PIR detects motion ↓ ESP32 ↓ Light ON

And:

Person approaches door ↓ ToF sensor ↓ ESP32 ↓ Door unlock/open

33. Key Formulas

Ultrasonic Distance

Distance = (Speed of Sound × Time) / 2

Water Tank Level

Water Level = Tank Height - Measured Distance

People Counting

Sensor A → Sensor B = Enter Sensor B → Sensor A = Exit

Key Points to Remember

PIR

Detects changes in infrared radiation, commonly caused by moving warm objects.

Ultrasonic

Measures distance by measuring the travel time of an ultrasonic pulse and its echo.

IR Proximity

Uses reflected infrared light to detect nearby objects.

ToF

Measures distance based on the time taken by light to travel to an object and return.

Radar

Uses radio/microwave signals to detect movement or presence.

The overall concept is:

OBJECT / PERSON ↓ ┌────────────┼────────────┐ ↓ ↓ ↓ PIR Ultrasonic ToF ↓ ↓ ↓ Motion Distance Distance └────────────┼────────────┘ ↓ ESP32 ↓ Decision / Action ↓ Light / Motor / Alarm

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy