# 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. ![Flow Controller Schema](images/Flow_Controller.svg) ![Front](images/Flow_Controller_front.png) ![Back](images/Flow_Controller_back.png) ![Panel](images/Flow_Controller_Panel.png) ![Case](CAD/Case.png) ![Case_exploded](CAD/Case_exploded.png) ![ZJ-S401](images/Sensor_ZJ-S401.png) ## Table of Contents 1. [Features](#features) 2. [Hardware Design (KiCad)](#hardware-design-kicad) 3. [Panel Design and GCode](#panel-design-and-gcode) 4. [Microcontroller Pin Mapping](#microcontroller-pin-mapping) 5. [Firmware Logic](#firmware-logic) 6. [Firmware Build & Flashing](#firmware-build--flashing) ## Features * **Microcontroller:** WCH CH32V003J4M6 (RISC-V). * **Display UI:** 8-pin connector for a custom OLED/Button panel (I2C SSD1315). * **Adjustable Threshold:** 2-button interface (Up/Down) using an analog resistor ladder (ADC) to save MCU pins. * **Fail-Safe Output:** N-channel MOSFET (2N7002) 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. * **Direct 24V Operation:** Powered directly from the CNC controller's 24V rail. ## Hardware Design (KiCad) * **Power:** 24V DC Input (via CNC connector J3). * **Sensor Compatibility:** ZJ-S401 Water flow sensor (plastic valve body, a water rotor and a hall-effect sensor). See [the working principle of the hall water flow sensor](https://www.seeedstudio.com/blog/2020/05/11/how-to-use-water-flow-sensor-with-arduino/). * **Output:** 3-pin CNC interface (+24V, GND, ALARM_CNC). Connects directly to standard CNC active-low sinking inputs. ## Panel Design and GCode The [../kicad2panel](../kicad2panel) folder contains a guide to panelize the board. The [../kicad2msla](../kicad2msla) folder contains a guide to use the MSLA for UV exposure. The [../kicad2gcode](../kicad2gcode) folder contains a guide to generate G-code to use CNC for drilling and cutting (optionally also for the isolation trace routing). ## Microcontroller Pin Mapping (Per Schematic) | Pin | Schematic Net | Function | MCU Port (SOP8) | | :--- | :--- | :--- | :--- | | 1 | `FLOW_PULSE_MCU` | Input: Pulse counter from Flow Sensor | PD6 | | 2 | `GND` | System Ground (VSS) | VSS | | 3 | `ALARM_TRIGGER` | Output: Drives the Gate of the 2N7002 | PA2 | | 4 | `+24V` | System Power (VDD) | VDD | | 5 | `I2C_SDA` | I2C Data (OLED Display) | PC1 | | 6 | `I2C_SCL` | I2C Clock (OLED Display) | PC2 | | 7 | `BUTTON_ADC` | ADC Input: 2-Button Resistor Ladder | PC4 | | 8 | `SWIO` | WCH-LinkE Programming Interface | PD1 | ## Firmware Logic (Overview) 1. **Interrupts:** Pin 1 (PD6) counts falling edges from the flow sensor. Flow (L/min) is calculated every second. 2. **Failsafe:** If `Current_Flow >= Threshold`, Pin 3 is driven **HIGH** (MOSFET ON, CNC OK). If `Current_Flow < Threshold`, Pin 3 is driven **LOW** (MOSFET OFF, CNC Alarm). 3. **UI:** ADC reads Pin 7 (PC4) to detect button presses for adjusting the threshold. 4. **Storage:** User-defined threshold is saved to MCU flash memory. ## Firmware Build & Flashing The firmware is written in C using the lightweight `ch32fun` framework. All source files and toolchain documentation are located in the `firmware` folder. **Step-by-step Setup:** 1. Follow the setup guide in [`firmware`](firmware) to install the RISC-V GCC toolchain and `minichlink`. 2. Connect your **WCH-LinkE** programmer to the 3-pin SWIO header `J1` (+3.3V, SWIO, GND). 3. Run `make` to compile the firmware. 4. Run `make flash` to upload the code to the CH32V003 microcontroller.