Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 19 of 47 · Industrial automation · Beginner

Production counter with batch alarm

A beam-break sensor counts every part that passes on a conveyor. Five LEDs show progress to a batch of five, and a horn sounds when the batch is complete.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Production counter with batch alarm: the board, its power supply and the parts wired to the 40-pin header
Machine safety. This project only shows signalling and logic. A real emergency stop must cut power through hard-wired safety-rated contacts and relays and must never depend on software or a Raspberry Pi.

Parts in this circuit

PartType
Beam sensorBeam sensor
Batch progressLED bar
ResetPush button
Batch hornBuzzer

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1RedBeam sensor - VCCPin 2 (5V)
2ColouredBeam sensor - OUTPin 11 (GPIO17)
3BlackBeam sensor - GNDPin 6 (GND)
4ColouredBatch progress - SignalPin 29 (GPIO5)
5ColouredBatch progress - SignalPin 31 (GPIO6)
6ColouredBatch progress - SignalPin 33 (GPIO13)
7ColouredBatch progress - SignalPin 35 (GPIO19)
8ColouredBatch progress - SignalPin 37 (GPIO26)
9BlackBatch progress - GNDPin 9 (GND)
10ColouredReset - SignalPin 13 (GPIO27)
11BlackReset - GNDPin 14 (GND)
12ColouredBatch horn - +Pin 12 (GPIO18)
13BlackBatch horn - -Pin 20 (GND)

Python code (gpiozero)

from gpiozero import Button, LEDBarGraph, Buzzer
from signal import pause

beam = Button(17)                       # beam broken = "pressed"
reset = Button(27)
bar = LEDBarGraph(5, 6, 13, 19, 26)     # shows the progress in the batch
horn = Buzzer(18)

BATCH = 5
total = 0

def part_passed():
    global total
    total += 1
    in_batch = total % BATCH
    print("Part", total, "- batch progress:", in_batch or BATCH, "of", BATCH)
    if in_batch == 0:
        bar.value = 1                    # all five LEDs on
        horn.beep(0.15, 0.1, n=3)
        print("BATCH COMPLETE:", total // BATCH, "batch(es) done")
    else:
        bar.value = in_batch / BATCH

def reset_count():
    global total
    total = 0
    bar.value = 0
    print("Counter reset")

beam.when_pressed = part_passed
reset.when_pressed = reset_count
print("Click the beam sensor to drop a part")
pause()

How the code works

You will learn

  • Counting events with a sensor and a global counter
  • Modulo (%) to find the position inside a batch
  • Why industry counts with sensors, not with timers

Try this

  • Drop five parts and listen for the horn.
  • Change BATCH to 3 and use only three LEDs.
  • Add a second beam that counts rejected parts separately.

On a real Pi

  • Use an industrial photoelectric sensor (NPN or PNP) and a level shifter or optocoupler. Never connect 24 V directly to a GPIO pin.
  • Real conveyors move fast, so a part may break the beam for only a few milliseconds. Python on a Pi is fine for slow lines. For fast counting use a PLC or a hardware counter.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
4330 ohm resistor, 1/4 W
One per LED
5Robu.in Amazon.in ThinkRobotics
5LED bar graph module (5 LEDs)1Robu.in Amazon.in ThinkRobotics
6Tactile push button (12 mm, with cap)1Robu.in Amazon.in ThinkRobotics
7Active buzzer, 5 V1Robu.in Amazon.in ThinkRobotics
8IR beam / break-beam sensor pair (3.3 V compatible)1Robu.in Amazon.in ThinkRobotics
9Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
10Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 18. Home alarm systemAll projects20. Machine stack light with start, stop and e-stop →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.