One of the most basic and important commands in CNC programming is the unit selection. This command tells the CNC machine whether to use inches or millimeters as the unit of measurement for all the coordinates and dimensions in the program. The unit selection command is also known as the G-code for length units.
Usage format
The unit selection command has two options: G20 and G21. G20 stands for inch mode and G21 stands for millimeter mode. The command is usually placed at the beginning of the program, before any other commands that involve coordinates or dimensions. The syntax is as follows:
G20 ; select inch mode
G21 ; select millimeter mode
Explanation
The unit selection command affects how the CNC machine interprets the values in the program. For example, if the program has a command like G01 X1.0 Y2.0
, the CNC machine will move the tool to the position (1.0, 2.0) in the current unit system. If the unit system is inch mode, then the position is (1.0 inch, 2.0 inch). If the unit system is millimeter mode, then the position is (1.0 mm, 2.0 mm).
The unit selection command also affects how the CNC machine displays the values on the screen. For example, if the unit system is inch mode, then the CNC machine will show the values in inches. If the unit system is millimeter mode, then the CNC machine will show the values in millimeters.
The unit selection command can be changed at any point in the program, but it is recommended to use only one unit system throughout the program to avoid confusion and errors. The unit system should also match the unit system of the drawing or design that the program is based on.
Example
Here is an example of a CNC program that uses the unit selection command. The program is for a milling machine that cuts a rectangular pocket with rounded corners on a workpiece. The program uses inch mode for the unit system.
O1000 ; program number
G20 ; select inch mode
G90 ; select absolute mode
G17 ; select XY plane
G54 ; select work offset
S1000 ; set spindle speed to 1000 RPM
M03 ; start spindle clockwise
G00 X0.5 Y0.5 ; rapid move to start point
Z0.1 ; rapid move to clearance plane
G01 Z-0.25 F5.0 ; linear move to cutting depth with feed rate 5.0 IPM
G41 D01 ; activate cutter compensation with tool diameter 0.1 inch
X1.5 ; linear move to first corner
G03 X2.0 Y1.0 R0.5 ; circular move to second corner with radius 0.5 inch
X3.5 ; linear move to third corner
G03 X4.0 Y0.5 R0.5 ; circular move to fourth corner with radius 0.5 inch
X0.5 ; linear move to close the pocket
G40 ; cancel cutter compensation
G00 Z0.1 ; rapid move to clearance plane
M05 ; stop spindle
M30 ; end of program
The text diagram of the pocket is shown below. The dimensions are in inches.
+-----------------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+-----------------+
0.0 4.0
0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2.0