G20 and G21 Units in CNC Programming Explained
G20 sets the machine to use inches as the unit of measurement, while G21 sets it to use millimeters. These codes tell the CNC machine how to interpret all dimensions and movements in the program.How It Works
Imagine you are giving directions to a friend, but you need to specify if the distances are in miles or kilometers. In CNC programming, G20 and G21 work like that—they tell the machine whether to measure distances in inches or millimeters.
When you use G20, the machine understands all coordinates and movements as inches. If you use G21, it switches to millimeters. This is important because the machine moves and cuts based on these units, so setting the correct one ensures the part is made to the right size.
Switching between these codes is like changing the ruler you use. The CNC controller reads the code at the start or anywhere in the program to know how to interpret the numbers that follow.
Example
This example shows how to set the units to inches with G20 and then move the tool to a position 2 inches in X and 3 inches in Y.
G20 ; Set units to inches G00 X2.0 Y3.0 ; Rapid move to X=2 inches, Y=3 inches
When to Use
Use G20 when your design and tooling measurements are in inches, common in the United States and some industries. Use G21 when working with metric measurements, which is standard in most other countries and many manufacturing environments.
Choosing the correct unit code prevents mistakes like cutting a part too large or too small. For example, if your drawing is in millimeters but you use G20, the machine will interpret those numbers as inches, making the part much bigger than intended.
Key Points
- G20 sets units to inches.
- G21 sets units to millimeters.
- Always set units before programming moves to avoid errors.
- Units affect all coordinate and distance values in the program.
- Switching units mid-program is possible but should be done carefully.