Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Tank level control with two float switches: the board, its power supply and the parts wired to the 40-pin header
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

PartType
Tank and floatsTank + floats
Pump contactorRelay 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.

#ColourFrom (part and leg)To (header pin)
1ColouredTank and floats - LOW floatPin 11 (GPIO17)
2ColouredTank and floats - HIGH floatPin 13 (GPIO27)
3BlackTank and floats - GNDPin 6 (GND)
4ColouredPump contactor - INPin 15 (GPIO22)
5BlackPump contactor - GNDPin 9 (GND)
6RedPump contactor - VCCPin 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

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.

#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
41-channel 5 V relay module with optocoupler
10 A 250 V AC contacts
1Robu.in Amazon.in ThinkRobotics
5230 V AC small water pump
Needs a proper starter and an RCCB
1Robu.in Amazon.in ThinkRobotics
6Closed ABS enclosure (IP54) for the mains side1Robu.in Amazon.in ThinkRobotics
73-way screw terminal block (rated 10 A)1Robu.in Amazon.in ThinkRobotics
8MCB 6 A single pole and RCCB 30 mA (by an electrician)
Mains protection
1Robu.in Amazon.in ThinkRobotics
9Float switch (water level), NO contact2Robu.in Amazon.in ThinkRobotics
10Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
11Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 23. Sequenced conveyor start and stopAll projects25. Take a photo with a button →

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.