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

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?

F CNC Code Explained: An Easy Intro for Beginners [Feedrate]

What is the F code used for in CNC programming?

The F code, or feedrate command, controls how fast the cutting tool moves.

Feed rates set with the F code are used with movement codes such as G01 (linear interpolation), G02 (clockwise interpolation), G03 (counterclockwise interpolation) as well as various canned cycles.

The feedrate command is ignored with one movement code, G00 (rapid travel). 

When the G00 code is used the machine moves at its max speed no matter what the feed rate was last set to.

The feed rate needs to be adjusted depending on the needs of the cutting process.

For example, the feed rate for machining aluminum will be different from the feed rate needed for hardened steel and so on.

Choosing the correct feed rate can mean the difference between a perfect part and a pile of scrap.

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

F code structure

The format for an F command is F3.0

Unlike some other commands, the feed rate code allows the use of decimal points.

One thing to be mindful of when using the F code is the feedrate unit mode. 

Codes for setting feedrate unit modes

There are two types of feed rates that can be selected and the codes used vary between machine types.

On milling machines:

G94 code: Sets the feedrate to inches or millimeters per minute.

G95 code: Sets the feedrate to inches or millimeters per revolution.

Milling machines are less likely to switch between the two modes. Many machines will be set to a feedrate per minute mode and stay in it.

a cnc mill with multiple fixtures and coolant lines
CNC Mill

On lathe machines:

G98 code: Sets the feedrate to inches or millimeters per minute.

G99 code: Sets the feedrate to inches or millimeters per revolution.

Lathes are more likely to switch between the two modes. Feedrate per revolution is more commonly used in CNC lathe programs.

cnc lathe cutting threads on part
CNC Lathe

Feedrate units

Feedrate per minute (mm/min or in/min)

This type of feed rate sets the distance that the cutting tool will travel in 60 seconds. 

This feedrate mode is more common when milling and does not depend on the spindle speed.

For imperial (inch) system units, the feed rate value contains one decimal place. For example, a feedrate of 12.5 inches per minute would be F12.5.

For metric (mm) system units, the feed rate value contains one decimal place, even if it is a whole number. For example, a feed rate of 200 mm per minute would be F200.0

Feedrate per revolution (mm/rev or in/rev)

This type of feed rate sets the distance the cutting tool will travel in one spindle revolution (rotation).

The feedrate mode is mostly used with lathes.

In imperial units, the feed rate number allows four decimals, while in metric units it allows only three decimals.

For example, a feed rate of 0.32945 mm/rev will be programmed as F0.329 and a feed rate of 0.08457 in/rev will be programmed as F0.0846 on most controls.

Most CNC machines can work with both types of feed rate. 

However, feed rate per revolution is commonly used when working with a lathe and machining centers (mills) tend to stick with feed rate per minute. 

Factors that affect the optimal feedrate

Many factors determine the best feedrate for your CNC including:

  • Size of the cutting tool
  • Material the cutting tool is made of
  • Shape of the cutting tool
  • Tool condition (sharpness)
  • Workpiece material
  • Coolant use
  • Machine condition
  • Spindle speeds
  • Depth of cut
  • Width of cut

Cutting tool

assorted cutting inserts for cnc machines with text that reads various inserts for cutting

The size of the tool will directly affect the milling, turning, or drilling process in terms of motion and machining time.

Also, the tool material affects cutting speeds and feed rates.

High-speed steel tools support less friction, which means they generally don’t allow high feed rates and speeds.

Carbide and ceramic tools have better resistance and can be used at high speeds to increase machining efficiency.  

The shape of the cutter and the angle of the tool also affect the cutting efficiency, and therefore the feed rate and speed.

Sharpened tools are ideal for reducing friction and chip load formation during cutting processes.

Workpiece material

Workpiece material has a similar affect to cutting tool material on feed rates.

The harder the material is, the more difficult it would be to cut it. Softer materials allow the fastest speeds and rates.

Materials such as titanium or stainless steel will cut very different than plastics or even aluminum.

Coolant

cnc machine table with coolant lines and spindle shown

Proper coolant use can help reduce heat buildup, lubricate the cut and allow higher feedrates.

Machine condition

a white and black cnc machine
Not everyone works with a machine this nice

Backlash or wear are common in older machines which can require decreased feed rates and spindle speeds for safety reasons.

Spindle speeds

The speed of the spindle plays an important role in determining the best feed rate. 

They don’t call it speeds and feeds for nothing.

There are many tables available online that suggest good starting points for picking the best spindle speed for different materials and machining processes.

Depth of cut

illustration showing the depth of cut on a cnc machine

It should go without saying but if the depth of the cut and the feed rate are large, then the friction will increase causing tool damage.

In these cases, the cutting speed should be reduced. A careful balance needs to be maintained between cutting tool life and total machining time.

Width of cut

The width of the cut affects chip thinning during the machining process. Usually, increasing feed rates can decrease this effect and promote user safety. 

How to calculate the feedrate

Figuring out your feed rate might mean doing other calculations first depending on what information you know.

You might need to switch between feed rate units or calculate the feed rate using the spindle speed.

Formulas for switching between units

In the formulas below, the following abbreviations are used:

  • mm/min = millimeters per minute
  • rev/min = revolutions per minute
  • Nt = number of teeth on the tool

Feedrate in (mm/min) = Feedrate in (rev/min)*Nt*RPM

Feedrate in (rev/min) = Feedrate (mm/min) / (Nt*RPM)

Calculating feedrates

The formula for calculating feed rate is Fc = (*D*S)/1000

To calculate the cutting feed rate, you need to consider the variables shown below too:

  • Fc = cutting feedrate
  • D = Diameter of rotating part (job or workpiece)
  • S = spindle feed

Always be careful to stay consistent with your units.

If you choose to work with imperial units, all the parameters involved in the equation need to be in imperial units. The same with working with metric units.

How to choose the appropriate feedrate

Many machinists struggle with establishing the appropriate feedrate for given machines and materials, and rightfully so considering all the factors involved.

Many rely on online databases, manufacturing catalogs, and CNC books such as Machinery’s Handbook or FANUC control guides. 

There are also tables available online with feed rates and speeds according to the tool material, workpiece material, machine and controls used that can serve as a starting point.

Others use CAM software to get a hint of the speeds and rates needed for different machining operations.

And some experienced machinists have trained their ears to set the correct feedrate as a rule of thumb. This is a less accurate method and can easily lead to errors.

Whichever option you choose, it is important to have some guidelines for feeds. 

Here are a few tips:

  • Increase the cutting speed by following this sequence: carbon steel < speed steel < carbide. Carbide is the tool material that allows the highest speed.
  • Increase the cutting speed by following this sequence: steel < iron < aluminum < lead. Lead is the workpiece material that allows the highest speed.
  • Use the manufacturers specifications and charts to adjust the feed rates and speeds. The manufacturers have likely done extensive testing.
  • You will usually be given a range of speeds and feeds depending on the tool and workpiece materials. Keep in mind that most of the time slower rates will mean longer tool life and less overheating.

Ways to change the current feedrate

Feedrate override

The feedrate override is a switch that can change the programmed feed rate from 0 to 200 percent. 

The feed-rate override switch is located on the CNC control panel and it’s operated manually by the setup person or operator to slow (or stop) cutting motions.

Although feedrate override is useful in certain situations, you need to be careful to avoid tool overheating or breakage when using it. 

Want to learn more about CNC G Code?

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

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

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

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.

Want to learn more about CNC G Code?

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.

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

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.

Want to learn more about CNC G Code?

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

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

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.

Want to learn more about CNC G Code?

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

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

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.

Want to learn more about CNC G Code?

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.

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

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.

Want to learn more about CNC G Code?

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

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.

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

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

Want to learn more about CNC G Code?

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

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.

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

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

Want to learn more about CNC G Code?