Fundamentals · reference
G-code and M-code reference: the codes to know by heart
Common G and M codes (motion, planes, units, compensation, canned cycles, spindle, coolant) with their meaning, an annotated example and the traps between controls.
G-code (originally standardized as EIA RS-274 and ISO 6983) is the language of CNC machines. G codes describe geometry and modes; M codes drive machine functions (spindle, coolant, program end). Every control has its dialect: this table follows the LinuxCNC documentation, close to Fanuc-style conventions, and flags common differences. Your control manual always has the last word.
Modal codes: the key idea
Most codes are modal: they stay active until another code from the same group replaces them. A G1 stays active line after line; a forgotten G91 makes the rest of the program incremental. That is the no. 1 source of “unexplainable” crashes.
Motion
| Code | Meaning | Notes |
|---|---|---|
G0 | Rapid move | Machine max speed; path is not necessarily straight on every control |
G1 | Linear interpolation at feed F | The basic cutting move |
G2 / G3 | Clockwise / counter-clockwise arc | Centre in I J K (or radius R) |
G4 | Dwell | P in seconds on LinuxCNC; milliseconds on some Fanuc-style controls depending on settings — check |
Planes, units, distances
| Code | Meaning |
|---|---|
G17 / G18 / G19 | XY / XZ / YZ plane (G18 is the usual lathe plane) |
G20 / G21 | Inches / millimetres |
G90 / G91 | Absolute / incremental coordinates |
G90.1 / G91.1 | Absolute / incremental arc centres I J K (LinuxCNC, Mach) |
G93 / G94 / G95 | Inverse-time / per-minute / per-revolution feed |
Origins and machine positions
| Code | Meaning |
|---|---|
G53 | Move in machine coordinates (non-modal) |
G54 to G59 | Work coordinate systems 1 to 6 — see Work offsets |
G28 / G30 | Return to a reference position (often via an intermediate point: watch axis order) |
G92 | “Temporary” coordinate offset — powerful and tricky, avoid unless you master cancelling it |
Tool compensation
| Code | Meaning |
|---|---|
G40 | Cancel cutter radius compensation |
G41 / G42 | Compensation left / right of the path |
G43 H… | Tool length compensation |
G49 | Cancel length compensation |
Canned cycles (drilling)
| Code | Meaning |
|---|---|
G81 | Simple drilling |
G82 | Drilling with dwell at bottom |
G83 | Peck drilling |
G73 | Chip-break peck (control-dependent) |
G84 | Tapping (rigid or floating depending on machine) |
G80 | Cancel canned cycle |
G98 / G99 | Retract to initial plane / R plane |
Lathe
| Code | Meaning |
|---|---|
G96 | Constant surface speed (CSS) |
G97 | Constant spindle RPM |
G7 / G8 | Diameter / radius mode (LinuxCNC) |
G76 | Threading cycle (parameters differ a lot between controls) |
Common M codes
| Code | Meaning |
|---|---|
M0 / M1 | Program stop / optional stop |
M2 / M30 | Program end (M30 rewinds on most controls) |
M3 / M4 / M5 | Spindle CW / CCW / stop — in plasma, often torch ON / OFF |
M6 | Tool change (with T…) |
M7 / M8 / M9 | Mist / flood / coolant off |
M62–M65 | Synchronized or immediate digital outputs (LinuxCNC), used for plasma THC among others |
Annotated example (milling, mm)
%
O1001 (PLATE 100x60 - PROFILE)
G21 G17 G40 G49 G80 G90 (mm, XY plane, cancels, absolute)
G54 (work offset 1)
T1 M6 (tool 1: 10 mm end mill)
S8000 M3 (spindle 8000 rpm, clockwise)
G43 H1 Z25. (length comp, safe Z)
M8 (flood coolant)
G0 X-8. Y-8.
Z2.
G1 Z-3. F300. (plunge)
G41 D1 X0. F800. (lead-in with radius comp)
Y60.
X100.
Y0.
X-2.
G40 X-8. Y-8. (lead-out, cancel)
G0 Z25. M9
M5
G28 G91 Z0. (Z reference return)
G90
M30
%
The speeds and feeds in this example are not recommendations: they come from the Feeds and speeds math and your tool maker’s data.
Common mistakes
G91left active after aG28 G91 Z0return: restoreG90immediately.- Arc rejected (“arc radius error”): incremental
I Jcentres while the control expects absolute (G90.1), or vice versa. G4 P1waiting 1 ms instead of 1 s (or the reverse) depending on dialect.G41activated on a move too short for the lead-in ramp.
Safety line
Start every program with a line that resets modes to a known state, for example G21 G17 G40 G49 G80 G90 (adapt G20/G21). It protects against modes left active by the previous program or a manual (MDI) entry.
FAQ
Fanuc, Haas, LinuxCNC, grbl G-code: is it the same?
The core (G0–G3, G17–G21, G90/G91, M3/M5…) is shared. Cycles, macros, subprograms and machine codes differ. grbl implements a subset; check its supported-code list.
Should I write G-code by hand?
To understand, fix, and run small MDI jobs, yes. For parts, CAM and a good post processor do the work.
Sources
One guide a week, French-first
New guides, used equipment in Quebec and price moves. No third-party ads.