Cattle trough fill controller
Build it step by step
0 of 8 doneTest the valve
Drive the gate by hand and check the valve clicks with the flyback diode fitted.
Wire the float
Test it with a bucket and watch the serial monitor.
Check the logic
Flip LOW_WHEN_CLOSED if the valve opens when the water is high.
Upload the sketch
Lower the float and watch it fill.
Raise the float
The valve should close and the fill time prints.
Force an alarm
Reduce MAX_FILL_MS and hold the float low.
Press reset
The alarm stops and the cycle restarts.
Install
Mount the float and keep the electronics dry.
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
- Development BoardsBuyESP32 DevKit V1 × 1
- SensorsBuy
- Motors & ActuatorsBuy
- SemiconductorsBuyMOSFET IRLZ44N × 1
- SemiconductorsBuy
- PassivesBuyResistor 220 Ω × 1
- PassivesBuyResistor 10 kΩ × 1
- Motors & ActuatorsBuy
- SwitchesBuy
- PowerBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Float switch | ESP32 GPIO14 and GND | Closed when the water is low, check yours |
| ESP32 GPIO26 | 220 Ω resistor then IRLZ44N gate | Valve drive |
| 10 kΩ resistor | IRLZ44N gate and ground | Pull-down so the valve is closed at reset |
| IRLZ44N source | Ground | Ground |
| IRLZ44N drain | Valve - | Switched return |
| 12 V adapter + | Valve + | Valve supply |
| 1N4007 diode | Across the valve, bar to the + side | Flyback protection |
| 12 V adapter - | ESP32 GND and IRLZ44N source | Common ground |
| ESP32 GPIO27 | Buzzer + | Alarm |
| Buzzer - | ESP32 GND | Ground |
| Reset button | ESP32 GPIO25 and GND | Clears 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.
| Check | Expected |
|---|---|
| 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 |
| Reset pressed | Back 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 →
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.
