x
Send Your Inquiry Today
Quick Quote

What is M Code? Deep Dive into CNC Programming

Most tutorials just tell you that “M03 is spindle forward rotation,” but they don’t explain why it sometimes causes the machine tool to jam. In CNC programming, G code and M code are two essential types of instructions. If the G code is said to be the “brain” of the machine tool, then the M code is the “hands and feet” of the machine tool. This article will introduce you to the PLC logic and timing control of M code in detail and how to avoid expensive programming errors. Whether you’re an engineer or a manufacturer, this article is worth reading.

What is M Code?

M Code

In CNC programs, M code usually refers to Miscellaneous Function. It is not used to determine how the tool moves, but rather to instruct the machine tool to do what it does. It is essentially the language in which CNC systems engage in dialogue with the electrical part (PLC) of the machine tool. These codes control all physical operations of the machine tool, such as spindle activation, tool changes, and coolant spraying.

Common M-Codes and Their Functions

M Code

As one of the main instructions of the CNC machine language, M codes are represented in the format “M+ numbers”, and each code has its own unique function. Here are the main M codes and their functions.

Program Flow Control 

This set of codes is mainly used to control the running rhythm of the program.

1. M00 – Program Stop

The machine tool stops working completely (including spindle stop, cooling stop, and feed stop).

Engineer Reminds: Use this code when you need manual intervention. For example, you need to turn the workpiece over during machining. And only when you press “Cycle Start”, the machine tool will continue to work.

2. M01 – Optional Stop 

The machine stops only when the “Op Stop” switch on the control panel is pressed.

Engineer Reminds: This M-code is very suitable for mass production. When programming, you can add M01 after each machining operation. 

3. M30 – End of Program & Rewind

Stop everything and return the cursor to the first line of the program.

Engineer Reminds: Almost all modern programming uses M30, so you can directly press the start button to process the next part.

Spindle & Coolant

These codes mainly create the physical environment required for cutting.

1. M03 – Spindle ON Clockwise

Rotate the spindle clockwise.

Engineer Reminds: This is almost the standard milling/drilling direction, but it must be used with an S code (for example: M03 S2000), or the machine tool will give an alarm signal.

2. M04 – Spindle ON Counter – Clockwise

Rotate the spindle counterclockwise

Engineer Reminds: This code is usually used for tapping cycles or when using left-hand tools.

3. M05 – Spindle Stop

Turn off the spindle power. This code is generally automatically executed before a tool change or at the end of a program.

4. M08 / M09 – Coolant Control

M08: Turn on flood coolant.

M09: Turn off all coolant.

Machine Functions & Tooling

1.M06 – Tool Change

Execute tool change action.

Engineer Reminds: M06 is not just a signal, it often invokes a built-in macro program. And this macro will be executed in sequence:

Z-axis return to origin -> Spindle orientation -> Tool arm rotation -> Confirm that the tool change is completed

Usually, you would write it as: T01 M06

2.M19 – Spindle Orientation

Rotate the spindle to a fixed angle and lock it in place via servo control.

Engineer Reminds: This is a prerequisite for you to change knives. For example, in boring operations, the M19 is essential if you need the boring tool tip to retract away from the hole wall.

3.M98 / M99 – Subprogram Call

M98: Jump to execute subroutines.

M99: The subprogram ends, returning to the main program.

Other M codes

Below is a collection table of some other M code types that you can consult for yourself.

CodeFunction
M02Program End (No Rewind)
M07Mist Coolant ON / High Pressure Coolant ON
M104th Axis Brake Clamp / Chuck Clamp
M114th Axis Brake Unclamp / Chuck Unclamp
M12Air Blast / Chip Blower
M16Special Tool Call / Tool Pre-select
M17Subprogram End (Common on Siemens/Heidenhain)
M29Rigid Tapping Mode
M31Interlock Bypass
M40Spindle Gear – Neutral
M41Spindle Gear – Low Range
M42Spindle Gear – High Range
M68Rotary Axis Brake Clamp
M69Rotary Axis Brake Unclamp
M78Tailstock Advance / Chip Conveyor ON
M79Tailstock Retract / Chip Conveyor OFF
M94Mirror Cancel / Feed Override Cancel
M95Sleep Mode / Mirror Image

It can be said that understanding the function of each M code is crucial for operators, which can not only avoid various misunderstandings, but also effectively improve their work efficiency.

Note:

M00-M09, M30, etc. comply with ISO standards, while the codes above M100 are usually defined by the machine tool manufacturer, so the functions will be different. For example, the code of Haas’s chip conveyor may be completely different from Mazak’s. Therefore, before using new machine tools from major brands, you must read the manual carefully.

How Does M-code Work?

M Code

In fact, the entire workflow of M code is a series of command executions, which are operation requests issued by the CNC system to the electrical system of the machine tool. If you want to grasp this process, you must first understand the CNC controller and PLC. The CNC controller is responsible for reading codes and calculating tool paths, while the PLC is responsible for controlling the physical switches of the machine tool, including relays, solenoid valves, and hydraulic pumps.

Execution Process

Let’s take the M code M08 as an example, so that you can understand the whole process more clearly.

At this time, when a CNC program executes a line of instructions containing M08 code, the following steps occur internally.

Step 1: Pause

The CNC processor will pause the path calculation and axis movement for the current machining operation.

Step 2: Send Signal

The CNC will send the binary signal of M08 to the PLC.

Step 3: Physical Action

Once the PLC receives the signal, it will connect the relay or contactor that controls the cooling pump through the internal ladder logic

Step 4: Wait for FIN

An important step. When the CNC sends a signal, it will enter a waiting state until the PLC provides feedback that “the task has been completed”.

Step 5: The Finish Signal

As long as the PLC determines that the action has been completed, it will send a FIN signal back to the CNC. After receiving the signal, the CNC will release the pause transition and continue to read the next line of code.

The Importance of M Codes in CNC Machining

M Code

In CNC machining, the importance of m code is self-evident. You have to know that without M code, then the CNC machine tool is just an expensive plotter, which can draw various shapes but cannot cut any material.

Enabling the Physics of Cutting

The most basic and important function.

  • Power Source Control: Control the tool to accurately cut the tool from all directions
  • Heat Management: Control the coolant switch to reduce the heat generated during the cutting process
  • Process Matching: Perfectly match the processing technology and the processing process

Fully Automated Production

M code is the key to fully automated production and is the core reason why many manufacturing factories can now lights-out manufacturing.

  • Automatic Tool Changer (ATC): The M code (M06) allows the machine tool to automatically change tools in seconds without human intervention.
  • Multi-Pallet: With pallet change (M60) or custom macros, the machine can machine one part while allowing the operator to clamp another part off-machine.
  • Unattended: With multiple codes, the machine tool can automatically feed, process, cut and other operations to realize automatic cycle work of the machine tool.

Optimizing Efficiency & Cycle Time

M code can help you optimize the processing process, thereby improving overall production efficiency.

  • Parallel Processing: Advanced programming techniques allow the next tool to be preselected while the machine is cutting.
  • Chip Removal Management: Through M12 or M78, the machine tool can automatically clean the cutting area.
  • Mode Switching: Many machine tools use specific M codes to activate high-speed machining mode.

Enhancing Flexibility

M code turns CNC programs into modular, scalable logic systems.

  • Modularity: With M98 and M99, you can write complex features as a separate module.
  • External equipment integration: Modern CNC is no longer a lonely individual and can work with various equipment systems:

    M code controls the robot: sending a signal to let the robotic arm load and unload materials.

    M code control probe: Activates the probe for online measurements.

    M code control feeder: realizes automatic advancement of bar materials.

  • Custom Macros: You can also encapsulate complex logic (such as mathematical operations) in custom M code.

Control & Safety

The final point is the safety and machine tool control issues that everyone pays more attention to.

Key node inspection: Many times, the operator will stop the machine to check the wear of the tool and measure the size of the part. At this time, M01 can help the operator complete this process well.

Safety interlock: M10 can ensure that the spindle only starts when the workpiece is tightly clamped. If any problem occurs, it will also prevent the program from running to avoid affecting personnel safety.

What Machines Use M-code?

M Code

It can be said that almost all industrial equipment involved in CNC technology uses M-code. The main machine tools are the following:

  • Milling Machine
  • lathes
  • Turning Centers
  • Grinders
  • Laser Cutting Machine
  • Plasma Cutters
  • Waterjet
  • EDM
  • 3D Printer

How to Read M Codes

M Code

In fact, reading M code is much simpler than G code, because G code is complex geometric coordinates, while M code is usually just a simple switch instruction.

The standard M code is very simple and generally consists of two parts:

Address Character (always a letter) + Numerical Value (usually 2 or 3 digits) 

  • M: It tells the controller to perform an auxiliary action
  • Numeric: Perform the corresponding action according to the corresponding number

It’s worth noting that most modern controllers allow leading zeros to be omitted. It means that when you program, M02 and M2 are the same. Just for the readability and standardization of the program, I still recommend retaining the two-digit format.

Example of Reading M Code

Standalone

This form is usually used for major actions that require machine tool time. For example:

M06 T01; (Tool change: stop everything and execute tool change action)

M01; (Selective stop: the machine is paused here)

Mixed

This should be your most common code form. For example:

G01 X120. Y60. F500. M08

  • G01 X120. Y60.: Move to coordinates (120, 60).
  • M08: Turn on the coolant at the same time.

Understand The Execution Sequence

This is the most confusing and critical part of reading M code. The execution of the action depends entirely on the parameter settings of the controller, but generally follows the following logic:

  • Pre-Movement: Some M codes will be executed before the axis moves, such as M06 (tool change is usually executed first, and then moved after completion)
  • Ost-Movement: Some M codes will be executed after the axis moves to the position, such as M00 or M30
  • Simultaneous: Most auxiliary functions are triggered at the same time as the movement, such as M08 or M03

Practical Exercises

1. Start

      N10 M03 S1200

  • Interpretation: The machine tool turns on the spindle forward rotation (M03) and sets the spindle speed to 1200 RPM (S1200).
2. Cutting

      N20 G01 Z-5.0 F150. M08

  • Interpretation: Use linear interpolation (G01) at a feed rate of 150mm/min to reach the Z-5.0 position, and turn on the coolant (M08) during this process.
3. Finish

      N30 G28 Z0. M05

      N40 M30

  • Interpretation N30: Z-axis returns to the reference point quickly (G28 Z0), and stops the spindle rotation (M05) after reaching the position.
  • Interpretation N40: End of the program and reset the cursor to the beginning (M30).
4. ATC with Pre-Select

      N100 G00 G91 G28 Z0. M09   ; (1)

      N110 T02 M06               ; (2)

      N120 T03                   ; (3)

      N130 G90 G54 G00 X50. Y20. S1500 M03 ; (4)

      N140 G43 H02 Z50. M08      ; (5)

Line by line interpretation:

  • G00 G91 G28 Z0. M09: The Z axis returns to the mechanical origin quickly, and M09 turns off the coolant.
  • T02 M06: Execute tool change (here, M06 is the core command).
  • T03: Preselect next tool (there is only T code here, no M06. This means the manipulator remains stationary, but the tool magazine turret quietly rotates in the background, positioning tool T03 to the corresponding slot)
  • N130 G90 G54 G00 X50. Y20. S1500 M03: After the new tool is installed, M03 starts the spindle to rotate forward at a speed of 1500
  • N140 G43 H02 Z50. M08: After tool length compensation is established, M08 restarts the coolant and prepares for cutting.
5. The 4th Axis Sequence

      N50 M11             ; (1)

      N60 G00 A90.        ; (2) 

      N70 M10             ; (3)

      N80 G01 Z-10. F100. ; (4)

Line by line interpretation:

  • N50 M11: Release the pneumatic or hydraulic brake of the rotary table.
  • N60 G00 A90.: Rotary stage quickly rotates to 90 degree position.
  • N70 M10: Relock the brakes.
  • N80 G01 Z-10. F100.: Cutting in a stable state.

Okay, through the above exercises, I believe you can basically understand how to read CNC program code.

One more thing I need to remind you. When you encounter unfamiliar codes, the first thing is not to guess. M codes vary greatly among different brands of machine tools. Second, you can also check the nameplate on the side of the machine, as many machine tools will have an “M-Code List” sticker on the door. Third, many excellent programmers will add bracket comments after special M code.

Difference Between G Code and M Code

M Code

The difference between the two is actually quite simple and straightforward. G-code is geometric code, while M-code is auxiliary code. The former instructs the tool how to move, while the latter instructs the machine tool what to do. At the same time, G code controls servo motors, while M code controls the relay, solenoid valve and PLC.

In addition, in terms of function, G code is mainly responsible for the position, trajectory and movement mode of the tool. The M code is responsible for the physical status and peripherals of the machine tool.

Another point is that the execution logic of the two is also different. G codes are usually modal, meaning that once you enter a G code, it remains in effect until canceled by another code of the same group. M-codes are usually non-modal, meaning that most M-codes are only valid on the current line.

M Code Utility Widget

M Code

Presumably, you will encounter the following problems in your daily work.In order to solve these problems, I have specially prepared these practical content for you, which I usually use. I hope they can help you.

The Universal Safe Start Block

Don’t rely on defaults. Reset your machine state explicitly at the start of every program.

O1001 (Sade Start)

G21 G17 G40 G49 G80 G90 (Geometry Safety)

M05 (Spindle Stop)

M09 (Coolant Off)

M30 (End of Previous Program check)

…

The New Machine Mapping Checklist

Before running the program on a new machine, you need to verify these specific codes in the manual.

Code FunctionStandard (Fanuc)Risk Level
Chuck Clamp/UnclampM10 / M11High (Part drop)
4th Axis BrakeM10 / M11 (Often conflicts)High (Motor overload)
Through Spindle CoolantM50 / M88Med (Seal damage)
Chip ConveyorM30s / M60sLow
Auto Door OpenM80sMed (Collision)

M code troubleshooting flowchart

In the actual machining process, the machine tool will inevitably get stuck. The following is a troubleshooting process for this problem.

Situation Description: The machine reads a line of M code and then stops. The green light is on, there is no alarm, but it does not move.

Debug Check Sequence:

  1. Check the light: Is the Cycle Start light still on? (Yes = Waiting for FIN signal).
  2. Check the physical action: Did the solenoid click? Did the cylinder move?
  • No Sound: Problem is CNC -> PLC output.
  • Moved but hanging: Problem is Sensor -> PLC input (The sensor didn’t tell the PLC it arrived).
  1. Check the diagnostics: Go to the PMC/PLC ladder view (Expert Mode).

Conclusion

M Code

That’s all about G-code. It can be said that if you want to better understand the M code, in addition to memorizing the code, you must also understand the action logic of the machine tool.

Furthermore, our DSR company is also a machining manufacturer, and we have professional equipment and operators. And they are all familiar with G code and M code. They can ensure that the entire machining process of the part will go smoothly and safely. If you have any CNC machining service needs, you can feel free to communicate with us. DSR will produce high-quality parts for you.

 

Note: While most M codes are standardized, it is arecommended to lways check your specific machine’s manual (Haas, Mazak, Fanuc, ect.) as manufacturers have the ability to create their own custom codes.

FAQ

Q1: Why are M-code simulators important in CNC programming?

 A: This is because it validates machine tool logic that pure geometric simulations often overlook, and can further verify the physical state of the machine tool.

Q2: Which industries benefit most from advanced M-code simulators?

A: Aerospace and precision medical device industry. 

Q3: How many M codes are there in cnc machining?

A: There is no absolute fixed total number of M-codes in CNC machining. While international standards originally defined about 100 base codes (from M00 to M99), modern CNC systems have greatly expanded to include a large number of codes above M100.

Q4: Which software supports G code editing and simulation? 

A: There are many types of software that support G code editing, mainly divided into two categories: general text editors and professional simulation verification software. For example, otepad++, CIMCO Edit, Vericut, NCSIMUL, etc.

Q5: Are M codes universal?

A: M codes are not completely universal, their versatility is limited to a very few basic instructions.

Q6: What software uses M-code?

A: There are three types of software, including CAM software (such as Mastercam, Fusion 360, NX), simulation verification software (such as Vericut, CIMCO Edit), and CNC system control software (such as Fanuc, Siemens).

Q7: What is the difference between M-code and DAX?

A: M-code and DAX are two completely different concepts. M-code is a hardware control instruction in CNC machining, while DAX is a data analysis formula language used in Microsoft Power BI and Excel Power Pivot for business intelligence calculations.

Q8: Who needs to know M-code? 

A: CNC programmers, machine tool operators, and equipment maintenance technicians should know how to use M-code.

Get Quote For Your Next Project

Ready to bring your design to life? DSR makes it easy to get started. Share your project details and our team will work closely with you.

  • Competitive Price
  • Sample 3-7 Days
  • Mass Order 20-30 Days
Quick Quote
Scroll to Top