# CNC Water Flow Controller A fail-safe water flow monitoring module designed to protect CNC spindles and laser cutter tubes. It reads impulses from a ZJ-S401 water flow sensor, displays the real-time flow rate on an I2C OLED display, and triggers an active-low alarm to the CNC controller if the flow drops below a user-defined threshold. Designed in **KiCad v9**. ## Features * **Microcontroller:** WCH CH32V003J4M6 (RISC-V, SOP-8). * **Display:** 0.96" I2C OLED (SSD1315) with buttons. * **Adjustable Threshold:** 2-button interface (Up/Down) using an analog resistor ladder (ADC) to save MCU pins. * **Fail-Safe Output:** N-channel MOSFET (MMBT7002) acting as an open-drain output. It actively pulls the CNC input to GND during normal operation. If power fails, wires break, or water stops, the connection opens and triggers the CNC alarm. * **EMI Protection:** Hardware RC debouncing and noise filtering on the sensor input. ## Hardware Design (KiCad) The project contains the schematic and PCB layout files. * **Power:** 5V DC Input from the CNC controller. * **Sensor Compatibility:** ZJ-S401 (or similar 5V Hall-effect flow sensors). * **Output:** Connects directly to standard CNC active-low sinking inputs (internal optocoupler). ### CH32V003 Pin Mapping | Pin (SOP-8) | Name | Function | | :--- | :--- | :--- | | 1 | PC4 | Output: `ALARM_TRIGGER` (Drives MMBT7002 Gate) | | 2 | PD1 | SWIO (WCH-LinkE Programming) | | 3 | PC2 | I2C SCL (OLED Display) | | 4 | PC1 | I2C SDA (OLED Display) | | 5 | VDD | +5V Power | | 6 | VSS | Ground | | 7 | PA1 | Input: `FLOW_PULSE_MCU` (From Sensor via RC Filter) | | 8 | PA2 | ADC Input: `BUTTON_ADC` (2-Button Resistor Ladder) | ## Firmware Logic (Overview) 1. **Interrupts:** PA1 counts falling edges from the flow sensor. Flow (L/min) is calculated every second. 2. **Failsafe:** If `Current_Flow >= Threshold`, PC4 is driven **HIGH** (MOSFET ON, CNC OK). If `Current_Flow < Threshold`, PC4 is driven **LOW** (MOSFET OFF, CNC Alarm). 3. **UI:** ADC reads PA2 to detect button presses for adjusting the threshold. 4. **Storage:** User-defined threshold is saved to MCU flash memory. ## Development & Programming To flash the CH32V003, you will need a **WCH-LinkE** programmer connected to the 3-pin SWIO header (3V3/5V, SWIO, GND).