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 Name | Quantity | Purchase Links |
|---|---|---|---|
| 1 | NodeMCU ESP8266 Board | 1 | Amazon | AliExpress |
| 2 | IR Sensor Module | 2 | Amazon | AliExpress |
| 3 | 0.96" I2C OLED Display | 1 | Amazon | AliExpress |
| 4 | 1 Channel Relay Module | 1 | Amazon | AliExpress |
| 5 | 5V Power Supply | 1 | Amazon | 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 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.
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.
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.
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.
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”.
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.
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.



























6 Comments
can i combine sensor mlx90614 with this project, but using 2 oled panel
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?
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?
after connecting all components oled display is not working
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?
data is not showing in widgets