Projects / Batch counter with a preset

Batch counter with a preset

Intermediate1.7 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the sensor

    VCC to 5V, GND, DO to D2.

  2. NANO

    Wire the buttons

    D4 and D5 each to GND through a button.

  3. NANO17.5 mA

    Wire the lamp and buzzer

    Red LED on D6 through 220 ohms, buzzer plus on D8.

  4. NANO

    Wire the display

    5V or 3V3, GND, SDA to A4, SCL to A5.

  5. NANO

    Upload the sketch

    The display shows the preset and a count of zero.

  6. NANO

    Pass items in front of the sensor

    The count rises by one each time.

  7. NANO17.5 mA

    Reach the preset

    The red LED lights and the buzzer beeps three times.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
TCRT5000 module VCCNano 5VPower the sensor
TCRT5000 module GNDNano GNDCommon ground
TCRT5000 module DONano D2Low when an item is seen (adjust the module's pot)
Preset buttonNano D4 and GNDAdds 5 to the preset
Reset buttonNano D5 and GNDClears the count
Red LED long legNano D6 through a 220 ohm resistorBatch full
Passive buzzer plusNano D8Beep (minus to GND)
OLED VCCNano 5VPower (check your OLED accepts 5 V, otherwise use 3V3)
OLED GNDNano GNDCommon ground
OLED SDA and SCLNano A4 and A5I2C, 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.

CheckExpected
Pass one itemCount 1
Hold an item stillStill 1
Press the preset buttonPreset rises by 5
Count reaches the presetRed light, three beeps, BATCH FULL
Press the reset buttonCount 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 →

Ask Lark AI

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.