G21 sets your CNC machine to metric mode. Once active, every coordinate, feed rate, and offset value in the program is read as millimeters.
If you’re working from a metric drawing — or running a program written in mm — G21 needs to be active.
Key Takeaways
- G21 switches the CNC to metric mode — all values are interpreted as millimeters
- It’s a modal command, so it stays active until you switch to G20 (inch mode)
- There’s no cancel command — either G20 or G21 must always be active
- Place G21 in your safety line at the start of every program that uses metric values
- Never switch between G20 and G21 mid-program
| G21 – At A Glance | |
|---|---|
| Function | Set metric mode (millimeters) |
| Type | Modal (Group 6) |
| Cancelled by | G20 (switches to inch mode) |
| Used with | Any program using metric dimensions |
What Does G21 Do?
G21 tells the CNC to read all values in millimeters. That includes X, Y, and Z coordinates, feed rates, offsets, and any other numerical input in the program.
This is called a modal command. Modal means it stays active on its own — you don’t have to repeat it every line. It stays in effect until something cancels or changes it.
One thing that trips up beginners: G21 stays active even if you restart the program or the machine loses power and restarts. The last active unit mode is remembered. That’s why safety lines exist.
G21 vs G20: Metric vs Inch Mode
G20 is the other unit mode. When G20 is active, the machine reads all values in inches. When G21 is active, it reads millimeters.
These two codes are a pair. You can’t cancel either one — you can only switch between them. One of them must always be active.
Do not switch between G20 and G21 in the middle of a program. If a program starts in metric, keep it metric throughout. Mixing units causes calculation errors and can crash the machine.
⇄ Inches / Millimeters Converter
Common values
| 1 in | = 25.4 mm |
| 0.5 in | = 12.7 mm |
| 0.25 in | = 6.35 mm |
| 0.125 in | = 3.175 mm |
| 0.001 in | = 0.0254 mm |
Where to Put G21 in Your Program
Most machinists place G21 (or G20) in the safety line at the very start of the program, along with other setup codes like G90 and G54.
Here’s what a typical safety line looks like in a metric program:
G21 G90 G40 G49 G80 (Safety line: metric, absolute, cancel comp/cycles)
G54 (Select work coordinate system)
G00 X0 Y0 (Rapid to starting position)
Placing G21 here makes sure the machine is always in the right mode — even if someone ran a different program before yours.
You can also place G21 at the start of a specific section of code if that section uses metric values and the rest of the program doesn’t. But again — avoid mixing units in the same program if you can help it.
What to Watch Out For
Know what units your part drawing uses. Most shops work in one system — either inches or millimeters — and stick to it.
If you’re not sure whether G20 or G21 is currently active on your machine, check the active modal codes on the control. Most controls show the current unit mode on the main status screen.
COMMON MISTAKE – Running a Metric Program in Inch Mode
Running a metric program on a machine that’s still set to G20 (inch mode). A 25mm move becomes a 25-inch move.
The machine will crash or alarm out almost immediately — but always double-check your unit mode before hitting cycle start.
FAQS
Does G21 reset when I turn off the machine?
It depends on the control. Most modern CNC controls remember the last active unit mode after a power cycle. That’s exactly why you should always include G21 (or G20) in your program’s safety line — so you’re never depending on whatever mode was left over from the last operator.
Can I switch from G20 to G21 in the middle of a program?
Technically yes, but you should avoid it. Switching unit modes mid-program is a serious source of errors and is not recommended on most controls. Keep your programs consistent — all metric or all inch.
What happens to feed rates when I switch to G21?
Feed rates are also interpreted in the active unit mode. In G21, a feed rate of F250 means 250 mm/min. In G20, F10 means 10 inches/min. This is another reason not to mix modes in a single program.
Is G21 the same on all CNC controls?
G21 for metric mode is standard across most Fanuc-based and Fanuc-compatible controls. Some older or less common controls may handle unit modes differently — always check your control’s programming manual if you’re unsure.