Education | Career | Guidance

Search This Blog

Top 20 M-Codes in CNC Programming (With Examples & Real Shop Floor Mistakes)

 Introduction

When we talk about CNC programming, most people immediately think about G-codes. But from my experience working with CNC machines, M-codes are just as important—sometimes even more critical in real machining situations.

M-codes (Miscellaneous codes) control the machine functions in CNC programming. While G-codes define tool movement, M-codes handle actions like spindle rotation, coolant, tool changes, and program control. From real shop-floor experience, M-codes are extremely important because a small mistake can lead to serious issues. For example, if the spindle is not started before cutting, it can cause tool breakage.   If the coolant is not turned on during heavy machining, it can lead to overheating and poor surface finish. Wrong tool change commands can even cause machine crashes. They are also called machine codes because they directly control the physical parts of the CNC machine, like the spindle motor, coolant system, and tool changer. So, they are not just programming instructions; they are actual machine operations.

Why M-Codes Matter in Real Machining

In practical machining:

  • Wrong spindle command → tool breakage
  • Coolant OFF → tool overheating
  • Wrong tool change → crash

Most beginners ignore M-codes, but in reality, they control the machine’s behaviour, not just movement.

Top 20 Important M-Codes – Quick Reference Table

M-Code

Function

M00

Program stop

M01

Optional stop

M02

Program end

M03

Spindle ON (Clockwise)

M04

Spindle ON (Counterclockwise)

M05

Spindle OFF

M06

Tool change

M08

Coolant ON

M09

Coolant OFF

M10

Chuck clamp

M11

Chuck unclamp

M19

Spindle orientation

M30

Program end & reset

M41

Low gear

M42

High gear

M48

Feed override enable

M49

Feed override disabled

M98

Call subprogram

M99

Return from subprogram

M07

Mist coolant ON

Important M-Codes Explained

1. M00 – Program Stop

  • Stops the program immediately
  • Used for inspection or manual intervention

Example: M00;

Mistake: Forgetting to restart the program

2. M01 – Optional Stop

  • Stops only if the optional stop switch is ON

3. Spindle Control (Very Important)

M03 – Spindle Clockwise

M03 S1200;

M04 – Spindle Counterclockwise

M04 S800;

M05 – Spindle Stop

M05;

Mistake: Starting feed without spindle ON → tool damage

4. M06 – Tool Change

T01 M06;

  • Automatically changes the tool

Mistake: Wrong tool number leads to a crash

5. Coolant Control

M08 – Coolant ON

M09 – Coolant OFF

M07 – Mist Coolant

Mistake: Running without coolant during heavy cutting

6. Chuck Control (Lathe)

M10 – Clamp
M11 – Unclamp

Mistake: Unclamping during spindle rotation → dangerous

7. M19 – Spindle Orientation

  • Aligns the spindle to a fixed position
  • Used in tool change or drilling

8. Program Control

M30 – Program End & Reset

M30;

  • Ends program and rewinds

M02 – Program End

  • Ends program without reset

9. Gear Control

M41 – Low Gear

M42 – High Gear

Used in machines with gear ranges

10. Subprogram Control

M98 – Call Subprogram

M98 P1000;

M99 – Return

M99;

Example CNC Program (With M-Codes)

O2001;

G21 G90 G54;

G28 U0.0 W0.0;

T0101; 

M03 S1200;

M08;

G00 X50.0 Z5.0;

G01 X30.0 Z-20.0 F0.2;

M09;

M05;

M30;

Common Mistakes (Very Important)

  • Forgetting M03 before cutting
  • Coolant OFF during heavy machining
  • Wrong tool in M06
  • Using M05 too early
  • Forgetting M30 at program end
  • Chuck unclamp (M11) at wrong time

Conclusion

M-codes control the machine’s actions, while G-codes control the tool movement.

To become a skilled CNC operator:

  • Don’t just memorise M-codes
  • Understand when and why to use them
  • Always perform dry runs and safety checks

Mastering both G-codes and M-codes ensures:

  • Better machining quality
  • Increased safety
  • Higher confidence on the shop floor

 Important Note (From Practical Experience)

Not all M-codes are standard across every CNC machine.

  • Different controllers (Fanuc, Siemens, Haas, etc.) may use different M-codes
  • Some M-codes are machine-specific.

Note: “I strongly request that all the above programs be carefully checked and dry-run before execution to prevent tool damage or machine errors.”

Frequently Asked Questions

1. What are M-codes in CNC?

M-codes are commands that control machine functions like spindle, coolant, and tool changes.

2. What is the difference between G-code and M-code?

  • G-code → Tool movement
  • M-code → Machine functions

3. Which M-code is most important?

M03, M05, M08, M06, and M30 are widely used.

4. What does M03 do?

Starts the spindle in a clockwise direction.

5. What does M08 do?

Turns coolant ON.

6. What is M30 used for?

Ends the program and resets it for the next cycle.

7. What is the difference between M00 and M01?

  • M00 → Always stops
  • M01 → Stops only if the optional switch is ON

8. Why is coolant important in CNC machining?

It reduces heat, improves tool life, and gives better surface finish.

9. What are M98 and M99?

Used for calling and returning from subprograms.

10. What is the function of M06?

It performs an automatic tool change.