Construction

G-Code Reference for CNC Machines

G-code and M-code command reference for CNC milling, turning, and 3D printing machines. Includes motion, work offsets, spindle, coolant, and common program structure.

G-code (ISO 6983 / RS-274) is the canonical language for CNC controllers. Commands are grouped into modal groups — once set, a G-command stays active until another command in the same group replaces it. Exact dialects vary between controllers (Fanuc, Haas, Mazak, LinuxCNC, GRBL, Marlin); this page covers the common core.

Motion (G-codes, modal group 1)

CodeNameWhat it does
G00Rapid moveTraverse at max machine feed, no cutting. Non-linear path on most controllers.
G01Linear feedStraight line at programmed feedrate (F). Used for cutting.
G02CW arcClockwise circular interpolation. Needs I/J/K (center offset) or R (radius).
G03CCW arcCounter-clockwise circular interpolation. Same params as G02.
G04DwellPause for P seconds or X seconds. Used after spindle commands.
G05.1Look-aheadFanuc AI contour / high-speed. Q1 on, Q0 off.
G33Thread cuttingLathes — synchronised feed to spindle. K = thread pitch.
G73Peck drill (high speed)Partial retract cycle. Fast chip clearing.
G76Fine boreBoring cycle with oriented spindle stop and shift.
G80Cancel canned cycleEnds drill/tap/bore cycles (G81–G89).
G81Drill canned cycleSimple drill-to-Z-then-retract.
G82Drill + dwellCounterbore: drill, dwell P, retract.
G83Peck drillRetract-to-R-plane between pecks for deep holes.
G84Tapping cycleSynchronised tap. Requires rigid-tap-capable spindle.
G85BoringFeed in, feed out (no dwell).

Plane / units / mode (modal groups 2, 6, 3)

CodeGroupMeaning
G17PlaneXY plane (mill default). Arcs use I/J.
G18PlaneXZ plane (lathe default). Arcs use I/K.
G19PlaneYZ plane. Arcs use J/K.
G20UnitsInches.
G21UnitsMillimetres.
G90DistanceAbsolute positioning (X = absolute coord).
G91DistanceIncremental positioning (X = distance from current).
G93Feed modeInverse time — F = 1 / minutes per move.
G94Feed modeFeed per minute (in/min or mm/min). Default on mills.
G95Feed modeFeed per revolution. Standard on lathes.

Tool & cutter compensation (groups 7, 8)

CodeWhat it does
G40Cancel cutter-radius comp.
G41Cutter comp left of programmed path (climb milling, RH tool).
G42Cutter comp right of programmed path (conventional milling, RH tool).
G43Tool-length comp positive (H word gives offset number).
G44Tool-length comp negative (rare).
G49Cancel tool-length comp.

Work coordinate systems (group 14)

CodeMeaning
G53Machine-zero coordinates (absolute, non-modal).
G54Work offset 1 (default on power-up for most controllers).
G55Work offset 2.
G56Work offset 3.
G57Work offset 4.
G58Work offset 5.
G59Work offset 6.
G54.1 P1..P48Extended work offsets (Fanuc).

M-codes (miscellaneous / machine functions)

CodeWhat it does
M00Program stop (wait for operator).
M01Optional stop (runs only if "optional stop" is on).
M02End of program (no rewind).
M03Spindle on, clockwise. Usually with S (rpm).
M04Spindle on, counter-clockwise.
M05Spindle stop.
M06Tool change (typically preceded by T#).
M07Mist coolant on.
M08Flood coolant on.
M09Coolant off.
M19Spindle orient (for boring/indexing).
M30End of program + rewind.
M41/M42Low/high spindle gear (lathes).
M48/M49Feed/speed override enable / disable.
M98/M99Call sub-program / return.

Address letters (parameters)

LetterTypical use
X Y ZLinear axes.
A B CRotary axes around X, Y, Z.
U V WSecondary linear axes (also incremental in some dialects).
I J KArc centre offset from start point (IJ for G17, IK for G18, JK for G19).
RArc radius, also retract plane in canned cycles.
FFeedrate (in/min or mm/min; per rev with G95).
SSpindle speed (rpm) or surface speed (with CSS mode).
TTool number (plus M06 to execute change).
H / DTool length offset number (H) / diameter offset number (D).
NBlock/sequence number.
P QParameters — dwell time (P in G04), subprogram number, cycle peck.
LLoop / subprogram repeat count.

Minimal mill program — face a rectangle

%
O0001 (FACE 60X40)
G21 G17 G40 G49 G80 G94           ; mm, XY, clear comps
G90 G54                           ; absolute, WCS 1
T1 M06                            ; face mill, tool change
G43 H1                            ; tool length comp
S3000 M03                         ; spindle on CW 3000 rpm
M08                               ; flood on
G0 X-10 Y0                        ; rapid to start
Z5                                ; safe Z
G1 Z-0.5 F200                     ; plunge
X70 F600                          ; cut pass 1
Y20                               ; step over
X-10                              ; pass 2
Y40
X70                               ; pass 3
G0 Z50                            ; rapid up
M09                               ; coolant off
M05                               ; spindle off
G28 G91 Z0                        ; home Z
G90
M30                               ; end
%

Common dialect differences

  • Fanuc: most industrial CNC controllers (Mazak, Haas, Doosan, DMG Mori). Uses %…% program delimiters, O#### program numbers.
  • Haas: Fanuc-compatible plus Haas-specific G/M codes (G150 general pocket, M95/96, etc.).
  • LinuxCNC / Mach3 / Mach4: hobby/retrofit controllers, close to Fanuc but with quirks (support arc R only for < 180°, etc.).
  • GRBL: Arduino-based, subset of RS-274. No tool changer, no fixtures beyond G54, limited canned cycles.
  • 3D printers (Marlin, Klipper): use G0/G1 + extruder E axis; G28 homing, G29 bed mesh; M104/M109 hotend, M140/M190 bed, M106/M107 fan.
  • Always consult the machine builder's manual for exact behaviour — especially of G05.x look-ahead, canned cycles, and macro variables (#100+).
Was this article helpful?