Skip to content

Project 2.1.12: Irrigation Failure Alert System

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


Overview

This project checks whether an irrigation pump is actually moving water when a watering cycle is commanded.

Students will combine a relay-driven pump output, a flow or pulse sensor, an alert buzzer, and a status LED into a fault-detection system.

The final system should run a short irrigation test window, count flow pulses, and warn the user if the measured flow is below the minimum expected threshold.

Required Components

Flow sensor or pulse sensor
Flow sensor or pulse sensor
LED and 220 Ω resistor
LED and 220 Ω resistor
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Push button
Push button
1-channel relay module
1-channel relay module
Pump or flow test rig
Pump or flow test rig
Buzzer
Buzzer
Breadboard and jumper wires
Breadboard and jumper wires

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Flow sensor output GPIO 4 GPIO 4 / physical pin 6 Pulse-count input
Start button GPIO 18 to GND GPIO 18 / physical pin 24 Active-low cycle request
Relay IN GPIO 15 GPIO 15 / physical pin 20 Pump control
OK LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 Lights when flow is acceptable
OK LED cathode GND Any GND pin LED return
Buzzer positive GPIO 17 GPIO 17 / physical pin 22 Failure alarm output
Buzzer negative GND Any GND pin Buzzer return

Wiring Diagram

Circuit Diagram

Raspberry Pi Pico 2 W
┌─────────────────────────────────┐
│                                 │
│  GPIO 4  ───────────────────────┤──── Flow sensor output
│  GPIO 18 ───────────────────────┤──── Start button ─── GND
│  GPIO 15 ───────────────────────┤──── Relay IN
│  GPIO 16 ─ 220 Ω ─ LED ─────────┤──── Flow OK indicator
│  GPIO 17 ───────────────────────┤──── Buzzer positive
│  GND     ───────────────────────┤──── Common ground
│                                 │
└─────────────────────────────────┘

Pump power path:
External supply positive -> Relay COM
Relay NO -> Pump positive
Pump negative -> External supply negative

Relay module power:
Relay VCC -> Module supply as labelled
Relay GND -> Pico GND and external supply GND where shared grounding is required

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: Place the Flow Sensor

Place the flow sensor in the test water path where it can measure water movement.

Identify VCC, GND, and signal output before wiring.

Step 3: Connect the Flow Sensor

Wire the flow sensor power pins according to the module label.

Connect the flow sensor GND to the Pico GND reference.

Connect the flow sensor output to GPIO 4.

Confirm the sensor output is safe for 3.3V Pico GPIO before powering the circuit.

Step 4: Place the Start Button

Place the start push button across the breadboard center gap.

Connect one side of the start button to GPIO 18.

Connect the opposite side of the start button to GND.

Step 5: Place and Wire the Relay Module

Identify relay VCC, GND, IN, COM, NO, and NC before wiring.

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 6: Place the OK LED

Place the OK LED on the breadboard.

Identify the long leg as the anode (+) and the short leg as the cathode (-).

Connect the OK LED long leg through a 220 Ω resistor to GPIO 16.

Connect the OK LED short leg to GND.

Step 7: Place and Connect the Buzzer

Place the buzzer on the breadboard and identify its positive (+) and negative (-) pins.

Connect the buzzer positive pin to GPIO 17.

Connect the buzzer negative pin to GND.

Step 8: 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] Flow sensor output connects to GPIO 4
  • [x] Start button connects between GPIO 18 and GND
  • [x] Relay IN connects to GPIO 15
  • [x] OK LED long leg connects through a 220 Ω resistor to GPIO 16
  • [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 external pump supply negative
  • [x] No loose jumper wires

Intermediate Note

If possible, spin or trigger the flow sensor manually before using the pump. This confirms the Pico can count pulses before water is involved.

Safety Note

Do not power the pump from the Pico. Keep electronics away from water, use an external pump supply, and never run the pump dry while testing flow.


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

  • Build the start-button, LED, buzzer, and flow-sensor inputs before connecting the live pump.
  • Make sure the flow sensor is installed in the correct direction if it is an inline type.

Test the input sensor

  • Test the start button and confirm it triggers a new measurement cycle.
  • If possible, spin or trigger the flow sensor manually to verify that pulses can be counted.
  • If the pulse count stays at zero, check the sensor direction and GPIO 4 wiring.

Test the output device

  • Verify the relay switching before connecting the real pump.
  • Confirm that the buzzer sounds only when the measured pulses are below the expected minimum.
  • Confirm that the OK LED lights only when the pulse count is acceptable.

Run the full system

  • Run one normal irrigation cycle and note the pulse count.
  • Then simulate a blocked or low-flow condition and confirm that the failure alert is triggered.
  • Increase or decrease MIN_PULSES only after measuring real healthy and failed flow counts.

Save the project

  • Save the final code.
  • Record the minimum pulse threshold used in your setup.
  • Write down what a healthy pulse count looked like during a normal cycle.

Additional Testing and Calibration Checks

  • Measure a normal pulse count first so MIN_PULSES is based on your actual sensor and flow rate.
  • A short test window is safer than a long continuous pump run while debugging.
  • If the sensor output is noisy, increase the sampling stability or adjust the expected pulse threshold slightly.
  • Test the buzzer alert only after you know the pulse count logic is correct.

Quick testing checklist

  • [ ] Start button triggers a test cycle
  • [ ] Relay switches the pump path correctly
  • [ ] Flow pulses are counted during the run window
  • [ ] The OK LED lights when flow is acceptable
  • [ ] The buzzer alerts when flow is below the minimum threshold

Full Project Code

from machine import Pin
import time

# GPIO 4 is treated as a LOW pulse from the flow sensor.
# Change this to 1 if your sensor outputs HIGH pulses.
FLOW_PULSE_VALUE = 0
RELAY_ON = 0
RELAY_OFF = 1
RUN_SECONDS = 5
MIN_PULSES = 10

flow_sensor = Pin(4, Pin.IN, Pin.PULL_UP)
start_button = Pin(18, Pin.IN, Pin.PULL_UP)
relay = Pin(15, Pin.OUT)
ok_led = Pin(16, Pin.OUT)
buzzer = Pin(17, Pin.OUT)

relay.value(RELAY_OFF)
ok_led.off()
buzzer.off()


def is_pressed(button):
    return button.value() == 0


def wait_for_release(button):
    while is_pressed(button):
        time.sleep_ms(50)


def flow_pulse():
    return flow_sensor.value() == FLOW_PULSE_VALUE


def count_flow_pulses(seconds):
    count = 0
    previous_state = flow_pulse()
    end_time = time.ticks_add(time.ticks_ms(), seconds * 1000)

    while time.ticks_diff(end_time, time.ticks_ms()) > 0:
        current_state = flow_pulse()
        if current_state and not previous_state:
            count += 1
        previous_state = current_state
        time.sleep_ms(10)

    return count


def beep_alert():
    for _ in range(3):
        buzzer.on()
        time.sleep_ms(150)
        buzzer.off()
        time.sleep_ms(150)


print("Irrigation failure alert system ready")

while True:
    if is_pressed(start_button):
        print("Test irrigation cycle started")
        relay.value(RELAY_ON)
        pulse_count = count_flow_pulses(RUN_SECONDS)
        relay.value(RELAY_OFF)

        if pulse_count >= MIN_PULSES:
            ok_led.on()
            print("Irrigation OK | pulses:{}".format(pulse_count))
        else:
            ok_led.off()
            print("ALERT: irrigation may have failed | pulses:{}".format(pulse_count))
            beep_alert()

        wait_for_release(start_button)

    time.sleep_ms(200)

How the Code Works

Code Section What It Does Why It Matters
FLOW_PULSE_VALUE Defines which GPIO 4 state counts as a flow pulse Makes the sensor logic explicit and easy to correct for your flow sensor
count_flow_pulses() Counts pulse edges during the irrigation window The system needs a measurable sign that water actually moved
MIN_PULSES Defines the minimum acceptable flow during the test window A simple threshold turns raw pulses into a pass/fail decision
Start button Lets students run a controlled test cycle on demand Manual testing is safer than leaving a fault test always armed
Relay control Runs the pump only during the short test window Prevents continuous pump operation while debugging
Failure alert outputs Use the buzzer and Shell message to flag missing flow Operators need a clear sign when irrigation failed
OK LED Lights when the pulse count is acceptable Gives a simple visual pass/fail indication

Expected Result

Pressing the start button should run one short irrigation cycle. If the flow sensor produces enough pulses, the OK LED should light and the Shell should report a healthy pulse count.

If the pulse count is too low, the relay should switch off, the OK LED should stay off, the buzzer should sound, and the Shell should report an irrigation failure.


Troubleshooting

Problem Possible cause Solution
The pulse count is always zero The flow sensor is miswired, installed backwards, or no real movement is reaching it Check the sensor direction, wiring, and whether water is actually moving through the device.
The buzzer always sounds MIN_PULSES is too high or the flow is genuinely weak Measure a real healthy pulse count and lower MIN_PULSES if needed.
The OK LED never turns on The flow threshold is never passed Print the raw pulse count and compare it with the minimum threshold.
The pump runs but the sensor never changes The flow sensor is not in the correct place or does not output pulses as expected Reposition the sensor and verify its output type in the kit documentation.
The relay clicks but the pump does not run The pump power path is incomplete or the external supply is off Check relay COM/NO wiring, pump supply voltage, and pump supply current rating.