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
| Code | Function | Notes |
|---|---|---|
| M00 | Program stop | Always stops. Press Cycle Start to go on |
| M01 | Optional stop | Stops only when Optional Stop is switched on |
| M02 | End of program | Older style, often without rewind |
| M30 | End of program and rewind | The usual program end |
| M03 | Spindle clockwise | Normal for right-hand tools |
| M04 | Spindle counter-clockwise | Common on lathes, depending on turret and tool orientation |
| M05 | Spindle stop | |
| M06 | Tool change | Machining centres |
| M08 | Coolant on | |
| M09 | Coolant off | |
| M19 | Spindle orientation | Common on mills; the number differs on some machines |
| M29 | Rigid tapping mode | Fanuc mills, before G84 |
| M98 | Call subprogram | Fanuc |
| M99 | End of subprogram / return | Fanuc |

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
- M03 instead of M04 on a lathe tool that needs reverse: the insert is rubbed backwards.
- Forgetting M09 before a tool change, and spraying coolant out of the open door.
- M30 in a subprogram instead of M99: the main program ends.
- 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.
Calculators, process sheet to program, simulation, codes and lessons. Free to start, no sign-in, on phone and laptop.
Open the app

