If G-codes are the muscles of a CNC program telling the machine where to move, how fast to go, and what path to take then M-codes (Miscellaneous Functions) are the nervous system.
Without M-codes, your machine would be a high-precision paperweight. It might move its axes perfectly, but the spindle wouldn’t spin, the coolant wouldn’t flow, and the tool would never change. M-codes control the machine’s auxiliary hardware by communicating directly with the PLC (Programmable Logic Controller).
In this guide, we will break down the logic of M-codes, their universal standards, and the specific “dialects” used by Fanuc, Heidenhain, and Sinumerik (Siemens).
1. The Anatomy of an M-Code
Technically, an M-code is an “on/off” switch for a machine function. When the CNC interpreter reads an M-code, it pauses the execution of the motion (G-code) and sends a signal to the PLC. The PLC performs the action (like opening a door or starting a pump) and sends a “Finish” signal back to the NC. Only then does the program move to the next line.
Execution Timing
There are two ways an M-code can execute relative to motion:
- Simultaneous: The M-code triggers at the same time the axis starts moving (e.g., starting coolant while moving to the part).
- Sequential: The machine completes all motion in the block before executing the M-code (e.g., M00 Program Stop).
2. The “Universal” M-Code Library
While manufacturers can customize M-codes, the industry follows the RS-274 standard for the most critical functions.
Spindle Control
- M03: Spindle On (Clockwise). Required for 90% of standard milling and turning.
- M04: Spindle On (Counter-Clockwise). Used for left-hand tapping or back-turning.
- M05: Spindle Stop. Essential before tool changes or at the end of a program.
Program Flow
- M00 (Program Stop): The machine stops dead. The operator must press “Cycle Start” to continue. Great for checking a dimension or flipping a part.
- M01 (Optional Stop): Only stops the machine if the “Optional Stop” button on the control panel is toggled ON.
- M30 (Program End & Reset): The “Mic Drop” of CNC. It stops all functions, resets the program to the first line, and often shuts off the spindle and coolant.
Tooling and Coolant
- M06 (Tool Change): Triggers the Automatic Tool Changer (ATC).
- M08 (Coolant On): Floods the work area with cutting fluid.
- M09 (Coolant Off): Essential to stop the spray before the door opens or the tool changes.
3. M-Code Implementation: Fanuc vs. Heidenhain vs. Sinumerik
Each control system handles these “Miscellaneous” tasks with a slightly different philosophy.
Fanuc: The Traditionalist
Fanuc relies heavily on the standard 2-digit or 3-digit M-code format. It is very strict about one M-code per block (though some modern parameters allow more).
- M98 / M99: These are the pillars of Fanuc programming. M98 calls a subprogram (a separate file), and M99 returns to the main program or loops it.
- M19: Spindle Orientation. This locks the spindle in a specific angular position, which is vital for tool changes or “boring bar” retracts.
Heidenhain: The Integrated Approach
Heidenhain uses M-codes but often integrates them into “Miscellaneous Functions” that can be applied with more nuance.
- M128 / FUNCTION TCPM: As discussed in our RTCP guide, these M-codes aren’t just switches; they change the entire kinematic behavior of the machine.
- M3 / M4: In Heidenhain, these are often programmed within the
TOOL CALLblock rather than a standalone line. - M89 / M99: Used for cycle calls. M89 makes a cycle “modal,” meaning it executes at every subsequent coordinate until canceled.
Sinumerik (Siemens): The Flexible Powerhouse
Siemens allows for “Named M-codes.” While it supports the standard M03/M08, it also allows programmers to create custom commands that look like text.
- Active Trafo (TRAORI): While it acts like an M-code, it’s technically a transformation. However, many Siemens machines use M-codes to trigger specific table clamping or unclamping cycles.
- M17: The Siemens version of “End of Subroutine.”
- QUICK M-codes: Siemens can process M-codes without waiting for the PLC “Finish” signal to increase high-speed performance, provided the hardware allows it.
4. Advanced M-Code Strategies
M-Code Handshaking
When you call an M06, the following happens behind the scenes:
- NC sees
M06. - NC stops the look-ahead buffer.
- PLC checks if the spindle is stopped (M05) and at the “Z-home” position.
- PLC commands the arm to swap tools.
- PLC sends an “I’m done” signal to the NC.
- Program continues.
Failure to “Handshake”: If a sensor (like a “Tool Clamped” proximity switch) fails, the PLC never sends the finish signal. This is why your machine might “hang” on an M-code line without an error message—it’s waiting for a signal that never comes.
Custom M-Codes for Macro Triggers
Did you know you can create your own M-codes? In the parameters of a Fanuc or Siemens control, you can “link” a number (like M101) to a macro program (like O9001).
- Example: You want a code that automatically moves the table to the door and opens it. You could program M100. When the machine reads M100, it secretly runs a G-code routine:
G00 G91 G28 Z0;G00 G53 X0 Y0;M11 (Door Open);
5. Summary Table: Common M-Codes Across Platforms
| Function | Fanuc | Heidenhain | Sinumerik |
| Program Stop | M00 | M00 | M00 |
| Spindle CW | M03 | M3 | M3 |
| Tool Change | M06 | TOOL CALL | M06 |
| Coolant Flood | M08 | M8 | M8 |
| Subprogram Call | M98 | CALL PGM | EXTCALL |
| Spindle Orient | M19 | M19 | SPOS |
| End of Program | M30 | M30 / STOP | M30 |
6. Safety and Best Practices
- One M-Code per Line: Even if your control allows more, it’s safer to separate them. For example,
M03 M08might try to start the spindle and coolant at the same millisecond, potentially causing a power surge or a PLC conflict. - M05 is your Friend: Always ensure the spindle is stopped before a tool change or before moving an axis into a position where an operator might reach in.
- The “Air Cut”: When testing a new M-code (like a custom pallet change), run it without a part. M-codes control mechanical hardware that can crash just as hard as an axis move.
M-codes are the unsung heroes of the CNC world. Mastering them allows you to automate your workflow, decrease cycle times, and protect your machine hardware.
