Complete M-Code List: CNC Programming Reference
M-code (Miscellaneous function) controls everything on your CNC machine except tool motion. While G-code tells the machine where to move the tool, M-code tells the machine what to do — start the spindle, turn on coolant, change tools, and control program flow. Every CNC program uses a combination of G-codes and M-codes working together.
Essential M-Codes
| Code | Function | Example | Notes |
|---|---|---|---|
| M03 | Spindle on CW | M03 S3000 | Most common spindle direction |
| M04 | Spindle on CCW | M04 S2000 | For left-hand tools |
| M05 | Spindle stop | M05 | Stop before tool change |
| M06 | Tool change | M06 T2 | Stops for tool change |
| M08 | Coolant on | M08 | Flood coolant |
| M09 | Coolant off | M09 | Turn off before M05 |
| M30 | End program + rewind | M30 | Standard program end |
Spindle Control Codes
M03 starts the spindle rotating clockwise at the speed set by the S-word. This is the standard direction for right-hand cutting tools.
M04 starts the spindle counterclockwise, used for left-hand taps and certain operations.
M05 stops the spindle. Always include M05 before M06 (tool change) and M30 (program end).
The spindle speed is set with the S-word. S is modal — once set it stays active until changed.
Coolant Control Codes
M08 turns on flood coolant. M07 turns on mist coolant. M09 turns off all coolant. Coolant should be turned off before stopping the spindle.
Tool Change Code
M06 stops the program and signals a tool change. On manual machines the operator changes the tool. On automatic tool changers the exchange happens automatically. Always stop the spindle with M05 before M06.
Program Flow Codes
M00 stops the program completely. Press Cycle Start to continue. M01 is an optional stop — only activates when the optional stop button is enabled. M02 ends the program. M30 ends the program and rewinds to the start, ready for the next cycle.
Subprogram Codes
M98 calls a subprogram by number. M99 returns from a subprogram to the calling program.
What’s Next?
Complete M-Code Reference Table
Spindle Control
| Code | Description | Example | Modal |
|---|---|---|---|
| M03 | Spindle on clockwise | M03 S3000 | Yes |
| M04 | Spindle on counterclockwise | M04 S2000 | Yes |
| M05 | Spindle stop | M05 | Yes |
M03 starts the spindle in the clockwise direction at the programmed RPM. This is the standard direction for right-hand cutting tools. The S-word sets the speed in RPM. S is also modal and stays active until changed. A typical sequence is:
M06 T1 ; Load tool
M03 S3000 ; Start spindle at 3000 RPM
; ... cutting operations ...
M05 ; Stop spindle
M06 T2 ; Change tool
M03 S4000 ; Start spindle at 4000 RPM for new tool
M04 reverses spindle direction for left-hand operations. M05 stops the spindle. Always stop the spindle before a tool change.
Coolant Control
| Code | Description | Modal |
|---|---|---|
| M07 | Mist coolant on | Yes |
| M08 | Flood coolant on | Yes |
| M09 | All coolant off | Yes |
M08 turns on flood coolant for most metal cutting. M07 turns on mist coolant for lighter operations. M09 turns off all coolant.
Proper coolant sequencing:
M03 S3000 ; Start spindle
M08 ; Turn on coolant
; ... cutting operations ...
M09 ; Coolant off
M05 ; Spindle off
Tool Change
| Code | Description | Example | Modal |
|---|---|---|---|
| M06 | Tool change | M06 T1 | No |
M06 stops the program and initiates a tool change. The T-word specifies which tool to load. On manual machines, the machine moves to the tool change position and waits. On automatic tool changers, the tool is swapped automatically in 2-5 seconds.
M05 ; Stop spindle
M06 T2 ; Change to tool 2
M03 S4000 ; Start spindle for new tool
Program Flow
| Code | Description | Notes |
|---|---|---|
| M00 | Program stop (full) | Press Cycle Start to continue |
| M01 | Optional stop | Only stops if button enabled |
| M02 | End of program | No rewind |
| M30 | End of program + rewind | Standard end code |
M00 stops the machine completely for operator inspection or part repositioning. M01 is useful for checks that are sometimes needed. M02 and M30 both end the program but only M30 rewinds to the start.
Subprograms
| Code | Description | Example |
|---|---|---|
| M98 | Call subprogram | M98 P1000 |
| M99 | Return from subprogram | M99 |
Subprograms let you reuse blocks of code without rewriting them. The main program calls a subprogram with M98, the subprogram executes, and M99 returns control to the main program.
; Main program
G90 G54
M98 P1000 ; Call the pocket subprogram
G00 X100 Y0
M98 P1000 ; Call it again at a new position
M30
; Subprogram
O1000
G91 G01 Z-1 F100
G01 X50
G01 Y50
G01 X-50
G01 Y-50
G90
M99
M-Code Programming Examples
Complete Production Program
%
O1000 (BRACKET PROGRAM)
N10 G90 G94 G17 G54
N20 G21
; TOOL 1: FACE MILL
N30 M06 T1
N40 M03 S4000
N50 M08 ; Coolant on
N60 G00 X-10 Y-10 Z5
N70 G01 Z0 F200
N80 G01 X110 F600
N90 G00 Z50
N100 M09 ; Coolant off
N110 M05 ; Spindle off
; TOOL 2: DRILL
N120 M06 T2
N130 M03 S2500
N140 M08 ; Coolant on
N150 G00 X20 Y20
N160 G81 R2 Z-15 F150
N170 X80
N180 Y80
N190 X20
N200 G80
N210 G00 Z50
N220 M09 ; Coolant off
N230 M05 ; Spindle off
N240 M30 ; Program end
%
M-Codes by Controller Type
Most M-codes are universal, but some vary by controller:
| Code | Fanuc | Haas | GRBL | LinuxCNC |
|---|---|---|---|---|
| M03 | Standard | Same | Same | Same |
| M05 | Standard | Same | Same | Same |
| M06 | Standard | Same | Limited | Same |
| M08 | Standard | Same | Same | Same |
| M30 | Standard | Same | Same | Same |
| M98 | Standard | Same | No | Same |
| M99 | Standard | Same | No | Same |
The basic codes (M03, M05, M08, M09, M30) work on every controller. Subprogram codes (M98, M99) are standard on Fanuc and Haas but not supported on GRBL.
Common M-Code Mistakes
1. M06 without M05. Some controllers reject a tool change command while the spindle is running. Always use M05 before M06.
2. Missing M30. A program without M30 may stop on the last line without rewinding. Always end with M30.
3. M08 without M09. Leaving coolant on after the program ends wastes coolant. Always include M09 before M30.
4. Wrong M03 vs M04 direction. Using M04 with a right-hand tool reverses the cutting direction. Verify the direction before cutting.
Summary
- M03/M04 control spindle direction and use S for speed
- M05 stops the spindle
- M06 changes tools — always stop spindle first
- M08/M09 control coolant
- M30 ends and rewinds the program
- M98/M99 manage subprograms
These eight M-codes cover 99 percent of CNC programming needs. Master these eight M-codes and you will be able to control any CNC machine effectively.
Understanding M-Code Modality
Most M-codes are modal — once activated they stay active until another code cancels them. M03 stays on until M05. M08 stays on until M09. This is convenient because you only write them once, but it means you must remember to cancel them.
One-shot M-codes like M06 (tool change) and M00 (program stop) execute once and do not need cancellation. They affect only the moment they are executed.
M-Codes for Advanced Functions
Beyond the essential codes, some controllers support advanced M-codes:
| Code | Function | Notes |
|---|---|---|
| M19 | Orient spindle | Positions spindle for tool change |
| M21 | Mirror axis | Mirrors X, Y, or Z |
| M25 | Chuck open (lathe) | Opens lathe chuck |
| M26 | Chuck close (lathe) | Closes lathe chuck |
| M41 | Low gear range | Changes spindle gear range |
| M42 | High gear range | Changes spindle gear range |
These codes are controller-specific and may not be available on all machines.
M-Code Safety Practices
- Always stop the spindle (M05) before changing tools (M06)
- Turn off coolant (M09) before stopping the spindle (M05)
- Include M05 and M09 in the safety block to ensure a known state
- End every program with M30
- Verify M-codes match your controller before running a new program
M-Code vs G-Code Summary
| G-Code | M-Code | |
|---|---|---|
| Controls | Motion and geometry | Machine auxiliary functions |
| Examples | G00, G01, G02, G90 | M03, M05, M06, M08 |
| Modality | Most are modal | Some modal, some one-shot |
| Written | Usually one per line | Often combined with G-codes |
Both code types work together in every CNC program. G-codes control the cut. M-codes control the machine that makes the cut possible.
Programming with M-Codes
When writing a program, M-codes follow a logical sequence:
- Start of operation: M06 (tool), M03 (spindle), M08 (coolant)
- During operation: M-codes stay active automatically
- End of operation: M09 (coolant off), M05 (spindle off)
- Between operations: M06 (next tool)
- Program end: M30
Following this sequence ensures the machine operates safely and efficiently.
Practical M-Code Applications
M00 for Part Inspection
When running a new program for the first time, use M00 to stop the machine at key points for inspection:
G00 Z50 ; Retract
M00 ; Stop — inspect the part
; Press Cycle Start to continue
This is especially useful during prove-out of new programs. The operator can check critical dimensions after the roughing pass before the finishing pass removes final material.
M01 for Production Checks
In production runs, M01 lets the operator skip or perform checks as needed. When the optional stop button is enabled, M01 stops the machine. When disabled, the program continues past M01 without stopping.
M98/M99 for Pattern Repetition
Subprograms combined with G91 are powerful for repeating patterns:
O1000
G91 G01 Z-1 F100
G01 X10 Y10
G01 X10 Y-10
G01 X-10 Y-10
G01 X-10 Y10
G00 Z2
G90
M99
This subprogram cuts a 10mm square pattern. Called from multiple starting positions, it creates an array of identical pockets.
Quick Reference Card
SPINDLE
M03 On, clockwise M03 S3000
M04 On, counterclockwise M04 S2000
M05 Stop spindle M05
COOLANT
M07 Mist on M07
M08 Flood on M08
M09 Coolant off M09
TOOL CHANGE
M06 Change tool M06 T1
PROGRAM FLOW
M00 Program stop M00
M01 Optional stop M01
M02 End program M02
M30 End + rewind M30
SUBPROGRAMS
M98 Call subprogram M98 P1000
M99 Return from sub M99
What to Remember
M-codes are simpler than G-codes because there are fewer of them and each has a clear purpose. Start with the spindle and coolant codes, add tool changes and program flow, then subprograms. Practice with simple programs that use each M-code in context.
Troubleshooting M-Code Problems
| Problem | Likely Cause | Fix |
|---|---|---|
| Spindle does not start | Missing M03 or S0 | Add M03 with S above zero |
| Spindle will not stop | Missing M05 | Add M05 before tool change |
| Coolant does not turn on | Missing M08 | Add M08 after spindle start |
| Coolant stays on after program | Missing M09 | Add M09 before M30 |
| Tool change does not happen | Missing M06 | Add M06 with tool number |
| Program stops unexpectedly | M00 in code | Check for M00 and remove if not needed |
| Program runs past the end | Missing M30 | Add M30 at program end |
| Subprogram does not return | Missing M99 | Add M99 at subprogram end |
Learning M-Codes Step by Step
Start by learning the four most important M-codes: M03 (spindle on), M05 (spindle off), M08 (coolant on), and M09 (coolant off). These four codes handle the most common machine functions and are used in every CNC program.
Add M06 (tool change) when you write programs that need multiple tools. Add M00 and M01 for inspection stops. Add M98 and M99 when you need subprograms for repeated patterns.
Each M-code is simple individually. The skill is using them together in the correct sequence.
Applying M-Codes in CAM Software
CAM software generates M-codes automatically in the post-processed G-code. Understanding what M-codes the CAM software generates helps you troubleshoot problems in the output.
Most CAM post-processors include:
- M06 with tool number at each tool change
- M03 with spindle speed for each operation
- M08 after spindle start if coolant is enabled in the CAM setup
- M09 before tool changes and program end
- M30 at the end of the program
If the CAM-generated program is missing any of these, check your post-processor configuration and tool setup in CAM.
Building an M-Code Quick Reference
Copy the quick reference card from this page and keep it near your machine. Having the most common M-codes visible saves time and prevents mistakes when you are writing or editing programs.
With practice, the M-codes become automatic. Every CNC programmer reaches a point where they do not think about M03 or M05 — the codes flow naturally as part of writing the program.
M-codes are the bridge between the geometry of G-code and the physical actions of the machine. Understanding them fully makes you a more capable and confident CNC programmer. Use this reference as a starting point and build your knowledge through hands-on programming practice.

Complete G-Code List: CNC Programming Reference with ExamplesJune 25, 2026 · Reference
CNC Machining Tolerances Guide: ISO 2768 Standards and Practical FitsJune 27, 2026 · Reference
CNC Terminology Glossary: 50+ Terms Every Machinist Should KnowJune 26, 2026 · Reference