The need
My brother is a winemaker. During bottling, he wanted to know how many bottles had been filled and corked, without counting them by hand.
A standalone ESP32 counter that detects each filled and corked bottle by infrared and shows the total live.

The counter at work during bottling
My brother is a winemaker. During bottling, he wanted to know how many bottles had been filled and corked, without counting them by hand.
On the machine, the bottles first go through a rotary system, then are placed on a conveyor. Counting happens on the rotary part: each time a bottle passes in front of it, the reflective infrared sensor detects it, the ESP32 increments the counter and the screen shows the new total.
The ESP32 drives a screen, a sensor, two buttons and two LEDs. The buttons are wired to ground and use the ESP32's internal pull-up resistors: a press pulls the input low.
| Part | ESP32 pin | Role |
|---|---|---|
| SSD1306 OLED screen | GPIO 2 (SDA), GPIO 15 (SCL) | Shows the total, I²C bus, address 0x3C |
| Reflective IR sensor | GPIO 13 | Output goes low when a bottle passes |
| Start/Stop button | GPIO 22 | Starts or pauses counting |
| Reset button | GPIO 23 | Resets the total to 0 |
| Green LED | GPIO 19 | Counting active |
| Red LED | GPIO 21 | Bottle in front of the sensor |
The program does not count the presence of a bottle but its arrival: a pass is counted on the falling edge of the sensor signal, when it goes from high to low. A bottle standing still in front of the sensor is therefore counted only once.
After each count, further edges are ignored for 150 ms. This debounce delay avoids counting a bottle twice when its edge or label makes the signal flicker. It also sets the maximum rate: 1 / 0.150 s ≈ 6.7 bottles per second, i.e. about 6.7 × 60 ≈ 400 bottles per minute, well above the machine's pace.
The total is saved to the ESP32's flash memory every 10 bottles, on stop and on reset: after a power cut during bottling, the counter restarts from the last saved total, with at most 9 bottles to recount.
I designed the housing in Inventor to fit an off-the-shelf universal clamp. It is made of two 3D-printed parts: the housing, which holds the electronics, the screen and the sensor, and a plate screwed onto the clamp. The counter can therefore be fitted to the machine without modifying it.

The project was first written in MicroPython, in four successive versions. The first lays the groundwork: a state machine that ignores detections that are too short, and saving the total. The next ones add the OLED screen, then the LEDs, then the splash screen and the large-digit display. The final version was rewritten in C++ with the Arduino environment. All versions are in the GitHub repo.