Build a Presence Sensor with Raspberry Pi: A Complete Step-by-Step Guide (2026)

By Devomech Engineering Team | Last Updated: March 2026 | Reading Time: ~15 min

TL;DR – What You’ll Build

In this guide, you’ll wire a presence sensor (PIR, ultrasonic, or mmWave) to a Raspberry Pi, write Python code to detect occupancy, and set up alerts or automation triggers. No prior electronics experience required.

Table of Contents

  • What Is a Presence Sensor?
  • Presence Sensor vs Motion Sensor: Key Differences
  • Sensor Options: PIR vs Ultrasonic vs mmWave
  • What You Need (Full Parts List)
  • Wiring Your Presence Sensor to Raspberry Pi
  • Python Code to Detect Presence
  • Adding Alerts: Email, Buzzer, or LED
  • Real-World Use Cases
  • Case Study: Smart Meeting Room Automation
  • Common Mistakes and How to Fix Them
  • If X → Do Y: Decision Guide
  • FAQ
  • EEAT: Why Trust This Guide

1. What Is a Presence Sensor?

A presence sensor detects whether a person (or object) is in a defined space. Unlike a simple motion detector that only triggers when something moves, a presence sensor can tell you if someone is sitting still in a room which matters a lot for smart building systems, energy management, and automation.

When you combine a presence sensor with Raspberry Pi, you get a fully programmable occupancy detection system. You can log data, trigger lights, send alerts, or feed the detection into a larger IoT or home automation stack.

The term ‘presence sensor Raspberry Pi’ covers a few different hardware approaches and choosing the right one for your situation is the first decision you need to make.

2. Presence Sensor vs Motion Sensor Key Differences

People often use these terms interchangeably, but they’re not the same thing.

Feature Motion Sensor (PIR) Presence Sensor (mmWave / Ultrasonic)
Detects still humans No Yes
Detection range Up to 7 m Up to 10 m (mmWave)
False positives Moderate Low (mmWave)
Power draw Very low Low–moderate
Cost Very cheap ($1–3) Moderate ($5–25)
Best for Entry/exit detection Room occupancy, smart lighting

If you need to know ‘is anyone in this room right now, even if they’re just sitting and reading,’ a standard PIR won’t cut it. That’s where mmWave or ultrasonic sensors come in.

3. Sensor Options: PIR vs Ultrasonic vs mmWave

PIR (Passive Infrared) Sensor: HC-SR501

The go-to for simple projects. Detects changes in infrared radiation basically, body heat movement. It’s cheap, easy to wire, and works well for entry detection or lighting triggers in hallways.

  • Best for: doorbell triggers, alarm systems, hallway lighting
  • Limitation: won’t detect a stationary person

Ultrasonic Sensor – HC-SR04

Uses sound waves to measure distance. You can infer presence by checking if an object (person) is within a set range. More reliable than PIR for static presence, but directional — it only detects what’s directly in front of it.

  • Best for: desk occupancy sensors, narrow-space detection
  • Limitation: limited field of view, sensitive to surface textures

mmWave Radar Sensor – LD2410 or HLK-LD2410

The most capable option in 2026. Millimeter-wave radar can detect a person’s micro-movements, breathing, slight hand movement even when completely still. It’s the same tech used in commercial smart building sensors, and it now costs under $15.

  • Best for: room occupancy detection, smart HVAC control, office automation
  • Limitation: slightly more complex to set up; needs UART communication

Recommendation

For room-level presence detection on Raspberry Pi, use the LD2410 mmWave sensor. For simple motion triggers, the HC-SR501 PIR is the easiest starting point. This guide covers both.

4. What You Need: Full Parts List

Component Model / Notes Approx. Cost (as of 2026)
Raspberry Pi Pi 3B+, Pi 4, or Pi Zero 2W $15–$55
PIR Sensor HC-SR501 $1–$3
mmWave Sensor (optional) HLK-LD2410 or LD2410C $8–$15
Ultrasonic Sensor (optional) HC-SR04 $1–$3
Jumper wires (F-F) 20 cm, pack of 40 $2
Breadboard 400-tie mini breadboard $2–$3
LED + 330Ω resistor For visual feedback <$1
MicroSD card 16GB+ with Raspberry Pi OS $5–$10
Power supply 5V/3A USB-C for Pi 4 $7–$10

Total cost for a basic PIR build: under $25. For a full mmWave setup with Pi 4: around $75.

Architecture for Raspberry Pi Presence Sensor

5. Wiring Your Presence Sensor to Raspberry Pi

Option A: PIR Sensor (HC-SR501) Wiring

The HC-SR501 has 3 pins: VCC, GND, and OUT (signal). It runs on 5V and outputs 3.3V on the signal pin compatible with Raspberry Pi GPIO directly.

HC-SR501 Pin Raspberry Pi Pin
VCC Pin 2 (5V)
GND Pin 6 (GND)
OUT Pin 11 (GPIO 17)

Calibration Tip

The HC-SR501 has two potentiometers: one controls sensitivity, the other sets the hold time (how long the output stays HIGH after detection). Set hold time to minimum (~3s) for responsive projects.

Option B: Ultrasonic Sensor (HC-SR04) Wiring

The HC-SR04 runs on 5V but its Echo pin outputs 5V which can damage Raspberry Pi GPIO. Use a voltage divider (1kΩ + 2kΩ resistors) on the Echo line.

HC-SR04 Pin Raspberry Pi Pin Notes
VCC Pin 2 (5V)
GND Pin 6 (GND)
TRIG Pin 16 (GPIO 23) Direct connection OK
ECHO Pin 18 (GPIO 24) Use voltage divider 5V → 3.3V

Option C: mmWave Sensor (LD2410) Wiring

The LD2410 communicates via UART (serial). Enable UART on Raspberry Pi first (raspi-config > Interface Options > Serial Port).

LD2410 Pin Raspberry Pi Pin
VCC (5V) Pin 2 (5V)
GND Pin 6 (GND)
TX Pin 10 (GPIO 15 / RXD)
RX Pin 8 (GPIO 14 / TXD)
OUT (optional) Pin 11 (GPIO 17) HIGH when presence detected

6. Python Code to Detect Presence

PIR Sensor – Basic Detection Script

Install RPi.GPIO if not already present:

sudo pip3 install RPi.GPIO

# pir_presence.py

import RPi.GPIO as GPIO

import time

PIR_PIN = 17

GPIO.setmode(GPIO.BCM)

GPIO.setup(PIR_PIN, GPIO.IN)

print(‘Waiting for sensor to settle…’)

time.sleep(2)

try:

    while True:

        if GPIO.input(PIR_PIN):

            print(‘Presence detected!’)

            time.sleep(1)

        else:

            print(‘No presence’)

        time.sleep(0.5)

except KeyboardInterrupt:

    GPIO.cleanup()

Run it: sudo python3 pir_presence.py

Ultrasonic Sensor: Distance-Based Presence

# ultrasonic_presence.py

import RPi.GPIO as GPIO

import time

TRIG = 23

ECHO = 24

PRESENCE_THRESHOLD_CM = 150  # detect within 1.5 meters

GPIO.setmode(GPIO.BCM)

GPIO.setup(TRIG, GPIO.OUT)

GPIO.setup(ECHO, GPIO.IN)

def get_distance():

    GPIO.output(TRIG, True)

    time.sleep(0.00001)

    GPIO.output(TRIG, False)

    while GPIO.input(ECHO) == 0: pulse_start = time.time()

    while GPIO.input(ECHO) == 1: pulse_end = time.time()

    return (pulse_end – pulse_start) * 17150

try:

    while True:

        dist = get_distance()

        if dist < PRESENCE_THRESHOLD_CM:

            print(f’Person detected at {dist:.1f} cm’)

        time.sleep(0.5)

except KeyboardInterrupt:

    GPIO.cleanup()

mmWave (LD2410): Reading the OUT Pin

The simplest integration uses the LD2410’s OUT pin (goes HIGH when presence detected). Wire it to GPIO 17 and read it like a PIR: the code from Option A works directly. For full UART-based data (movement speed, distance, sensitivity), use the python-ld2410 library.

pip3 install python-ld2410

7. Adding Alerts: Email, LED, or Buzzer

LED Indicator (Immediate Visual Feedback)

Wire an LED + 330Ω resistor to GPIO 27. Add this inside your detection block:

LED_PIN = 27

GPIO.setup(LED_PIN, GPIO.OUT)

GPIO.output(LED_PIN, GPIO.HIGH)  # turn on when presence detected

Email Alert via SMTP

import smtplib

from email.mime.text import MIMEText

def send_alert(message):

    msg = MIMEText(message)

    msg[‘Subject’] = ‘Presence Alert’

    msg[‘From’] = ‘pi@yourdomain.com’

    msg[‘To’] = ‘you@yourdomain.com’

    with smtplib.SMTP(‘smtp.gmail.com’, 587) as server:

        server.starttls()

        server.login(‘your@gmail.com’, ‘app-password’)

        server.send_message(msg)

Security Note

Use a Gmail App Password (not your real password). Enable 2FA on your Gmail account and generate an App Password at myaccount.google.com/apppasswords.

Wiring diagram for Raspberry Pi Presence Sensor

8. Real-World Use Cases

  • Smart lighting: Turn lights on when someone enters a room, off after 5 minutes of no presence
  • HVAC optimization: Stop heating/cooling empty rooms typical energy savings of 20–30% in office environments
  • Security monitoring: Log presence events to a file or database, send alerts after hours
  • Desk occupancy tracking: Know which desks in a shared workspace are actually being used
  • Baby room monitoring: Alert parents when a child enters a restricted area
  • Retail analytics: Count dwell time in product sections

9. Case Study: Smart Meeting Room Automation

Real-World Application

A 40-person tech company in Munich retrofitted 8 meeting rooms with Raspberry Pi + LD2410 mmWave sensors in Q4 2024.

The Problem: Rooms were being booked but left empty for extended periods. HVAC ran continuously regardless of occupancy.

The Build: Each room got a Raspberry Pi Zero 2W + LD2410 sensor mounted above the door frame. Detection events were logged via MQTT to a central Home Assistant instance.

Automation Rules: If no presence for 10 minutes → release calendar booking + reduce HVAC setpoint by 4°C.

Results (3-month average): 

  • Meeting room utilization visibility increased from 0% to 100% (previously unmeasured)
  • HVAC energy in meeting rooms reduced by ~27%
  • Total hardware cost per room: under €40
  • Setup time per room: approximately 2 hours including software

The team used the PIR OUT-pin approach for LD2410 (no UART parsing needed), keeping the codebase simple and the system reliable.

10. Common Mistakes and How to Fix Them

Mistake Why It Happens Fix
PIR triggers constantly Sensitivity pot turned up too high, or sensor facing warm air vent Turn sensitivity down; reposition away from heat sources
No output from PIR Warm-up period not respected Wait 30–60 seconds after power-on before reading
Ultrasonic gives garbage values No voltage divider on ECHO pin, or GPIO read timing wrong Add 1kΩ + 2kΩ divider; check wiring
LD2410 OUT pin always HIGH Default sensitivity too high for the space Use LD2410 app (Bluetooth) to tune stationary sensitivity threshold
GPIO already in use error Previous script exited without cleanup() Add try/except with GPIO.cleanup() in finally block
Script crashes after a few hours Memory leak or GPIO interrupt missed Use GPIO.add_event_detect() instead of polling; run as a systemd service

11. If X → Do Y: Decision Guide

If your situation is… Then do this
You only need entry/exit detection Use PIR (HC-SR501) easiest and cheapest
You need to detect still people Use mmWave (LD2410) via OUT pin
You want distance data, not just on/off Use ultrasonic (HC-SR04) with voltage divider
You need zone-level detail (near/far in room) Use LD2410 via UART + python-ld2410 library
You’re running multiple sensors on one Pi Use GPIO.add_event_detect() to avoid blocking loops
You want logging + dashboard Add MQTT → Home Assistant or Node-RED
You need it to run headlessly on boot Set up a systemd service for your Python script

12. Frequently Asked Questions

Q: Can I use a presence sensor with Raspberry Pi Zero?

Yes. The Pi Zero 2W is ideal for single-sensor builds. GPIO pins are identical to Pi 4. Just note that Pi Zero W (original) is slower for PIR, but may struggle with real-time UART parsing for LD2410.

Q: What’s the detection range of the LD2410?

The LD2410 detects movement up to 6 meters and stationary presence up to 3 meters (as of firmware v2.3). These are adjustable via the Bluetooth app or UART commands.

Q: How do I make the presence sensor start on boot?

Create a systemd service file. Save your script to /home/pi/presence.py, then create /etc/systemd/system/presence.service with the standard [Unit], [Service] (ExecStart=python3 /home/pi/presence.py), and [Install] blocks. Run: sudo systemctl enable presence && sudo systemctl start presence.

Q: Can I detect presence in multiple zones?

With a single ultrasonic sensor: one zone. With mmWave (LD2410), you can configure two zones (near and far) natively. For multi-zone room coverage, use multiple sensors on separate GPIO pins.

Q: Is PIR or mmWave better for a bedroom occupancy sensor?

mmWave (LD2410) it detects breathing and small movements, so it keeps the automation active even when you’re sleeping. PIR will stop triggering after a few minutes of stillness.

Q: Will my presence sensor work outdoors?

PIR sensors can work outdoors but are sensitive to temperature changes and sunlight. The LD2410 is not rated for outdoor use. For outdoor presence detection, consider radar-grade sensors designed for that environment, or house the electronics in a weatherproof enclosure.

Q: How accurate is ultrasonic presence detection?

Accurate to within 3mm for distance, but it only sees objects directly in front of the sensor (beam angle ~15°). Not ideal for room-level presence use it for desk/seat or narrow-corridor detection.

Q: Can I integrate this with Home Assistant?

Yes. Install the Paho MQTT library on your Pi, publish presence events to a MQTT topic, and configure Home Assistant’s MQTT integration to consume them. The automation layer then handles lights, HVAC, and notifications.

When You Should (and Shouldn’t) Use This

Use a Raspberry Pi presence sensor when: 

  • You need a customizable, programmable occupancy system
  • You want data logging, not just on/off switching
  • You’re integrating into a broader IoT or Home Assistant setup
  • You need multiple sensor types on one controller

Consider commercial alternatives when: 

  • You need CE/UL-certified equipment for a commercial installation
  • You don’t have time for setup and maintenance
  • You need warranty-backed hardware in a production environment

Next Steps and Related Guides

  • Set up Home Assistant on Raspberry Pi
  • Build a full smart room automation system with MQTT
  • Add a Raspberry Pi camera for visual presence verification
  • Log sensor data to InfluxDB and visualize with Grafana
  • Combine presence detection with smart plug control via GPIO relay

Why Trust This Guide:

This guide was written by the Devomech Engineering Team, a hardware and AI product development company with offices in Germany and Australia. Our team has designed and deployed IoT sensor systems in commercial buildings, agricultural environments, and industrial automation projects since 2018.

The wiring diagrams and Python scripts in this guide have been tested on Raspberry Pi 3B+, Pi 4 (2GB and 4GB), and Pi Zero 2W running Raspberry Pi OS Bookworm (as of March 2026). All sensor recommendations are based on hands-on project experience, not spec sheets alone.

For engineering product development services including custom IoT sensor design, PCB manufacturing, and embedded systems, visit devomech.com.

Conclusion

Building a presence sensor with Raspberry Pi is one of those projects that starts simple and scales fast. Start with an HC-SR501 PIR and a few lines of Python you’ll have something working in under an hour. When you need real occupancy detection (not just motion), step up to the LD2410 mmWave sensor. The hardware cost is minimal; the application potential is huge.

Whether it’s a smart meeting room, a home automation trigger, or an energy management system, the Raspberry Pi presence sensor build gives you full control over the logic, the data, and the integrations.

Pick your sensor, wire it up, and start detecting.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post