Skip to content

Project 2.6.34: Smart Water Tank Refill System

Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython


Overview

This project builds a water tank refill controller that starts filling when one water-level sensor reports a low level and stops when the same sensor reports a full level.

Students will use one analog water-level sensor, a relay-controlled pump, a status LED, and a buzzer for timeout alerts.

The final system should start the pump below the low threshold, stop it at the full threshold, and shut down safely if filling takes too long.

Required Components

Water level sensor module
Water level sensor module
LED and 220 Ω resistor
LED and 220 Ω resistor
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
1-channel relay module
1-channel relay module
Small DC pump
Small DC pump
External power supply
External power supply
Buzzer
Buzzer
Breadboard and jumper wires
Breadboard and jumper wires

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Water-level sensor VCC 3.3V Physical pin 36 Use a 3.3V-safe sensor output.
Water-level sensor GND GND Any GND pin Common ground
Water-level sensor AOUT GPIO 26 GPIO 26 / physical pin 31 ADC water-level input
Relay IN GPIO 15 GPIO 15 / physical pin 20 Pump control
Status LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 Pump activity indicator
Buzzer positive GPIO 17 GPIO 17 / physical pin 22 Short fault beeps only
Pump supply path Relay COM to NO contacts Not a Pico GPIO External power only

Wiring Diagram

smart water tank refill system

Raspberry Pi Pico 2 W

GPIO 26  <--- Water-level sensor AOUT
GPIO 15  ---> Relay IN
GPIO 16  ---> 220 ? resistor ---> Status LED anode
GPIO 17  ---> Buzzer positive
3.3V     ---> Water-level sensor VCC
GND      ---> Water-level sensor GND, LED cathode, buzzer negative, and relay GND

Relay COM ---> External pump-supply positive
Relay NO  ---> Pump positive
Pump negative ---> External pump-supply negative

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.

Step 2: Position the Water-Level Sensor

Place the water-level sensor in the test tank so it can measure the changing water level. Keep its electronics above the water line and identify its VCC, GND, and AOUT pins before wiring.

Step 3: Connect the Water-Level Sensor

Connect sensor VCC to 3.3V, GND to Pico GND, and AOUT to GPIO 26. Use a 3.3V-safe analog output.

Step 4: Place and Wire the Relay Module

Identify relay VCC, GND, IN, COM, NO, and NC. Connect relay IN to GPIO 15. Power the relay module from the supply required by its label. Connect relay GND to Pico GND and to the external pump supply GND where shared grounding is required.

Step 5: Place the Status LED

Place the status LED on the breadboard. Connect its long leg through a 220? resistor to GPIO 16. Connect its short leg to GND.

Step 6: Place the Buzzer

Place the buzzer on the breadboard and identify its positive (+) and negative (-) pins. Connect the buzzer positive pin to GPIO 17 and the buzzer negative pin to GND.

Step 7: Wire the Pump Through the Relay

Connect the external pump supply positive wire to relay COM. Connect relay NO to the pump positive lead. Connect the pump negative lead to the external pump supply negative wire.

Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] Water-level sensor VCC connects to 3.3V
  • [x] Water-level sensor GND connects to GND
  • [x] Water-level sensor AOUT connects to GPIO 26
  • [x] Relay IN connects to GPIO 15
  • [x] Status LED long leg connects through a 220? resistor to GPIO 16
  • [x] Status LED short leg connects to GND
  • [x] Buzzer positive pin connects to GPIO 17
  • [x] Buzzer negative pin connects to GND
  • [x] Pump positive path uses relay COM and NO
  • [x] Pump negative connects to the external pump supply negative
  • [x] No loose jumper wires

Intermediate Note The code uses internal pull-ups and treats a wet sensor as LOW. Test the low and high sensors one at a time before connecting the pump.

Safety Note Keep the Pico, breadboard, USB cable, and jumper wires away from water. Use an external pump supply, confirm where incoming water will go, and never let the pump run dry.


Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring, library, or code problems.

Hardware Setup

  • Mount the single water-level sensor securely in the test tank before powering the system.
  • Keep its electronics and all Pico wiring above the water line.

Test the Input Sensor

  • Print the analog water-level percentage while the water level changes.
  • Record typical empty and full readings, then adjust LOW_LEVEL and FULL_LEVEL if necessary.

Test the Output Device

  • Test the relay, LED, and buzzer before connecting the real pump.
  • Confirm that the buzzer sounds only when the maximum-fill timeout is reached.

Run the Full System

  • Start with the level below LOW_LEVEL and confirm that the pump starts.
  • Raise the water level above FULL_LEVEL and confirm that the pump stops.
  • Check that the max-fill timeout protects the system if the tank does not fill as expected.

Save the Project

  • Save the final code and record the empty and full sensor readings used to set the thresholds.
  • Record how long a normal refill takes so you can choose a sensible maximum-fill timeout.

Additional Testing and Calibration Checks

Calibration and tuning notes

  • Adjust LOW_LEVEL and FULL_LEVEL using readings from your own tank and sensor placement.
  • Use a short timeout during classroom testing so a fault condition can be demonstrated without waiting too long.
  • Test the refill path with plain water and a small tank before scaling up.

Quick testing checklist

  • [ ] The sensor percentage changes as the water level changes
  • [ ] Pump starts only below LOW_LEVEL
  • [ ] Pump stops at or above FULL_LEVEL
  • [ ] Buzzer sounds when the maximum-fill timeout is reached
  • [ ] The timeout stops the pump if the tank never fills

Full Project Code

After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below into a new file, or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.

from machine import ADC, Pin
import time

RELAY_ON = 0
RELAY_OFF = 1
MAX_FILL_S = 60
LOW_LEVEL = 35
FULL_LEVEL = 75

water_sensor = ADC(26)
relay = Pin(15, Pin.OUT)
status_led = Pin(16, Pin.OUT)
buzzer = Pin(17, Pin.OUT)

filling = False
fill_started = 0

relay.value(RELAY_OFF)
status_led.value(0)
buzzer.value(0)


def water_level_percent():
    return int((water_sensor.read_u16() / 65535) * 100)


def alarm_burst():
    for _ in range(2):
        buzzer.on()
        time.sleep(0.15)
        buzzer.off()
        time.sleep(0.15)


def start_fill(reason):
    global filling, fill_started
    relay.value(RELAY_ON)
    status_led.value(1)
    filling = True
    fill_started = time.time()
    print("Pump ON:", reason)


def stop_fill(reason):
    global filling
    relay.value(RELAY_OFF)
    status_led.value(0)
    filling = False
    print("Pump OFF:", reason)


print("Smart water tank refill system ready")

while True:
    level = water_level_percent()
    now = time.time()

    if filling and level >= FULL_LEVEL:
        stop_fill("tank reached full threshold")
    elif (not filling) and level <= LOW_LEVEL:
        start_fill("water level below low threshold")
    elif filling and (now - fill_started) >= MAX_FILL_S:
        stop_fill("maximum fill time exceeded")
        alarm_burst()
    else:
        print("WaterLevel:{}% Status:{}".format(level, "REFILLING" if filling else "MONITORING"))

    time.sleep(1)

How the Code Works

Code Section What It Does Why It Matters
water_level_percent() Converts the sensor's ADC reading to a percentage Makes the changing water-level value easy to use and understand
LOW_LEVEL and FULL_LEVEL Define the single sensor's start and stop thresholds The pump starts low and stops when the same sensor reports full
MAX_FILL_S Stops the pump if filling takes too long A timeout can prevent dry running or overflow from a failed sensor
alarm_burst() Uses the buzzer only for a timeout fault Students learn that alarms should signal problems, not normal operation

Expected Result

When the water level is below LOW_LEVEL, the pump should start and the LED should turn on. When the same sensor reports a level at or above FULL_LEVEL, the pump should stop. If filling takes too long, the system should stop the pump and beep the buzzer.


Troubleshooting

Problem Possible cause Solution
The pump never starts The sensor reading is above LOW_LEVEL, or the relay is miswired Print the water-level percentage and verify RELAY_ON / RELAY_OFF
The pump never stops FULL_LEVEL is too high or the sensor reading does not increase Check the sensor position and lower FULL_LEVEL after testing
The buzzer alarms immediately MAX_FILL_S is too short Increase MAX_FILL_S after measuring a normal refill time
The timeout always trips The water path is blocked or the tank cannot reach FULL_LEVEL Inspect the tubing and pump flow, then verify the sensor reading