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 language most CNC controllers read. Many commands are modal: once set, a code stays in effect until another code from the same modal group replaces it. Exact dialects vary between controllers (Fanuc-style, Haas, LinuxCNC, GRBL, Marlin); this page covers the common core and says where controls differ. Group numbers below follow LinuxCNC and the NIST RS-274/NGC interpreter. To check a program line by line, paste it into the G-code Explainer & Simulator, which explains each block and plots the toolpath.

Motion and canned cycles

CodeNameWhat it does
G00Rapid moveTraverse at the machine's maximum rate, no cutting (motion, group 1). LinuxCNC moves in a straight coordinated line; on some controls (e.g. Haas) each axis moves at full speed, so the path can dog-leg. Keep rapids clear of the part and clamps.
G01Linear feedStraight line at the programmed feed rate (F). Used for cutting (group 1).
G02CW arcClockwise circular interpolation (group 1). Needs I/J/K (centre offset from the start point) or R (radius). With R, a negative value selects the arc that is longer than 180° (LinuxCNC / RS-274/NGC).
G03CCW arcCounter-clockwise circular interpolation. Same parameters as G02.
G04DwellPause; non-modal (group 0), not a motion code. On LinuxCNC, P is seconds and may be fractional (G4 P0.5). On Haas, P without a decimal point is milliseconds (G04 P10 = 10 ms, P10. = 10 s). Commonly used to break chips at the bottom of a hole.
G33Thread cuttingLathes: spindle-synchronised motion; K is the distance per revolution (pitch).
G73Peck drill (high speed)Canned cycle with a short retract between pecks to break chips.
G76Threading / boring (varies)Meaning depends on the control: on LinuxCNC (and on lathes) G76 is the multi-pass threading cycle; some mill controls use G76 for a boring cycle. Check your control's manual.
G80Cancel canned cycleEnds the active drilling/tapping/boring cycle (G73, G81–G89).
G81Drill canned cycleDrill to Z, then retract.
G82Drill + dwellDrill to Z, dwell P at the bottom, retract (spot facing, counterbores).
G83Peck drillDeep-hole drilling: retracts to the R plane between pecks to clear chips. Peck parameters differ between controls.
G84Tapping cycleRight-hand tapping cycle. On LinuxCNC, G84 does not synchronise feed to the spindle (use a floating tap holder); rigid tapping there is G33.1. Industrial controls differ, so check your manual.
G85BoringFeed in, feed out (no dwell).

Plane, distance, units and feed mode (groups 2, 3, 5, 6)

CodeGroupMeaning
G17Plane (2)XY plane: the mill default. Arcs use I/J.
G18Plane (2)XZ plane, used on lathes. Arcs use I/K. LinuxCNC still defaults to G17 in lathe mode, so put G18 in the program preamble.
G19Plane (2)YZ plane. Arcs use J/K.
G20Units (6)Inches.
G21Units (6)Millimetres.
G90Distance (3)Absolute positioning (X is a coordinate). On Haas lathes G90 is also the O.D./I.D. turning cycle.
G91Distance (3)Incremental positioning (X is a distance from the current position).
G93Feed mode (5)Inverse time: F is 1 ÷ minutes per move.
G94Feed mode (5)Feed per minute (in/min or mm/min); the mill default. On Haas lathes, feed per minute is G98 and G94 is the end-facing cycle.
G95Feed mode (5)Feed per revolution. On Haas lathes this is G99.
G98Return mode (10)Mill canned cycles: return to the initial Z between holes. On Haas lathes: feed per minute.
G99Return mode (10)Mill canned cycles: return to the R plane between holes. On Haas lathes: feed per revolution.

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 12)

CodeMeaning
G53Machine coordinates for this block only (non-modal).
G54Work offset 1 (the usual default at start-up).
G55Work offset 2.
G56Work offset 3.
G57Work offset 4.
G58Work offset 5.
G59Work offset 6.
ExtendedAdditional work offsets: syntax varies by control (e.g. Haas uses G154 Pn).

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. On LinuxCNC, M2 and M30 both end the program and reset the modal state; Cycle Start restarts from the top of the file.
M03Spindle on, clockwise. Usually with S (rpm).
M04Spindle on, counter-clockwise.
M05Spindle stop.
M06Tool change (typically preceded by T#). Some controls also stop the coolant here.
M07Mist coolant on.
M08Flood coolant on.
M09Coolant off.
M19Spindle orient (for boring/indexing).
M30End of program and reset (LinuxCNC: also exchanges pallet shuttles). On Marlin 3D printers M30 is not a program end: it deletes a file from the SD card (Marlin reference).
M41/M42Low/high spindle gear range (lathes, and mills with a gearbox).
M48/M49Feed/speed override enable / disable (LinuxCNC). Other controls use M48 for something else, for example a probe repeatability test on Marlin; see the M-code reference by controller.
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 plane, clear comps)
G90 G54                           (absolute, work offset 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)
%

Comments are in ( ) because ";" means "end of block" on Fanuc/Haas-style controls. Open a program like this in the simulator to see each line explained, the toolpath plotted and the run time estimated.

Common dialect differences

  • Fanuc and Fanuc-compatible controls: % … % program delimiters, O#### program numbers.
  • Haas: Fanuc-compatible plus Haas-specific codes. On Haas lathes, G98/G99 are feed per minute / per revolution and G90/G94 are turning/facing cycles; G04 P without a decimal point is milliseconds.
  • LinuxCNC: follows the NIST RS-274/NGC interpreter closely. A negative R gives arcs over 180°; G76 is the threading cycle; rigid tapping is G33.1; lathe X is radius unless diameter mode (G7) is set.
  • GRBL (hobby routers and laser engravers): a subset of RS-274. Supports G54–G59 work offsets, but no cutter compensation (G41/G42), no macro variables, and no canned cycles (only G80 is accepted).
  • 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 canned cycles, dwell units, G28, and macro variables (#100+).

Sources

  • LinuxCNC documentation: G-codes, M-codes, overview and modal groups (linuxcnc.org/docs).
  • NIST, The NIST RS274NGC Interpreter — Version 3, NISTIR 6556 (Kramer, Proctor, Messina, 2000).
  • Haas Automation mill and lathe G-code / M-code pages.
  • Grbl v1.1 README (github.com/gnea/grbl).

Frequently asked questions

What is the difference between G00 and G01?

G00 is a rapid move at the machine's maximum rate with no cutting; G01 is a straight cutting move at the programmed feed rate F. On LinuxCNC a G00 move is a straight coordinated line, but some controls (such as Haas) move each axis at full speed, so a rapid can take a dog-leg path.

What do G02 and G03 mean?

G02 is a clockwise arc and G03 a counter-clockwise arc. The arc is defined either by I/J/K (the centre's offset from the start point) or by R (the radius). In LinuxCNC and the NIST RS-274/NGC interpreter, a negative R selects the arc that is longer than 180°.

What are G54 to G59?

They select work coordinate systems 1 to 6 (modal group 12), so the same program can run on parts clamped in different places. G53 moves in machine coordinates for one block only. Extra offsets beyond G59 use a control-specific code, such as G154 on Haas.

What is the difference between M02 and M30?

Both end the program. On LinuxCNC, M2 and M30 both reset the modal state (G54, G17, G90, G94, cutter compensation off, spindle stopped) and Cycle Start restarts the program from the top; M30 also exchanges pallet shuttles. Check your control's manual for any difference it makes.

How long is a G04 dwell?

It depends on the control. On LinuxCNC, G4 P is in seconds and may be fractional (G4 P0.5 waits half a second). On Haas, a P value without a decimal point is in milliseconds, so G04 P10 is 10 ms while G04 P10. is 10 seconds.

Was this article helpful?