Mastering G20 and G21: Inch and Metric Units in CNC Programming
In CNC machining, using the correct units is absolutely critical. A seemingly small mistake – programming in inches when the machine is expecting millimeters (or vice-versa) – can lead to scrapped parts, damaged tools, machine crashes, and even safety hazards. The G20
and G21
G-codes are used to specify the units:
G20
: Inch mode (all dimensional values are interpreted in inches).G21
: Metric mode (all dimensional values are interpreted in millimeters).
This article provides a comprehensive guide to understanding and using G20
and G21
on both CNC lathes and machining centers (mills). We’ll cover:
- The difference between inch and metric units in CNC.
- The syntax of
G20
andG21
. - How unit selection affects all dimensional values (coordinates, offsets, feed rates).
- Examples for both lathes and mills.
- Interaction with other G-codes.
- Control-specific variations (Fanuc, Siemens, Haas, Mazak, Mitsubishi).
- Troubleshooting tips.
- The role of CAM software.
Crucial Note: Always consult the programming manuals for your specific CNC control system and machine tool. While G20
and G21
are generally standardized, there might be subtle variations or machine-specific behaviors.
1. Inch vs. Metric in CNC: A Critical Distinction
CNC machines can operate in either inch (imperial) or metric units. The choice between G20
and G21
determines how the control interprets all dimensional values in your program:
- Coordinates (X, Y, Z, A, B, C, U, V, W): The positions of the tool.
- Feed Rates (F): The speed of the tool’s movement (when using per-minute feed,
G94
/G98
). - Tool Offsets (H, D, and values set with G10): Tool length, diameter, and wear compensation.
- Work Offsets (G54-G59, etc.): The location of the part zero.
- Parameters (in some cases): Rarely, and with extreme caution,
G10
can be used to modify machine parameters, and these parameters might be unit-dependent. - Canned Cycle Values
The Consequences of a Mistake:
If you program in inches (G20
) but the machine is expecting millimeters (G21
), all movements will be 25.4 times smaller than intended. This will likely result in the part not being machined correctly (if at all).
If you program in millimeters (G21
) but the machine is expecting inches (G20
), all movements will be 25.4 times larger than intended. This is extremely dangerous and will almost certainly result in a crash.
Always double-check your units!
2. G20 and G21 Syntax
The syntax is simple:
G20 ; Set units to inches
G21 ; Set units to millimeters
G20
and G21
are modal G-codes. They remain active until the other unit selection command is programmed. It is essential to include either G20
or G21
in a safety block at the beginning of your program:
O0001 (My Program - INCH)
G20 G17 G40 G49 G80 G90 ; Safety block: Inch, XY plane, cancel comp, absolute
; ... rest of program ...
O0002 (My Program - METRIC)
G21 G17 G40 G49 G80 G90 ; Safety block: Metric, XY plane, cancel comp, absolute
; ... rest of program ...
Never assume the machine’s default units. Always program G20
or G21
explicitly.
3. G20/G21 on CNC Lathes
On CNC lathes, G20
(inch) and G21
(metric) affect:
- X-axis: Diameter values (usually).
- Z-axis: Longitudinal position.
- Tool Offsets: Both geometry and wear offsets.
- Feed Rates: If using
G94
(feed per minute), theF
value will be interpreted in inches/minute (G20
) or millimeters/minute (G21
). If usingG95
the feed will be inches/revolution or millimeters/revolution. - Canned Cycle Parameters: All dimensional parameters within canned cycles (e.g.,
G71
,G76
).
Examples (Lathe):
- Simple Facing and Turning (Inch - G20):
O0003 (Facing and Turning - Inch)
G20 G18 G40 G99 ; Inch, XZ plane, cancel comp, feed/rev
T0101 ; Select tool
G97 S1000 M03 ; Constant RPM
G00 X2.5 Z0.1 ; Rapid (X = diameter)
G01 Z0.0 F0.005 ; Face
G01 X-0.0625 ; Feed past center
G00 Z0.1 ; Rapid retract
G01 X2.0 ; Approach for turning
G01 Z-1.5 F0.01 ; Turn
G00 X10.0 Z10.0 ; Rapid home
M30 ; Program end
%
- Simple Facing and Turning (Metric - G21):
O0004 (Facing and Turning - Metric)
G21 G18 G40 G99 ; Metric, XZ plane, cancel comp, feed/rev
T0101 ; Select tool
G97 S1000 M03 ; Constant RPM
G00 X60.0 Z2.0 ; Rapid (X = diameter)
G01 Z0.0 F0.1 ; Face
G01 X-1.0 ; Feed past center
G00 Z2.0 ; Rapid retract
G01 X50.0 ; Approach for turning
G01 Z-40.0 F0.2 ; Turn
G00 X100.0 Z100.0 ; Rapid home
M30 ; Program end
%
4. G20/G21 on CNC Machining Centers (Mills)
On machining centers, G20
(inch) and G21
(metric) affect:
- X, Y, Z (and any rotary axes): All coordinate values.
- Feed Rates: If using
G94
(feed per minute), theF
value is interpreted in inches/minute (G20
) or millimeters/minute (G21
). If usingG95
(feed per revolution) the units are inches/rev or mm/rev. - Tool Offsets: Tool length offsets (
H
codes withG43
/G44
) and cutter compensation offsets (D
codes withG41
/G42
). - Work Offsets:
G54
-G59
and extended work offsets. - Canned Cycle Parameters: All dimensional parameters.
Examples (Milling):
- Simple Contouring (Inch - G20):
O0005 (Contouring - Inch)
G20 G17 G40 G49 G80 G90 ; Inch, XY plane, cancel comp, absolute
T01 M06 ; Tool change
G00 G54 X0 Y0 Z0.1 ; Rapid to start
S1200 M03 ; Spindle on
G01 Z-0.125 F20.0 ; Feed to depth
G01 X2.0 Y0.0 F10.0 ; Linear move
G02 X3.0 Y1.0 R1.0 ; Clockwise arc
G01 Y2.0 ; Linear move
G00 Z0.1 ; Rapid retract
G28 G91 Z0;
M30 ; Program end
%
- Simple Contouring (Metric - G21):
O0006 (Contouring - Metric)
G21 G17 G40 G49 G80 G90 ; Metric, XY plane, cancel comp, absolute
T01 M06 ; Tool change
G00 G54 X0 Y0 Z2.0 ; Rapid to start
S1200 M03 ; Spindle on
G01 Z-3.0 F500.0 ; Feed to depth
G01 X50.0 Y0.0 F250.0 ; Linear move
G02 X75.0 Y25.0 R25.0 ; Clockwise arc
G01 Y50.0 ; Linear move
G00 Z2.0 ; Rapid retract
G28 G91 Z0;
M30 ; Program end
%
5. Interaction with Other G-Codes (Review)
G00
,G01
,G02
,G03
: Coordinates are interpreted in the active units.G10
: Offset values are in the active units.G41
/G42
: Compensation value (D
word) is in the active units.G43
/G44
(Mills): Tool length offset (H
word) is in the active units.- Canned Cycles: Parameters are in the active units.
G90
/G91
: Not directly affected, but be aware of the units.G94
/G95
(Lathes) /G98
/G99
(Mills): Crucially important for feed rate units.
6. Control-Specific Variations
- Fanuc:
G20
(inch) andG21
(metric) are standard. - Siemens:
G70
/G71
(older controls),G700
/G710
(newer controls) are used for inch/metric selection. This is different from Fanuc. - Haas:
G20
(inch) andG21
(metric) are standard. - Mazak:
G20
(inch) andG21
(metric) are standard in EIA/ISO mode. Mazatrol uses unit selection within the conversational programming. - Mitsubishi:
G20
(inch) andG21
(metric) are standard. - Other Controls: Always consult your documentation.
7. Troubleshooting
- Dimensional Errors: The most likely cause is a unit mismatch (
G20
vs.G21
). Check:- The
G20
/G21
command in your program. - The machine’s default setting (if any).
- All dimensional values in your program (coordinates, offsets, feed rates).
- Your CAD/CAM system’s unit settings.
- The
- Incorrect Feed Rates: If the feed rate seems drastically wrong, double-check the units and the
G94
/G95
orG98
/G99
setting. - Alarm Messages: The control might display an alarm if there’s a unit mismatch.
8. Best Practices
- Always include
G20
orG21
at the beginning of your program (safety block). Never rely on the machine’s default setting. - Use Comments: Clearly indicate the units used in your program.
- Double-Check Everything: Verify all dimensions, offsets, and feed rates.
- Simulation: Use simulation software to catch unit-related errors before cutting metal.
- Consistent Units: If your shop uses both inch and metric units, establish clear procedures and standards to prevent mix-ups. Consider using separate programs or even separate machines for inch and metric jobs.
- CAM: If using CAM, ensure the post-processor is setup correctly.
9. CAM Software
CAM software typically handles unit selection automatically. You define the units for your project (inch or metric), and the post-processor generates the appropriate G20
or G21
command in the G-code. However, it’s still crucial to:
- Verify the Post-Processor Settings: Make sure the post-processor is configured for the correct units.
- Check the Generated Code: Always review the generated G-code (including the
G20
/G21
command) before running it on the machine.
10. Frequently Asked Questions (FAQ)
- Q: What’s the difference between G20 and G21?
- A:
G20
selects inch units.G21
selects metric units (millimeters).
- A:
- Q: Which one should I use?
- A: Use the units that match your drawings, tooling, and shop standards.
- Q: Can I switch between G20 and G21 in the same program?
- A: Technically, yes, but it’s extremely risky and generally not recommended. It’s very easy to make mistakes when switching units within a program. It’s much better to stick to one unit system per program.
- Q: My program is in inches, but the part is coming out tiny!
- A: You likely forgot to program
G20
, or the machine’s parameters are set to metric.
- A: You likely forgot to program
- Q: What happens if I use a metric tool with a G20 program?
- A: You must convert all tool offsets and dimensions to inches, or you will have incorrect results.
11. Conclusion
The G20
(inch) and G21
(metric) commands are fundamental to CNC programming. Using the correct units is absolutely essential for accurate and safe machining. Always include either G20
or G21
explicitly in your programs, double-check all dimensional values, and be aware of how unit selection affects other G-codes and machine parameters. Consistent use of these commands, combined with careful program verification, will help you avoid costly mistakes and ensure the success of your CNC machining projects.