M05 CNC Code Explained: An Easy Intro for Beginners [Spindle Off]

What does a M05 code do?

The M05 code turns the spindle off.

M05 cancels any spindle rotation that has been set with either the M03 code (clockwise spindle rotation) or M04 code (counterclockwise spindle rotation).

Which codes are used to turn the spindle on?

Spindle direction

There are two codes for turning the CNC spindle on.

M03 turns it on in a clockwise direction. M03 is used with right hand tooling.

M04 turns it on in a counterclockwise direction. M04 used with left hand tooling.

If you need to change directions, then it is best to stop the spindle before changing directions.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M5 vs M05

Some people get confused about whether the 0 is required to include with an M05 code.

To clear this up, there is no need to include the zero in the code. 

The CNC machine will read the code the same. It really is just a matter of preference.

Often you will see the full M05 code used in textbooks or other reference materials. In practice though, many prefer the shortened M5 code in their programs.

If you are working on your own, then go with whichever format you prefer. 

If you work in a bigger shop, make sure to stick to the format that the business has been using.

What other codes will stop the spindle?

There are multiple codes that turn the spindle off. 

Most of them perform multiple functions such as stopping various machine functions all at once. Some of those codes are:

M00 and M01 are both stops.

They both will turn off the spindle, turn off the coolant, stop any machine movement and pause your program until the cycle start button is pushed again. 

The difference between the two is that M00 will always cause the machine to act in this way and M01 will only cause the CNC to perform these action if the optional stop switch or button is set to on.

M02 and M30 are both program end codes. 

They will cause the machine to shut everything down just like the M00 and M01 codes but instead of pausing the program, they stop it.

M02 is a legacy code from when many CNC programs were made on tape for the machine to read. The M02 code would cause the machine to end the program. 

The M30 command would end the program and then rewind the tape back to the beginning of the program.

In many newer CNC machines, the M02 code is treated the same as the M30 code. In cases where the default is not to treat the codes the same, the machine often has a setting that can allow it to treat them both as M30 codes.

M02 codes are not needed on newer CNC machines. M30 is the standard code to end a CNC program.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M06 CNC Code Explained: An Easy Intro for Beginners [Tool Change]

What does a M06 code do?

The M06 code tells the CNC to perform a tool change.

The machine will swap the tool that is in the ready position of the automatic tool changer into the spindle.

Preparing the CNC for a tool change

You should be aware of some of the characteristics of your cutting tool. The size, as in diameter and length need to be taken into account.

A cutter with a very large diameter may need to be placed near empty spots in your tool changer so it doesn’t smash into other tools.

Length isn’t usually as big of an issue but still keep it in mind to make sure you have enough clearance with the machine and with your part.

Another big thing to consider is the weight of the cutter. Check your machine manual because the capabilities of various machines differ. 

Remember that the total weight of everything you are putting in the spindle or automatic tool changer is what counts, not just the cutter.

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

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

Executing a tool change

Certain conditions need to be met for a safe tool change.

Machine movement needs to be stopped and the machine should be in the tool change position. Often this position is the same as the home position.

The spindle should be stopped and the tool to be swapped in should be in the ready position.

Performing a tool change without an automatic tool changer

If your machine doesn’t have an automatic tool changer, then the operator (probably you) will need to change the tools manually.

The best way to do this is to put a program stop in your program using the M00 code. You should include comments in your program at the program stop to tell the operator what they are expected to do.

What other CNC codes are used with the M06 command?

The main code used with the M06 command is the T code.

The T code selects the tool that will be swapped in.

The line of code performing the tool change will usually be something like:

T02 M06

In this example tool #2 is readied for the tool change and then the M06 tells the CNC to swap the tool into the spindle.

M6 vs M06

Some people get confused about whether the 0 is required to include with an M06 code.

To clear this up, there is no need to include the zero in the code. 

The CNC machine will read the code the same. It really is just a matter of preference.

Often you will see the full M06 code used in textbooks or other reference materials. In practice though, many prefer the shortened M6 code in their programs.

If you are working on your own, then go with whichever format you prefer.

If you work in a bigger shop, make sure to stick to the format that the business has been using.

Frequently asked questions about the M06 code

Do you use the M06 code if you want to switch to an empty spindle?

Yes, just call up a tool holder location without a tool in it. 

When the M06 command is used the machine will swap in the empty spot, leaving no tool in the spindle.

Do you need to use the M06 command if you don’t have an automatic tool changer?

No, if you don’t have an automatic tool changer, the M06 code isn’t needed.

Without an automatic tool changer all tool changes will need to be done manually.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M07 CNC Code Explained: An Easy Intro for Beginners [Mist Coolant]

What does the M07 CNC code do?

M07 is a modal command which turns on mist coolant.

Mist coolant is compressed air and coolant (usually oil). 

It can be more efficient than flood coolant and in some situations be better at clearing chips than flood coolant, but it is used far less often than flood coolant with the M08 command.

M07 [mist coolant] vs M08 [flood coolant]

The most common type of coolant used in CNC machining is flood coolant using the M08 code.

Flood coolant is where the cutter and workpiece are literally flooded with the coolant fluid. This works to lubricate the part, remove chips, and reduce heat buildup.

Mist coolant with the M07 code combines the coolant fluid with compressed air and sprays it on the cutting area.

Mist coolant uses less coolant fluid and can be less messy depending on your CNC setup, but it isn’t as good at reducing heat buildup as flood coolant is.

cnc machine table with coolant lines and spindle shown
Flood coolant in action

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

Advantages of using coolant

The three main advantages of using coolant with your CNC machine are:

  • Lubricate the cutter
  • Clear chips
  • Reduce heat buildup

What are the other options for using coolant?

We discussed mist and flood coolant above but there are other options available as well including:

  • Air: This type of coolant reduces heat and clears the chips, but it does not lubricate the workpiece. This type of coolant is used more often with sensitive materials.
  • High pressure: High pressure coolant is similar to flood coolant. The only difference is that it hits the workpiece at greater than 1000psi. The higher pressure and higher quantity of coolant helps cool the cutter better and remove more chips.
  • Through spindle: Through spindle coolant delivers the coolant directly where it is needed. It is better at clearing chips in many cases because the coolant from the spindle pushes the chips out of a hole or pocket.

How to turn off coolant

The M-code to turn coolant off is M09.

However, there are other stopping commands that also turn off coolant as part of their function including:

Frequently asked questions about the M07 code

Does every CNC machine have coolant capabilities?

Yes and no.

You can expect that all industrial level CNC machines will have coolant capabilities.

Home or hobbyist level machines such as CNC routers often do not have coolant capabilities, but they can usually be added on if needed.

12 (2)
CNC routers do not usually come with coolant functions

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M08 CNC Code Explained: An Easy Intro for Beginners [Flood Coolant]

What does the M08 CNC code do?

M08 is a modal command which turns on flood coolant.

Flood coolant is literally flooding or washing the cutter and/or workpiece in liquid coolant. The coolant helps remove chips from the cutting area.

If chips are not moved from the cutting area, then they can end up being recut which tends to cause heat buildup and can damage your cutting tool.

Flood coolant also cools the cutter and workpiece and lubricates the cutter. 

Both of these things help extend the life of your cutter as well.

cnc machine table with coolant lines and spindle shown
Flood coolant in action

M08 [flood coolant] vs M07 [mist coolant]

The most common type of coolant used in CNC machining is flood coolant with the M08 code.

The second most common option is mist coolant using the M07 code.

Mist coolant combines the coolant fluid with compressed air and sprays it on the cutting area. 

Mist coolant uses less coolant fluid and can be less messy depending on your CNC setup, but it isn’t as good at reducing heat buildup as flood coolant is.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

Advantages of using coolant

The three main advantages of using coolant with your CNC machine are:

  • Lubricate the cutter
  • Clear chips
  • Reduce heat buildup

What are the other options for using coolant?

We discussed mist and flood coolant above but there are other options available as well including:

  • Air: This type of coolant reduces heat and clears the chips, but it does not lubricate the workpiece. This type of coolant is used more often with sensitive materials.
  • High pressure: High pressure coolant is similar to flood coolant. The only difference is that it hits the workpiece at greater than 1000psi. The higher pressure and higher quantity of coolant helps cool the cutter better and remove more chips.
  • Through spindle: Through spindle coolant delivers the coolant directly where it is needed. It is better at clearing chips in many cases because the coolant from the spindle pushes the chips out of a hole or pocket.

How to turn off coolant

The M-code to turn coolant off is M09.

However, there are other commands that turn off coolant as part of their function including:

Frequently asked questions about the M08 code

Does every CNC machine have coolant capabilities?

Yes and no.

You can expect that industrial level CNC machines will have coolant capabilities.

Home or hobbyist level machines such as CNC routers often do not have coolant capabilities, but they can usually be added on if needed.

12 (2)
CNC routers do not usually come equipped with coolant functions

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M09 CNC Code Explained: An Easy Intro for Beginners [Coolant Off]

What does the M09 CNC code do?

The M09 M-code turns off the CNC machine’s mist and flood coolant functions.

This shuts off the “normal” coolant flow.

Other types of coolant, such as through spindle coolant, usually have their own code for turning the specialized coolant functions on and off.

Advantages of using coolant

The three main advantages of using coolant with your CNC machine are:

  • Lubricate the cutter
  • Clear chips
  • Reduce heat buildup

M07 [mist coolant] vs M08 [flood coolant]

The most common type of coolant used in CNC machining is flood coolant with the M08 code.

Flood coolant is where the cutter and workpiece are literally flooded with the coolant fluid. 

This works to lubricate the part, remove chips, and reduce heat buildup.

cnc machine table with coolant lines and spindle shown
Flood coolant close up

Mist coolant combines the coolant fluid with compressed air and sprays it on the cutting area. 

Mist coolant uses less coolant fluid and can be less messy depending on your CNC setup, but it isn’t as good at reducing heat buildup as flood coolant is.

Mist coolant is activated with the M07 code.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

What are the other options for using coolant?

We discussed mist and flood coolant above but there are other options available as well including:

  • Air: This type of coolant reduces heat and clears the chips, but it does not lubricate the workpiece. This type of coolant is used more often with sensitive materials.
  • High pressure: High pressure coolant is similar to flood coolant. The only difference is that it hits the workpiece at greater than 1000psi. The higher pressure and higher quantity of coolant helps cool the cutter better and remove more chips.
  • Through spindle: Through spindle coolant delivers the coolant directly where it is needed. It is better at clearing chips in many cases because the coolant from the spindle pushes the chips out of a hole or pocket.

Things to consider when using the M09 code

You never want to turn off coolant while the cutter is still cutting. 

On the opposite end of things, you should never turn coolant on if the cutter is already cutting.

Always start and stop coolant flow when the cutter is not actively engaged with the part.

Which other CNC codes turn coolant off?

The main M-code to turn coolant off is M09.

However, there are other codes that turn off coolant as part of their function including:

Frequently asked questions about the M09 code

Does every CNC machine have coolant capabilities?

Yes and no.

You can expect that industrial level CNC machines will have coolant capabilities.

Home or hobbyist level machines such as CNC routers often do not have coolant capabilities, but they can usually be added on if needed.

homemade cnc router machine
CNC routers do not usually come equipped with coolant functions

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M30 CNC Code Explained: An Easy Intro for Beginners [Program End With Rewind]

What does the M30 CNC code do?

The M30 code ends the program and tells it to go back to the start of the program. 

This is sometimes called program end with rewind.

The rewind has to do with the way CNC program used to be read. 

They were on tapes or cards. Using M30 would tell the machine to rewind so that the machine was ready to run the program again.

The M30 code turns off the following machine functions:

  • The movement of all axes
  • Spindle rotation
  • Coolant

The other code that can be used to end a program is M02. The M02 code ends the program but does not rewind the program back to the start.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

What is the difference between M02 and M30

The main function of both the M02 and M30 commands are basically the same, they stop the program.

However, there are a few differences to keep in mind.

Here is a list with the main differences between the two codes.

M02

M30

Program end, no rewind

Program end with rewind

Primarily for older machines

Works on older and new machines

Stops the program and leaves it at the current location

Stops the program and rewinds it back to the start

Modal commands remain active

Resets the machine to the default modes

M02 is not used often anymore. M30 is the main code used to end a program.

M02 was used for a couple of reasons back when programs were on tape or cards.

Some machines were known to have issues rewinding the program and might destroy the program tape. In other instances, the program would be taped together as a loop which meant it didn’t need to be rewound. Continuing on from the end would loop back to the start of the program.

It is worth checking your machine manual to understand how your machine will read a M02 code. Some machines can even be set to read an M02 code as if it were an M30 code. Check your manual.

How to end a subprogram

M02 and M30 can be used to end your main program, but how do you end a subprogram?

The only way to end a local subprogram, subprogram, or macro is by using the M99 command.

The M99 code tells the machine to return to the program where the subprogram was called up. Usually this means returning to the main program. 

You can run subprograms within subprograms. This is called nesting.

Nesting can get tricky to read, so the majority of programs will only run subprograms from the main program to avoid confusion.

If the programmer ends the sub-program using M30 instead of M99, the machine will not return to the main program. 

M99 must be used to tell the CNC machine to return to the line after the subprogram command (M98) was used.

Best practices for using the M30 code

You should always take safety steps before stopping the program with the M30 code.

Always check where the spindle is before finishing the program. This means it is in a safe place where it will not crash or cause an accident.

For CNC mills this can mean rapid traveling the spindle to a safe Z-value before using the M30 code.

It is also a good practice to turn off the coolant using the M09 code before stopping the program.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M97 CNC Code Explained: An Easy Intro for Beginners [Subprogram Call by Line]

What does the M97 code do?

The M97 CNC code is a subprogram call by line number.

This means that the machine will run a subprogram contained within the main program.

The machine will jump to the line number in the current program that is called out with the M97 code.

At the end of the subprogram, the M99 code will be used. This signifies the end of the subprogram.

Once the machine reads the M99 code, it will return to the line in the program after the initial M97 code.

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

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

Other codes used with the M97 code

Format for using an M97 code

Here is an example of an M97 code in use:

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 code it will return to the line after the example shown above.

What are subprograms used for?

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. 

They can also be used for machine related functions such as preparing for a tool change.

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.

Differences between a subprogram and the main program

The commands to end the main program and a subprogram are different.

The main CNC program ends with either M30 code (for most machines) or M02 (for older machines). No further code will be run after either of these codes.

A subprogram ends with the M99 command

When using the M97 command, M99 returns the machine to the line which called the subprogram. The machine will continue to run code in the program from that point.

Let’s look at that program flow again to see how the machine moves from the subprogram call (M97) to the subprogram and back to the main program after the M99 code.

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

Benefits of using a subprogram

The greatest benefit of using subprograms is reducing the lines of code which makes the program easier to read and edit.

Subprograms can also reduce the number of errors in the program. Less lines of code means less possible sources of problems.

Drawbacks to using a subprogram

Subprograms are meant to make the program easier to work with for both the programmer and anyone using it. If not done correctly, they can have the opposite effect.

Programmers should remember that part of their job is making sure that the program is easy to use for the operator.

It can also be easy to have the wrong modal commands or offsets chosen when starting or ending a subprogram. 

Following a good program format that uses safety lines or blocks of code can protect from this happening.

If it is important that a mode or offset is needed in a specific section of your program, it is best to use the necessary code to make sure things are set correctly. 

Assuming your machine is in the correct mode already is dangerous. Set the correct modes when you need them.

Making subprograms inside your subprograms (nesting) also has the potential to cause confusion. Repeatedly switching between programs can be confusing for both programmers and operators.

M97 vs M98

There are two ways to run subprograms: M97 and M98.

The difference between M97 and M98 is the program location they move to.

M97 will jump to a new line in the current program.

M98 will run an entirely different program.

M98 Code Flow

Frequently asked questions about the M97 code

How do modal commands work in subprograms?

Modal commands work the same way as they do in the main program. This means they stay on until changed or turned off.

If a modal command is on when starting the subprogram, it will stay on while running the subprogram. The same is true when switching from the subprogram to the main program.

The safest thing to do is make sure your safety blocks cover the required codes for each section of code.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M98 CNC Code Explained: An Easy Intro for Beginners [Subprogram Call]

What does the M98 code do?

The M98 CNC code is a subprogram call by program number.

This means that the machine will run a separate program stored on the machine.

The machine will jump from the line in the current program to the start of the subprogram called up with the M98 code.

At the end of the subprogram, the M99 code will be used. This signifies then end of the subprogram. 

Once the machine reads the M99 code, it will return to the line in the original program where the M98 code was used. 

The machine will continue reading the program starting with the line after the M98 code.

M98 Code Flow

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

Other codes used with the M98 code

Format for using an M98 code

Here is an example of an M98 code in use:

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 command instead of M30

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.

What are subprograms used for?

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. 

They can also be used for machine related functions such as preparing for a tool change.

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.

Differences between a subprogram and the main program

The commands to end the main program and a subprogram are different.

The main CNC program ends with either M30 code (for most machines) or M02 (for older machines). No further code will be run after either of these codes.

A subprogram ends with the M99 command. 

When using the M98 command, M99 returns the machine to the line in the original program which called the subprogram. The machine will continue to run code in the program from that point.

Let’s look at that program flow again to see how the machine moves from the subprogram call (M98) to the subprogram and back to the main program after the M99 code.

M98 Code Flow

Benefits of using a subprogram

The greatest benefit of using subprograms is reducing the lines of code which makes the program easier to read and edit.

Subprograms can also reduce the number of errors in the program. Less lines of code means less possible sources of problems.

Drawbacks to using a subprogram

Subprograms are meant to make the program easier to work with for both the programmer and anyone using it. If not done correctly, they can have the opposite effect.

Programmers should remember that part of their job is making sure that the program is easy to use for the operator.

It can also be easy to have the wrong modal commands or offsets chosen when starting or ending a subprogram. 

Following a good program format that uses safety lines or blocks of code can protect from this happening.

If it is important that a mode or offset is needed in a specific section of your program, it is best to use the necessary code to make sure things are set correctly. 

Assuming your machine is in the correct mode already is dangerous. Set the correct modes when you need them.

Making subprograms inside your subprograms (nesting) also has the potential to cause confusion. Repeatedly switching between programs can be confusing for both programmers and operators.

For a more in-depth description of subprogram nesting, see additional info on the topic further down in this post.

M97 vs M98

There are two ways to run subprograms: M97 and M98.

The difference between M97 and M98 is the program location they move to.

M97 will jump to a new line in the current program.

M98 will run an entirely different program.

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

Frequently asked questions about the M98 code

How do modal commands work in subprograms?

Modal commands work the same way as they do in the main program. This means they stay on until changed or turned off.

If a modal command is on when starting the subprogram, it will stay on while running the subprogram. The same is true when switching from the subprogram to the main program.

The safest thing to do is make sure your safety blocks cover the required codes for each section of code.

What is nesting?

So far, we have concentrated on running a subprogram from our main program, but did you know you can also run a subprogram from within a subprogram?

This is called nesting.

Nesting can be a powerful tool but can quickly get out of control. 

Most CNC controllers will allow up to four level deep nesting.

This means calling a subprogram in a subprogram in a subprogram in a subprogram in your main program.

Confusing right? That is why in most cases it isn’t a good idea to nest that deep. For most applications, one level deep is enough.

Even two level nesting can be hard to follow. Three and four levels are definitely not advised if you are reading this post which is aimed at beginners.

Tips for numbering your subprograms

Create a system for numbering your programs and subprograms to avoid confusion.

Some choose to set aside blocks of numbers for each type of program. For example, O0001-O4999 for main programs and O5000-O9999 for subprograms.

Others make their subprograms closely follow their main program numbers. If the main program is O1000 then the subprograms will be O1001, O1002 and so on.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

M99 CNC Code Explained: An Easy Intro for Beginners [Subprogram End]

What does the M99 code do?

The M99 CNC code ends the current subprogram.

Once the machine reads the M99 code, it will return to the line in the program after the subprogram was called up.

If the subprogram was started using M97 (subprogram call by line number) then the machine will return to the line in the current program just after the M97 code.

If the subprogram was started using M98 (subprogram call by program number) then the machine will return to the line in the original program just after the M98 code was used.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

Other codes used with the M99 code

M97 vs M98

There are two ways to run subprograms: M97 and M98.

The difference between M97 and M98 is the program location they move to.

M97 will jump to a new line in the current program.

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

M98 will run an entirely different program.

M98 Code Flow

What are subprograms used for?

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. 

They can also be used for machine related functions such as preparing for a tool change.

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.

Differences between a subprogram and the main program

The commands to end the main program and a subprogram are different.

The main CNC program ends with either M30 code (for most machines) or M02 (for older machines). No further code will be run after either of these codes.

A subprogram ends with the M99 command. 

It doesn’t matter if you use the M97 or M98 command to call your subprogram, M99 will return the machine to the next line in the original program after the M97 or M98 code was used. 

The machine will continue to run code in the program from that point.

If an M30 code is used to end a subprogram then the machine will stop running. It will not return to the line after the subprogram call like it would when using the M99 command.

Benefits of using a subprogram

The greatest benefit of using subprograms is reducing the lines of code which makes the program easier to read and edit.

Subprograms can also reduce the number of errors in the program. Less lines of code means less possible sources of problems.

Drawbacks to using a subprogram

Subprograms are meant to make the program easier to work with for both the programmer and anyone using it. If not done correctly, they can have the opposite effect.

Programmers should remember that part of their job is making sure that the program is easy to use for the operator.

It can also be easy to have the wrong modal commands or offsets chosen when starting or ending a subprogram. Following a good program format that uses safety lines or blocks of code can protect from this happening.

If it is important that a mode or offset is needed in a specific section of your program, it is best to use the necessary code to make sure things are set correctly. 

Assuming your machine is in the correct mode already is dangerous.

Making subprograms inside your subprograms (nesting) also has the potential to cause confusion. Repeatedly switching between programs can be confusing for both programmers and operators.

For a more in-depth description of subprogram nesting, see additional info on the topic further down in this post.

Frequently asked questions about the M99 code

How do modal commands work in subprograms?

Modal commands work the same way as they do in the main program. This means they stay on until changed or turned off.

If a modal command is on when starting the subprogram, it will stay on while running the subprogram. The same is true when switching from the subprogram to the main program.

The safest thing to do is make sure your safety blocks cover the required codes for each section of code.

What is nesting

So far, we have concentrated on running a subprogram from our main program, but did you know you can also run a subprogram from within a subprogram?

This is called nesting.

Nesting can be a powerful tool but can quickly get out of control. 

Most CNC controllers will allow up to four level deep nesting. This means calling a subprogram in a subprogram in a subprogram in a subprogram in your main program.

Confusing right? That is why in most cases it isn’t a good idea to nest that deep. For most applications, one level deep is enough.

Even two level nesting can be hard to follow. Three and four levels are definitely not advised if you are reading this post which is aimed at beginners.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

D CNC Code Explained: An Easy Intro for Beginners [Diameter Offset]

What does the D CNC code do?

The D code is used to choose the D offset that is used when cutter compensation is turned on.

The D offset value is a number stored in the CNC machine controls that tells the CNC the size 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.

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

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!

What is cutter compensation?

Cutter compensation is a CNC mode that allows the CNC controller to adjust for the size (diameter) of the cutting tool. 

Setting the size of the cutting tool allows you to use the same program with multiple different cutting tools.

Turning cutter compensation on tells the machine to shift the cutting tool either left with G41 or right with G42

The shift is half of the diameter of cutting tool, otherwise known as the radius.

illustration that compares a circles radius and diameter
The circle center would be the center of your cutter

Visualizing which way the machine will shift can be a little tricky. The shift happens as if you are looking towards the direction that the cutter is moving.

The images below show how the CNC will move when cutter compensation is off (canceled) with G40 and when it is on using either G41 (cutter compensation left) or G42 (cutter compensation right).

Study the images below to understand how cutter compensation adjusts the path of your cutting tool.

illustration that shows how a CNC will act when there is no cutter compensation mode active
illustration that shows how a CNC will act when using cutter compensation left with the G41 code
illustration that shows how a CNC will act when using cutter compensation right with the G42 code

What are the benefits of using cutter compensation with a D offset?

The benefit of using cutter compensation and D offsets is that adjustments can easily be made to the part being machined without the need for rewriting the whole program.

For instance, if you were to machine your part and find that the part was larger than planned then the D offset could be changed to adjust the size of the part.

D offsets get changed for a variety of reasons. 

The part could finish at an unexpected size, the cutting tool could wear changing the size or the cutting tool may need to be changed. 

All of these examples could be fixed through changing the D offset stored in the controller.

Without the use of a D offset, the CNC program would need to be rewritten to make these adjustments. This would be more difficult and require a lot more time.

Can you have more than one D offset?

Yes, you can have more than one D offset but you cannot have more than one D offset active at a time.

This means you can have multiple D offsets stored in your machine controller, one for each of the tools in your machine. 

However, only one can be on at any time.

D offsets are stored in the offset library of your CNC machine.

Generally, the D offset number will be the same as the tool number it is associated with.

Offset Library
Tool offset table with D offsets listed on the right side

Where are D offset values stored?

Most CNC machines allow the cutting tool information to be stored in table form. This tool library is called the tool offset table.

mach 3 tool offset table
Tool offset table

The tool offset table, sometimes called tool table or offset library, is simply a table of values that list the characteristics of the cutting tool, such as the diameter and length of the tool.

Tool offset tables vary from machine to machine. 

Some will allow you to store the length and diameter offsets together with a single tool. Some will require separate entries for diameter and tool length offsets.

D offset numbering

The D offset for tool number 01 will also be 01. This is best practice.

T01 = tool number 1

D01 = D offset number 1

The D offset number does not have to be the same as the tool number. Sometimes this won’t even be possible depending on the machine.

When possible, it is a great idea to number the offsets the same as the tool they will be assigned to.

D offset vs H offset

D offsets store the diameter of the cutting tool. 

H offsets store the length of the cutting tool.

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

Similar to how the G41 and G42 codes are used to turn on cutter compensation with a D offset, the G43 code is used to turn on tool length compensation using an H offset.

Both compensation modes also have cancel commands to turn them off. The G40 code turns off cutter compensation using D offsets and the G49 code turns off tool length compensation using H offsets.

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.

Ready to master CNC programming?

Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!