Skip to content

Project 3.2.2: Automatic Sump Pump

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


Overview

The Pico reads a sump water-level sensor and switches a relay-driven pump on or off using hysteresis.

Sump pits need automatic response so rising water is removed before overflow causes flooding.

A Pico 2 W prototype with a water-level sensor, relay output, pump load, and LED indicator.

Water-level control, minimum run-time planning, and careful external-power safety for pumps.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Relay module
Relay module
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Water pump
Water pump
Water level sensor
Water level sensor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Water sensor VCC GPIO 17 GPIO 17 / physical pin 22 Optional switched sensor power
Water sensor GND GND Physical pin 38 Common ground
Water sensor AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Analog water reading
Relay IN GPIO 15 GPIO 15 / physical pin 20 Relay control
Relay VCC External 5V or module-rated supply Not a Pico GPIO pin Use the correct module supply
Relay GND GND Physical pin 38 Share ground unless fully isolated
LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 Pump state LED
LED cathode GND Physical pin 23 or 38 Ground return

Wiring Diagram

Automatic Sump Pump wiring diagram

  GPIO 17 -> water sensor VCC
  Water sensor AO -> GPIO 26
  GPIO 15 -> relay IN
  GPIO 16 -> 220 ohm resistor -> LED anode
  External supply -> relay-switched pump
  Common GND -> sensor, relay, LED

Step-by-Step Assembly

  1. Connect the water sensor output to GPIO 26 and its ground to Pico GND.
  2. Use GPIO 17 as the sensor power pin if you want to reduce constant probe exposure.
  3. Connect the relay control input to GPIO 15 and power the relay module correctly.
  4. Connect the LED to GPIO 16 through a 220 ohm resistor and connect the LED cathode to GND.
  5. Dry-test the relay before connecting the real pump and external water path.

Testing Individual Components

Before running the full project, test each subsystem separately. This makes it easier to find wiring, library, or logic problems before full integration.

  1. Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
  2. Test the input sensor: Measure dry and high-water raw values first so the control thresholds come from real conditions.
  3. Test the output device: Test the relay and LED safely without the pump connected.
  4. Test the decision logic: Confirm that the pump turns on at the high threshold and turns off only after the low threshold is crossed.
  5. Run the full system: Run the full system with controlled rising and falling water levels and confirm the printed state matches the hardware behavior.
  6. Validate the prototype: Observe whether different sensor depths or turbulence cause unstable switching.
  7. Save the project: Save the validated program on the Pico as main.py and keep a copy on the computer for future edits.

Full Project Code

After completing and checking the circuit connections, open Thonny IDE, copy and paste this code 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

POWER_PIN = 17
WATER_PIN = 26
RELAY_PIN = 15
LED_PIN = 16
PUMP_ON_LEVEL = 38000
PUMP_OFF_LEVEL = 24000
MIN_RUN_MS = 5000

power = Pin(POWER_PIN, Pin.OUT)
water = ADC(WATER_PIN)
relay = Pin(RELAY_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)
pump_on = False
pump_started_ms = 0

relay.off()
led.off()
print("Automatic sump pump ready")


def read_level(samples=6):
    power.on()
    time.sleep_ms(50)
    total = 0
    for _ in range(samples):
        total += water.read_u16()
        time.sleep_ms(10)
    power.off()
    return total // samples


while True:
    now = time.ticks_ms()
    level = read_level()

    if not pump_on and level >= PUMP_ON_LEVEL:
        pump_on = True
        pump_started_ms = now
    elif pump_on and level <= PUMP_OFF_LEVEL and time.ticks_diff(now, pump_started_ms) >= MIN_RUN_MS:
        pump_on = False

    relay.value(1 if pump_on else 0)
    led.value(1 if pump_on else 0)
    print("Water level: {} | Pump: {}".format(level, "ON" if pump_on else "OFF"))
    time.sleep(1)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Threshold and timing setup Defines pump on, pump off, and minimum run-time values These constants control whether the pump behaves smoothly or chatters Tune the thresholds only after collecting real high and low water readings
Sensor power and read_level Powers the sensor briefly and averages several samples This reduces corrosion and noisy switching behavior If your sensor requires continuous power, change the power strategy only after validation
Pump control logic Uses hysteresis and a minimum run-time to decide pump state A real pump system should not switch rapidly on tiny water changes Adjust MIN_RUN_MS after testing the physical pump response
Relay output Drives the relay and LED based on the pump state Students can compare the computed control decision with the hardware action If your relay module is active low, invert the relay output logic here

Expected Result

The serial monitor reports the current reading or state clearly.

The hardware output responds when the decision logic changes state.

Subsystem behavior matches the thresholds, timing, or rules described in the document.

Validation Checks

  • Normal condition test: confirm the system stays in its safe or idle state under baseline conditions
  • Warning condition test: move the input close to the chosen threshold and confirm the transition is sensible
  • Critical condition test: trigger the strongest alert or control state and confirm the output response is correct
  • Calibration test: compare the sensor or timing against a known reference or repeated trial
  • Limitation test: deliberately create an awkward or noisy condition and note how the prototype behaves

Deployment and Limitations

  • This prototype could be used in school, farm, or sanitation demonstrations with safe low-voltage loads
  • Before deployment, the load wiring, enclosure, and power design need careful review
  • Actuator systems need maintenance planning because relay wear and sensor drift affect reliability

Troubleshooting

Problem Possible Cause Solution
Pump short-cycles On and off thresholds are too close or minimum run-time is too short Increase the hysteresis gap or lengthen MIN_RUN_MS
Pump never stops Off threshold is too low or the sensor reading never falls enough Measure actual low-water values and recalibrate PUMP_OFF_LEVEL
Pump never starts High threshold is too high or the relay path is wrong Lower PUMP_ON_LEVEL temporarily and dry-test the relay
Sensor reading drifts Probe corrosion or turbulence is affecting the signal Dry the probe after testing and consider a more robust sensor style