Engineering · Raspberry Pi · Project 24 of 47 · Industrial automation · Advanced
Tank level control with two float switches
The classic PLC water tank. A low float starts the pump, the pump runs until the high float is wet, and then stops. The tank simulates water level so you can watch the cycle.

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 |
|---|---|
| Tank and floats | Tank + floats |
| Pump contactor | Relay module |
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 | Coloured | Tank and floats - LOW float | Pin 11 (GPIO17) |
| 2 | Coloured | Tank and floats - HIGH float | Pin 13 (GPIO27) |
| 3 | Black | Tank and floats - GND | Pin 6 (GND) |
| 4 | Coloured | Pump contactor - IN | Pin 15 (GPIO22) |
| 5 | Black | Pump contactor - GND | Pin 9 (GND) |
| 6 | Red | Pump contactor - VCC | Pin 2 (5V) |
Python code (gpiozero)
from gpiozero import Button, OutputDevice
from time import sleep
low = Button(17) # pressed = water has reached the low float
high = Button(27) # pressed = water has reached the high float
pump = OutputDevice(22, active_high=False)
# Ladder rung: PUMP = (NOT low OR PUMP) AND NOT high
# - Water below the low float: the pump starts.
# - The PUMP contact holds it on while the water rises.
# - Water at the high float: the pump stops.
while True:
run = ((not low.is_pressed) or pump.value) and not high.is_pressed
if run != bool(pump.value):
pump.value = run
print("Pump ON (tank filling)" if run else "Pump OFF (tank full)")
sleep(0.1)
How the code works
- The low float is wet when the water level is above it. When the level falls below it, "not low.is_pressed" becomes true and the pump starts.
- Once the pump runs, the PUMP contact in parallel keeps the rung powered even though the low float is wet again. That gap between the floats is the hysteresis: the pump does not switch on and off at every ripple.
- When the water reaches the high float, the NC HIGH contact opens, the rung loses power and the pump stops.
- The tank in the simulator fills at 9 % per second with the pump on and empties according to the Water use slider.
You will learn
- Level control with two floats (hysteresis in hardware)
- A seal-in in a real process
- Reading a live ladder diagram
Try this
- Raise Water use to 100 %. Can the pump still fill the tank? Why not?
- Set Water use to 0 % and watch the pump stop at the high float.
- Add a "pump running too long" alarm that stops the pump after 30 seconds.
On a real Pi
- Real float switches use a magnet and a reed switch. Wire them NC or NO so a broken wire is safe, and use a pump contactor sized for the motor.
- Add a dry-run protection: if the pump runs but the level does not rise, stop it. This protects the pump.
- Use a PLC or a level controller for plant duty. This is a learning model.
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 | 1-channel 5 V relay module with optocoupler 10 A 250 V AC contacts | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 230 V AC small water pump Needs a proper starter and an RCCB | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Closed ABS enclosure (IP54) for the mains side | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | 3-way screw terminal block (rated 10 A) | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | MCB 6 A single pole and RCCB 30 mA (by an electrician) Mains protection | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Float switch (water level), NO contact | 2 | Robu.in Amazon.in ThinkRobotics |
| 10 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | 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.