G54.APP

G-code deep dive

M0, M1, M2, M30: Program Stops and Program End

M0M1M2M30M47

Four codes control when a program pauses and how it ends:

...
M9               ; coolant off
M5               ; spindle stop
G53 G0 Z0        ; retract
G53 G0 X-10 Y200 ; present the part toward the operator
M30              ; end, rewind, ready for the next part

Stops: M0 and M1

M0 is a scheduled interruption: flip the part over, blow out a pocket, insert a bushing. The program is the checklist — the stop happens at exactly the right line every time.

M1 is the prove-out tool. Posts put one before each tool change; with the optional-stop switch on, a new job runs one tool at a time with an inspection hold between. Once the job is trusted, switch off and M1 lines vanish.

The critical caveat for both: what the machine does to spindle and coolant during a stop is control-dependent. Some stop everything, some keep the spindle turning behind the closed door. If hands go anywhere near the work during the stop, the program must command M5/M9 first — never trust the stop code to do it.

OperationLinuxCNCGRBLCentroidFANUCMach3Mach4Notes
Compulsory program stop M0 M0 M0 M0 M0 M0 Cycle-start to resume.
Optional stop M1 M1 M1 M1 M1 M1 Skipped unless the OSTOP switch is on.
Program end (no rewind) M2 M2 M2 M2 M2 M2
Program end + rewind M30 M30 M30 M30 M30 M30 What most CAM emits at file end.
Re-run program from start M47 M47 Mach4: partial

— means the control has no equivalent code. Full cross-control tables: the complete G-code & M-code reference.

Ends: M2 and M30

The practical difference on modern controls is small: M30 rewinds to the top and generally resets more modal state (offsets stay; things like G91 or an active canned cycle don't survive on most controls — the exact reset list varies). Because "ready for the next part" is what production wants, M30 is the standard ending and M2 mostly appears in legacy files. Mach adds M47 — jump back to the first line without stopping — for continuous demo/warmup loops; treat it as the special case it is.

One habit worth stealing from good posts: end programs with the machine in a boring state — coolant off, spindle stopped, Z high, table presented forward — so the ending is identical whether the file ran clean or an operator hit reset halfway.

Common mistakes

Frequently asked questions

What is the difference between M0 and M1?

M0 always stops and waits for cycle start. M1 stops only if the control's 'optional stop' switch is enabled — with it off, M1 lines are ignored entirely. Posts drop an M1 before each tool change so an operator proving out a job can inspect between tools, then run production with the switch off.

What is the difference between M2 and M30?

Both end the program; M30 also rewinds to the top, ready for the next cycle-start (on tape machines it literally rewound the tape). M30 is what virtually every CAM post emits. Reset behavior differs slightly by control — M30 typically also resets more modal state.

Does M0 turn off the spindle and coolant?

Control-dependent — and that's the danger. On many machines the spindle keeps turning through an M0; on others it stops. Never program an M0 expecting to reach into the work area. If the stop is for chip clearing or inspection, precede it with M5 and M9 explicitly.

Why does my program stop before every tool change?

The optional-stop switch is on and the post emits M1 before each M6. That's the intended prove-out behavior — turn off optional stop on the control panel for uninterrupted runs.

See it, don't guess it

Paste a program into the free G54.APP viewer and watch these codes execute — toolpaths, stock removal, and machine state, live in your browser. Nothing is uploaded.

Open the G-code viewer