Projects / Cattle trough fill controller

Cattle trough fill controller

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. ESP32

    Test the valve

    Drive the gate by hand and check the valve clicks with the flyback diode fitted.

  2. ESP32

    Wire the float

    Test it with a bucket and watch the serial monitor.

  3. ESP32

    Check the logic

    Flip LOW_WHEN_CLOSED if the valve opens when the water is high.

  4. ESP32

    Upload the sketch

    Lower the float and watch it fill.

  5. ESP32

    Raise the float

    The valve should close and the fill time prints.

  6. ESP32

    Force an alarm

    Reduce MAX_FILL_MS and hold the float low.

  7. ESP32

    Press reset

    The alarm stops and the cycle restarts.

  8. ESP32

    Install

    Mount the float and keep the electronics dry.

In short

How do you build a cattle trough fill controller?

A float and valve fill a trough automatically and alarm if it never fills, a sign of a leak. You need ESP32 DevKit V1, Float switch (vertical), 12 V solenoid water valve, MOSFET IRLZ44N, Rectifier Diode 1N4007, Resistor 220 Ω, Resistor 10 kΩ, Active buzzer 5 V, Tactile push button 6 mm, 12 V 2 A DC adapter, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
12

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Float switchESP32 GPIO14 and GNDClosed when the water is low, check yours
ESP32 GPIO26220 Ω resistor then IRLZ44N gateValve drive
10 kΩ resistorIRLZ44N gate and groundPull-down so the valve is closed at reset
IRLZ44N sourceGroundGround
IRLZ44N drainValve -Switched return
12 V adapter +Valve +Valve supply
1N4007 diodeAcross the valve, bar to the + sideFlyback protection
12 V adapter -ESP32 GND and IRLZ44N sourceCommon ground
ESP32 GPIO27Buzzer +Alarm
Buzzer -ESP32 GNDGround
Reset buttonESP32 GPIO25 and GNDClears an alarm

Code

This is a low-voltage model with a 12 V solenoid valve for clean water. The valve closes if a fill takes longer than the limit, so a broken pipe or a stuck float cannot run water all day, then the buzzer sounds until you press reset and check the trough. Fit the float so it cannot jam, keep all electronics and wiring dry and away from the trough, and use only a valve rated for the water pressure, which is usually very low for a gravity tank. Add a mobile alert later with an ESP32 notification service of your choice, using placeholders for any account details. Logging the fill count shows daily water use, which helps spot leaks early. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Fills a trough, times each fill and alarms if a fill takes too long (a leak or a stuck valve).
const byte FLOAT_PIN = 14, VALVE = 26, BUZ = 27, RESET_BTN = 25;
const bool LOW_WHEN_CLOSED = true;                   // float switch closes when water is LOW
const unsigned long MAX_FILL_MS = 20UL * 60 * 1000;  // alarm if a fill takes more than 20 minutes

enum State { FULL, FILLING, ALARM };
State state = FULL;
unsigned long fillStart = 0;
int fillsToday = 0;

bool waterLow() {
  bool closed = (digitalRead(FLOAT_PIN) == LOW);
  return LOW_WHEN_CLOSED ? closed : !closed;
}

void setup() {
  Serial.begin(115200);
  pinMode(FLOAT_PIN, INPUT_PULLUP);
  pinMode(RESET_BTN, INPUT_PULLUP);
  pinMode(VALVE, OUTPUT); digitalWrite(VALVE, LOW);
  pinMode(BUZ, OUTPUT);
}

void loop() {
  switch (state) {
    case FULL:
      if (waterLow()) { digitalWrite(VALVE, HIGH); fillStart = millis(); state = FILLING; Serial.println("filling"); }
      break;
    case FILLING:
      if (!waterLow()) {
        digitalWrite(VALVE, LOW); fillsToday++; state = FULL;
        Serial.printf("full after %lu s, fills today %d\n", (millis() - fillStart) / 1000, fillsToday);
      } else if (millis() - fillStart > MAX_FILL_MS) {
        digitalWrite(VALVE, LOW);                    // close the valve so a leak cannot run on
        state = ALARM; Serial.println("ALARM: fill too long, possible leak");
      }
      break;
    case ALARM:
      digitalWrite(BUZ, (millis() / 400) % 2);
      if (digitalRead(RESET_BTN) == LOW) { digitalWrite(BUZ, LOW); state = FULL; delay(300); }
      break;
  }
  delay(200);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Float lowValve opens
Float upValve closes and the fill time prints
Float held low past the limitValve closes and the buzzer sounds
Reset pressedBack to normal

If it does not work

Valve never opens

Check the 12 V supply, the gate drive and the diode direction.

Valve opens when full

Flip LOW_WHEN_CLOSED.

Alarm too soon

Increase MAX_FILL_MS for a slow supply.

ESP32 resets

Check the common ground and the supply.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the cattle trough fill controller?

ESP32 DevKit V1, Float switch (vertical), 12 V solenoid water valve, MOSFET IRLZ44N, Rectifier Diode 1N4007, Resistor 220 Ω, Resistor 10 kΩ, Active buzzer 5 V, Tactile push button 6 mm, 12 V 2 A DC adapter, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the cattle trough fill controller take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the cattle trough fill controller works?

Float low: Valve opens. Float up: Valve closes and the fill time prints. Float held low past the limit: Valve closes and the buzzer sounds.

Valve never opens: what should I check?

Check the 12 V supply, the gate drive and the diode direction.

Valve opens when full: what should I check?

Flip LOW_WHEN_CLOSED.

Alarm too soon: what should I check?

Increase MAX_FILL_MS for a slow supply.

ESP32 resets: what should I check?

Check the common ground and the supply.