- Home
- PLC Solution Library
- Simulators
- Occupancy counter
PLC Solution Library · Occupancy counter simulator
Count who comes in and who goes out, and turn the light red at the limit
A shop or a car park with a capacity limit has to know, at every moment, how many people or vehicles are inside. In this simulator, one ESP32 PLC 21 CORE reads two beams or two loops at each entrance, tells entries from exits, keeps the count and drives the traffic light and the barrier.
The simulator also shows what the PLC cannot see: how many are really inside. Run a scenario and watch where the count and reality drift apart.
Choose your scenario
Each button sends one person or one vehicle through the entrance. The scenarios send many, with their own timing.
Your design choices
These are choices you make in your own counter program. They are explained below.
Example limits
| Site | Limit | Amber from | Sensors |
|---|---|---|---|
| Shop door | 20 people | 18 | 2 beams, one door both ways |
| Car park, public | 34 vehicles | 31 | 2 loops per lane, entry and exit lanes |
| Car park, card holders | 40 vehicles | - | Card reader on RS-485 |
Example values for this simulator. The car park keeps 6 of its 40 spaces for card holders, so the FULL sign for the public turns on at 34. Beams A and B are 15 cm apart, a person crosses the door in about 2 s, a beam blocked for more than 4 s raises an alarm. Your limits come from your licence, your fire safety plan and your layout.
Counter record
| time | entrance | sensor sequence | PLC decision | count | really inside |
|---|
One row per sequence that the PLC closed. Your PLC can keep each row with the time of its real-time clock and publish the count, so a screen at the door or a dashboard in the office shows it. The column really inside exists only in this simulator.
How does a PLC tell an entry from an exit?
With two sensors a short distance apart, A on the outside and B on the inside. The order in which they break gives the direction. The PLC follows the four states of the pair, the same way it reads the two channels of an encoder.
- Entry: A breaks, then A and B together, then only B, then both clear. The PLC adds 1.
- Exit: the same states in the opposite order, B first. The PLC subtracts 1, but never goes below 0.
- Turned back: A breaks and clears again without B, or the sequence starts at A and ends at A. Nobody passed, so the PLC counts nothing.
- Blocked: a sensor stays broken for more than 4 s. Someone stands in the door, or a box covers the beam. The PLC raises an alarm and the count does not move until the beam clears.
With one sensor only, every break counts as an entry: people who leave through the same door, and people who turn back, add to the count. Set Count a pass when to Sensor A only and run the Busy shop scenario.
How does the light work at the limit?
The PLC compares the count with two thresholds. Below the amber threshold the light is green. From amber to the limit it is amber: almost full. At the limit it turns red, and at a car park the FULL sign turns on and the entry barrier does not open for the public.
When does it leave red? With limit minus 1, one person out lets one person in, and the light changes on every pass at a busy door. With limit minus 3, the light stays red until three have left, so people at the door get a clear signal and enter in small groups. You choose between a full shop and a calm door.
At the car park, card holders have 6 reserved spaces. The PLC asks the card reader for the card number on RS-485 and opens the barrier for a valid card while the total stays below 40, even when the sign says FULL for the public. Run the Full car park scenario.
The light, the sign and the barrier guide people and vehicles, they are not a safety function. Emergency exits must never depend on the count. The barrier controller keeps its own safety loop or photocell, so the boom never closes on a vehicle, as the standard for power operated gates and barriers asks (EN 12453). The PLC only gives the open command and reads the barrier state.
Why does the count drift, and how do you correct it?
A beam sees a shadow, not a person. Some passes give a sequence that does not match what really happened, and each one moves the count away from reality by one.
- Two people side by side break the beams together and give one entry. The count is one short.
- Two people very close, one behind the other, can also give one long sequence and one count.
- One person in and one out at the same moment break the beams in a mixed order. The PLC may read one direction for two people, or none.
- A trolley or a pram can break the beams twice for one person, depending on its height and the height of the beams.
Small errors add up over a day. Three ways to keep them small: mount the beams at a height and distance that suit your door, add a staff button for plus 1 and minus 1, and set the count to zero when the site is closed and empty. For tighter counts, use a people counter made for the job, such as a thermal or stereo camera sensor, and read its count over Modbus or a pulse output.
Run the Counting errors scenario. At the end the shop closes empty, and the count shows what drifted during the day.
What happens to the count after a power cut?
A count kept only in RAM starts again at zero. At a car park with 30 vehicles inside, the light turns green, the barrier opens for the public, and the car park fills over its limit while drivers search for spaces that do not exist.
The ESP32 has non-volatile storage (NVS) in its flash memory. The PLC writes the count there at each change and reads it back at start-up. NVS spreads its writes over the flash, which helps the memory last with many writes a day. Check the number of writes of your site against the flash endurance in the ESP32 documentation, or save every few seconds instead of at each change.
While the PLC has no power, it sees no passes. A count saved in flash is right only if nobody passed during the cut. A barrier that stays closed without power keeps vehicles out, and the exit must still let them out. Run the Power cut scenario with both options of Keep the count after a power cut.
Design choices you make
1. One sensor or two
One sensor per door is enough when a door is only an entry or only an exit. A door used both ways needs two sensors, or a counter that gives the direction itself.
2. When the light leaves red
A gap between the count that turns the light red and the count that clears it, a hysteresis, avoids a light that flickers at the limit. A bigger gap leaves the site a little below its limit for longer.
3. Where the count lives
The count lives in the PLC, next to the sensors. The screen at the door and the dashboard only show it. If the network or the screen fails, the light and the barrier keep working with the right count.
4. The reset at closing time
The real-time clock of the PLC knows when the site closes. When nobody passes for 30 minutes after that time, the PLC sets the count to zero and writes the count it had before in its record, so you can see how much it drifted. If staff stay inside after closing, set the time later or reset from a key switch.
5. How fast the inputs must be
People and cars are slow. A break of a beam lasts tenths of a second, so the PLC reads it in its normal program loop. Interrupt inputs matter for fast pulses, such as an encoder, not for a door.
The case studies behind this simulator
This simulator puts together patterns from three of our cases: a shop that counts people at the door with a capacity limit, a service station that counts the vehicles on the road and in the service area, and a company car park with RFID cards and a barrier. Each case describes its own site. This simulator shows the counting logic they share.
- Shop occupancy control with an M-Duino PLC: a photocell at the door, a capacity on the panel and a message when the shop is full
- Service station traffic with an ESP32 PLC: vehicles on the road, entries, exits and stay time
- RFID car park access with an M-Duino: the user list on the PLC and the gate in front of the car
What you see here is a general pattern. The limits, counts, times and faults are examples, not data from those cases.
To open a gate from a database instead of a card reader, try the gate control simulator.
Learn the building blocks
These examples cover the parts of this counter on an ESP32 PLC.
- Persistent counters in NVS on an ESP32 PLC (PLC Solution Library), to keep the count after a power cut
- Production pulse counter on an ESP32 PLC (PLC Solution Library), to count clean pulses from a digital input
- NTP-synced real-time clock on an ESP32 PLC (PLC Solution Library), for the closing time
- A Modbus TCP register map (PLC Solution Library), to publish the count and the limit to a screen
More examples: PLC Solution Library · all simulators.
ESP32 PLC 21 CORE: the specs that matter here
| Inputs | 13: 6 analog/digital (0-10 V, 11-bit) and 7 digital optoisolated (5 to 24 Vdc), 2 with interrupt |
|---|---|
| Outputs | 8: 5 digital optoisolated (5 to 24 Vdc, 70 mA) and 3 analog/digital/PWM (0-10 V, 12-bit, 20 mA) |
| Communication | Ethernet 10/100, Wi-Fi 2.4 GHz, Bluetooth LE, RS-485, RS-232 or TTL serial (DIP switch), I2C, SPI |
| Controller | ESP32 dual core, up to 240 MHz, 16 MB flash, 520 KB SRAM |
| Built in | Real-time clock DS3231 with backup battery |
| Power supply | 12 to 24 Vdc, with polarity protection |
| Mounting | DIN rail TS35, IP20, inside a cabinet |
| Programming | Arduino IDE with the Industrial Shields ESP32 boards package |
Example I/O for the car park
| Signal | PLC I/O | Count |
|---|---|---|
| Loop detectors: entry A and B, exit C and D (relay contacts) | Digital inputs, optoisolated | 4 |
| Barrier up: entry and exit | Digital inputs, optoisolated | 2 |
| Staff buttons: count plus 1, count minus 1, acknowledge | Digital input and analog/digital inputs used as digital | 3 |
| Barrier fault from the barrier controllers | Analog/digital input used as digital | 1 |
| Open command: entry barrier and exit barrier | Digital outputs, optoisolated, through interposing relays | 2 |
| Entrance light green and red, FULL sign | Digital outputs, optoisolated, through interposing relays | 3 |
| Entrance light amber | Analog/digital output used as digital, through an interposing relay | 1 |
| Card reader at the entry | RS-485 | 1 |
| Count, limit and alarms to a screen or a dashboard | Ethernet or Wi-Fi | 1 |
This example uses 10 of the 13 inputs and 6 of the 8 outputs. At the shop door, the same PLC needs only 2 inputs for the beams, 3 outputs for the light and one network link for the screen. Loop detectors and beams with a relay or a 24 V PNP output connect to the optoisolated inputs. The digital outputs give up to 70 mA and the analog/digital outputs up to 20 mA, so lamps and barrier commands go through interposing relays. For more entrances, add a second PLC on the network or choose a larger ESP32 PLC. Check your levels against the ESP32 PLC family datasheet. Full specs: ESP32 PLC 21 CORE · ESP32 PLC family.
FAQ
Which beam sequence is an entry, an exit or a person who turns back?
With beam A outside and beam B inside, an entry is A, then A and B, then B, then both clear. An exit is the same in the opposite order. A sequence that starts and ends on the same beam means someone turned back, and the PLC counts nothing. The PLC adds or subtracts 1, never goes below zero, and compares the count with the limit to drive the light at the door.
Why are two sensors needed at a door used both ways?
One sensor only says that something passed, not in which direction. With one sensor, people who leave and people who turn back add to the count. Two sensors give four states, and the PLC follows them like the two channels of an encoder to tell an entry from an exit and to ignore someone who turns back.
Why does a people count drift during the day?
Because a beam sees a shadow, not a person. Two people side by side give one entry, two people very close can give one long sequence, and a trolley can break the beams twice. Each case moves the count by one. Staff buttons for plus 1 and minus 1, a reset to zero when the site is closed and empty, and a counter made for people keep the error small.
How does a car park know it is full?
Each lane has two induction loops with a loop detector. The order in which the loops see the vehicle gives the direction, and the PLC keeps the count of vehicles inside. When the count reaches the limit for the public, the PLC turns on the FULL sign and does not open the entry barrier for public vehicles. Card holders can still enter while reserved spaces are free.
How do you keep the count after a power cut?
Save it in non-volatile memory and read it back at start-up. On an ESP32 PLC, the NVS storage in flash keeps the count, and it spreads its writes over the flash. A count kept only in RAM starts again at zero, and a full site looks empty. Passes during the cut are not seen in either case.
Can the occupancy count be shown on a screen at the entrance?
Yes. The PLC keeps the count and publishes it over Ethernet or Wi-Fi, for example as Modbus TCP registers or MQTT messages. A panel PC or a display reads it and shows it next to the limit. The count stays in the PLC, so the light and the barrier keep working if the screen or the network fails.
Is an occupancy counter a safety system?
No. The light, the sign and the barrier guide people and vehicles. Emergency exits must stay free whatever the count says, and the barrier controller keeps its own safety loop or photocell so the boom never closes on a vehicle. The PLC gives commands and reads states. It does not replace those functions.
Send me this architecture
Tell us what you count, how many entrances and lanes you have, what the limit is and where the count must be shown. Our team replies with a proposed architecture for your site. Your simulator settings go with the request.