ESP32 Projects

Object Counter with IR Sensor & OLED Display using ESP32

Learn How to Make a Smart Object Counter with ESP32, IR Sensor & OLED Display – Full Tutorial

Last Updated on August 13, 2025 by Engr. Shahzada Fahad

Object Counter with ESP32:

Object Counter with IR Sensor & OLED Display using ESP32- Have you ever wanted to count how many times something passes a certain point? like

  • People entering a room
  • Boxes on a conveyor belt or even just
  • How many times you waved your hand?

An ESP32 object counter uses an IR sensor to detect items, displaying the total on a bright OLED screen.

Well today, we are building a smart Object Counter using an IR proximity sensor, an ESP32, and an OLED display.




And here is the coolest part; just wave your hand in front of the IR Sensor and the number goes up instantly on the screen.

No delay. No contact. Just smooth real-time counting.

And that’s not all; even if the system turns off, or there’s a power failure; or you intentionally reset the controller, your count does not disappear!

You know Why?

Because we have got it saved safely in the EEPROM; kind of like a memory card for your numbers.

It will stay right there, ready for you when the system powers back on.

The only way to reset it back to zero; is by pressing this pushbutton.

A complete DIY electronics project: a working object counter built with an ESP32 board and an IR sensor.

Total control; in your hands.



Now some of you might be wondering

“Why didn’t I just use an Arduino?”

Well, here’s the deal;

I picked the ESP32 for a couple of really good reasons.

First, the ESP32 comes with built-in Bluetooth, so later on, I can send this counter value wirelessly to my phone; no need to connect any extra Bluetooth modules. This way I can monitor the counter value in my custom designed Android application.

The ESP32 gives you everything in one package, making your project simple, compact, and powerful.

Live data from an ESP32 object counter, with the current tally displayed on a bright OLED screen.

Second, you already know; the ESP32 also has WiFi built right in; and that opens up a world of possibilities!

I can send this data to the cloud; whether it’s Blynk, Ubidots, Thingspeak, Google Sheets; you name it.

Basically, what I am trying to say is; you can send this number anywhere in the world in real-time.

So if you are planning to expand this project later; maybe build a people counter for your shop, or track items remotely; ESP32 is definitely the way to go. I highly recommend it.



Amazon Links:

ESP32 WiFi + Bluetooth Module (Recommended)

IR Proximity Sensor DFRobot

SSD1306 Oled Display Module

Other Tools and Components:

Arduino Nano USB C type (Recommended)

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Components Used in This Project:

In this project, we are using the ESP32 Dev Module, a Digital Adjustable Infrared Sensor from DFRobot, a pushbutton, and the SSD1306 OLED Display. I am sure you know about most of these sensors expect the IR Sensor.

The DFRobot IR proximity sensor, used to detect items for an accurate electronic object counter.

This Digital IR Proximity Sensor has an adjustable detection range of 0 to 200 centimeters.

  • This sensor uses infrared light to detect objects
  • it sends out a beam, and when an object comes in front of it, the beam reflects back to the sensor.

Right here at the back of the sensor, you will see this tiny screw; this is used to adjust the detection distance.

You can rotate it to increase or decrease how far the sensor can detect objects.

And right next to it, there is a small red LED; this is just an indicator light.

Whenever the sensor detects an object, this LED turns on instantly, letting you know that the sensor has been triggered. You can clearly see in the above image; my hand is in front of the sensor so that’s why the RED LED is ON.

It’s compact;  just 75 by 18 millimeters; and can be used in many applications like counting objects, or even as a smart door trigger.

It gives a digital output:

  • 0 means no object detected
  • 1 means an object is in range



IR Sensor Specification

  • Output Form: PNP normally open
  • Signal Type: Digital signal
  • Operating Voltage: 3~5V
  • Interface: XH2.54-3Pin
  • Detection range: 0~200 cm
  • Switch Level: 0, No obstacles in the area; 1, There are obstacles in the area.
  • Operating Temperature: -15~60 ℃
  • Temperature Effect: Within -15 ℃ to + 60 ℃, the detection distance is below 10% at + 25 ℃
  • Storage Temperature: -25~75 ℃
  • Humidity: 35% ~ 95% RH
  • Size: 75 * Φ18 mm

Object Counter Circuit and Interfacing:

We have connected the signal wire of the IR Proximity Sensor to GPIO 25 on the ESP32. Connect the other two wires to the 3.3V and GND.

The pushbutton is connected to GPIO 15, and is used to reset the counter value.

For the display, we are using the SSD1306 OLED. Connect its SDA and SCL pins to the GPIOs 21 and 22 on the ESP32 Dev Module. Connect its voltage and ground pins to the ESP32’s 3.3V and GND pins.

Now, here is what is happening behind the scenes:




 ESP32 Object Counter Arduino Code



How the Code Works: Saving to EEPROM

First, we include the necessary libraries to talk to the OLED display and to save data in the ESP32’s internal memory using the Preferences library. We define the screen size, and then create a display object using the Adafruit SSD1306 library. Next, we set up two GPIO pins — one for the IR sensor, which is connected to pin 25, and one for the reset pushbutton on pin 15..

We use an interrupt for the IR sensor, so whenever an object passes by and triggers the sensor, the handleIR function runs instantly. We have added a small debounce delay of 300 milliseconds so it doesn’t count the same object multiple times. Each time an object is detected, the counter increases, and we mark the display for update and the value to be saved.

In the setup function, we initialize Serial, read the last saved counter value from EEPROM, and prepare the OLED display. If the OLED doesn’t initialize, we stop everything and show an error in the serial monitor. Once everything is good, we show the current count on the screen.

In the main loop, we check if there’s a new count to show; if yes, we update the display. If the value needs to be saved, we write it back to EEPROM so that even if the ESP32 resets or loses power, the counter won’t be lost. Finally, we check if the reset button is pressed; and if it is, we clear the counter, update the display, and also reset the saved value in memory. All this happens smoothly in real time; making the system fast, reliable, and super responsive.

Object Counter Practical Demonstration:

Watch this; I wave my hand in front of the sensor;

A complete DIY object counter setup, where an item is detected and tallied as it moves past the sensor.

And boom! The number increases!

Let me try again.

Another wave; and there it goes again; the count goes up.

The complete system for a DIY object counter, featuring an ESP32 microcontroller and an IR proximity sensor.



Now I will press the pushbutton on GPIO 15.

A pushbutton connected to an ESP32 board, used to reset the value on an electronic object counter.

And just like that; the counter resets to zero.

An ESP32 object counter using EEPROM to save the tally, ensuring the count persists after power loss.

But here is the real magic; on the display you can see the value is 8.

Now I will turn off the ESP32.

An ESP32 circuit demonstrating how to use EEPROM to store data for a reliable object counter system.

Turn it back on.

A DIY object counter project demonstrating how to write the count value to the ESP32's EEPROM for storage.

And look at that; the count is still there, saved perfectly, just like before.

That’s the power of using EEPROM.

So, that’s all for now.



Watch Video Tutorial:

 


Discover more from Electronic Clinic

Subscribe to get the latest posts sent to your email.

Engr. Shahzada Fahad

Engr. Shahzada Fahad is an Electrical Engineer with over 15 years of hands-on experience in electronics design, programming, and PCB development. He specializes in microcontrollers (Arduino, ESP32, STM32, Raspberry Pi), robotics, and IoT systems. He is the founder and lead author at Electronic Clinic, dedicated to sharing practical knowledge.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Discover more from Electronic Clinic

Subscribe now to keep reading and get access to the full archive.

Continue reading

Electronic Clinic
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.