Master CNC
Blog · 5 min read · 26 Oct 2026

M codes every CNC operator should know

G codes say how to move; M codes switch things on the machine: spindle, coolant, tool change, program stop. A small set of them is the same on almost every Fanuc-style control. The rest are set by the machine builder, so always keep your machine's M code list close.

The common M codes

CodeFunctionNotes
M00Program stopAlways stops. Press Cycle Start to go on
M01Optional stopStops only when Optional Stop is switched on
M02End of programOlder style, often without rewind
M30End of program and rewindThe usual program end
M03Spindle clockwiseNormal for right-hand tools
M04Spindle counter-clockwiseCommon on lathes, depending on turret and tool orientation
M05Spindle stop
M06Tool changeMachining centres
M08Coolant on
M09Coolant off
M19Spindle orientationCommon on mills; the number differs on some machines
M29Rigid tapping modeFanuc mills, before G84
M98Call subprogramFanuc
M99End of subprogram / returnFanuc
List of common M codes: M00, M01, M03, M04, M05, M06, M08, M09, M30, M98, M99
The M codes that are the same on most Fanuc-style machines.

M00 or M01?

Use M01 after each tool in a new program. With Optional Stop on, the machine stops so you can check the part and the next tool; with it off, the program runs without stopping. Use M00 only where the machine must stop every time, for example to turn the part over or remove chips from a blind hole.

Machine-builder M codes

Everything outside the common list is set by the machine builder: chuck open and close, tailstock, parts catcher, bar feeder, door, chip conveyor, air blow, C-axis and live tools. The same function can have a different number on two machines in the same shop. Never copy an M code from another machine's program without checking the list for this machine.

Okuma OSP and Haas

The main codes (M00, M01, M03 – M05, M08, M09, M30) are the same on Okuma OSP and Haas. Subprogram calls are different on Okuma OSP: it uses CALL and RTS instead of M98 and M99. See Fanuc vs Okuma OSP codes.

One M code per block

Many controls allow only one M code in a block unless a parameter allows more. A block like M03 M08 can give an alarm or ignore one of them. The safe habit is one M code per block, or put M08 with the first move: G00 X42. Z2. M08.

A typical lathe tool start and end

T0101
G50 S2500
G96 S180 M03
G00 X52. Z2. M08
...
G00 X150. Z100. M09
M05
M01

Common mistakes

  1. M03 instead of M04 on a lathe tool that needs reverse: the insert is rubbed backwards.
  2. Forgetting M09 before a tool change, and spraying coolant out of the open door.
  3. M30 in a subprogram instead of M99: the main program ends.
  4. Copying machine-builder M codes between different machines.

The Master CNC app lists G and M codes with examples for Fanuc, Okuma OSP and Haas in the Codes tab. Your machine builder's M code list is the final reference for your machine.

Try it in Master CNC

Calculators, process sheet to program, simulation, codes and lessons. Free to start, no sign-in, on phone and laptop.

Open the app