Quick Guide to CNC Safety Lines [Common Codes]

a graphic of a cnc machine with text that says learn g code today safety lines

Safety lines, sometimes called a safety block, are a section of code within a CNC program which are used to protect the machine, the workpiece, and the operator.

Safety lines are used to make sure the machine is in the proper mode/state which means that the machine will understand all the commands we give it correctly.

If you need the machine to work in inches (or mm in some parts of the world), it is important to frequently set the machine to work in those units. 

This is especially true if you will be switching between units when machining.

While many won’t switch between imperial and metric units, it is common to switch between absolute and incremental positioning modes.

The safety lines used will vary by machine, however there are many which are common between the majority of machines.

Want to learn more about CNC G Code?

Types of codes found in a safety line

Safety lines can contain G codes for coordinate systems, measurement units, tool compensation, and cancel functions. 

Each one of these commands is necessary to move the tool safely. 

Modal commands and cancel commands are two of the most common codes used in CNC safety lines.

Below are the most common safety codes found in CNC programs:

Positioning mode – G90/G91

The first code normally found in a safety line will be the machine’s positioning mode.

There are two positioning modes available to use. Absolute and incremental positioning.

Absolute positioning uses the G90 command. 

Absolute positioning mode tells the machine to interpret all locations it is given as measured from a fixed zero location (origin).

graph paper example of absolute positioning with multiple points as examples

Incremental positioning uses the G91 command.

Incremental positioning modes tells the CNC that the zero location is wherever the machine currently is. 

Once the CNC makes a move in incremental mode, the new location becomes the zero location.

graph paper example of incremental positioning with multiple points as examples

Units mode – G20/G21

Selecting the system of units you will be working in is important. This is why the units mode is found in the safety lines of the program.

Imperial units are set with G20 (inches) and metric units are set with G21 (mm).

You don’t want to move 4 inches when you meant to 4 millimeters.

Setting the units mode is critical to running your CNC.

comparison of units of measurement for cnc programming

Plane selection – G17/G18/G19

Selecting the plane you will be working with is necessary. Not because you will often switch between planes, but because it’s possible.

Most people will never use anything other than the standard XY plane with G17, but setting it will make sure that it is set correctly.

The alternatives to G17 are G18 for the ZX plane and G19 for the YZ plane.

Canned cycle cancel – G80

G80 is the g code used to cancel any active canned cycle.

Canned cycles are used to perform repetitive machining. This includes drilling, reaming, boring, counterboring and other operations.

Canned cycles are modal which means they are on until they are switched or turned off. 

In the case of a drilling canned cycle, once it is turned on every new location the machine reads will be a spot where it drills a hole.

Obviously, you need to stop drilling holes eventually. Using G80 cancels the drilling canned cycle or another other canned cycles that might be active.

Cutter compensation cancel – G40

Cutter compensation is when the CNC machine control changes how it reads the program. With cutter compensation on, the machine will shift a set distance based on the size of the cutting tool when running the program.

illustration that shows how a CNC will act when using cutter compensation left with the G41 code

Cutter compensation is turned on with either G41 or G42. G41 shifts the tool left and G42 shifts it right.

illustration that shows how a CNC will act when using cutter compensation right with the G42 code

The G40 code turns cutter compensation off.

illustration that shows how a CNC will act when there is no cutter compensation mode active

Tool length compensation cancel – G49

Cutter compensation is accounts for the diameter of the cutting tool.

Tool length compensation accounts for the length of the cutter from the CNC spindle.

G49 is the code used to cancel tool length compensation.

Both compensation modes adjust for the size of your cutter. This allows you to run the same program with different tools.

Other safety codes

Codes will vary by machine. Check your instruction manual for the specific codes you need to use in the safety lines.

The codes listed here are certainly a good start.

an illustration of a cnc machine that shows how a g43 code offsets the program

Safety lines for starting up your CNC

Setting the initial modes for your machine is an important part of every CNC program. If the modes are not set then the machine will stay in it’s initialized state.

Modes such as the unit system (mm/inch) can be set in the machine parameters. If no code is specifically given in the program then the machine will remain in the initialized state.

This is not a good programming practice because there is plenty of opportunity for errors to occur. Use safety lines to make absolutely sure your CNC is in the correct mode.

Here is an example of a safety line to use at the beginning of a program:

O1000

(Start of safety code)

G20 G17 G40 (G20 inch, XY plane, cancel cutter compensation)

G49 G64 G80 (cancel tool length compensation, normal cutting mode, cancel canned cycles)

G90 G94 G98 (absolute mode, feed per minute, initial plane for canned cycles)

(end of safety code)

Frequently asked questions about CNC safety lines

Can you put all of your safety codes on the same line?

The number of codes that can fit in the same line depends on the CNC machine.

Many machines have limitations for how many M-codes you can put on one line. G codes are generally only limited to one code from each group.

For example, G00, G01, G02 and G03 are all in the same group (movement). Only one of the codes from this group should be used on any particular line.

Want to learn more about CNC G Code?

Leave a Comment