CoupeFab

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.

Reviewed 4 min read Level : Beginner DraftWritten by CoupeFab from the official documents cited. Technical review by Michaël Brassard pending.

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.

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

CodeMeaningNotes
G0Rapid moveMachine max speed; path is not necessarily straight on every control
G1Linear interpolation at feed FThe basic cutting move
G2 / G3Clockwise / counter-clockwise arcCentre in I J K (or radius R)
G4DwellP in seconds on LinuxCNC; milliseconds on some Fanuc-style controls depending on settings — check

Planes, units, distances

CodeMeaning
G17 / G18 / G19XY / XZ / YZ plane (G18 is the usual lathe plane)
G20 / G21Inches / millimetres
G90 / G91Absolute / incremental coordinates
G90.1 / G91.1Absolute / incremental arc centres I J K (LinuxCNC, Mach)
G93 / G94 / G95Inverse-time / per-minute / per-revolution feed

Origins and machine positions

CodeMeaning
G53Move in machine coordinates (non-modal)
G54 to G59Work coordinate systems 1 to 6 — see Work offsets
G28 / G30Return 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

CodeMeaning
G40Cancel cutter radius compensation
G41 / G42Compensation left / right of the path
G43 H…Tool length compensation
G49Cancel length compensation

Canned cycles (drilling)

CodeMeaning
G81Simple drilling
G82Drilling with dwell at bottom
G83Peck drilling
G73Chip-break peck (control-dependent)
G84Tapping (rigid or floating depending on machine)
G80Cancel canned cycle
G98 / G99Retract to initial plane / R plane

Lathe

CodeMeaning
G96Constant surface speed (CSS)
G97Constant spindle RPM
G7 / G8Diameter / radius mode (LinuxCNC)
G76Threading cycle (parameters differ a lot between controls)

Common M codes

CodeMeaning
M0 / M1Program stop / optional stop
M2 / M30Program end (M30 rewinds on most controls)
M3 / M4 / M5Spindle CW / CCW / stop — in plasma, often torch ON / OFF
M6Tool change (with T…)
M7 / M8 / M9Mist / flood / coolant off
M62–M65Synchronized 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

  • G91 left active after a G28 G91 Z0 return: restore G90 immediately.
  • Arc rejected (“arc radius error”): incremental I J centres while the control expects absolute (G90.1), or vice versa.
  • G4 P1 waiting 1 ms instead of 1 s (or the reverse) depending on dialect.
  • G41 activated 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

Spotted an error or want something added? Tell us →

One guide a week, French-first

New guides, used equipment in Quebec and price moves. No third-party ads.

CASL compliance: the box is not pre-checked. Nothing is sent without this express consent.