3.6 KiB
3.6 KiB
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.
Table of Contents
- Features
- Hardware Design (KiCad)
- Panel Design and GCode
- Microcontroller Pin Mapping
- Firmware Logic
- 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.
- Output: 3-pin CNC interface (+24V, GND, ALARM_CNC). Connects directly to standard CNC active-low sinking inputs.
Panel Design and GCode
The scripts folder contains a guide to panelize the board (Flow_Controller_Panel.kicad_pcb) and howto generate gcode.
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)
- Interrupts: Pin 1 (PD6) counts falling edges from the flow sensor. Flow (L/min) is calculated every second.
- Failsafe: If
Current_Flow >= Threshold, Pin 3 is driven HIGH (MOSFET ON, CNC OK). IfCurrent_Flow < Threshold, Pin 3 is driven LOW (MOSFET OFF, CNC Alarm). - UI: ADC reads Pin 7 (PC4) to detect button presses for adjusting the threshold.
- 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:
- Follow the setup guide in
firmwareto install the RISC-V GCC toolchain andminichlink. - Connect your WCH-LinkE programmer to the 3-pin SWIO header
J1(+3.3V, SWIO, GND). - Run
maketo compile the firmware. - Run
make flashto upload the code to the CH32V003 microcontroller.


