Saturday, May 1, 2021

Person Counter

 For an index to all my stories click this text.

I am as you might know in the Netherlands and until now we had a lockdown. At this moment the measures are a bit less stringent. I work at a company that sells all kinds of electrical equipment. We do that through our webshop and our physical shop. At first the shop has been closed. Then customers were allowed to visit on appointment. And now customers are free to visit the shop however a maximum of 50 customers at the same time is allowed.

At the entrance is a counter that is manned for collecting pre-ordered goods. The person behind that counter can keep track of the amount of customers. But that is a tedious task to do on paper. So I proposed to the board of directors to build an electronic person counter. And they agreed.

So last sunday I started working on the customer counter. I build two prototypes. The first is an hardware electronic one. The second is an app for android phones. I thought the app might be handy as all our employees are equipped with an Android phone. I managed to get both prototypes working within a few hours.

I present you here the hardware version.

Actually this is an easy project and could be made by anyone who read my book on the ESP32 as the mentioned parts (buttons and display) are all discussed in my book.

Person counter.

I wanted it to be as simple as possible so anyone can operate it.
There should be one button that must be pressed when a customer enters the building and another button that must be pressed when a customers leaves the building.
Next there should be a display that shows how many customers are present.

Well this is easy. For the display I used an TM1637 4 digit 8 segment display and further I needed two buttons. So this could easily be build with an ESP8266.

On monday I took the prototype with me and performed a test to see if it was functional. During the day it occurred to me that it would add some good information for our management if there was a total customer count. So at the end of the day we could see how many customers in total visited our shop that day.

Tuesday was kings-day (the birthday of our King) which is a national holliday but due to the pandemic no festivities were allowed so I had the time to add the third button and to write the additional code, which was in fact a matter of a few minutes. And I put the hardware on stripboard.

Wednesday was the first day we were officially open again so I performed another test with the final setup. And it performed really well.

Within 3 days from prototype to final setup !!

Breadboard prototype.



As you can see the breadboard setup is really simple. I used a Wemos D1 mini. The TM1637 display has its data line (DIO) attached to D5 and the Clock (CLK) line attached to D6.
The Add-1 button is attached to D1 and the Subtract-1 button to D2. The total-count button is attached to D7.
Normally I always use pull-up resistors but for simplicity I decided to use the internal PULLUP resistors of the Wemos D1.

The person count software

The TM1637 display needs a library. I used the TM1637Display library made by avishorp that is included in the Arduino's library manager. You can manually install it from the following link:
https://github.com/avishorp/TM1637

#include <TM1637Display.h>
 
const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display


const int upbut = D1;
const int downbut  = D2;
const int totbut = D7;
 
int person = 0;
int total = 0;
 
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
 
void setup()
{
 Serial.begin(9600);
 display.setBrightness(0x0a); //set the diplay to maximum brightness

 pinMode(D1, INPUT_PULLUP);
 pinMode(D2, INPUT_PULLUP);
 pinMode(D7, INPUT_PULLUP);
}
 
 
void loop()
{

 if (digitalRead(upbut) == 0)
 {
   person = person +1;
   total = total +1;
  if (person >50)
     {
       display.showNumberDec(8888);
     }
   delay(500);
 }

 if (digitalRead(downbut) == 0)
 {
   person = person -1;
   if (person <0)
   {
     person = 0;
   }
    delay(500);
 }

 if (digitalRead(totbut) == 0)
 {
   display.showNumberDec(total);
   delay(3000);
 }
 display.showNumberDec(person);
}



The program is straightforward. Nevertheless I will explain it here.

#include <TM1637Display.h>
 
const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display


The library is loaded and the CLK pin from the TM1637 is connected to D6 and the datapin to D5.

TM1637Display display(CLK, DIO);

The driver is started with a reference to the used pins.

void setup()
{
 Serial.begin(9600);
 display.setBrightness(0x0a); //set the diplay to maximum brightness

 pinMode(D1, INPUT_PULLUP);
 pinMode(D2, INPUT_PULLUP);
 pinMode(D7, INPUT_PULLUP);
}


The serial-port is initiated. Although this is not further used. I just added it for future expansion.
The TM1637 is set to maximum brightness and the pins for the buttons are defined as INPUT with the internal PULLUP resistors.

In the loop the magic happens.

 if (digitalRead(upbut) == 0)
 {
   person = person +1;
   total = total +1;
  if (person >50)
     {
       display.showNumberDec(8888);
     }
   delay(500);
 }


If the up button is pressed the person variable is increased AND the total variabele is increased. The person variable represents the number of persons present at that moment. The total variable contains the total amount of visitors.

If the person variable exceeds 50 the maxcimum number of people that are allowed is reached and the display shows 8888 as an error message.

 if (digitalRead(downbut) == 0)
 {
   person = person -1;
   if (person <0)
   {
     person = 0;
   }
    delay(500);
 }


If the downbutton is pressed the person variable is decreased. So one person less is present. Naturally a safety is build in so the counter can not get below 0.

 if (digitalRead(totbut) == 0)
 {
   display.showNumberDec(total);
   delay(3000);
 }
 display.showNumberDec(person);
}


If the Total button is pressed the display shows the total amount of people that has visisted the shop this day.

And that is all.

The person counter in real life.

Here is my breadboard setup.



When the grey button is pressed the display shows the total number of customers that has visited the show that day.
Pressing the green button ads one person to the person count and to the total.
Pressing the red button subtracts one person from the person count.



And here is the final version on stripboard.

Only one thing to do: design and print a case.

Why using an ESP8266.

An ESP8266 seems overkill for such a simple project. I could have used an Attiny85 as only 5 IO pins are used. However there are some considerations.
The ESP8266 has an USB port so the complete setup can be powered from a USB phone adapter. And the ESP8266 has Wifi !!!

Wifi can be used for future enhancements of the software.
There is a fixed limit of a maximum of 50 people in the software. I can build a webpage that allows me to alter this amount.
Look at my story about a time server: http://lucstechblog.blogspot.com/2020/02/time-is-of-essence.html
The software can be expanded  to measure how many customers are present every hour. This is valuable management information.
Another option is to measure which days are the busiest.

So enough possibilities for future expansions.
For now it works and that is the most important part.
We can see how many people are in the shop and that is important because if there are more as 50 people in the shop we risk a fine of several thousand euro's.

Till next time

Luc Volders