M-Code List: CNC M-Codes Explained

M-codes switch machine functions on and off, such as the spindle, the coolant and tool changes, and stop or end the program.

M-codes ("miscellaneous" codes) turn machine functions on and off. Where G-codes describe motion, M-codes start and stop the spindle and coolant, change tools, pause the program and call subprograms.

The common M-codes below are close to universal. Beyond them, M-codes vary much more between machines than G-codes do: builders use the higher numbers for their own options, such as chip conveyors, pallet changers and probes. Always check your machine's manual.

Common M-codes

Code Function
M00 Program stop
M01 Optional stop (only with the Optional Stop switch on)
M02 End of program
M03 Spindle on, clockwise
M04 Spindle on, counter-clockwise
M05 Spindle stop
M06 Tool change
M07 Mist coolant on
M08 Flood coolant on
M09 Coolant off
M30 End of program and rewind
M98 Call a subprogram
M99 Return from a subprogram

Program stops: M00, M01, M02 and M30

  • M00 stops the program until the operator presses Cycle Start. The spindle and coolant usually stop too. Use it to turn a part over or clear chips.
  • M01 is an optional stop. It only stops if the Optional Stop switch is on, which makes it useful for checking the first part of a batch and then running the rest without stopping.
  • M02 ends the program.
  • M30 ends the program and rewinds it to the start, ready for the next part. It is the usual way to end a program.

Spindle: M03, M04 and M05

M03 starts the spindle clockwise, the normal direction for right-hand tools, and M04 starts it counter-clockwise. The speed comes from the S word, usually on the same line: S10000 M03. M05 stops the spindle. Programs normally stop the spindle before a tool change and at the end.

Tool change: M06

M06 changes to the tool given by the T word, as in T2 M06. On machines with an automatic tool changer, the machine fetches the tool itself. On many controls, a T word on its own only gets the next tool ready, and M06 makes the change.

Hobby machines running GRBL don't change tools themselves. Their senders pause at the tool change so you can swap the tool by hand and measure it.

Coolant: M07, M08 and M09

M07 turns on mist coolant and M08 flood coolant. M09 turns all coolant off. Machines with through-spindle coolant or air blast use other, machine-specific codes for those.

Subprograms: M98 and M99

M98 calls a subprogram stored as a separate program, such as M98 P1000 to run program O1000. M99 at the end of the subprogram returns to the line after the call. M99 at the end of a main program makes it loop back to the start. On Haas machines, M97 calls a subprogram within the same program. LinuxCNC normally uses O-word subroutines instead.

Example: two tools with coolant and an optional stop

This program profiles a rectangle with a 6 mm end mill, stops at M01 so the operator can check the part, then changes to a drill and drills a hole. Each tool runs with flood coolant.

(M-code example: two tools with spindle, coolant and stops)
G21 G17 G90 G54
T1 M6 (6 mm flat end mill)
S10000 M3
M8
G0 X0 Y0 Z5
G1 Z-2 F300
G1 X40 F800
Y20
X0
Y0
G0 Z25
M9
M5
M1 (optional stop: check the part before drilling)
T2 M6 (6 mm drill)
S2000 M3
M8
G0 X20 Y10 Z5
G1 Z-6 F150
G0 Z25
M9
M5
M30
Open this example in CutViewer

In CutViewer, the Tools list shows T1 and T2 with the lines and time for each.

  • T1 M6 changes to tool 1, and S10000 M3 starts the spindle at 10,000 RPM clockwise.
  • M8 turns on flood coolant before cutting, and M9 turns it off afterwards.
  • M5 stops the spindle before the tool change.
  • M1 is an optional stop to check the profiled part.
  • T2 M6 changes to the drill, then the spindle and coolant start again.
  • M30 ends and rewinds the program.

How CutViewer handles M-codes

CutViewer reads M03 and M04 as spindle on and M05 as spindle off, and warns about any cutting move made with the spindle off. M06 tool changes fill the Tools list in the Job panel, with the lines, cutting distance and time for each tool. M00, M01, M02 and M30 are read as stops and program ends. Coolant codes (M07, M08, M09) don't affect the drawing, and subprogram calls (M98, M99) are not followed, so moves inside a subprogram aren't drawn.

Frequently Asked Questions

What is the difference between G-codes and M-codes?

G-codes (preparatory codes) set up motion and modes, such as a straight feed move with G01 or absolute positioning with G90. M-codes (miscellaneous codes) control machine functions, such as starting the spindle with M03, turning on coolant with M08 or changing tools with M06.

What is the difference between M00 and M01?

M00 always stops the program. M01 only stops it if the operator has turned on the Optional Stop switch on the control, so it is used for stops that are only needed sometimes, such as checking the first part.

What is the difference between M02 and M30?

Both end the program. M30 also rewinds it to the beginning so it is ready to run again. On most modern controls the two behave the same, but M30 is the usual choice.

Are M-codes the same on every machine?

The common ones, M00 to M09 and M30, are close to universal. Beyond those, M-codes vary a lot between machine builders, because they control options such as pallet changers, chip conveyors, probes and through-spindle coolant. Check the list in your machine's manual.

Does GRBL support M06?

GRBL doesn't change tools itself. Senders such as Carbide Motion and Universal Gcode Sender can pause the program at a tool change so you can swap the tool by hand and set its length.

Check your own program before you cut: open it in CutViewer to see the toolpath in 3D, the run time and any problems.

Open CutViewer