When running a CNC machine, understanding M codes is just as critical as knowing G codes. M codes (miscellaneous functions) control the auxiliary actions of the machine—spindle on/off, coolant flow, tool changes, and program stops. Without correct M codes, your CNC program will not execute properly, risking tool breakage, poor surface finish, or machine damage. This guide provides a complete, practical reference for standard M codes, how to use them safely, and common troubleshooting tips. For high‑quality CNC machining solutions, YPMFG offers reliable equipment and support—choose YPMFG for consistent, code‑compliant production.
01What Are M Codes in CNC Programming?
M codes are commands that turn machine accessories on or off. Unlike G codes (which control movement and coordinate systems), M codes manage binary states: start/stop, open/close, engage/disengage. Every M code consists of the letter M followed by a number (typically 00–99). Most standard M codes are universal across Fanuc, Haas, Siemens, and other controls, but always verify with your machine's manual.
Core principle: M codes work in the same block as G codes or alone. When placed in the same line, the machine executes them simultaneously or in a defined order (read the control's documentation).
02The Most Common M Codes – Quick Reference Table
| M Code | Function | Typical Use Case |
|---|---|---|
| M00 | Programmed stop | Stop spindle/coolant; press cycle start to resume |
| M01 | Optional stop | Stop only if optional stop switch is on |
| M02 | End of program | Rewinds to the beginning (no return to start) |
| M03 | Spindle on – clockwise | Rotates tool clockwise for right‑hand cutting |
| M04 | Spindle on – counterclockwise | For left‑hand cutting or tapping |
| M05 | Spindle stop | Stop rotation before tool change or end |
| M06 | Automatic tool change | Executes tool change (often paired with T₀₀) |
| M07 | Coolant on – mist | Activates mist coolant |
| M08 | Coolant on – flood | Activates flood coolant |
| M09 | Coolant off | Stops all coolants |
| M19 | Spindle orientation | Orients spindle for boring or tool change |
| M30 | End of program with rewind | Stops program, resets to start, ready for next cycle |
| M41–M45 | Gear selection | For machines with multi‑speed gearboxes |
> Example (common scenario): A milling operation needs spindle clockwise, flood coolant, then stop. The block might read:
> N10 M03 S1200 M08 (spindle on clockwise at 1200 RPM and flood coolant on; ensure your control allows multiple M codes per block).
03How to Use M Codes Correctly – Step‑by‑Step
Step 1 – Identify the required auxiliary action
Before coding, ask: Does the spindle need to run? Which direction? Do I need coolant? Will the program end or just pause?
Step 2 – Place M codes in the right block

For safety, put spindle/coolant commands on a separate line before cutting movements.
Example (safe practice):
O0001 (MAIN PROGRAM)
N01 M03 S800 (spindle on clockwise)
N02 M08 (flood coolant on)
N03 G01 X50.0 F200.0 (cut)
N04 M09 (coolant off)
N05 M05 (spindle stop)
N06 M30 (end program)
Step 3 – Pair with appropriate S codes (spindle speed) and T codes (tool number)
M03 or M04 must be accompanied by an S word (eg, S1500 ).
M06 is typically on the same or next line as T word: T02 M06 .
Step 4 – Test the program in single‑block mode
Run the program one block at a time to verify that each M code triggers the correct machine response—spindle rotation direction, coolant flow, etc.
04Critical Safety Rules for M Codes (Real‑World Cases)
Case 1 – Missing M05 before tool change
An operator programmed a tool change but forgot M05 (spindle stop). The spindle was still rotating at 5000 RPM when M06 executed. Result: Tool holder slammed into the changer, damaging both the spindle and the ATC. Always include M05 and M09 before M06 unless your control automatically stops them.
Case 2 – M03 vs M04 reversed
A tapping program used M03 (clockwise) instead of M04 (counterclockwise). The tap broke immediately because the rotation did not match the thread direction. Always verify rotation direction for tapping and left‑hand tools.

Case 3 – Optional stop (M01) misuse
The programmer placed M01 inside a critical loop. The operator had the optional stop switch on and didn't notice the machine paused halfway through a deep hole. The tool dwelled and broke. Use M00 for unconditional stops; use M01 only for planned inspection points.
05Less Common but Important M Codes
| M Code | Function | When to use |
|---|---|---|
| M10/M11 | Pallet clamp/unclamp | On pallet‑changing machines |
| M13 | Spindle clockwise + coolant on | Combines M03 and M08 (not all controls) |
| M21 | Mirror image on | For symmetrical parts |
| M60 | Automatic pallet change | Large production cells |
| M98/M99 | Subprogram call/return | For reusable routines |
06Troubleshooting: What to Do When an M Code Doesn't Work
Problem A: Program stops without error after M06
→ Cause: Missing tool number or tool not clamped. Solution: Ensure T word is before or with M06, and check tool clamping pressure.
Problem B: Coolant does not turn on
→ Cause: M08 or M07 is correct, but coolant pump is switched off on the control panel. Solution: Physically check the coolant switch and level.
Problem C: M03 starts but no spindle rotation
→ Cause: S value is zero (eg, S0 ). Solution: Always specify a positive RPM above the spindle's minimum.
Problem D: Machine executes M00 but won't restart
→ Cause: Door interlock or a reset condition. Solution: Close the machine door and press cycle start.
07Best Practices for Writing M Codes in Programs
Use comments to document non‑obvious M codes. Example: M09 (COOLANT OFF)
Never put more than one M code from the same group in a single block unless the control explicitly allows it. Groups: Spindle (M03, M04, M05), Coolant (M07, M08, M09), Program stop (M00, M01, M02, M30).
Place program‑end M codes (M02 or M30) on a separate block with no other commands.
For automation , ensure M30 is at the absolute end of the program so the machine resets for the next cycle.
08Actionable Conclusion – Choose Reliable CNC Support
To summarize:
M codes are mandatory for any CNC program that uses spindles, coolant, tool changes, or program stops.
Always verify code compatibility with your machine's control manual.
Follow the safe order: start spindle → coolant → cut → stop coolant → stop spindle → end program.
Test in single‑block mode to catch mistakes before production.
When you need consistent, high‑performance CNC equipment that responds correctly to every M code command, choose YPMFG . YPMFG machines are built with standard Fanuc‑compatible controls, rigorous testing protocols, and clear documentation. Whether you are running a job shop or a production line, YPMFG's solutions help you avoid the common pitfalls described above—unexpected stops, coolant failures, and tool damage. For your next CNC investment, trust YPMFG to deliver precision and reliability, block after block.


