project
Water dispenser firmware that fails safe
Bare-metal C for a commercial three-output dispenser. Three files, no RTOS, and a tray sensor that locks everything off until it sees a fresh button press.
What I took away
On PIC mid-range parts, writing outputs to LAT rather than PORT is not optional. PORT writes do a read-modify-write on the physical pin voltage and can corrupt adjacent pins during fast transitions.
Safety-critical flags should default to their safe value at startup, not their permissive one, so the machine always begins in a known-safe posture regardless of external conditions.
A Hall sensor’s open-drain output can only pull low, never drive high — the idle high has to come from a pull-up. That also means a disconnected sensor reads high, which is the safe state here rather than a dangerous one. Worth checking that direction deliberately.
Firmware Engineering: Embedded Fluid Dispenser
Overview
I was brought onto this project to write the bare-metal firmware from scratch for a custom beverage dispenser PCB designed by a collaborator. The hardware utilizes a PIC16F877A microcontroller (running at 4 MHz) to drive a water pump and three discrete solenoid valves handling hot, cold, and sparkling water lines via NPN transistor switching.
The Challenge: Defensive Safety UX
While a standard two-state machine (NORMAL and TRAY_FULL) easily handles basic overflow lockout via a Hall effect tray sensor, consumer safety required a deeper look at physical user behavior.The primary risk was the “tray-replacement surprise.” If a user continuously holds down a dispensing button while sliding an emptied tray back into position, the firmware would naturally clear the error state and immediately trigger fluid flow. This creates an immediate risk of hot water burns or accidental spills.
The Solution: The “Fresh-Press” Lockout Rule
To eliminate this edge case, I implemented a strict input-arming sequence within the firmware:State Disruption: The instant the Hall effect sensor detects a full tray, all outputs are unconditionally killed, the status LED switches to red, and user buttons are locked out.The Fresh-Press Rule: Upon transitioning back to NORMAL state, a fresh_press_required software flag is set.Edge-Triggered Arming: The firmware continuously monitors the debounced button states. It refuses to drive any solenoids until it confirms a clean falling-edge transition—meaning the user must completely release all buttons before the system will accept a new, deliberate press.
Technical Stack
- Microcontroller: Microchip PIC16F877A (8-bit, 4 MHz)
- Language/Environment: Embedded C / MPLAB XC8
- Peripherals Used: GPIO (Button polling/interrupt debouncing, Transistor switching), External Interrupt / Pin Change (Hall effect sensor monitoring)