← Back to ideas

Redson Dev · Idea

IoTIntermediateAges 18+A weekend

Real-time Occupancy Sensor for Small Spaces

Published June 10, 2026

This project offers a practical solution for monitoring foot traffic in various settings, helping managers optimize resource allocation and improve customer flow. Anyone operating a small clinic, a local library, or a community center could build this to understand peak times and staffing needs, track visitor numbers, or even just ensure spaces aren't overcrowded. The system uses simple infrared sensors to count entries and exits, providing real-time data on space occupancy.

What you'll need

  • ESP8266 NodeMCU development board
  • 2x HC-SR501 PIR motion sensors
  • Breadboard
  • Jumper wires
  • USB A to Micro-USB cable
  • Small enclosure (plastic or cardboard)

Step-by-step

  1. 01

    Hardware Assembly

    Connect the VCC pin of both HC-SR501 sensors to the 3.3V pin of the ESP8266. Connect the GND pin of both sensors to the GND pin of the ESP8266. Connect the OUT pin of the first sensor to ESP8266 pin D1 (GPIO5) and the OUT pin of the second sensor to ESP8266 pin D2 (GPIO4). Ensure the sensors are positioned facing each other with an unobstructed path between them.

  2. 02

    Arduino IDE Setup

    Download and install the Arduino IDE if you haven't already. Go to 'File > Preferences' and add `http://arduino.esp8266.com/stable/package_esp8266com_index.json` to the 'Additional Boards Manager URLs'. Then, open 'Tools > Board > Boards Manager', search for 'esp8266', and install the 'esp8266 by ESP Community' package.

  3. 03

    Write the Firmware

    Open a new sketch in Arduino IDE. Include libraries for Wi-Fi and the internal web server (e.g., `ESP8266WiFi.h` and `ESP8266WebServer.h`). Define two global variables for entry and exit counts. In the `setup()` function, initialize the serial monitor, set the sensor pins as inputs, and connect to your local Wi-Fi network. In the `loop()` function, read the state of both sensors and implement logic to increment entry or exit counts based on the sequence of sensor activation. For example, if sensor 1 triggers then sensor 2, it's an 'entry', and vice-versa for 'exit'.

  4. 04

    Implement Web Interface

    Within your `setup()` function, initialize an `ESP8266WebServer` object on port 80. Create a handler for the root path ('/') that dynamically generates an HTML page displaying the current entry and exit counts. Include a simple refresh meta tag in the HTML header for automatic updates. In the `loop()` function, ensure `server.handleClient()` is called continuously to process incoming web requests.

  5. 05

    Upload and Test

    Select 'NodeMCU 1.0 (ESP-12E Module)' under 'Tools > Board'. Connect your ESP8266 board via USB. Select the correct COM port under 'Tools > Port'. Upload the sketch. Once uploaded, open the Serial Monitor to see the device's assigned IP address upon connecting to Wi-Fi. Access this IP address from any web browser on the same network to view the real-time occupancy data. Test by walking past the sensors in both directions and observe the count updates.

Tips

  • Adjust the sensitivity and delay potentiometers on the HC-SR501 sensors for optimal performance in your environment.
  • For power efficiency, consider putting the ESP8266 into deep sleep mode for short intervals if continuous monitoring isn't strictly necessary.
#occupancy-sensor#esp8266#infrared#data-logging#web-interface