A G21 code will switch the CNC into metric mode.
Once the G21 code is used, all sizes and locations in the program will be treated as millimeter values until the mode is changed.
This type of G code is called a modal command.
Modal commands remain in effect until they are canceled. This holds true even if you restart your program. Until you cancel the command or change it, the G21 code would remain.
This can cause trouble if you aren’t paying attention.
For this reason, most CNC programs will be created with start-up or safety commands. The safety commands make sure that the machine is always in the correct modes.
When to use a G21 code?
A G21 code can be found at any point within the program but the most likely location is at the start of the program or at the start of a new section of code.
For instance, you might switch the CNC into metric mode by using the G21 command immediately before starting to cut a slot in the part.
The machine may already be in metric mode or it could be in inch mode but placing the G21 command immediately before the new block of code for the slot operation will make sure you are working in the correct units.
All kinds of things can happen when machining and it isn’t uncommon for the machine to crash or some part of a program needs to be re-run.
Placing the safety commands (G21 in this case) at the start of the new block of code makes sure your machine does exactly what was intended.
What to think about when using a G21 code?
When you use a G21 code you simply need to be aware of what type of units you are working in. Most machine shops will work in either inches or mm.
There are definitely some shops out there that will use mixed units, but in most cases CNC programs will be written in one or the other for every program they make.
Know whether your parts are in inches or millimeters.
G21 [mm mode] vs G20 [inch mode]
It should be pretty obvious from the names but the other unit mode to consider is G20 – Inch mode.
When G20 is the active units code, the machine will read all locations and values as in inches.
Neither code can be canceled. The only way to turn on off is to switch to the other.
It is not advised to switch units in your program.
CNC codes that are similar to G21
All of the codes listed in the table below are modal commands that affect the way the CNC machine interprets size and location values that are entered.