Table of Contents

m code in cnc machining

M-Code in CNC Machining: Meaning, Importance, and Types

CNC programming is fundamental to the use of modern CNC machines. These machines operate on different command codes, such as the M code, which allow them to deliver precise and accurate results, mitigating human error.

However, these CNC machine instructions often vary from machine to machine, making them complex to implement and need to be input by an expert CNC programmer. Thus, understanding the intricacies of the M codes is integral to achieving optimal CNC machining benefits.

This guide discusses m code, its importance, and its common types. Continue reading to the end to learn how to program CNC machines with M-code, standard M-code simulators, and typical machines that use M-code commands!

What Is M-Code in CNC Programming?

sample m code program
Sample M-code Program

M code, or miscellaneous code, is a core aspect of CNC machining that works with G-codes in CNC software. While the G-code directs the machine’s geometric movements, the M-code dictates the machine’s non-geometric movements. M codes often begin with the letter “M” and include other auxiliary commands essential for controlling non-geometric CNC machine functions. CNC programmers use only one M code in each program information block to avoid overlapping commands, which can result in programming conflict and operational errors.

The M-code specifically handles various machine functions, including coolant control, spindle speed, turning the spindle on and off, and tool change. As such, M code represents the manufacturing operation’s backbone, allowing the CNC machine to execute precise actions other than simple movements. Machine operators often input these programs into a CNC machine using CAD or CAM software. This computer-aided manufacturing (CAM) software helps expedite manufacturing by automating the code generation process.

Furthermore, M-code is a commonly used source code in MATLAB script and functions. M codes are used in binary offset carriers, spot beam transmissions, and modernized Navstar security algorithms. In some cases, programmers use M code to send military signals, reject false signals, and identify and remove any potential erroneous data points from a dataset.

Origin of M-Code Programming

M-code programming dates back to the mid-20th century, when the transition from labor-intensive manual to automated manufacturing was evident. John T. Parsons and Frank L. Stulen are often regarded as the pioneering engineers who developed numerical control concepts. The first CNC machines compatible with M-code and G-code were invented through the Massachusetts Institute of Technology (MIT) collaborative efforts and funding from the U.S. Air Force. Consequently, this innovation significantly transformed the manufacturing industry, allowing unparalleled precision and production efficiency.

Why Use M-Code Commands in CNC Machining?

m code words list
M-Code Words List

CNC programmers use several M codes due to their extensive operational benefits. Here are some of the common reasons why M-code commands are necessary for CNC programs:

  • Integration with G-Code: Generally, M-code doesn’t work in isolation. It functions in tandem with the G-code to form a comprehensive
  • Greater Flexibility: Computer numerical control machines execute extensive manufacturing tasks with M-code. The versatile nature of M code facilitates a wide range of applications in product manufacturing, whether it is turning on the coolant (M08, stopping the spindle (M05), changing a tool (M06), or deactivating a clamp (M11).
  • Optimization of CNC Operations: M-code improves the CNC machining operation by offering particular instructions for different machine functions. This machine control language facilitates the automation of machine actions such as tool change and program stop and on. Hence, it reduces human intervention and potential machining errors.

How Does a M-Code Command Work?

First, One must understand the integration of an M-code with CNC programming and its specific actions to understand its workings. More importantly, M-codes are sets of CNC machine instructions that direct the machine’s non-geometric operation and functions.

For example, the M03 and M04 control the spindle on and off, while the command M06 instructs the CNC machine to change the machining tool. These instructions were written in CNC programming language, and the CNC machine’s control system interprets it and executes the command. The working principles of M-code can be categorized into two major aspects including:

  • M-Code Programming: This involves directing the CNC machine’s actions with specific codes. These codes are usually written in a particular sequence and format based on the requirement of the particular CNC machine used.
  • Execution and Control: The CNC machine reads and implements the commands once the M-code has been programmed. The machine controller handles the command code interpretation and executes the expected actions, ensuring accurate and consistent operations.

How to Read M Codes

To read M code commands correctly, you must be familiar with the specific codes and their corresponding function. Generally, an M-code command comprises the letter “M” accompanied by a number, each symbolizing a particular machine function. These codes are drafted within the CNC program and read by the machine’s control unit, interpreting them into precise actions. Take the M05 code, for example. It reads as “Spindle Off”. Hence, CNC programmers or machine operators must understand the meaning behind each machine control code to ensure a seamless and accurate machining process.

How to Program a CNC Machine with M-Code Commands

You must understand the G- and M-codes well to program a CNC machine with M-codes successfully. Besides, CNC machine programming can be done manually, in operation, or with CAD/CAM systems. It would help if you used specific codes to command auxiliary functions like tool changes, coolant, spindle rotation, etc. Here is a stepwise guide on how to program a CNC machine with M-code commands:

  • Understand the M-code: Each M-code controls a specific miscellaneous function on the CNC machine. It is crucial to understand the exact actions of each code. CNC machines often have varying operating procedures and capabilities.
  • Write the Program: You must choose a compatible programming language with your machine and write a CNC program. Incorporate M-codes into your G-code to control auxiliary functions during the machining process. For instance, if you wish to change the cutting tool at a point in the operation, you would include the M06 command in your program, followed by the tool number. Also, you would use the M08 command code to activate the coolant during the machining process.
  • CNC Machine Set Up: Prepare the CNC machine by installing the appropriate CNC tools, and fixtures and setting up the raw material. Ensure the workpiece material is held in the right position and the machine is well calibrated.
  • Load and Run the Program: Input the G-code program that contains the M-codes in the CNC machine’s control unit and start the machining process. The CNC machine will execute the program, following the predetermined toolpaths and auxiliary functions that the M codes control.
  • Monitor the Process: Monitor the machining procedure carefully and ensure all auxiliary functions are appropriately executed according to the plan. Also, make necessary changes to the machine setting and program as required.
  • Inspect the Final Product: It is crucial to scrutinize the final product once the machining operation ends. This ensures the machined part meets the necessary specifications and quality standards.

Common Types of M Codes

m code for cnc machining
M Code for CNC Machining

There are different types of M code commands, each with a specific function in CNC machining tasks. In this section, we will explore some of the prominent M codes for fanuc controllers and the function they govern:

M-Code Table
M-Code Description
M00 Program stop; a compulsory stop for operator intervention.
M01 Optional stop; stop based on operator setting in machine control.
M02 End of program; no rewind or return to program’s start.
M03 Spindle start; rotates spindle clockwise at specified speed.
M04 Spindle start; rotates spindle counterclockwise.
M05 Spindle stop; halts spindle rotation.
M06 Automatic tool change (ATC) to specified tool in program.
M07 Coolant on (mist); activates mist coolant system.
M08 Coolant on (flood); activates flood coolant system.
M09 Coolant off; deactivates all coolant systems.
M30 End of program with reset; similar to M02 but also resets the entire program.
M98 Subprogram call; calls a separate program or subroutine.
M99 Subprogram end; indicates the end of a subroutine.

M00 Program Stop

The M00 code indicates a program stop, terminating all current machine operations. This machine command code allows the machinists to run troubleshooting tasks, perform certain adjustments, or inspect the workpiece.

M01 Optional Program Stop

M01 signifies an optional program stop in CNC machining operations. Even though it can initiate a pause in CNC operations, it can be bypassed based on the operator’s preferences or machine configurations.


This M-code signals the end of the program, indicating the completion of the current machining task.

M03 Spindle on Clockwise

The M03 command code activates the spindle to rotate in a clockwise direction, especially in various CNC cutting and milling operations.

M04 Spindle on Counterclockwise

Like the M03 code, this code commands the spindle to rotate counterclockwise to execute various machining tasks.

M05 Spindle Off

The M05 code deactivates the spindle, commanding it to stop operation. It is a crucial command code for safety and changing between operations.

M06 Tool Change

The M06 code signals the CNC machine to change the cutting tool, ensuring a seamless transition between various machining functions.

M08 Coolant On

The M08 command activates the coolant system. It plays a vital role in material handling and temperature control.

M09 Coolant Off

M09 code commands the CNC machine to deactivate its coolant system. It is often used when it is no longer required or at the end of a particular process.

M30 Program End, Return to Start

The M30 code denotes the end of a program and returns the CNC machine to its starting point in preparation for the next task or inspection.

Other Commonly Used M-Codes

There are several other M-codes with specific needs within CNC machining operations. However, it is essential to note that the exact definition of each code may vary between different CNC machine models and manufacturers. Below are some of these codes:

  • M07: Coolant on (mist)
  • M10: Clamp On
  • M11: Clamp Off
  • M19: Spindle Orientation
  • M29: Rigid Tap Mode
  • M30: End of Program Reset
  • M40: Spindle Gear at the Middle
  • M68: Hydraulic Chuck Close
  • M69: Hydraulic Chuck Open
  • M98: Subprogram call
  • M99: Subprogram end

Examples of Standard M-Code Simulators

M-code simulators are useful for examining and visualizing a CNC program before its execution. Here are some of the best M-code simulators widely used by CNC programmers:

  • Mach3
  • CNC Simulator Pro
  • NC Viewer
  • CAMotics
  • Mastercam Simulator
  • G-Wizard Editor
  • NCSim

Typical CNC Machines that Use M-code Commands

cnc turning
CNC Turning

M-code applies to a broad spectrum of CNC machines, each providing different manufacturing needs. The common types of CNC machines that use M-code in executing various functions include:

  • Electric Discharge Machines (EDMs)
  • Lathes
  • Milling Machines
  • Water Jet Cutters
  • Grinders
  • Plasma Cutters
  • Laser Cutters

Key Differences Between M-code and G-code

Although G-code and M-code often complement each other in CNC programs, their use in CNC machine controls differs. The G-code governs the CNC machine’s position and movement, including rapid position to a particular XY plane, circular interpolation, and linear feed movement. These machine instructions are related to geometric functions and serve in product design.

Conversely, M code commands control machine operations like tool changes and the on or off of spindle or coolant systems. They are related to machine functions and perform various miscellaneous operations. Besides, the M code activates the CNC machine’s programmable logic controller (PLC).

Practical Safety Considerations in M-Code Programming

Safety is an essential factor to bear in mind when programming CNC machines with M-code. Here are some important considerations:

  • Ensure you follow the CNC machine’s manufacturer guidelines and industry standards for safer machining operations.
  • Familiarize yourself with the specific M-code for the CNC machine used for the operation.
  • Practice regular CNC machine inspection and maintenance to ensure optimal performance.
  • Ensure appropriate tooling paths and machine functions to prevent damage and collisions.


M code is a universal CNC machine control language and a complimentary partner to G code in CNC programming. CNC machines cannot function without these instructions. Hence, M codes instruct the machine on non-geometric machine operations in basic operations and complex machining processes. This guide has discussed everything you need to know about M codes and why they are essential to your machining jobs.

at machining cnc machining service
AT-Machining CNC Machining Services

However, AT-Machining is the ideal CNC machining expert to consult should you need professional assistance with your CNC machine programming. We are a top provider of CNC machining services with a team of certified CNC programmers, machinists, and engineers capable of handling all your manufacturing needs. Our certified teams have experience reviewing 3D CAD models and manufacturing high-quality parts through CNC programming and Machining. Do not hesitate to contact us for your machining project. We can provide you with the results you desire!

photo 2

Hey there, I’m Sam!

From AT-Machining, I’m a CNC Machining Expert in this field for more than 20 years.  We offer cost-effective machining services from China. Ask for a quote for your ongoing or upcoming projects now!

Best Regards, Sam, Co-Founder

AT Machining

On-demand CNC Machining With Custom Finishes. You Design It, We'll Make It.