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:
- axis words (
X10= 10 mm under G21, 10 in under G20) - feed rates (
F600= 600 mm/min vs 600 in/min) - arc centers and radii (
I,J,K,R) - offset values written by
G10blocks
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.
| Operation | LinuxCNC | GRBL | Centroid | FANUC | Mach3 | Mach4 | Notes |
|---|---|---|---|---|---|---|---|
| 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:
- 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).
- 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
- Omitting the units code entirely. The program then inherits whatever mode the last job (or the machine default) left behind. Fine for weeks, then one day it isn't.
- Switching units mid-program. Legal on most controls, and almost never worth it: every stored modal value (feed, offsets in flight) has to be reinterpreted. Post the whole file in one system.
- Editing a G20 file with metric numbers (or vice versa). Hand-patching a couple of lines into a file without noticing its units mode plants a 25.4× error in the middle of an otherwise-good program.