Batch counter with a preset
Build it step by step
0 of 7 doneWire the sensor
VCC to 5V, GND, DO to D2.
Wire the buttons
D4 and D5 each to GND through a button.
Wire the lamp and buzzer
Red LED on D6 through 220 ohms, buzzer plus on D8.
Wire the display
5V or 3V3, GND, SDA to A4, SCL to A5.
Upload the sketch
The display shows the preset and a count of zero.
Pass items in front of the sensor
The count rises by one each time.
Reach the preset
The red LED lights and the buzzer beeps three times.
How do you build a batch counter with a preset?
An IR reflective sensor counts items as they pass, an OLED shows the count against a preset, and a beep and a red light tell the packer when the batch is full. Two buttons set the preset and reset the count. You need Arduino Nano, TCRT5000 IR reflective sensor module, SSD1306 OLED Display, Tactile push button 6 mm × 2, Passive buzzer, LED 5 mm red, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 100 minutes.
- Level
- Intermediate
- Time
- About 100 min
- Parts
- 9
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- DisplaysBuy
- SwitchesBuy
- Motors & ActuatorsBuyPassive buzzer × 1
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| TCRT5000 module VCC | Nano 5V | Power the sensor |
| TCRT5000 module GND | Nano GND | Common ground |
| TCRT5000 module DO | Nano D2 | Low when an item is seen (adjust the module's pot) |
| Preset button | Nano D4 and GND | Adds 5 to the preset |
| Reset button | Nano D5 and GND | Clears the count |
| Red LED long leg | Nano D6 through a 220 ohm resistor | Batch full |
| Passive buzzer plus | Nano D8 | Beep (minus to GND) |
| OLED VCC | Nano 5V | Power (check your OLED accepts 5 V, otherwise use 3V3) |
| OLED GND | Nano GND | Common ground |
| OLED SDA and SCL | Nano A4 and A5 | I2C, address 0x3C |
Code
The TCRT5000 module pairs an infrared LED with a light sensor. When an item passes in front of it, the light bounces back and the module output goes low. Set its little pot so the output only changes when an item is within a few centimetres and not for the belt or table behind. The sketch counts one item each time the output falls, ignoring anything within 120 milliseconds of the last count so a wobbling item is not counted twice. The OLED shows the count against the preset, and when the count reaches the preset the red LED lights and the buzzer beeps three times. The preset goes up in fives with one button and is kept in the Nano's memory, and the other button clears the count for the next batch. Use this for counting small items on a bench or a hand-fed tray. It must not control a machine or act as a guard, and keep fingers clear of any moving belt.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const uint8_t SENSOR = 2;
const uint8_t BTN_PRESET = 4;
const uint8_t BTN_RESET = 5;
const uint8_t LED = 6;
const uint8_t BUZZER = 8;
const unsigned long MIN_GAP_MS = 120; // two items cannot pass faster than this
unsigned int count = 0;
unsigned int preset = 20;
bool seen = false;
bool full = false;
unsigned long lastItem = 0;
void draw() {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("Preset ");
oled.print(preset);
oled.setTextSize(4);
oled.setCursor(0, 18);
oled.print(count);
oled.setTextSize(1);
oled.setCursor(0, 56);
oled.print(full ? "BATCH FULL" : "Counting");
oled.display();
}
void batchFullAlert() {
for (int i = 0; i < 3; i++) {
tone(BUZZER, 1500, 150);
delay(250);
}
}
void setup() {
pinMode(SENSOR, INPUT);
pinMode(BTN_PRESET, INPUT_PULLUP);
pinMode(BTN_RESET, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Wire.begin();
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
EEPROM.get(0, preset);
if (preset == 0xFFFF || preset == 0 || preset > 999) preset = 20;
draw();
}
void loop() {
bool item = digitalRead(SENSOR) == LOW;
if (item && !seen && millis() - lastItem > MIN_GAP_MS) {
lastItem = millis();
count++;
if (count >= preset && !full) {
full = true;
digitalWrite(LED, HIGH);
draw();
batchFullAlert();
}
draw();
}
seen = item;
if (digitalRead(BTN_PRESET) == LOW) {
preset += 5;
if (preset > 200) preset = 5;
EEPROM.put(0, preset);
full = (count >= preset);
digitalWrite(LED, full);
draw();
delay(300);
}
if (digitalRead(BTN_RESET) == LOW) {
count = 0;
full = false;
digitalWrite(LED, LOW);
draw();
delay(300);
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Pass one item | Count 1 |
| Hold an item still | Still 1 |
| Press the preset button | Preset rises by 5 |
| Count reaches the preset | Red light, three beeps, BATCH FULL |
| Press the reset button | Count zero, light off |
If it does not work
Counts with nothing there
Turn the module's pot until the output stays high.
Double counts
Raise MIN_GAP_MS or move the sensor so each item crosses once.
Missed items
Move the sensor closer or turn the pot the other way.
Display blank
Check the OLED address 0x3C and the wires.
Preset resets
Check the sketch writes it to EEPROM.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the batch counter with a preset?
Arduino Nano, TCRT5000 IR reflective sensor module, SSD1306 OLED Display, Tactile push button 6 mm × 2, Passive buzzer, LED 5 mm red, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the batch counter with a preset take to build?
About 100 minutes for a intermediate maker, in 7 steps.
How do I know the batch counter with a preset works?
Pass one item: Count 1. Hold an item still: Still 1. Press the preset button: Preset rises by 5.
Counts with nothing there: what should I check?
Turn the module's pot until the output stays high.
Double counts: what should I check?
Raise MIN_GAP_MS or move the sensor so each item crosses once.
Missed items: what should I check?
Move the sensor closer or turn the pot the other way.
Display blank: what should I check?
Check the OLED address 0x3C and the wires.
