Engineering · Raspberry Pi · Project 46 of 47 · Industrial automation · Intermediate
Bottle filling station
A beam sensor sees a bottle under the nozzle. The pump fills it for 3 seconds. A bar of LEDs counts filled bottles for each box of five.

CAUTION: 230 V single-phase mains. The relay in this project switches a mains load (Live and Neutral, 230 V AC, 50 Hz in India). Mains can kill. Practise with the simulator or a low-voltage lamp. Build the mains side only with a qualified electrician, in a closed insulated box, behind a fuse or MCB and a 30 mA RCCB. Never touch the relay screw terminals while anything is connected.
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
| Part | Type |
|---|---|
| Bottle sensor | Beam sensor |
| Filling pump | Relay module |
| Box of 5 | LED bar |
| Box full horn | Buzzer |
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.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Red | Bottle sensor - VCC | Pin 2 (5V) |
| 2 | Coloured | Bottle sensor - OUT | Pin 11 (GPIO17) |
| 3 | Black | Bottle sensor - GND | Pin 6 (GND) |
| 4 | Coloured | Filling pump - IN | Pin 15 (GPIO22) |
| 5 | Black | Filling pump - GND | Pin 9 (GND) |
| 6 | Red | Filling pump - VCC | Pin 4 (5V) |
| 7 | Coloured | Box of 5 - Signal | Pin 29 (GPIO5) |
| 8 | Coloured | Box of 5 - Signal | Pin 31 (GPIO6) |
| 9 | Coloured | Box of 5 - Signal | Pin 33 (GPIO13) |
| 10 | Coloured | Box of 5 - Signal | Pin 35 (GPIO19) |
| 11 | Coloured | Box of 5 - Signal | Pin 37 (GPIO26) |
| 12 | Black | Box of 5 - GND | Pin 14 (GND) |
| 13 | Coloured | Box full horn - + | Pin 12 (GPIO18) |
| 14 | Black | Box full horn - - | Pin 20 (GND) |
Python code (gpiozero)
from gpiozero import Button, OutputDevice, LEDBarGraph, Buzzer
from time import sleep
bottle = Button(17) # beam broken = bottle in place
pump = OutputDevice(22, active_high=False)
box = LEDBarGraph(5, 6, 13, 19, 26)
horn = Buzzer(18)
FILL_TIME = 3
BOX_SIZE = 5
filled = 0
print("Click the beam sensor to place a bottle")
while True:
bottle.wait_for_press()
print("Bottle detected: filling")
pump.on()
sleep(FILL_TIME)
pump.off()
filled += 1
in_box = filled % BOX_SIZE
print("Bottle", filled, "filled")
if in_box == 0:
box.value = 1
horn.beep(0.2, 0.1, n=3)
print("BOX FULL: replace the box")
sleep(2)
box.value = 0
else:
box.value = in_box / BOX_SIZE
How the code works
- The beam sensor is a Button: broken beam means pressed.
- The pump runs for FILL_TIME seconds. Nothing else is read while it runs, so a second bottle cannot start a second fill.
- The remainder (filled % BOX_SIZE) tells how many bottles are in the current box.
You will learn
- A timed process step
- Counting items and batches
- Using a beam sensor to start a cycle
Try this
- Click the beam sensor 5 times, 3.5 seconds apart.
- Click again while the pump is running. What happens?
- Add a stop button that switches the pump off at once.
On a real Pi
- A filling pump is a liquid pump. Keep the liquid, the Pi and all mains wiring apart and use a splash-proof box.
- Real fillers use a flow meter or a load cell, not only a timer. Time-based filling changes with the liquid level and the pump wear.
- Add a bottle-present check during filling, and stop the pump if the bottle is removed.
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.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 330 ohm resistor, 1/4 W One per LED | 5 | Robu.in Amazon.in ThinkRobotics |
| 5 | LED bar graph module (5 LEDs) | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Active buzzer, 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | 1-channel 5 V relay module with optocoupler 10 A 250 V AC contacts | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | 230 V AC small water pump Needs a proper starter and an RCCB | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Closed ABS enclosure (IP54) for the mains side | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | 3-way screw terminal block (rated 10 A) | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | MCB 6 A single pole and RCCB 30 mA (by an electrician) Mains protection | 1 | Robu.in Amazon.in ThinkRobotics |
| 12 | IR beam / break-beam sensor pair (3.3 V compatible) | 1 | Robu.in Amazon.in ThinkRobotics |
| 13 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 14 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
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.