G54-G59 CNC Codes Explained: An Easy Intro for Beginners [Work Offsets]

What does a G54 code do?

The G54 command tells the CNC machine where your part is located.

To put it differently, the G54 code sets the work offset zero location to be used currently in your CNC program.

On most CNC machines, the G54 through G59 codes are for selecting these work offsets or work coordinate systems. G54 is the first code in this group and the most frequently used work offset G code.

These work offset codes (G54-G59) are each matched to coordinate locations which have been set in the CNC machine’s offset library.

CNC program zero list showing XYZ coordinate locations
Work offset locations

Selecting a work offset code tells the CNC machine which work coordinate system to use. The coordinate locations tell the machine where the zero location is. This is the location where the X, Y, and/or Z coordinate values = zero.

G54 is a modal command

Once the G54 work offset code is used, all sizes and locations in the program will be relative to the zero location of the part until the offset is switched to a different work offset code such as G55 or G56.

This type of G code is called a modal command.

Modal commands remain in effect until they are canceled. This is true even if you restart your program.

Until you cancel the command or change it, the modal command G54 will stay in effect.

Obviously, 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 and this includes having the correct work offset chosen.

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

G54 vs G55-G59

All of the CNC codes from G54 to G59 act the same. They are all work offsets.

Everything in this article that applies to the G54 command also applies to all of the other work offsets including G55-G59.

Work offsets work like presets on your radio, except you store a location instead of a radio frequency. You can then call it up quickly and switch between them as needed.

This makes operations such as referencing different sides of a part or machining multiple parts at once easier.

two work offsets being used on the same part
Two work offsets being used to drill holes on a single part

Each of these commands is modal so they will stay on until turned off or changed.

The picture below shows how multiple work offsets can be set in a CNC and how they compare to the machine zero location.

visual to show cnc work offsets G54-G59 with the zero locations shown

When to use a G54 code

A G54 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.

Example time!

Imagine you have a part that you want to drill holes in and then counterbore the same holes.

counterbore on metal part
A hole with a counterbore

In the program there will be a section of code for drilling the holes and a section of code for counterboring them.

Even though those operations both use the G54 offset, the G54 command would be given at the start of each of these sections of code.

This allows the programmer or operator to make sure the correct work offset mode is active.

Our imaginary example program is shown below.

an example of how modal cnc modal work offsets work

At times, CNC programs might be run out of sequence. 

If the program didn’t call out the necessary modes (in this case, the correct work offset) at the start of each section, then bad things can happen when the machine starts to perform an operation based off a different work offset/location.

In our example above with the holes and counterbores, imagine you finish the L&W roughing portion of the code and you check the holes only to find out they are undersize.

Time to make them bigger.

To do this you decide to only run the hole section of the code. This saves time compared to running the whole program right?

If you haven’t called out the G54 command in the hole section of the code, then the machine will continue using the G55 work offset that was used for the L&W roughing.

This can definitely end up with a crashed machine and/or damaged parts.

For this reason, safety lines are included in the CNC program at the start of each section.

The CNC machine needs to have the necessary modes set before any machining operation and the correct work offset is one of those modes.

Which work offset is the most commonly used?

The most common CNC work offset is the first one, G54.

Other work offsets are G55-G59.

Some machines may have more, but G54-G59 are the ones found on almost all CNC machines.

CNC codes that are similar to G54

G53 – Machine coordinate system

G53 is used to send the machine to a location based on the machine zero.

Typically, this is the home/return position for the machine.

Unlike G54, the G53 command is not modal. The G53 code only affects the line it is used on. It is a one-time use code.

This is a handy code to use, but not all machines have it. Keep in mind that older machines, and even some of the newer ones might think the code is something else.

Not all CNC codes are universal across the different manufacturers .

You can expect that G54 will be the same on every machine, but I can’t say the same for G53.

G28 – Machine coordinate system

The G28 code is used to send the machine to a location and then to the machines zero location in one or more of axes.

How does G10 affect G54 and other work offsets

A G10 code is used to change an offset value.

The format for using G10 looks like this:

G10 L2 P1 X1.5 Y2.3 Z 3.0

The L code is for the offset type.

The P code is the offset number.

G10 codes are not something for beginners. Also, the code format isn’t the same across all CNC controls. It is important to know how your machine will react to a G10 codes. Consult your manual.

Want to learn more about G Code for your CNC?

X, Y, & Z CNC Codes Explained: An Easy Intro for Beginners [Coordinates]

What kind of coordinate system is used in CNC machining?

Almost every CNC machine uses a Cartesian coordinate system based on an X, Y, and Z-axis.

The coordinate system allows the machine to identify locations, move in specific directions and establish positions in a three-dimensional space.  

Coordinate systems are formed by the axes (X, Y & Z), planes, and an origin where the three axes meet. 

Using an X, Y or Z code in a CNC program tells the machine to go to a specific location along those axes. Location changes can be in one or more axes. 

If only a Z axis coordinate is given then the machine will only move in the Z axis. This is the same for the other axes as well.

XY Plane

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

Terms to know

To understand the coordinate system used by CNC machines, you need to first understand a few terms and concepts.

Coordinates

a number line that shows positive and negative numbers as well as the origin location

A coordinate is a location given in one or more axes.

Axis or plural axes

An axis is a straight line.

Along this straight line, each axis has a positive and a negative side.

The negative side continues on forever with larger and larger negative numbers. -1, -10, -99, -20,346 on til negative infinity.

On the other end of the axis are larger and larger positive numbers. 1, 5, 24, 578, 356,728 and on til positive infinity.

The two sides of the axis are separated by a center point.

At the center point the value of the axis is 0. This is called the origin.

Where the X and Y axes cross is the XY origin

Origin

The origin is the zero location of one or more axes. 

Typically, when talking about origins we are referring to the zero location of multiple axes.

CNC mills are usually 3 axis machines and lathes are 2 axis. Both types of machines can have more axes but let’s keep things simple.

the X and Y axes shown on graph paper with the origin identified.

Less axes are usually easier to understand so let’s start with 2 axis coordinates.

With two axes, the origin would be the (0,0) location.

The is the location where the two straight line axes cross. This could be any two axes (XY, YZ, XZ) but generally we are referring to the X and Y axes. (0,0) is (X=0, Y=0).

Note that the two axes are perpendicular, or at 90 degrees to each other.

When talking about three axes, we are referring to the X, Y and Z axes.

When working with three dimensional coordinates, the origin is the spot where all three axes meet.

This is the (0,0,0) location.

Each of the axes are still perpendicular to each other. The order of axes is the same as before with the Z axis added on so (0,0,0) is (X=0, Y=0, Z=0).

a lathe with the X and Z axis shown and the spindle identified
A 2 axis lathe
an illustration that shows the X, Y and Z axes on a CNC machine
A 3 axis mill

Plane

XY Plane

A plane is a two-dimensional flat surface. 

The most common one when talking about CNC machines is the XY plane. The XY plane is shown as the grid in the picture above.

The plane consists of all the possible coordinate location combinations possible in the X and Y axes.

There are three different types of plane combinations: XY, YZ, and XZ, and each plane has four quadrants with corresponding negative and positive values, two axes and an origin.

Quadrant

coordinates on a graph paper background that shows the different X and Y quadrants

A quadrant is an area of the coordinate system plane. The four quadrants are shown below.

Quadrant 1 has positive X and positive Y values.

Quadrant 2 has negative X and positive Y values.

Quadrant 3 has negative X and negative Y values.

Quadrant 4 has positive X and negative Y values.

CNC machinists will want to pay attention to what happens when their machines switch quadrants. 

Often changing the direction of travel from one direction to another will result in small defects or unintended features on the workpiece depending on the quality of the CNC machine.

Understanding Cartesian coordinates on a CNC machine

Usually, an easy way to understand the coordinate system for your CNC machine is to follow the Right-Hand Rule.

Hold your hand out palm up with your thumb and index finger pointed outwards, and your middle finger pointed upwards. 

Place your hand in front of your CNC machine, aligned with the machine’s spindle, and you’ll see the axes line up perfectly.

  • The thumb is the X-axis.
  • The index finger is the Y-axis.
  • The middle finger is the Z-axis.

The three fingers point to the positive side of each axis. The negative side is in the opposite direction.

Right Hand Rule
The thumb and index finger make an L shape and the middle finger is pointed straight up

Let’s compare that to our 3 axis mill to see how they line up.

an illustration that shows the X, Y and Z axes on a CNC machine

How are coordinates used in CNC machining?

Most CNC machines use a conventional cartesian coordinate system and assign the order of axes movement as follows:

  • X-axis allows movement “left” and “right”
  • Y-axis allows movement “forward” and “backward”
  • Z-axis allows movement “up” and “down”

However, there are a few exceptions to this rule, that will depend on the machine model or manufacturing company.

Some machines can switch the Z and Y axis, which can lead to confusion. Be sure to check all these details in your machine’s manual.

Movement in the coordinate system is related to the movement of your cutting tool. Many times, the cutting tool may not move in one or more axes but instead the control will move the table to act as if the tool moved.

Machine reference point

Every CNC machine has its own origin point or Home location that will serve as the machine’s coordinate system’s origin.

The machine reference point is a known point for the CNC machine.

You might move the zero location using a work offset such as G54, but the machine is calculating everything based off the reference point.

The CNC control allows you to do this to make the program easier to create and understand.

Work coordinate system

A work coordinate system sets a new origin location for the machine to use when running the CNC program.

You wouldn’t want to program to random coordinate locations in your machine. You also wouldn’t want to make a new program every time you wanted to make the same part on a different CNC machine.

The solution is a work coordinate system.

We touched on it earlier but using a work offset such as G54, G55 or one of the others commonly available on CNC controllers allows you to set your part or a fixture as the origin location.

Once the part or fixture is set as the X, Y and Z zero location, the program can be run.

Using a work coordinate system or work offset allows the CNC programmer and operator more flexibility in their programs and setup.

Often the work offset origin location will be either be where a corner of the part or the center of the part intersects the top surface.

Absolute vs incremental coordinates

Everything we have talked about so far has been discussing absolute coordinates.

Absolute coordinates are a type of coordinates that are based on a fixed origin (zero) location.

In CNC machines, absolute coordinates are set using the G90 code.

There are exceptions but most CNC programs are mainly written using absolute coordinates.

The other type of coordinates that can be used are incremental coordinates.

graph paper example of absolute positioning with multiple points as examples
graph paper example of incremental positioning with multiple points as examples

Incremental coordinates have a constantly changing origin location. Each time the CNC machine moves to a new location, that location becomes the origin. 

In other words, each new move is relative to the machine’s current location.

Incremental coordinates are set using the G91 code in a CNC program. They are usually reserved for specific, repetitive features such as a series of holes that need to be drilled or something similar.

The pictures above show the same machine movement in the two different positioning modes. The locations given to the machine are given in parentheses ( ).

Polar coordinates

Credit “ToolNotes.com © 2021, used by permission”

Most beginners don’t need to be too concerned with polar coordinates, but it is still helpful to be aware that they exist.

Polar coordinates are another way of specifying machine locations, like Cartesian coordinates.

Instead of X, Y and Z locations, polar coordinates use a radius, an angle, and a Z location.

Polar coordinates vs cartesian coordinates

Polar coordinates make calculations easier with circular motion, arcs, and circular paths.

On the other hand, Cartesian coordinates make linear movement easier to comprehend, and it is far more commonly used.

CNC machines are set to operate with a cartesian system by default. However, most CNC machines and controls include the option to use polar coordinates if needed.

CNC machines with multiple axes

Hobbyist CNC machines usually work with three axes (X, Y, & Z) as explained above.

Industrial grade machines can often be found with one or more additional axes. The most common is the addition of a rotary 4th axis.

4th, 5th and 6th axis machines are not uncommon.  

Each of these axes rotates around one of the first 3 axes. The 4th axis rotates around X. The 5th around Y. The 6th around Z.

Want to learn more about CNC G Code?

T CNC Code Explained: An Easy Intro for Beginners [Tool Selection]

What does the T CNC code do?

On most CNC machines, the T code tells the machine the tool to place in the tool changer to prepare it for a tool change.

The M06 command will then perform the tool change.

On some CNC machines, the T code will actually switch to the tool called out.

The T command is a very important CNC code because most programs will use multiple tools during the execution of a program.

It is not uncommon for dozens of tool changes to be performed in the process of running a more complex program.

When does the T code get used?

The T code is used at the start of a program to make sure the correct tool is loaded before any machining is performed.

After this initial setup, the T code is used every time the machine switches to a new tool.

If tool #1 is currently in the CNC spindle, calling tool #2 with the T02 command will ready it in the tool changer. Using the M06 command will swap tools.

Readying the tool before the switch saves precious machining time, something that is more important for high volume production machining.

Even though the tool is already loaded in the tool changer, it is advised to still call the T02 command in this instance to make sure that the correct tool is loaded.

The code would look like this:

T02

<machining code here>

T02 M06

Again, this format is used to ensure that the correct tool is placed in the CNC spindle during the tool change.

Without using the T02 command right before the M06 tool change command, it is possible that another is loaded in the tool change position and will be swapped in.

Imagine if you needed to re-run a portion of the code and started your program after the initial T02 code. If you didn’t repeat the T02 code right before the tool change then the machine would use the last tool number from its memory.

There is a good chance that it isn’t swapping the correct tool which can mean a machine crash.

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

How to change tools

CNC cutting tools are changed using the M06 command. On some machines (usually older CNCs), the T code will cause the machine to perform a tool change.

Check your machine manual to know how your machine will react.

If your machine is equipped with an automatic tool changer, then it is likely that you will be using the M06 command to change tools.

an automatic tool changer on a dmg mori cnc machine
Automatic tool changer carousel

Where should you document your tools?

Information about your tools should be stored in two locations, on your machine setup sheet and in your tool offset library.

The setup sheet is used to communicate to the machine operator what tools are expected to be loaded in the machine while running the program.

The tool offset library, sometimes called the tool offset table stores information related to the size of your cutting tools. This includes both the diameter (D offset) and tool length or height (H offset).

Offset Library
Offset library with H offsets on the left and D offsets on the right

The D offset and H offset values allow the machine to compensate for the size of the cutting tool and accurately machine the part.

Offsets and your cutting tools

Two of the most important characteristics of your cutting tools are their length and diameter. 

These two characteristics are stored as H (height) and D (diameter) offsets.

Knowing these two values and storing them in your offset library allows the machine to adjust for the size of the cutter when running the program. 

Without these adjustments using height (H) and diameter (D) offsets, the program would need to be rewritten for each new tool. Not the most efficient way to machine parts.

illustration of a cnc cutting tool that shows what an H offset is

The value of the H offset is the distance between the end of the spindle and the end of the cutting tool.

The H code is used to select the H offset stored in the offset library when tool length compensation is turned on with the G43 code.

illustration of a cnc cutting tool that shows what an D offset is

The D code is used to choose the D offset to be used when cutter compensation is turned on with either the G41 – Cutter compensation left or G42 – Cutter compensation right codes are used.

The D offset value is a number stored in the offset library that tells the CNC the size (diameter) of the cutting tool. 

Once the machine knows the size of the cutter it can adjust how it runs the program based on that size.

Frequently asked questions about the T code

How is the T code used differently on mills vs lathes

As stated above, most CNC milling programs will use more than one tool to complete the machining of a workpiece.

Therefore, using the T code for tool changes is necessary.

On lathes, the T change is used less often because the same cutter can perform many turning operations.

However, operations such as parting-off, threading, and drilling require specific tools. The number of tools allowed on a CNC lathe depends on the number of cutters that fit in the machine’s turret.

What does T0X.0X mean?

You may see some codes that have two numbers separated by a dot along the T code (e.g. T01.01).

This formatting exists because some controllers such as Fanuc allow different offsets to be assigned to the same tool.

Depending on the type of operation performed by the tool, you may need to change its offset values. T01.01 is tool #1, offset 1. T01.02 would be tool #1, offset 2.

Want to learn more about CNC G Code?

S CNC Code Explained: An Easy Intro for Beginners [Spindle Speed]

What is the S CNC code used for?

The S code is used together with a number value to set the speed of the spindle on a CNC machine.

The number value can have up to four digits and cannot have a decimal point. 100.5 is not an acceptable value to use when setting the spindle speed. 100 or 101 would be acceptable alternatives.

The number value is in revolutions per minute (RPM).

For example, to set the machines spindle to 3000 RPM use “S3000”.

The majority of machines will allow four digits to be used to set the spindle RPM. So anything from 1 to 9,999 RPM. Some machines will allow five digits which allows them to go from 1 to 99,999 RPM

The required spindle speed will vary and can be higher or lower depending on the material of the workpiece, surface finish requirements and the limitations of the CNC machine itself.

When to use the S code?

S codes show up in CNC programs at many locations including at the beginning of the program, after setting the spindle rotation direction (clockwise/counterclockwise) and also whenever there is a change of material, surface, or tool.

Setting the spindle speed at the start of each new section of the program ensures that the spindle is running at the correct RPM if part of the program needs to be rerun for any reason.

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

Types of spindle control

The spindle is a very important part of any CNC machine. It doesn’t matter if you are working with a mill or a lathe.

Almost everything you do with your CNC will involve the spindle in some way or another.

Below are the types of control that you have over the spindle and some of it’s related functions as well as the codes they use.

Direction of rotation – M03 & M04

Spindle direction

The direction of rotation of the spindle can be set in the CNC program by using either the M03 code (clockwise rotation) or the M04 code (counterclockwise rotation).

Using either of these codes turns the spindle on in the direction chosen.

Most machines use tooling that requires using clockwise rotation of the spindle (M03).

Spindle stop – M05

The M05 code is used to stop the spindle.

M05 is normally used when there is a tool change or at the start of a new section of the program.

The spindle will also stop after the tool goes to the home position at the end of the program when using the M30 code.

The emergency stop button can also be used to stop the spindle in the case of emergencies.

Speed

There are two types of speed control for a CNC spindle. 

CNC mills mainly work in RPMS and you usually will not need to specify this in your program.

CNC lathes on the other hand use G96 and G97 to switch between the two speed modes.

Revolutions per minute – G97

Using this spindle speed mode, the spindle will turn at a constant rate as specified by the S code.

For example if you set the speed with S5000, the machine spindle will rotate at 5,000 RPMs until it is changed with another S code or the spindle is stopped using the M05 code.

Constant surface speed – G96

The other spindle speed mode is constant surface speed using the G96 command.

This code instructs the machine to maintain a constant cutting speed at the tip of the tool.

A smaller diameter workpiece will need to spin faster to maintain the same surface speed as a larger part.

The picture below shows how if both parts make one revolution, then the larger part will have traveled faster because it needs to cover a larger distance in the same amount of time.

illustration that shows the difference between the cutting speed of a smaller and larger diameter for cutting speed

Using the constant surface speed mode will cause the machine to change the RPMs of the spindle as the size of the part changes so that the cutting speed is constant.

This way of programming can provide a better surface finish and extend tool life.

Spindle orientation – M19

The M19 command is used to precisely load a tool into the spindle. 

This is more critical with certain canned cycles such as boring.

Tool change – M06

Most machining centers allow the swapping of tools in and out of an automatic tool changer.

Automatic tool changers increase the productivity of the machine by decreasing the amount of time needed to swap cutting tools.

Common codes used with the S command

Spindle speed vs cutting speed

Both spindle speed and cutting speed are commonly used together, but they mean different things.

The cutting speed given by the F code controls how fast the tool advances in a given machining process. The cutting speed controls the forward movement at which the tool will remove material while cutting.

Meanwhile, spindle speed (S code) is the number of turns the spindle can do in one minute (revolutions per minute). Both are really important factors to be aware of when you are programming.

Speeds and feeds. Take note because this will be referred to often.

Proper speeds and feeds prevent tool breakage and increase tool life while also allowing for a higher quality surface finish.  

How should you change your spindle speed based on the workpiece?

Small pieces usually require less speed than workpieces of big diameters.

However, it also depends on the process and the material you are machining.

If the material is very rigid, it is recommended to use a low spindle speed at first and low feed rates, later you use a higher spindle speed for an optimal end finish of the workpiece and higher feedrate.

Want to learn more about CNC G Code?

R CNC Code Explained: An Easy Intro for Beginners [Multi Use Code]

What does the R CNC code do?

There are two main functions for the R code when CNC programming, radius size and setting the retract (R) plane.

When combined with the G02 or G03 circular interpolation commands, the R code specifies the size of the radius to be machined.

When combined with canned cycles such as G73 through G89, the R code specifies the location of the R plane or clearance height to be used.

Radius size with G02 or G03

There are two types of circular movement (interpolation) that can be used when CNC machining.

They are G02 for clockwise movement and G03 for counterclockwise movement.

a comparison of the type of movement created with G00, G01, G02 and G03 cnc g codes

The format for using both codes is:

G02 (or G03) X10 Y15 R5 F30

The X and Y locations are the end point of the circular movement. 

The R value is the radius size of the circle and F is the feedrate.

illustration that compares a circles radius and diameter

Circular movement can also be performed using IJK codes instead of XY locations and an R value.

Both methods are common, so be on the lookout for either type.

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

R plane with canned cycles

The R plane is a specific height that the CNC will reference when performing a canned cycle.

First it is the height that the machine will rapid travel to and begin the machining part of the canned cycle.

It is also the height that the machine will return to if the G99 [return to R plane] command is active.

If the G99 [return to initial plane] command is active then the machine will return to the start of the canned cycle instead.

visualization of how a cnc machine moves using g98 and g99 codes shows motion of travel for the machine

The G98 and G99 codes are used to switch between the two types of clearance modes available when running canned cycles.

The G98 code moves the cutter further up and away because in most instances the canned cycle is started at a higher Z height location further away from the part. This increases the time that it takes the machine to run the program.

The G99 command is used to keep the machine closer to the part being machined. It should only be used when you are sure that there are no obstacles in the way such as the part itself, clamps or fixtures.

Format for using the R code with canned cycles

The format for using canned cycles is:

G81 X0 Y0 R5 Z1 F5 G98

G81 is the canned cycle. This can be replaced with any other canned cycle as needed.

The R code sets the retract plane height. 

The X, Y and F codes are not required. 

X and Y are the location where the canned cycle will be run. F is the feedrate of the cutter.

Often, the X and Y codes will be placed on the previous line.

It is good practice to include the feedrate (F) code.

Want to learn more about CNC G Code?

Q CNC Code Explained: An Easy Intro for Beginners [Peck Depth]

What does the Q CNC code do?

The Q code is used to specify the depth of each peck when peck drilling with either the G73 or G83 code.

The “peck” is how much farther down the machine will drill on each pass.

When peck drilling, the machine drills down, backs up, drills further down, backs up, drills even further down and repeats until the cycle has been completed.

Difference between peck and standard drilling

illustration that shows the difference between peck drilling and standard drilling in a CNC machine

The main difference between peck and standard drilling is that the peck drilling process removes the material in multiple steps, while standard drilling is a one-step process.

Both commands are intended to make holes, but the peck drilling cycle allows more control over how the hole is drilled.

Peck drilling is the first option for CNC programmers when making blind holes. The pecking process helps break chips as well as remove them and any other debris from the hole during the drilling process.

Trouble removing chips from the hole when drilling can cause size and/or surface finish issues.

Standard drilling is far more common when dealing with simple through (thru) holes.

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

Types of peck drilling

There are two peck drilling cycles, G73 and G83.

They are both canned cycles with one key difference. 

Using the G83 code will retract completely out of the hole after each peck to the retract plane. 

Using the G73 code will only retract to the start of each peck.

an illustration that shows the difference between the G73 and G83 CNC codes

The G73 canned cycle is a peck drilling cycle but with a shorter retract intended for relatively shallow holes. 

Meanwhile, the G83 command is peck drilling with a full retract intended for deep holes.

Both commands are meant to help break and clear chips. 

G83 does a better job of this by fully retracting out of the hole but with the added expense of a longer cycle time.

On the other hand, G73 is designed to break up stringy chips, while G83 is intended to pull chips up and out of deep holes.

Other codes used with the Q code

The Q code is used in peck drilling canned cycles G73 and G83. 

You can also expect to find the following codes used in those canned cycles:

Format for using the Q code

The format for using a Q code is shown below:

G98 G83 R1.0 Z-5.0 Q1.0 F25.0

This example shows a peck depth (Q) of 1.0.

This means that the machine will drill the hole in 1.0 unit steps. This would be 1 inch or 1mm depending on which unit mode you currently have active.

Want to learn more about CNC G Code?

P CNC Code Explained: An Easy Intro for Beginners [Multi Use Code]

What does the P CNC code do?

The P code is used for multiple uses in CNC programming.

Those uses depend on which other codes the P code is combined with.

The four uses of the P code are selecting a subprogram, setting a dwell time, selecting an offset number, and setting a scale factor.

The most common uses of the P code are to select a subprogram and to set dwell times.

Subprogram number when choosing a subprogram with M97 or M98

Subprograms are a separate CNC program selected to run from within another program.

They are used to perform repetitive machining operations or sequences such as drilling, counterboring and countersinking a hole.

CNC machines run the lines of code in a program in order. 

Using subprograms allows the programmer to jump around to different sections of the current program or to run a different program and come back to the current program.

When used with either the M97 code or the M98 code, the P code tells the machine where to go run the subprogram. 

This will either be a specific line in the current program (M97) or an entirely different program (M98).

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

The P code when using M97 [subprogram call by line]

illustration that shows the flow of a cnc program when using the m97 command to call a subprogram

M97 P1234 L5

This line of code will tell the machine to move to line N1234 of the current program. 

The L code tells the CNC machine to run the subprogram five times. The L code can be left out if the subprogram will only be run once.

Line 1234 will be after the program end command (M30). Once the machine reaches the M99 command it will return to the line after the example shown above.

The P code when using M98 [subprogram call by program number]

M98 Code Flow

M98 P5678 L2

This line of code will tell the machine to go run program number 5678 two times. 

The L code tells the CNC machine to run the subprogram twice. The L code can be left out if the subprogram will only be run once.

Program 5678 will perform any necessary machining functions and then end with an M99 code instead of an M30 code

The M99 command will make the machine return to the line after the example shown above.

On the other hand, both commands (M98 and M97) use parameters K (or L) as the number of repetitions of a given subprogram. 

The use of the letter K or L depends on the CNC model. Always check the machine’s manual for further details.

Dwell time with G04

Another use of the P code is to set a dwell time.

A G04 code makes the cutting tool stop moving for specified amount of time. Following that amount of time, the machine will proceed to the next line of code.

There are some variations between machines when using G04.

Not all machines will use the P code to set the dwell time. 

Some CNCs use either F, U or X instead of P.

Also, some machines will read the value after the P code as seconds and some will read it as milliseconds.

1 second = 1,000 milliseconds.

So P300 could mean 300 seconds (5 minutes) or 0.3 seconds depending on how your CNC reads the code. Makes sure to verify how you machine will read the P code when using the G04 command.

Dwell time with G82 and G89

G82 [drill cycle with dwell] and G89 [boring cycle with dwell] are both canned cycles with dwells.

Similar to the G04 command, G82 and G89 both use the P code to specify how long to stop and wait at the bottom of the hole. 

Usually this time is in milliseconds. Machines vary though so make sure your machine isn’t reading the P code as full seconds.

Fixture offset number with G10

Using the P code with G10 tells the machine which fixture offset to change.

Below is a list of the values used with the P code and which fixture offset they correspond to:

Code

Fixture Offset

G10 P1

G54

G10 P2

G55

G10 P3

G56

G10 P4

G57

G10 P5

G58

G10 P6

G59

Scale factor with G51

CNC programs can be scaled up or down using the G51 command.

The P code is used to set the scaling factor when used with G51.

This means that a P2 code will tell the machine to make everything in the program 2x larger.

On the opposite end, a P0.5 code will tell the machine to make everything half the normal size.

Conclusion

The P code can be used in many different ways when CNC programming.

Using the P code to select a subprogram is pretty much universal across the different CNC machine manufacturers.

The other uses of the P code are less standard. 

At times the CNC will read the code different such as seconds vs. milliseconds or it will require a different code altogether such as in the case of dwell times with G04.

Make sure you check your individual machine to understand the requirements of your individual CNC.

Want to learn more about CNC G Code?

O CNC Code Explained: An Easy Intro for Beginners [Program Number]

Welcome to the exciting world of CNC programming!

If you’re curious about the O CNC code and want to learn how to use it, you’ve come to the right place.

In this easy-to-follow guide, we’ll teach you everything you need to know. Don’t worry if you’re new to this, because the O code is super easy to learn.

So, let’s jump in to discover the O CNC code together!

What does the O code do?

The O code lists the CNC program number. 

It is found on the first line of a CNC program.

Example program numbers

Code BlockProgram Number
O00011
O12341234
O0555555

What range of numbers can you use with the O code?

Most controllers allow the use of program numbers O0001 through O9999.

Some newer machines allow a larger range of program numbers, but they would rarely be used or needed.

O code program example

Code BlockCode Description
O0001(Program number: O0001)
G90 G80(Absolute positioning mode, cancel canned cycles)
G54 G0 X0 Y0 (Select coordinate system G54, rapid move to X0 Y0)
M3 S1000(Start spindle clockwise at 1000 RPM)
G43 H1 Z0.1(Tool length compensation active, move Z to 0.1)
G1 Z-0.5 F100(Linear move to Z-0.5 at a feed rate of 100)
X1 Y1(Linear move to X1 Y1)
X0 Y0(Linear move back to X0 Y0)
G0 Z1(Rapid move to Z1, retracting the tool)
M30(End of program and rewind)

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

Using the O code when running subprograms with M97 and M98

Subprograms are reusable pieces of code that are often used for repetitive tasks such as drilling holes or changing a tool.

There are two codes used to call subprograms; M97 & M98.

When using M98, the P code is used to choose the subprogram to be run.

For example, M98 P5678 will tell the CNC to run subprogram O5678.

M98 Code Flow

M98 calls a separate program.

The M97 code calls a subprogram by line number in the current program.

Important: M97 and M98 call subprograms differently. M97 jumps to another line in the current program. M98 moves to a completely separate program.

An example of this is M97 P500 will tell the machine to jump to line number 500 in the current program. The N code notes the line number.

illustration that shows the flow of a cnc program when using the m97 command to call a subprogram

Want to learn more about CNC G Code?

N CNC Code Explained: An Easy Intro for Beginners [Program Line Number]

What does the N CNC code do?

N codes are used in a CNC program to identify the block or line of the program.

In most programs, the blocks increase by 5 or 10 on each line. This allows you to add lines in between if the program needs to be edited.

N codes are not required.

They can be left out entirely if the programmer chooses to do so.

Some example N code sequences are shown below:

Increment By

N Code Sequence

1

N01

N02

N03

N04

N05

5

N05

N10

N15

N20

N25

10

N10

N20

N30

N40

N50

Where does the N code show up in the CNC program?

If they are used, generally N codes will show up at the beginning of each block and typically run through the entire program.

There are some programmers who chose to only use line numbers (N codes) on important lines such as the start of a new tool.

Why are N codes used?

N codes help the programmer follow along with the program. 

This is especially true for new programmers.

Identifying each line of code with a sequence (N) number makes it easier to find or modify different sections of the program.

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

Drawbacks to using N codes

Using N codes throughout your program does make the program file size larger because it contains more characters. 

This is more of a problem for older machines with very limited memory.

Format for using an N code

The N-code should be the first thing on every block (line) of the program.

This is how it looks in practice:

N10 G90 F100 S1200 T01.01

N20 G00 X36 Z10

N30 X26 Z2

N40 G01 Z-12 M03

N50 X36 M05

N60 G00 Z10

N70 M30

Notice how the N numbers jump up by 10 on each line.

If we needed to insert a couple more lines of code into the program it might end up looking like this:

N10 G90 F100 S1200 T01.01

N20 G00 X36 Z10

N30 X26 Z2

N34 Y20

N38 X40

N40 G01 Z-12 M03

N50 X36 M05

N60 G00 Z10

N70 M30

When editing the program we tried to keep the lines spaced out in case it needs to be edited again in the future. 

We could have numbered the new lines as N31 and N32 or N32 and N34. It really is up to the person editing the program. 

Good practice is to try and leave gaps in the numbers if possible.

Best way to number your N codes

The standard recommended format for numbering your lines of code is to increment by 5 or 10. 

This will leave spaces between each line in case the programmer needs to edit the program in the future.

N codes when using the M97 code

The M97 code is used for calling a subprogram by line number.

The format for using an M97 code is:

M97 P125

In this example, the machine will read the subprogram call and then jump to line N125 in the current program. 

Using the M97 command allows you to jump around your program.

When using subprograms with M97, the best practice is to put your subprogram lines at the very bottom of the program. 

Doing this will clearly separate your subprogram from the rest of the program.

When you use the M99 code to end a subprogram called with the M97 code, the machine will jump back to the next line after the M97 was used.

The picture below shows how the program flow works.

illustration that shows the flow of a cnc program when using the m97 command to call a subprogram

Frequently asked questions about N codes

Do you have to use sequence (N) numbers in your program?

No, sequence numbers with the N code are not required but they are highly recommended. 

Using them will make your program easier to read and follow.

Additionally, increasing the size of the program is less of an issue with newer machines than it was in years past.

Can you use a negative N number?

No, it is not possible to use negative numbers to identify the lines of a program.

Can you use a N number with a decimal point?

No, you will need to use whole numbers when using the N code.

What happens if my sequence (N) numbers are out of order?

The CNC will read them from top to bottom. 

If you sequence numbers are out of order such as:

N5

N20

N15

N10

N30

The machine will still read them in the order they are shown above. Unless you are using subprograms, the machine just ignores the sequence (N) numbers.

Want to learn more about CNC G Code?

H CNC Code Explained: An Easy Intro for Beginners [Height Offsets]

What does the H CNC code do?

The H CNC code tells the machine to which height (H) offset to use with the current cutting tool.

The value of the H offset is the distance between the end of the spindle and the end of the cutting tool.

The H offset/code is used when tool length compensation with either the G43 or G44 codes are used in the program.

illustration of a cnc cutting tool that shows what an H offset is

What is tool length compensation?

Every time a new tool is placed in the CNC spindle, the length of the tool is different. 

This is often the case even when putting the same tool back into the spindle.

Depending on the types of tolerances you are working with, the tool sitting in the spindle 0.001” higher or lower can create a part that is out of spec.

To account for the difference in tools, an H offset value can be assigned to each cutting tool.

Setting up your machine and program in this way allows you to use the same program with a variety of different tools.

You wouldn’t want to have to rewrite your program just because you got a new end mill and it is 0.010” shorter than the last one. Tool length compensation lets you account for this type of change in your cutting tool.

There are two types of tool length compensation, positive compensation with G43 and negative compensation with G44.

G43 is the most common type of tool length compensation and used in the vast majority of CNC programs. 

If you are just learning what tool length compensation is, then ignore G44 for now. You aren’t going to be using it.

Learn CNC Programming – It’s Easier Than You Think!

Learning G Code doesn’t have to be difficult…

If you know what to focus on.

Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.

Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!

Make Learning G Code Easy

H offset vs D offset

D offsets store the diameter of the cutting tool. 

D offsets are called up using the D code.

illustration of a cnc cutting tool that shows what an D offset is

H offsets store the length of the cutting tool and are called up using the H code.

Both offset types allow more flexibility when running the program and when switching between various cutting tools.

Both offset types are stored in the tool offset table.

mach 3 tool offset table
Tool offset table

H offset numbering and tools

Generally, the H code number is the same as the tool number in the Tool Offset Library.

For example, when using tool #1 (T1), it is advised to pair it with H offset #1 (H1). The same can be said for tool #2 and H offset #2 and so on.

Using a pattern like this makes it easier for the operator or programmer to understand.

Machines vary with how they handle offset numbering. 

Unfortunately, some machines won’t allow you to set a diameter (D) offset and height (H) offset for the same offset number.

If you are unable to use the H1 offset with tool #1 then try to create your own logical pattern. 

Usually this means starting the numbering of one type of offset at a higher number such as H21 or H51. This is more of an issue with older CNCs.

How to use the H code in a program?

T01 M06  

G00 X4.0 Y5.0

G43 H1 Z2.0

G01 Z-3.0 F100

In this example, the machine will switch to tool #1.

There will then be a rapid movement to the XY location of (4.0, 5.0) or X=4.0 and Y=5.0.

Tool length compensation is then turned on with the G43 command using the H1 height offset. 

The machine will then move to a Z location of 2.0. 

This will be a rapid movement as well because the movement mode has not been changed since the G00 rapid travel command was used.

The last line sets the CNC to straight line movement (linear interpolation) with a feed rate of 100. The machine will move to a Z location of -3.0.

Want to learn more about CNC G Code?