Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » IoT Bidirectional Visitor Counter using ESPP8266 & MQTT
ESP8266 Projects IoT Projects

IoT Bidirectional Visitor Counter using ESPP8266 & MQTT

Mamtaz AlamBy Mamtaz AlamUpdated:May 29, 20236 Comments8 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
IoT Based Visitor Counter
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

Overview: IoT Visitor Counter

In this project, we will make IoT IR Based Bidirectional Visitor Counter using NodeMCU ESPP8266 & Ubidots MQTT. You can use this project for monitoring the total number of incoming, outgoing, and current visitors online from any part of the world using Ubidots Dashboard. To count the incoming and outcoming visitors, it is better to use an Infrared or IR Sensor. The Visitors data uploads automatically to Ubidots cloud using the NodeMCU ESP8266 WiFi Module.

You can use this NodeMCU Bidirectional Visitor counter in the hall, Shopping mall, office, functions in the entrance gate to count the total number of visitors. The device counts the total number of people entering through the gate & also counts the total number of people leaving through the different gates. And finally, it calculates the total number of current people by subtracting the outgoing visitor from the incoming visitor. When even a single person is found inside the room, the light turns on automatically. When no people are present in the room, the light turns off automatically.

In one of my previous projects, I made the Visitor Counter Project using Arduino. But this time we will send the data to the cloud instead of watching it on OLED Screen. We can make this IoT Visitor Counter using ESP8266 Wifi Module, a pair of IR Sensor, OLED Display, and a Relay Module. Apart from these main components we also need some passive electronic components & PCB Board. The project is simply easy and even a beginner can make it without any difficulty.



Bill of Materials

The list of components that you need for making IoT Bidirectional Visitor Counter are as follows. You can purchase all the components online from Amazon.

S.N.Components NameQuantityPurchase Links
1NodeMCU ESP8266 Board1Amazon | AliExpress
2IR Sensor Module2Amazon | AliExpress
30.96" I2C OLED Display1Amazon | AliExpress
41 Channel Relay Module1Amazon | AliExpress
55V Power Supply1Amazon | AliExpress

IR Sensor as Visitor Detector

The main element of this IoT project is the IR Sensor which works as a Obstacle Detector. Whenever the IR sensor detects an interrupt it counts the person and adds it to the previous value.

IR Sensor Module

IR Sensor module has the great adaptive capability of the ambient light. It has an infrared transmitter and a receiver. The infrared emitting tube emits a certain frequency which when encounters an obstacle reflect back to the signal. The reflected signal is then received by the receiver tube. The other components in the circuit are Opamp, Variable Resistor & output LED.

The Sensor consists of the following electronics components.

1. IR LED Transmitter

IR LED emits light, in the range of Infrared frequency with a wavelength of 700nm – 1mm. IR LEDs have a light-emitting angle of approx. 20-60 degrees and has a range up to 5-10cm.

2. Photodiode Receiver

Photodiode acts as the IR receiver as its conducts when light falls on it. Photodiode looks like a LED, with a black color coating on its outer side.

3. LM358 Opamp

LM358 is an Operational Amplifier (Op-Amp) used as a voltage comparator in the IR sensor. The comparator circuit compares the threshold voltage set using the preset and the photodiode’s series resistor voltage. When the Photodiode’s series resistor voltage drop is greater than the threshold voltage, the Op-Amp output is high or vice-versa.

When the Op-Amp output is high the LED at the output terminal turns ON. This indicates an object detection.

4. Variable Resistor

The variable resistor here is preset. It is used to calibrate the distance range at which the object should be detected.


IoT Bidirectional Visitor Counter Circuit or Schematic

The Circuit for NodeMCU ESP8266 Bidirectional Visitor Counter is very simple. Here is a circuit diagram for the project. You can design the schematic using a Fritzing Software.

IoT Bidirectional Visitor Counter

Connect the I2C Pins (SDA & SCL) of 0.96″ OLED Display with NodeMCU D2 & D1 Pins. Connect the output pin of the pair of IR Sensors to D5 & D6 of NodeMCU. One of the IR Sensors will act as the incoming visitor counter and the other as the outgoing visitor counter. Similarly, connect a 5V Relay Module to the D4 Pin of NodeMCU. Both the IR Sensors and Relay Module works at 5V VCC. You can supply 5V from Amica NodeMCU Vin pin or Lolin NodeMCU VU Pin.



Schematic & PCB Designing

You can assemble and try this circuit on breadboard. But if you don’t want to assemble the circuit on breadboard, then you can check this schematic and build a custom PCB. I prefer EasyEDA, an online Schematic & PCB Designing Tool for drawing the Schematic.

ESP8266 Visitor Counter

After designing the ESP8266 Bidirectional Visitor Counter Schematic, you can convert the schematic to PCB. The PCB looks something like this shown in the image below.


PCB Ordering & Assembly

The Gerber File for IoT IR Based Bidirectional Visitor Counter with Light Control PCB is given below. You can simply download the Gerber File and order the PCB from ALLPCB at 1$ only.

Download Gerber File: IoT IR Based Visitor Counter PCB

You can use this Gerber file to order high quality PCB for this project. To do that visit the ALLPCB official website by clicking here: https://www.allpcb.com/.

You can now upload the Gerber File by choosing the Quote Now option. From these options, you can choose the Material Type, Dimensions, Quantity, Thickness, Solder Mask Color and other required parameters.

After filling all details, select your country and shipping method. Finally you can place the order.

After a week, I got the PCB. The PCB quality is superb and has a very high Finish.

NodeMCU ESP8266 Bidirectional Visitor Counter PCB

I then assembled all the components on the PCB. The PCB board along with the mounted component makes the final hardware perfect. Now we need to test the device along with the code.




Setting Up Ubidots

We need to setup the Ubidots dashboard in order to recieve the Visitor Counter data from ESP8266 MQTT. In order to set up Ubidots, visit https://ubidots.com/ & create a New Account using your email address.

After login into Ubidots Dashboard, we need an API Key. To get the API key, click on the top right of the dashboard on the profile option. You will see an option called “API Credentials”. Click on the option.

Now when you click on Default Token and copy the API Token. You need an API Token in the code part to receive the data.


Source Code/Program

The Source Code for esp8266 Bidirectional Visitor Counter with Automatic Light Control System is given below. The code requires SSD1306 & GFX OLED library for compilation and Ubidots Library. First download the following libraries and add it to the Arduino IDE.

1. Download SSD1306 Library: https://github.com/adafruit/Adafruit_SSD1306
2. Download Adafruit GFX Library: https://github.com/adafruit/Adafruit-GFX-Library
3. Download Ubidots MQTT Library: https://github.com/ubidots/ubidots-mqtt-esp

From this code part, change the Wifi SSID, Password & Ubidots Token. Use the same API Token. Some of the networks don’t work with MQTT Protocol because of the Security issue. You can try another network if the device fails to connect to the MQTT Server.

1
2
3
#define TOKEN "*********************************" // Your Ubidots TOKEN
#define WIFINAME "************" //Your SSID
#define WIFIPASS "*************" // Your Wifi Pass

You can now copy the code and upload it to the NodeMCU ESP8266 Board.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
. #include "UbidotsESPMQTT.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define TOKEN "*********************************" // Your Ubidots TOKEN
#define WIFINAME "************" //Your SSID
#define WIFIPASS "*************" // Your Wifi Pass
 
#define SCREEN_WIDTH 128    // OLED display width, in pixels
#define SCREEN_HEIGHT 64    // OLED display height, in pixels
#define OLED_RESET -1       // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
#define inSensor 14
#define outSensor 12
 
#define relay 0
 
int inStatus;
int outStatus;
 
int countin = 0;
int countout = 0;
 
int in;
int out;
int now;
 
Ubidots client(TOKEN);
 
void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}
 
 
void setup()
{
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
 
  pinMode(inSensor, INPUT);
  pinMode(outSensor, INPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
 
  Serial.println("Visitor Counter Demo");
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(20, 20);
  display.print("Visitor");
  display.setCursor(20, 40);
  display.print("Counter");
  display.display();
  delay(3000);
 
 
}
 
void loop()
{
  if (!client.connected())
  {
    client.reconnect();
  }
 
  inStatus =  digitalRead(inSensor);
  outStatus = digitalRead(outSensor);
  if (inStatus == 0)
  {
    in = countin++;
  }
 
  if (outStatus == 0)
  {
    out = countout++;
  }
 
  now = in - out;
 
  if (now <= 0)
  {
    digitalWrite(relay, HIGH);
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0, 15);
    display.print("No Visitor");
    display.setCursor(5, 40);
    display.print("Light Off");
    display.display();
    Serial.println("No Visitors! Light Off");
    delay(500);
  }
  else
  {
    digitalWrite(relay, LOW);
 
    display.clearDisplay();
    display.setTextColor(WHITE);
 
    display.setTextSize(1);
    display.setCursor(15, 0);
    display.print("Current Visitor");
    display.setTextSize(2);
    display.setCursor(50, 15);
    display.print(now);
 
    display.setTextSize(1);
    display.setCursor(0, 40);
    display.print("IN: ");
    display.print(in);
 
    display.setTextSize(1);
    display.setCursor(70, 40);
    display.print("OUT: ");
    display.print(out);
 
    display.display();
 
    Serial.print("Current Visitor: ");
    Serial.println(now);
    Serial.print("IN: ");
    Serial.println(in);
    Serial.print("OUT: ");
    Serial.println(out);
    delay(500);
  }
  int relaystatus = digitalRead(relay);
  client.add("in", in);
  client.add("out", out);
  client.add("now", now);
  client.add("Light Status", relaystatus);
  client.ubidotsPublish("ESP8266");
  client.loop();
  Serial.println();
  delay(100);
}


Testing IoT ESP8266 Based Visitor Counter Project

The above code for ESP8266 with MQTT Ubidots fulfills all the requirements of the project. You can upload the code to the ESP8266 WiFi Module. Once you upload the code, the ESP8266 board will try connecting to the WiFi Network. Once it connects to the network, you can open the Serial Monitor. You can simply see all the happening on Serial Monitor..

The Serial monitor will display the connection status of the WiFi Network along with the IP Address. If there is a successful wifi connection and a further connection to Ubidots, the data will upload automatically after every set interval. The Serial Monitor will also display the number of incoming visitors, outgoing visitors, current visitors & Light Status.

On the Hardware Part, when no visitors are there, the OLED will simple show “No Visitors & Light Status OFF”.

IR Sensor Based Visitor Counter IoT

Similalry, when someone is found inside the room, the light will turn on automatically and OLED Screen shows the number of incoming, outgoing, and current visitors.

IoT Based Bidirectional Visitor Counter



Monitoring Visitors Status Online on Ubidots Dashboard

Initially Ubidots dashboard will display no devices. But as soon as the device is connected to a MQTT Server, you will see a device appearing on the Ubidots device Section.

Now click on the device that you see on Ubidots Dashboard. In my case, I can see esp8266 as I am using NodeMCU ESP8266 Board. In this dashboard, you will see the data for visitors. So the data only changes when an event occurs.

Now, go back to the dashboard. On the top right side of the dashboard click on the “+” Symbol. You will see there are so many widgets. You can click and create a widget display for all individual parameters from incoming visitor to current visitor. For example, you can select Metric for displaying visitors.

Do the same thing for all other remaining 4 variables as well. Finally, after designing the dashboard with the help of a widget, you will get a beautiful real-time dashboard as shown in the image below.

You can also visit the mobile version of the Ubidots dashboard. Even the mobile screen will show the Visitor Counts data on the widgets display.

IoT Visitor Counter Project


Video Tutorial & Guide

IoT Based Bidirectional Visitor Counter using ESP8266 & MQTT
Watch this video on YouTube.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleInterfacing DHT11 Temperature Humidity Sensor with Raspberry Pi Pico
Next Article DIY Soil EC Salinity Meter using Arduino & Soil EC Sensor

Related Posts

ESP32 Fingerprint Attendance System with Live Web Dashboard

ESP32 Fingerprint Attendance System with Live Web Dashboard

Updated:June 21, 2026
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

Updated:June 14, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

Updated:May 10, 20262K
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

Updated:May 2, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

Updated:April 27, 20261K
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

Updated:April 27, 20262K
View 6 Comments

6 Comments

  1. Yudha on March 13, 2022 11:33 AM

    can i combine sensor mlx90614 with this project, but using 2 oled panel

    Reply
  2. Matt on September 6, 2022 11:46 PM

    Is it possible to have this project without the light? i just want to count people in and out and potentially connect it to another counter for many entries/exits?

    Reply
  3. Volkan Cebeci on November 5, 2022 8:14 AM

    I really liked the project. Can I use PIR sensors instead of IR sensors? At the same time, I can design how it can count when more than one person passes side by side. Can you make a video for that too?

    Reply
  4. Nath Sai on December 13, 2022 12:46 AM

    after connecting all components oled display is not working

    Reply
  5. Rohit kumar singh on March 25, 2023 12:41 PM

    using above code if i place my hand continuosly in front of IR sensor counting my incoming visitors my counts keeps on increasing but this should not be the case its equivalent that a person is standing in front of sensor and sensor keeps on increasing the count but actually it has to count only one . How to solve this issue?

    Reply
  6. thotakuri on May 5, 2023 11:23 AM

    data is not showing in widgets

    Reply

CommentsCancel reply

Latest Posts
ESP32 Fingerprint Attendance System with Live Web Dashboard

ESP32 Fingerprint Attendance System with Live Web Dashboard

June 21, 2026
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

June 14, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
Top Posts & Pages
  • ESP32 Fingerprint Attendance System with Live Web Dashboard
    ESP32 Fingerprint Attendance System with Live Web Dashboard
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
  • IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32
    IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32
  • MAX30102 & Arduino: Heart Rate + Blood Oxygen Monitoring
    MAX30102 & Arduino: Heart Rate + Blood Oxygen Monitoring
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (205)
    • ESP32 MicroPython (7)
    • ESP32 Projects (82)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.