The Sensing Principle: PIR vs. Microwave

Pyroelectric Infrared (PIR) sensors detect motion by measuring rapid changes in thermal radiation within their field of view. A pyroelectric crystal generates a tiny surface charge when exposed to fluctuating infrared energy—like a 98.6°F human walking across a 72°F background. The sensor's Fresnel lens focuses this IR energy into distinct detection zones; motion is only registered when the heat signature crosses from one zone to another, creating a differential voltage spike that the onboard comparator translates into a logic signal.

Microwave radar sensors (like the RCWL-0516) operate on the Doppler effect, emitting continuous 5.8 GHz RF waves and measuring the frequency shift of the reflected signal. While microwave sensors can detect micro-movements (like breathing) and penetrate non-metallic walls, they are notoriously prone to false triggers from ceiling fans, plumbing vibrations, and movement in adjacent rooms. For standard indoor room occupancy, PIR remains the superior, low-power baseline.

Output Signals: Digital Pulses vs. Analog Envelopes

A common mistake in embedded design is conflating digital logic outputs with analog sensor envelopes. Understanding exactly what the output actually is prevents blown GPIO pins and noisy data.

  • Digital Output (Standard PIR): Modules like the HC-SR501 or Panasonic EKMB output a strict binary voltage. When motion is detected, the OUT pin drives HIGH (either 3.3V or 5V, depending on the module's supply and logic design). When clear, it pulls LOW to 0V. The Raspberry Pi GPIO expects exactly 3.3V. Feeding a 5V HIGH signal into an RPi pin will degrade or destroy the SoC over time.
  • Analog Output (Microwave IF): Some microwave modules expose an 'IF' (Intermediate Frequency) pin. This is an analog waveform representing the raw Doppler shift envelope. Do not wire this directly to the RPi. The RPi lacks native analog-to-digital conversion (ADC). You must route analog signals through an external ADC like the MCP3008 via SPI before the RPi can read them.
Bench Tip: Always verify the HIGH state voltage of your sensor's OUT pin with a multimeter before connecting it to the Raspberry Pi. Many cheap HC-SR501 clones output 5V on the data pin even when powered by 3.3V due to onboard diode routing. Use a simple 2-resistor voltage divider (e.g., 2kΩ and 3.3kΩ) to step 5V down to a safe 3.3V.

Wiring and Pinout for Raspberry Pi GPIO

The wiring for a standard digital PIR sensor is straightforward, but power delivery dictates reliability. The Raspberry Pi's 3.3V rail (Pin 1) can supply up to 50mA, which is sufficient for low-power PIRs but marginal for older, unregulated modules. For stable operation, power the sensor from the 5V rail (Pin 2) and use a level-shifter or voltage divider on the data line, or select a native 3.3V sensor.

Standard Digital PIR Wiring to Raspberry Pi 4/5
Sensor Pin Function RPi GPIO / Power Pin Supply Range & Notes
VCC Power Input Pin 2 (5V) or Pin 1 (3.3V) 3.3V to 5V DC. Check module datasheet for minimum dropout.
GND Ground Reference Pin 6 (GND) Must share common ground with RPi.
OUT Digital Signal Pin 11 (GPIO 17) Must be ≤ 3.3V. Use voltage divider if sensor outputs 5V.

For authoritative pin mapping, always cross-reference your specific board revision with the official Raspberry Pi Pinout guide to avoid conflicting with UART or I2C reserved pins.

Signal Math: Raw GPIO Edges to Occupancy Time

The raw reading from a digital PIR is a boolean (1 or 0). The physical unit we actually care about in home automation is Occupancy Duration (Seconds). Because PIR sensors only trigger on movement, a person sitting perfectly still will cause the sensor to time out and drop LOW, even if they are still in the room. Therefore, we must mathematically integrate the pulse widths over a rolling window to determine true room occupancy.

The formula for total occupancy time ($T_{occ}$) over a measurement period is the sum of all valid HIGH pulses, filtering out electrical bounce (noise spikes under 50ms):

$T_{occ} = \sum_{i=1}^{n} (t_{fall, i} - t_{rise, i}) \quad \text{where} \quad (t_{fall} - t_{rise}) > t_{debounce}$

Here is the production-ready Python code using the gpiozero library to calculate this physical unit in real-time:

from gpiozero import MotionSensor
from signal import pause
import time

# Initialize on GPIO 17, ignore bounces shorter than 0.1s
pir = MotionSensor(17, bounce_time=0.1)

occupancy_seconds = 0.0
last_motion_time = None

def motion_started():
    global last_motion_time
    last_motion_time = time.time()
    print("[EVENT] Motion Detected")

def motion_stopped():
    global occupancy_seconds, last_motion_time
    if last_motion_time is not None:
        pulse_width = time.time() - last_motion_time
        occupancy_seconds += pulse_width
        print(f"[METRIC] Pulse Width: {pulse_width:.2f}s | Total Occupancy: {occupancy_seconds:.2f}s")
        last_motion_time = None

pir.when_motion = motion_started
pir.when_no_motion = motion_stopped

print("Monitoring RPI motion sensor... Press Ctrl+C to exit.")
pause()

Interference, Calibration, and False Triggers

Environmental interference is the primary cause of abandoned motion-sensing projects. Understanding the specific failure modes allows you to physically position the sensor to avoid them.

  • Thermal Drafts (HVAC): PIR sensors are blinded or falsely triggered by rapid ambient temperature shifts. Mounting a sensor directly above an HVAC supply vent will cause the heated/cooled air to wash over the Fresnel lens, creating continuous false positives. Maintain at least a 2-meter clearance from air registers.
  • Solar Loading: Direct sunlight sweeping across a room as the earth rotates contains massive IR energy. If a beam of sunlight crosses the sensor's detection zones, it will saturate the pyroelectric crystal. Use sensors with integrated optical filters (like the Panasonic EKMB series) that block visible light and specific IR bands outside the human thermal signature.
  • Pet Immunity: Standard sensors will trigger on dogs and cats. 'Pet-immune' sensors achieve this not through software, but by altering the Fresnel lens geometry to ignore the lower spatial zones (floor level) and by requiring a larger thermal mass to cross multiple zones simultaneously to trigger the comparator.

Calibration: Cheap modules like the HC-SR501 feature two physical potentiometers: one for sensitivity (distance) and one for time delay (how long the OUT pin stays HIGH after motion stops). These are notoriously difficult to tune precisely and drift with temperature. High-end industrial sensors are factory-calibrated via laser-trimmed resistors and require zero field calibration, relying instead on software timing in your Python script.

Decision Tree: Which RPI Motion Sensor to Buy

Stop guessing based on forum posts from 2018. Use this decision path to select the exact right module for your Raspberry Pi build.

Sensor Selection Decision Matrix
If your project requires... Then choose this technology... Specific Module / Part Number
A budget under $3, and you tolerate manual pot tuning and occasional false triggers from pets. Generic Unregulated PIR HC-SR501 (Requires 5V to 3.3V level shifting)
Detecting presence through drywall, glass, or plastic enclosures where line-of-sight is blocked. 5.8 GHz Microwave Radar RCWL-0516 (Requires ADC for analog, or direct GPIO for digital)
Native 3.3V logic, zero calibration, high pet immunity, and reliable long-term occupancy tracking. Industrial Digital PIR Panasonic EKMB1201111
The Default Recommendation:
For 95% of Raspberry Pi home automation, security, and occupancy projects, buy the Panasonic EKMB1201111 (typically $12.00 - $15.00 from distributors like Digi-Key or Mouser). It operates natively on 3.3V to 5V, outputs a clean 3.3V logic HIGH directly to the RPi GPIO without voltage dividers, features a 12-meter detection range, and includes advanced digital signal processing to reject RF interference and small-animal thermal noise. It is the definitive Panasonic PIR standard for embedded engineers. Pair it with the Python integration script above, and your occupancy tracking will be rock solid.