📚 Encyclopedia

3D Printing & CAD · updated 2026-10-13

G-code reference for CNC milling

cncgcodemillinggrblfanucreference

A working reference for the subset of G-code that CAM programs (Fusion 360 and hobby GRBL posts) emit for 3-axis milling: program structure, the common codes, comments, precision, and the height levels you see in the motion. Collected while building the G-code parser and optimizer for cncfileoptimizer.com.

Program structure

%                          program delimiter (some posts)
O1001                      program number (Fanuc) or (1001) as a comment
(PROGRAM NAME)
(T1 D=6. CR=0. - ZMIN=-5. - FLAT END MILL)     tool list
G90 G94 G17                absolute, feed per minute, XY plane
G21                        millimeters (G20 = inches)
G28 G91 Z0.                home Z (incremental), then
G90                        back to absolute
(2D CONTOUR1)              operation name
M5 / M9                    spindle / coolant off from previous op
T1 M6                      tool change
S5000 M3                   spindle speed, clockwise
G54                        work offset
G0 X38.7 Y1.357            rapid to start
G43 Z15. H1                tool length comp, to clearance
Z5.                        rapid to feed height
G1 Z0.3 F333.              feed down
... G1/G2/G3 cutting ...
G0 Z15.                    retract
M9 / G28 G91 Z0. / M30     coolant off, home Z, end and rewind

Motion and modal G-codes

Code Meaning
G0 Rapid (no cutting)
G1 Linear feed at F
G2 / G3 Arc clockwise / counterclockwise, center by I,J (K) offsets or radius R
G17 / G18 / G19 Arc plane XY / XZ / YZ
G20 / G21 Inch / millimeter
G28 Return home via an optional intermediate point
G40 / G41 / G42 Cutter radius compensation off / left / right
G43 / G49 Tool length offset on (H register) / off
G53 Move in machine coordinates (bypass work offsets)
G54 to G59 Work coordinate systems 1 to 6
G80 Cancel canned cycle
G81 / G83 Drill / peck drill canned cycles
G90 / G91 Absolute / incremental
G91.1 Incremental arc centers
G94 Feed per minute
G98 / G99 Canned cycle returns to initial Z / to R level

M-codes

M0 stop (operator resumes), M1 optional stop, M2 end, M3 / M4 spindle CW / CCW, M5 spindle stop, M6 tool change, M7 mist, M8 flood, M9 coolant off, M30 end and rewind.

Comments

Parentheses (like this) in Fanuc-style posts; some GRBL posts use ; comment. Fusion tool comments follow (T{n} D={diameter} CR={corner radius} [TAPER={deg}DEG] - ZMIN={deepest z} - {TYPE}), where CR 0 is a flat end mill and CR = D/2 a ball. Operation names ((ADAPTIVE1), (3D PARALLEL1)) mark operation boundaries, never individual passes.

Number precision

Fanuc posts: 3 decimals metric, 4 inch, always with a decimal point. GRBL/hobby posts: 4 decimals metric, 6 inch. A parser should accept 0 to 6 decimals. The post processor's xyzFormat = createFormat({decimals: N}) sets it.

The height levels (as Fusion names them)

  1. Clearance height (about 25 mm+ above stock): between operations; the highest G0 Z.
  2. Retract height (5 to 15 mm): between passes within an operation; the key delimiter for detecting passes.
  3. Feed height (1 to 3 mm): where rapid descent becomes a controlled feed.
  4. Top of stock (usually Z0) and bottom height (the deepest cut).

Controller dialects

Output targets differ in arc support, comment style, line numbers, tool-change handling and decimal places; the optimizer on cncfileoptimizer.com writes 17 controller dialects. GRBL ignores tool changes, so hobby workflows use one file per bit (or an M0 pause).

See also

Linked from

Sources: cnc memory (fusion360-gcode-research.md, architecture.md)