Proximity and Presence Detection
Proximity and Presence Detection
Overview
Section titled “Overview”“Detect whether someone is there” is really three different questions: is an object close by, is a person moving, is a person present but still. Pick the wrong part and the product dies on one complaint: “the lights went off while I was sitting still”. By the end of this section you will be able to:
- Separate what reflective IR, PIR and mmWave radar each can and cannot see
- Solve false and missed detections with output behaviour and mounting conditions, not with the sensitivity trimmer
- Handle the three practical PIR details: output level, power-on blanking time, retriggerable mode
- Give deliverable selection conclusions for three typical needs: corridors, occupied rooms, and inside-equipment detection
Prerequisites
Section titled “Prerequisites”Before starting this section, make sure you:
- Completed Section 2 of Chapter 25 (digital inputs and debounce)
- Have a reflective IR proximity module, a PIR module (HC-SR501 class) and an mmWave presence module
- Have one person willing to sit still for five minutes (the most valuable test resource in this section)
Key Concepts
Section titled “Key Concepts”Three questions, three part families
Section titled “Three questions, three part families”| Need | Suitable part | Why |
|---|---|---|
| In-device presence at millimetres to centimetres | Reflective IR, ToF | Short range, definite target, fast |
| Someone passed by (corridor, stairwell) | PIR | Cheap, low power, motion is enough |
| Lights stay on while occupied (office, WC, meeting room) | mmWave radar | Senses micro-motion of a stationary person (breathing, limbs) |
| Counting people, per-zone statistics | Camera or dedicated counting | Only arrays and imagery give individual counts — at a privacy cost |
| Detection through a non-metallic cover | mmWave radar, ultrasonic | IR barely passes covers; PIR cannot |
PIR measures change in thermal difference, not people: a person sitting motionless in the centre of the field will drop out after the hold time expires. That is physics; turning the sensitivity knob does not fix it. If you need “still lit while seated”, change the principle.
Reflective IR: simple, but dependent on colour and distance
Section titled “Reflective IR: simple, but dependent on colour and distance”Modules like TCRT5000 emit and receive their own reflection; typical sensing distance is a few millimetres to a few centimetres, strongly dependent on the target surface:
- Black and absorbent surfaces reflect almost nothing; white and polished metal reflect strongly → one threshold is not consistent across targets
- Daylight contains near-infrared; outdoors or by a window the receiver saturates, so shading is needed
- Digital output uses an on-board comparator plus trimmer for switch-like use; analogue output suits adaptive thresholds
- Position it as in-device paper detection, wheel tachometry, edge detection — not human presence
PIR: output level, blanking time, retriggerable behaviour
Section titled “PIR: output level, blanking time, retriggerable behaviour”For HC-SR01-class modules, three things must be measured before design-in:
- Output high-level amplitude: modules are often supplied at 5 V; the output high may approach the supply rail or may already be limited near 3.3 V. Measure before connecting to an ESP32; if it approaches 5 V, divide down or use a 3.3 V variant (the level rules from Section 3 apply)
- Power-on blanking time: most PIR modules need an initialisation period (seconds up to a couple of minutes, depending on part and trimmers) during which they are unstable or falsely trigger. Firmware must include a boot quiet period and must not count events in it
- Retriggerable selection (a jumper on many boards): retriggerable mode keeps extending the hold time while motion persists; non-retriggerable forces the output low when the hold time expires. The difference in lighting experience lives exactly here — treat it as a factory configuration item and document it
Three further real sources of false and missed detection: airflow (HVAC, door draughts), heating pipes and direct sun inside the field, and pets at the lower edge of coverage. The Fresnel lens segmentation defines the covered sectors, so mounting height and orientation matter more than sensitivity.
mmWave radar: sees stationary people, but has mounting rules
Section titled “mmWave radar: sees stationary people, but has mounting rules”Common 5.8 GHz / 24 GHz / 60 GHz presence modules output either a high level or UART frames (some add PWM for distance). Characteristics:
- Passes non-metals (plastic enclosures, wood, glass, with weak attenuation through thin walls) and does not pass metal (reflection and shielding) — a radar inside a metal luminaire housing is useless
- Reads micro-motion: a stationary person still breathes and shifts, so presence is held; but a truly motionless person plus too-low sensitivity still drops out
- Sees through partitions into the next room: the most common site complaint. Reduce sensitivity or distance band, or change position and orientation away from partition walls
- Neighbouring same-frequency radars interfere: two nearby units produce flapping or “handover”. Fix with different bands, separation, angled mounting, or synchronised/frequency-hopping parts
- Privacy advantage: no imagery, so WCs, bedrooms and meeting rooms clear customer and compliance review far more easily than camera solutions
Output types determine the software structure
Section titled “Output types determine the software structure”| Part | Typical output | Software note |
|---|---|---|
| Reflective IR proximity | Digital level via comparator | Needs debounce (Section 2); threshold is colour-dependent |
| PIR | Digital level, constant during hold time | Count “rising edge + blanking”; do not treat repeats inside hold time as extra people |
| mmWave (switch type) | Digital level | As above; hold time is usually adjustable and must match the business timeout policy |
| mmWave (UART type) | Frames (distance / micro-motion / presence) | Frame checksum and sequence are mandatory; drop frames that fail CRC; mind baud and pin power-on sequencing |
What I would write into a proposal
Section titled “What I would write into a proposal”- Corridors, stairwells, WC lobbies: PIR is enough, lowest cost; either accept “drops out when still” or add a long hold as a stopgap
- Offices, meeting rooms, WCs (long dwell): mmWave presence, explicitly stating the through-wall risk and mitigation
- In-device arrival and speed: reflective IR or ToF, nothing to do with humans
- People counting or per-zone statistics: do not force presence radar into it; go to imagery with explicit privacy notice
- Metal enclosures: PIR and IR are unusable (metal and IR-opaque covers), use mmWave and confirm the antenna window is non-metallic
Implementation
Section titled “Implementation”PIR: boot blanking, edge counting, hold-time de-duplication
Section titled “PIR: boot blanking, edge counting, hold-time de-duplication”const uint8_t PIR_PIN = 15; // measure the output level first; divide if neededconst unsigned long BOOT_BLANK_MS = 60000; // power-on quiet period, tune per partconst unsigned long EDGE_DEDUP_MS = 2000; // de-duplicate edges inside one hold
unsigned long bootMs = 0;unsigned long lastEdgeMs = 0;uint32_t events = 0;uint8_t lastLevel = LOW;
void setup() { Serial.begin(115200); pinMode(PIR_PIN, INPUT); // do not enable internal pull-ups if the module drives them bootMs = millis();}
void loop() { uint8_t lvl = digitalRead(PIR_PIN); unsigned long now = millis();
if (now - bootMs < BOOT_BLANK_MS) { lastLevel = lvl; return; } // quiet period: track only
if (lvl == HIGH && lastLevel == LOW) { // rising edge if (now - lastEdgeMs > EDGE_DEDUP_MS) { events++; Serial.printf("motion event #%lu\n", events); } lastEdgeMs = now; } lastLevel = lvl;}mmWave UART frames: drop anything failing the checksum
Section titled “mmWave UART frames: drop anything failing the checksum”const uint8_t RX_PIN = 16;HardwareSerial radar(2);
// Illustrative layout: header 0xF4 0xF3 ... payload ... checksum (vendors differ — implement against your protocol document)bool parseFrame(uint8_t *buf, size_t len) { if (len < 4 || buf[0] != 0xF4 || buf[1] != 0xF3) return false; uint8_t sum = 0; for (size_t i = 0; i + 1 < len; i++) sum += buf[i]; return sum == buf[len - 1]; // checksum failure: discard, keep previous state}
void pollRadar() { static uint8_t buf[32]; static size_t n = 0; while (radar.available() && n < sizeof(buf)) { buf[n++] = radar.read(); if (n >= 4 && parseFrame(buf, n)) { Serial.printf("radar: present=%d raw=", buf[2] > 0); for (size_t i = 0; i < n; i++) Serial.printf("%02x ", buf[i]); Serial.println(); n = 0; } } if (n >= sizeof(buf)) n = 0; // overflow guard so one burst of noise cannot jam the buffer}Implement the frame format against the protocol document supplied with your module — the code above is structural only. Do not copy an internet “universal frame”, or the checksum will never pass.
Verification
Section titled “Verification”Device behaviour checks
Section titled “Device behaviour checks”| Check | Expected result | Verified |
|---|---|---|
| Measured PIR output high level | Voltage recorded, decision made on dividing | |
| Measured PIR power-on blanking time | Duration recorded and coded into BOOT_BLANK_MS |
|
| Retriggerable jumper, both modes | Hold-extension versus fixed hold clearly visible | |
| Person motionless in field for 5 min | PIR drops to low after hold time (missed detection by design) | |
| Same condition with mmWave | Presence retained; record the stable duration | |
| HVAC airflow aimed at the field | Record false triggers, establishing mounting constraints | |
| Pet height at lower field edge | Record triggering, use it to set mounting height |
Site constraint checks
Section titled “Site constraint checks”| Check | Expected result | Verified |
|---|---|---|
| Radar through a plastic luminaire cover | Still works; behind a metal cover it fails (proving the material rule) | |
| Radar facing a partition wall | Record the distance at which it detects the next room; set sensitivity and orientation from it | |
| Two radars at 30 cm and at 2 m apart | Near-field interference is reproducible; issue a minimum spacing | |
| Reflective IR on black vs white targets | Response differs strongly at one threshold, proving colour dependence | |
| Direct sunlight on reflective IR | Saturation or false triggering, proving shading need |
Troubleshooting
Section titled “Troubleshooting”Issue 1: lights turn off while someone is sitting still
Section titled “Issue 1: lights turn off while someone is sitting still”Cause: a PIR was used; it measures motion, not presence.
Solutions:
- Switch to mmWave presence, or accept the experience and lengthen hold time (symptomatic)
- Hybrid: PIR starts the light plus a periodic “occupancy confirmation” (door sensor, button press) to extend — fine for cost-sensitive cases
- State the “stationary dropout” behaviour in the proposal, so it is not filed as a defect later
Issue 2: frequent false triggers at night
Section titled “Issue 2: frequent false triggers at night”Possible causes: airflow (HVAC, door gaps), heating pipes in view, moving reflections or sunlight, PIR sensitivity trimmed too high, radar sensitivity too high and reading through walls.
Solutions:
- Change height and orientation, and use baffles to cut the offending sector — the cheapest and most effective fix
- Dial PIR sensitivity down a step and require N consecutive triggers before acting
- Reduce radar distance band or sensitivity and orient it away from partitions
Issue 3: the MCU sees nothing, or the line is always high
Section titled “Issue 3: the MCU sees nothing, or the line is always high”Possible causes: module output exceeds the IO requirement (or exceeds 3.3 V with damage risk), readings taken during blanking time, wiring errors.
Solutions:
- Measure the output level first, then decide direct / divider / level shifter
- Retest after the blanking period; codify it and document it
- Use the raw-level printout technique from Section 2 to separate hardware from logic faults
Issue 4: neighbouring radars interfere
Section titled “Issue 4: neighbouring radars interfere”Symptoms: flapping presence states when two units are close.
Solutions:
- Increase separation, angle them differently, or use different-frequency parts
- Enable synchronisation or frequency hopping if the part supports it
- Put minimum spacing into the installation manual instead of relying on field tuning
Best Practices
Section titled “Best Practices”- Do: ask “present or moving?” before choosing parts; do not let a sensitivity trimmer paper over a wrong selection
- Do: make blanking time, hold time and de-duplication window configuration parameters, documented and factory-settable
- Do: measure through-wall distance in radar projects and deliver a mounting-orientation drawing as part of the handover
- Do: prefer non-imagery for occupancy data with a privacy statement; if imagery is used, notify and define retention explicitly
- Don’t: use PIR or reflective IR inside a metal enclosure for presence detection
- Don’t: use reflective IR proximity as a human-presence sensor (colour and distance dependence dominate)
- Don’t: count repeats within one hold time as “multiple people”
Summary
Section titled “Summary”Key points of this section:
- Three questions, three families: arrival → reflective IR/ToF; passing → PIR; stationary presence → mmWave
- PIR measures motion only; “lights out while sitting still” is a property, so either change principle or document the experience
- Three mandatory PIR measurements: output level, power-on blanking time, retriggerable mode; mounting beats sensitivity tuning
- mmWave passes non-metals, not metals; through-partition detection is the main site problem, so measure it and ship an orientation guide
- Non-imagery presence detection is the privacy-favourable answer, which is the right way to sell value to the customer
Next section: thermal imaging and temperature mapping — answering “where is it hot” with low-cost arrays, and the measurement boundary emissivity imposes.
We provide ESP32 ODM design-to-manufacturing services. From prototype to production — the team behind this tutorial can build it with you.
💡 Mention tutorial.imagrove.com in your Alibaba chat with us — get 10% off your custom project quote.
First published attutorial.imagrove.com/sensor-module-selection/10-proximity-and-presence-detection/Please credit Imagrove and link back to this page when quoting or republishing.
Questions or issues with this tutorial?
Sign in with GitHub to leave a comment. If a step fails for you, tell us — we verify it and update the tutorial.
© Imagrove · ESP32 IoT Tutorial