G54.APP

G-code deep dive

G20 vs G21: Inch and Millimeter Mode in G-code

G20G21

G20 and G21 set the units mode: whether every dimension word in the program — coordinates, feeds, offsets given in G10 blocks, arc words — reads as inches (G20) or millimeters (G21). The pair is supported identically on every control in the table below, which makes it one of the most portable codes in G-code… and still one of the most dangerous to omit.

G21 G90 G17 G54    ; the classic safety preamble: mm, absolute, XY, WCS 1

What the mode actually changes

Units mode applies to values read from the program after the mode is set:

It does not retroactively convert machine state: stored work offsets and the tool table live in the control's native units and are displayed converted. Spindle speed (S) is RPM either way, and dwell time (P on G4) is time, not distance.

OperationLinuxCNCGRBLCentroidFANUCMach3Mach4Notes
Program in inches G20 G20 G20 G20 G20 G20
Program in millimeters G21 G21 G21 G21 G21 G21

— means the control has no equivalent code. Full cross-control tables: the complete G-code & M-code reference.

The 25.4× problem

A program written for millimeters, run while the control sits in inch mode, executes every move 25.4 times larger than intended. The first rapid usually hits a travel limit — the lucky outcome — or something solid. The reverse error cuts a part 25.4× too small, which at least fails safely.

Two habits eliminate it:

  1. Every program states its units in the first line of the preamble. Never rely on the control's power-on default (it's configurable and someone before you may have changed it).
  2. Simulate before running. A units mismatch is instantly obvious in a viewer — the toolpath is comically larger or smaller than the stock. It's the cheapest pre-flight check there is.

Common mistakes

Frequently asked questions

Which is metric — G20 or G21?

G21 is millimeters, G20 is inches. A mnemonic that works: 21 → 2.54 cm in an inch → metric. Every control in the table uses the same pair.

What happens if I run a metric program in inch mode?

Every coordinate and feed is interpreted 25.4× too large. A 10 mm move becomes a 10 inch move — usually straight through a clamp or past the machine's travel limits. It is one of the most common causes of first-run crashes, which is why programs must state G20 or G21 explicitly in the preamble.

Does G20/G21 change my work offsets and tool table?

Generally no — stored offsets live in the machine's native unit system and are converted for display. But behavior varies by control and settings, which is exactly why switching units mid-program is discouraged: set it once at the top and leave it.

Does a 3D printer use G20/G21?

Marlin-family firmwares accept them (defaulting to G21 mm), but slicers always emit metric. G20 support in printer firmware is vestigial — some firmwares have dropped it entirely.

See it, don't guess it

Paste a program into the free G54.APP viewer and watch these codes execute — toolpaths, stock removal, and machine state, live in your browser. Nothing is uploaded.

Open the G-code viewer