G20 vs G21: Inch vs Metric Programming in CNC – Avoiding Costly Mistakes
In CNC programming, G20 and G21 are modal commands used to set the unit system for all subsequent commands:
- G20 = Inch programming
- G21 = Metric programming (millimeters)
Selecting the wrong mode can result in part sizes 25.4 times larger or smaller than intended — potentially damaging machines, tools, or scrap parts.
📏 What Do G20 and G21 Do?
| G-Code | Unit System | Example Value | Actual Movement |
|---|---|---|---|
| G20 | Inches | X1.0 | Move 1 inch (25.4 mm) |
| G21 | Millimeters | X1.0 | Move 1 millimeter |
These codes change how the machine interprets all distance values — including X, Y, Z positions, feed rates (F), arc radii (R), and more.
🔹 G21 – Metric Programming
G21
G0 X50 Y25
- Machine moves to 50mm in X and 25mm in Y
- Most commonly used outside the U.S.
- Standard for most metric-based CAM software
🔸 G20 – Inch Programming
G20
G0 X2.0 Y1.0
- Machine moves 2 inches in X and 1 inch in Y
- Common in the U.S. and aerospace industries
⚠️ Common Mistake: Unit Mismatch
Let’s say a program written in mm is loaded with the machine still in G20 (inch mode):
G20
G1 X100 Y50 ; This will move 100 inches (2540mm)!
🚨 This could crash the machine or move beyond its limits.
✅ Best Practice: Declare Units Explicitly
At the start of every program, include one of the following:
G21 ; Metric mode
or
G20 ; Inch mode
This ensures predictable behavior, regardless of machine memory or operator habits.
🔄 Switching Between G20 and G21
You can switch mid-program, but it’s rare and dangerous:
G21
G1 X50 F500
G20
G1 X2.0 F20.0
This requires you to manually convert all coordinates and feedrates. ⚠️ Avoid unless absolutely necessary.
🧠 CAM Software and Units
Most CAM programs (Fusion 360, SolidCAM, Mastercam, etc.) allow you to:
- Choose unit system per project
- Export G-code with G20 or G21 included
🔍 Always verify the post-processor output before running on the machine.
🔁 Inch to Metric Conversion
| Inch (in) | Millimeter (mm) |
|---|---|
| 1 in | 25.4 mm |
| 0.5 in | 12.7 mm |
| 0.1 in | 2.54 mm |
| 0.01 in | 0.254 mm |
Use proper CAD/CAM tools or reliable calculators when switching between units.
🧪 Safe Start Template
%
O2001 (Safe G-code Start)
G21 G90 G54 G17 G40 G49 G80
...
%
This sets:
- G21: millimeter mode
- G90: absolute positioning
- G54: work offset
- G17: XY plane
📦 Summary Table
| G-Code | Function | Units Used | Risk If Wrong |
|---|---|---|---|
| G20 | Inch programming | inches | Oversized part |
| G21 | Metric programming | millimeters | Undersized or wrong cut |
✅ Best Practices
- Always start with G20 or G21
- Match the G-code unit to the CAD/CAM setup
- Never assume the machine’s current mode
- Include units declaration even in subprograms
- Avoid mid-program unit switching
🔚 Final Thoughts
Choosing between G20 and G21 isn’t just about personal preference — it’s a machine-critical decision. The wrong choice can cost time, tools, and material.
The best CNC programmers always declare their units explicitly and verify them before pressing “Cycle Start.”
By doing so, you ensure that your machine speaks the same language as your code.
Leave a comment