G20 and G21 in CNC Programming

Unit-Modes G-codes G20 and G21

Introduction

In CNC programming, precision is paramount. A single misplaced decimal point or an incorrect unit of measurement can lead to costly errors, scrapped parts, or even machine damage. Two critical G-codes G20 and G21 form the foundation of dimensional accuracy by defining the unit system used throughout a CNC program. These codes ensure consistency between design intent (CAD/CAM), machine behavior, and tool offsets.

This blog post explores the roles of G20 (inch units) and G21 (metric units) in CNC programming, their implications across major control systems (Fanuc, Siemens Sinumerik, Heidenhain, and Fagor), and practical examples to help you avoid common pitfalls. Whether you’re a seasoned machinist or a beginner, this guide will deepen your understanding of how units shape CNC operations.


What Are G20 and G21?

G20: Imperial Units (Inches)

  • Function: Sets the machine’s coordinate system to inches (1 unit = 1 inch).
  • Use Case: Common in the United States, Canada, and industries that follow imperial standards (e.g., aerospace, automotive).
  • Example: A move specified as X1.5 positions the tool 1.5 inches along the X-axis.

G21: Metric Units (Millimeters)

  • Function: Sets the machine’s coordinate system to millimeters (1 unit = 1 mm).
  • Use Case: Universally adopted in most countries and industries, including Europe, Asia, and global manufacturing.
  • Example: A move specified as X30.0 positions the tool 30 millimeters along the X-axis.

Key Considerations

  • Mutual Exclusivity: Only one unit code (G20 or G21) should be active at a time. Mixing units in a single program causes errors.
  • Machine Defaults: Some machines boot up in G20 or G21 mode based on parameter settings. Always verify the active unit at program start.
  • Tool Offsets: Offsets must match the active unit system. A tool measured in millimeters will behave incorrectly in an inch-based program.

Why Unit Selection Matters

  1. Global Manufacturing Standards:
  • Metric units (G21) dominate globally, while imperial units (G20) persist in legacy systems and specific markets.
  • CAM software often defaults to one system, requiring programmers to align the CNC program accordingly.
  1. Precision and Safety:
  • A 0.001-inch move is vastly different from a 0.001-mm move. Misunderstanding units can lead to catastrophic crashes.
  • Tool compensation values (e.g., cutter radius compensation) must match the unit system.
  1. Interoperability:
  • Shops serving international clients must switch between G20 and G21 to meet diverse specifications.

G20 and G21 Across CNC Control Systems

While G20 and G21 are standardized in ISO 6983, implementations vary slightly across control systems. Below are examples for Fanuc, Siemens Sinumerik, Heidenhain, and Fagor.


1. Fanuc Controls

Fanuc, the most widely used CNC control, adheres closely to standard G-code conventions.

  • Syntax:
  • G20 (Inch Mode)
  • G21 (Metric Mode)
  • Example Program:
  O0001 (Program Name)  
  G21 (Set metric units)  
  G17 G40 G49 (Plane selection, cancel tool offset)  
  G90 G54 (Absolute positioning, work offset)  
  M03 S2000 (Spindle on clockwise at 2000 RPM)  
  G00 X30.0 Y20.0 Z5.0 (Rapid move to position)  
  G01 Z-2.0 F100 (Linear interpolation at 100 mm/min)  
  • Switching Units:
  • To switch from G21 to G20 mid-program:
    gcode G21 (Metric Mode) ... (Operations in mm) G20 (Switch to inch units) ... (Operations in inches)
  • Note: Frequent switching is discouraged; stick to one unit per program.

2. Siemens Sinumerik Controls

Sinumerik uses G20/G21 similarly but integrates additional settings for flexibility.

  • Syntax:
    • G20 (Inch Mode)
    • G21 (Metric Mode)
  • Machine Parameter Settings:
    • Use Machine Data (MD) to set the default unit at startup (e.g., MD 10210: $MC_GEOAX_UNIT_IS_METRIC = 1 for metric).
  • Example Program:
  ; SINUMERIK PROGRAM  
  G21 (Metric Units)  
  G90 G54 D1 (Absolute positioning, tool offset D1)  
  S1500 M03 (Spindle on)  
  F200 (Feedrate in mm/min)  
  G00 X50.0 Y30.0  
  G01 Z-5.0  
  • Special Notes:
  • Sinumerik supports mixed units in tool offsets (e.g., metric tool length with inch geometry) via TOFRAME commands.

3. Heidenhain Controls

Heidenhain’s conversational programming (e.g., TNC 426/640) simplifies unit selection but also supports ISO G-code.

  • ISO Mode Syntax:
  • G20 (Inch)
  • G21 (Metric)
  • Conversational Mode:
  • Units are selected during program creation in the graphical interface.
  • Example ISO Program:
  BEGIN PGM MM_MODE MM  
  G21 (Metric Units)  
  TOOL CALL 1 Z R0.1  
  G0 X50.0 Y30.0  
  G1 Z-2.0 F100  
  • Key Difference:
  • In Heidenhain’s proprietary mode, units are embedded in the program header (BEGIN PGM NAME MM for metric).

4. Fagor Controls

Fagor controls (popular in European machinery) follow standard G-code conventions with minor syntax variations.

  • Syntax:
  • G20 (Inch)
  • G21 (Metric)
  • Example Program:
  N10 G21 (Metric Units)  
  N20 G90 G54 (Absolute Positioning, Work Offset)  
  N30 M03 S3000 (Spindle On)  
  N40 G00 X40.0 Y25.0  
  N50 G01 Z-3.0 F150  
  • Best Practice:
  • Use machine parameters to lock the unit system and prevent accidental overrides.

Common Errors and Troubleshooting

1. Unit Mismatch in CAM Output

  • Problem: A CAM system exports toolpaths in inches, but the CNC program starts with G21.
  • Solution: Verify CAM settings match the CNC unit code. Use post-processors to enforce consistency.

2. Machine Defaults

  • Problem: A machine boots into G20, but the operator assumes G21.
  • Solution: Always include G20/G21 at the start of the program. Disable default unit settings in machine parameters if necessary.

3. Tool Offset Confusion

  • Problem: A 10.000 mm end mill is entered as 0.3937 inches in the tool table, but G21 is active.
  • Solution: Ensure tool data matches the active unit system. Use tool management software to automate conversions.

4. Decimal Precision

  • Problem: G20 programs require tighter decimal precision (e.g., 0.0001 inches vs. 0.001 mm in G21).
  • Solution: Adjust machine resolution parameters to avoid rounding errors.

Best Practices for Using G20 and G21

  1. Start Every Program with G20 or G21 : Explicitly declare the unit system at the top of the program to override machine defaults.
  2. Align CAM and CNC Settings : Confirm that CAD/CAM units, post-processor settings, and CNC control units are identical.
  3. Label Programs Clearly : Add comments (e.g., (G21 - METRIC) at the top) to avoid confusion during setup.
  4. Use Machine Parameters for Lockdown : Prevent accidental unit changes by restricting operator access to critical settings.
  5. Double-Check Tool Offsets : Measure tools in the active unit system and verify entries in the offset table.
  6. Avoid Mid-Program Switching : If switching units is unavoidable, cancel all offsets (G40/G49) and reinitialize coordinates.

Advanced Applications: Hybrid Systems and Automation

Modern CNC machines often integrate robotics or additive manufacturing modules, where unit consistency becomes even more critical.

Example: 5-Axis Mill with Robotic Loader

  • The mill operates in G21 (metric), while the robot’s gripper positions are programmed in G20 (inches).
  • Solution: Use external sensors or machine vision to reconcile units, or convert all positions to a single system in the PLC logic.

Additive Manufacturing + CNC Hybrid

  • A hybrid machine may use microns (0.001 mm) for laser deposition and millimeters for milling.
  • Solution: Use G21 and scale factors in the CAM software to maintain precision.

Conclusion: Why G20 and G21 Are the Unsung Heroes of CNC

While G20 and G21 may seem like simple codes, they underpin the entire CNC workflow. From the initial design in CAD to the final toolpath execution, these codes ensure that every millimeter and inch is accounted for. Whether you’re programming a Fanuc-controlled lathe, a Siemens Sinumerik mill, or a Heidenhain-equipped Swiss machine, mastering unit selection is non-negotiable.

By adhering to best practices, cross-referencing control-specific nuances, and fostering collaboration between design and machining teams, manufacturers can eliminate costly errors and achieve flawless part production. Remember: in CNC, the devil is in the details—and G20/G21 are the gatekeepers of those details.


Appendix: Quick Reference Table

Control SystemG20 (Inch)G21 (Metric)Notes
FanucG20G21Standard implementation
SinumerikG20G21Use MD parameters for defaults
HeidenhainG20G21ISO mode; conversational mode uses header tags
FagorG20G21Simple syntax; align with CAM

Always consult your machine’s manual for control-specific nuances!


Discover more from digit chain

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top