First M Codes to Learn [Where to Start for Beginners]

a graphic of a cnc machine with text that says learn g code today first m codes to learn

What is an M code in CNC programming?

M codes are miscellaneous codes. They control various functions of the CNC machine such as turning coolant flow or the spindle on and off.

M codes are the second largest group of codes after G codes.

M codes vary from machine to machine, but we have covered the most common ones below.

You can expect that these codes will work the same on most machines:

Want to learn more about CNC G Code?

Common M codes

Stops [M00, M01]

The M00 code is called a program stop.

It stops the spindle and coolant flow. The M00 program stop also stops all machine movement and pauses reading the program.

It is typically used when changing the tool manually or there is a need or adding tapping oil to a tap before it touches the workpiece.

The machine will continue reading the program after a program stop once the cycle start button is pushed. It doesn’t automatically restart the spindle or coolant though. Those must be turned back on in the program.

The M01 code is an optional stop. It stops everything that the M00 code does but only if the optional stop button or switch is on.

haas cnc control panel with optional stop button highlighted

If the optional stop switch is off, the machine will ignore the optional stop code and keep running the program.

Program end codes [M02, M30]

While the M00 and M01 codes pause the program, the M02 and M30 codes end it.

M30 is the most common program end code. It ends the program and rewinds the program back to the start.

This means that if you press cycle start after the M30 code, the machine will run the program again.

fanuc cnc control panel with cycle start button highlighted

M02 is a program end without rewind.

This is mostly a legacy feature from when programs were run on tape. In fact, many newer machines will treat a M02 code as if it were an M30 code.

Spindle commands [M03, M04, M05]

M03 turns the spindle on in a clockwise direction.

M04 turns the spindle on in a counterclockwise direction.

M05 turns the spindle off. The spindle should be turned off first before changing the spindle direction.

Coolant commands [M07, M08, M09, M88, M89]

M08 turns flood coolant on. This floods the cutting area with coolant to keep heat from building up which can damage the part or the cutter.

It also helps remove chips from the cutting area which can cause issues as well.

The M09 code turns the coolant off.

Many machines will have other coolant options that can be turned on as well. M07 to turn on mist coolant is a common one and so are M88 to turn thru spindle coolant on and M89 to turn it off.

Tool change [M06]

Most machining centers (mills) have automatic tool changers.

Tools get loaded into the ready to change position with the T code.

When the M06 code is used, the machine swaps the tool currently in the spindle with the tool in the ready position.

an automatic tool changer on a dmg mori cnc machine
An automatic tool changer on a DMG Mori CNC

Subprogram codes [M97, M98, M99]

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.

The format for using both codes is the same. They both use the P code and the L code.

M97 P1234 L3

For a M97 code, the P number is the line number of the subprogram inside the current program.

For a M98 code, the P number is the program number for the subprogram that will be run.

The L code is the number of times that the subprogram will be repeated.

The M99 code ends both types of subprograms and jumps to the next program line after the M97 or M98 code was used. Subprograms are not ended with an M30 code like a normal program is.

M codes vs G codes

M codes control different machine function such as the spindle or coolant.

G codes are another type of code used in CNC programming and they are called preparatory functions. This means they prepare the machine to do something.

Usually this involves moving the machine or controlling how it moves. Codes such as G00 [rapid travel] or G81 [drilling canned cycle] make the machine move in a certain way. Codes such as G90 [absolute mode] or G21 [metric mode] control how the machine reads the movement.

Both G and M codes are heavily used in CNC programming. It is important to understand both types of codes. 

For this reason we have a post on the First G Codes to Learn as well. Check it out because understanding the codes in that post and this one will give you a good foundation for understanding CNC programming.

Want to learn more about CNC G Code?

CNC Spindle Controls Explained [M03, M04, M05 and More]

a graphic of a cnc machine with text that says learn g code today spindle controls

Getting started with CNC spindle controls

The most basic codes to control your CNC spindle are:

  • M03 = spindle on clockwise
  • M04 = spindle on counterclockwise
  • M05 = spindle off

But there is more to controlling your CNC spindle than simply turning it on and off.

For instance, most CNC machines have multiple ways to turn the spindle off and each way has it’s own use case. 

Controlling the spindle speed is another function that has multiple modes. Choosing the wrong one could potentially be disastrous. 

Learn the ins and outs of controlling your spindle below.

picture of the inside of a cnc machine with the spindle identified

Want to learn more about CNC G Code?

Codes for turning your spindle on

There are two CNC commands for turning the spindle on, M03 and M04.

  • M03 = spindle on clockwise
  • M04 = spindle on counterclockwise

The S code must also be used to set the speed of the spindle.

M03 [spindle on, clockwise]

M03 turns the spindle on in a clockwise direction at a set speed. The set speed is the speed set with the most recent S code.

Clockwise rotation with M03 is the most common spindle rotation direction when CNC machining. It is used with right hand cutting tools.

M04 [spindle on, counterclockwise]

M04 turns the spindle on in a counterclockwise direction at a set speed. Just like M03, the set speed is the value used with the most recent S code.

M04 is used with left hand cutting tools.

Counterclockwise spindle rotation is much less common than clockwise. This is especially true for machining centers (mills). For lathes, M03 is still more common but M04 gets more use.

Codes for turning your spindle off

The M05 code is the only code that just turns the spindle off.

However, there are other codes listed below that turn off multiple machine functions and the spindle is included in those functions.

M05 [spindle off]

The code M05 is used to stop the spindle.

It is normally used when there is a tool change or other change in the machining process.

When the spindle is turned back on with an M03 or M04 code after being stopped, the spindle will return to the same speed it was previously set at with the last S code.

M00 [program stop]

The M00 code is called a program stop. It stops the current program in the middle of running.

Using the M00 code turns off the following functions:

  • Movement of all axes
  • Spindle rotation
  • Coolant flow
  • The machine pauses reading the program

When the M00 code is used, nothing is reset. In other words, any active modes or parameters will stay as they are.

The program is merely paused when it reads the M00 code.

fanuc cnc control panel with cycle start button highlighted
Cycle start button on a Fanuc control panel

The machine will resume reading the program when the cycle start button is pressed after the program stop has occurred.

One thing to keep in mind is that the machine doesn’t turn on functions when cycle start is pushed. If the M00 code turns something off such as the spindle, it will need to be turned back on in the program.

M01 [optional stop]

The M01 code is called an optional stop. It stops the current program in the middle of running, but only if the optional stop switch or button is on.

If the optional stop switch is not on, then the machine will ignore any M01 codes it reads and continue running the program like normal.

haas cnc control panel with optional stop button highlighted

When the machine reads an M01 code and the optional stop switch is on, it will turn off the following functions:

  • Movement of all axes
  • Spindle rotation
  • Coolant flow
  • The machine pauses reading the program

When the M01 code is used, nothing is reset. Same as with the M00 code.

In other words, any active modes or parameters will stay as they are. The program is merely paused when it reads the M01 code.

The machine will resume reading the program when the cycle start button is pressed after the program stop has occurred.

One thing to keep in mind is that the machine doesn’t turn on functions when cycle start is pushed. If the M01 code turns something off such as the spindle, it will need to be turned back on in the program.

M30 [program end]

The M30 code ends the program. 

This makes it different from M00, M01 or M05.

M00 and M01 shut down various machine functions and pause the program.

M30 shuts down machine functions and ends it. Pressing cycle start after an M30 code will start the entire program over again.

How to set spindle speeds

CNC spindle speeds are set using the S code.

S code

Using the S code sets the spindle speed that will be used whenever the spindle is turned on. The units are revolutions per minute (RPM).

The same spindle speed value will be maintained until it is changed.

Below is a small section of code with comments in parentheses that show what will happen as the machine reads the code.

M03 S3000   (Spindle On CW, Speed = 3,000 RPM)

M05              (Spindle Off)

M04              (Spindle On CCW, Speed = 3,000 RPM)

Notice how the spindle turns back on at the same set speed. This happens even if the spindle rotation direction is changed from clockwise to counterclockwise or vice versa.

S codes show up frequently in programs at the beginning of a new section of code. This can be switching tools or switching machining operations such as going from cutting the outside profile of the part to drilling holes.

Some other notes about the S code:

  • Must be a whole number (no decimals such as 3000.5)
  • Most machines have a limit to the range of spindle speeds that can be used. 1-9999 is common but 1-99999 is also used by some machines

Spindle speed modes

In CNC programming there are two codes that control the spindle speed mode, G96 and G97.

G97 is the most common and sets the spindle mode to RPM.

G96 sets the spindle mode to constant surface speed which adjusts the speed of the spindle based on the size of the part being machined.

G97 [speed in RPM]

When the G97 mode is active, the machine will maintain a constant spindle speed based on the RPM value set with the most recent S code.

If G97 is active, spindle speed will only change when the S code is changed.

G96 [constant surface speed]

When the G96 mode is active, the machine will maintain a constant surface speed.

Imagine a monster truck tire. Now picture rolling the monster truck tire one complete revolution in one minute. In that one minute, you might move 20 feet.

Now imagine you have a normal car tire. If you roll the car tire one revolution in one minute, you might only travel 4 feet.

You covered a lot less distance in that one minute, which means you traveled at a slower speed.

When things are spinning based on an RPM speed, the actual speed that the cutting edge of the tool is traveling can change.

Using constant surface speed with the G96 code tells the machine to change the spindle speed over time as the size of the part changes so that the speed where the cutting action is happening remains the same.

This is a simplified explanation of how G96 works. For more information about G96 see our full guide to the G96 command.

How to change the tool in your spindle

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

an automatic tool changer on a dmg mori cnc machine
Automatic tool changer holding multiple tools in a DMG Mori CNC

Most machining centers have automatic tool changers that can switch tools into and out of the CNC spindle.

The tool to be placed into the spindle is called up with the T code. This places the new tool in location so that it is ready to be switched.

The M06 command is then used to perform the tool change.

Here is an example section of code:

T01    (Tool 1 is readied for change)

M06    (Tool 1 is placed in the spindle)

T02    (Tool 2 is readied for change)

T03    (Tool 3 is readied for change)

M06    (Tool 3 is placed in the spindle)

Notice that even though tool 2 was readied for change, it never actually was put in the spindle. Instead, tool 3 was readied before the tool change occurred and it was placed in the spindle instead.

Want to learn more about CNC G Code?

CNC Offsets Explained [D, H and Work Offsets]

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

What is a CNC offset?

Offsets are the adjustments that the CNC machine will make based on different features of the machine.

There are three types of offsets:

  • Diameter offsets
  • Height offsets
  • Work offsets

Want to learn more about CNC G Code?

Types of CNC offsets

Each of the three offset types has a specific purpose. 

They each allow for flexibility when running your program. This includes allowing you to use the same program even if you need to change cutting tools.

Height (H) offsets

The height offset refers to the location difference between the spindle and the cutting tool.

This difference in location is stored in the machines offset library. Height offsets are usually stored in the same number location as the tool.

For instance, T01 (tool 1) and H01 (height offset 1) or T05 (tool 5) and H05 (height offset 5).

This makes it easier to match up the necessary offset with the correct tool.

The G43 code is used to turn on tool length compensation using an H offset.

For example, G43 H01 turns on tool length comp with the first H offset.

Once tool length compensation is turned on with the G43 code, it will stay on until it is turned off with the G49 cancel code or switched to a new H offset such as H02 or H05.

Diameter (D) offsets

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

While H offsets adjust for the length of the cutter, D offsets compensate for the diameter of the cutting tool.

D offsets are also stored in the machines offset library. D offsets are usually stored in the same number location as the tool they are used with.

T04 (tool 4) and D04 (diameter offset 4) would usually be matched together.

G41 and G42 are the two cutter compensation modes that are used with D offsets.

They tell the machine to adjust the path of the cutter so that the edge of cutter follows the path given in the CNC program. G41 shifts the cutter left and G42 shifts the cutter right.

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

With cutter compensation off using the G40 code, the machine will move the center of the cutter along the path in the program. This can make it difficult to get the correct size, especially when using different or multiple cutters.

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

Telling the machine the size of the cutters with your D offsets allows it to account for them and run the same program with different tools ang get the same size part. 

If you didn’t have cutter compensation, then you would need to create a new version of the program every time you wanted to use a new tool.

Just like tool length compensation with G43, cutter comp with G41 and G42 are modal so those adjustments will stay on until switched or turned off with the G40 code.

Work offsets (G54-G59)

Work offsets are basically the stored location of your part in the machine. You use them to set the XYZ zero location when working in absolute positioning mode.

There are multiple work offsets available. The six most common are G54 through G59. Most machines will also have others available, but the format varies from machine to machine.

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

Fanuc controls are one of the most common controllers. If they allow additional work offsets, the Fanuc format for using them is G54.1 P1, G54.1 P2 and so on. The P number is the additional offset.

In most cases the six offsets of G54 through G59 that are common on all machines will be more than enough.

One benefit of having multiple work offsets is how easy they make it to run multiple parts at once. You can set the work offset for multiple parts and run the same program with a new work offset each time.

This works well when you a fixture that holds multiple parts in your CNC.

Where are offsets stored?

Offsets are stored in the tool offset table or library.

This tool table allows you to describe the different dimensions of the tool that will affect the program.

This includes the tool radius or diameter (D offset) and its length (H offset).

Tool offset library on an Okuma CNC machine

Some machines allow you to store the H and D offsets together in one offset. In this example, tool T01 would use both H01 and D01 as offsets. 

Other machines may require the offsets to be stored individually. When the offsets are stored individually, the programmer should maintain a system so that the offset numbers used are consistent. In other words, the first H offset would always be stored in the 01 location and the first D offset would always be stored in the 51 location.

Having a system like this will help prevent errors in your program.

Want to learn more about CNC G Code?

Quick Guide to the N CNC Code [Program Line Number]

a graphic of a cnc machine with text that says learn g code today N 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 though and can be left out if the programmer chooses.

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

Want to learn more about CNC G Code?

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

N codes 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.

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 one to show up 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 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.

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?

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?

Modal G & M Codes for Your CNC [What They Do & How to Use Them]

a graphic of a cnc machine with text that says learn g code today modal codes

What is a modal command?

Modal commands are a type of CNC code that stay active once turned on until the code is either turned off or switched to another code in the same group.

For example, if left cutter compensation is turned on with G41 it will stay on until either the G42 code switches the machine to right cutter compensation or the G40 command is used to turn off cutter compensation altogether.

Want to learn more about CNC G Code?

Why and how are modal commands used?

Modal commands are useful because they increase productivity. Modal commands can save time for the CNC programmer and make the CNC program smaller.

Using modal codes allows you to not have to write the same code repeatedly. This also has the added benefit of making the program easier to read.

By using modal commands you set the machine modes once and then switch them as needed.

A good example of this is your unit mode.

Most programs will be written in either inches or millimeters. It is not common for a program to switch between the two unit types. Using modal commands allows you to use the G20 code and set the machine in inch mode at the start of the program and leave it on.

Without the capability to use modal commands, you would need to tell the machine that every new line of code was in inches. This would make for a very busy, hard to read program with a bunch of codes repeated throughout.

Modal commands make the programming easier to create and easier to read.

Remember you can only have one modal command from the same group active at a time. Also, there are some commands which are one-shot codes. This means they only affect the line they are used on.

The various modal groups are shown below.

Modal command groups

To really drive the point home, remember you can only have one modal code active from each group. 

If you use G20 to put the machine in inch mode and then use the G21 command to put the machine in metric mode, they are not both active. The G20 command gets turned off when the G21 command is turned on.

Some of the groups of codes have a cancel command which can be used to turn all codes in the same group off. 

Not every code group has a cancel command though. 

For instance, you will always be in either G20 (inches) or G21 (mm). It is not possible to turn both off. One or the other must be active.

If a code group has a cancel command, it is discussed in the individual group sections below.

Movement [G00, G01, G02, G03]

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

One of the most frequently used group of codes is movement codes.

CNC machines need to move to cut, drill, and grind parts along with many other functions.

Many times, large portions of your CNC program will be exclusively movement codes as the machine performs its cutting operations.

In the movement group of codes there are four different codes:

You move with G01, G02 and G03 to make cutting movements to make all kinds of shapes and cuts on the part, but what about G00?

G00 is rapid movement and is used to move the machine as quickly as possible when it is not in the process of cutting.

Moving rapidly reduces the total machining time, otherwise known as cycle time. While this might not make a huge difference for making one part in your garage, shaving a minute off each part for an order of 1,000 pieces really adds up.

Units [G20, G21]

comparison of units of measurement for cnc programming

We talked about it above but the codes used to set your units are very important.

G20 sets the machine in imperial units (inches) and G21 sets the machine in metric units (mm).

Switching between units is not advised. Set your units and stick with them.

Cutter compensation [G40, G41, G42]

Cutter compensation is a mode that allows the machine to adjust for the size of the cutting tool.

This allows the same program to be used with multiple different cutting tools. Without cutter compensation, you would need to write a new program each time you had a new cutter.

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

The first code in the group is the cutter compensation cancel command, G40. When this code is active, the CNC will move the center of the cutting tool along the tool path listed in the program.

This is useful when you want to drill a hole at a specific location.

The two cutter compensation modes which cause the machine to adjust the tool path are:

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

Both modes adjust the path of the cutting tool so that the edge of the cutter follows the toolpath of the program.

G41 is used when climb milling and G42 is used when conventional milling.

G41 is used far more often than G42.

Both G41 and G42 take into account the D offset value to tell the CNC how much to adjust the tool path. The D offset value is listed when turning on cutter compensation. For example, to turn on left cutter compensation with the first D offset the line of code would be:

G41 D1

Tool length compensation [G43, G49]

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

Just like the diameter of the cutter needs to be accounted for, so does the length of the cutting tool.

G43 is the code to turn on tool length compensation. This code is used along with an H offset value which is stored in the CNC controller similar to the D offsets used in cutter compensation.

Tool length compensation is used for many of the same reasons cutter compensation with G41 and G42 is used. It allows the machine to adjust for the length of the cutting tool and not require a new program to be created each time the tool is changed.

Technically, G43 is for positive tool length compensation and G44 is for negative tool length compensation.

For anyone just learning about modal commands, they should ignore G44 for the time being. G44 is not used often and is not something that a beginner can expect to deal with.

Focus on G43 and G49 when first learning CNC programming.

Speaking of G49. G49 is the code to cancel tool length compensation. Remember cancel just means turn it off.

You might think that you would need to turn off tool length compensation much like you would with cutter compensation, but this is rarely true.

Instead, the machine will switch between tools by using different H offsets for different length cutters.

When tool length compensation is off using a G49 code, the machine will move the tip of the spindle to each new location instead of the tip of the cutter. Because you will almost always have a cutting tool in your spindle, tool length compensation with G43 is almost always on.

Fixture/work offset [G54-G59]

Fixture offsets, sometimes called work offsets are how the machine knows where your part is located in the machine.

They tell the machine the zero location in the three axes (X, Y, & Z). The machine will execute the program based on this zero location.

Using work offsets allows you to easily set up for a different part or even use one program to run multiple parts at once. You can tell the machine the location of your first part by using G54, run your program then set the work offset to G55 so the machine knows where the second part is and proceed to run the same program again.

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

Just like your D and H offsets allow flexibility in your CNC programming and setup, work/fixture offsets with G54-G59 do the same.

It should be noted that G54 through G59 are the most common work offsets used and can be found on most machines. In addition to these six offsets, most machines (especially new models) will be able to use many more offsets. How they are used varies from machine to machine so check your machine manual.

Positioning mode [G90, G91]

Positioning modes tell the CNC machine how to interpret each new location it is given.

There are two types of positioning modes which can be used:

Absolute positioning with G90 tells the machine that all dimensions are measured from the datum/origin (0,0,0). This is a fixed point in the machine that will not change unless the work offset is changed.

Incremental positioning with G91 tells the machine that each new location becomes the zero location (0,0,0).

The pictures below show the same tool path in both positioning modes. The coordinate location of each new move is listed in parentheses ().

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

Canned cycles [G80, G81, G82, G83, G84, G85, G86, G87, G88, G89]

Canned cycle are modal commands which are used for programming repetitive CNC operations.

They have the benefit of making your CNC program both shorter and easier to read and understand.

Canned cycles are a big topic on their own so I will only include a brief description of what each code is used for. If you need more information about any of the individual canned cycles, then click the links below:

  • G73 – deep hole peck drilling
  • G74 – left-hand tapping cycleG80 – canned cycle cancel
  • G81 – simple drilling cycle
  • G82 – drilling cycle with dwell
  • G83 – peck drilling cycle
  • G84 – right-hand tapping cycle
  • G85 – boring cycle – feed in, feed out
  • G86 – boring cycle – feed in, spindle stop, rapid out
  • G87 – back boring cycle
  • G88 – boring cycle – feed in, dwell, spindle stop, rapid out
  • G89 – boring cycle – feed in, dwell, feed out

Plane returns [G98, G99]

Plane returns tell the machine how to act after finishing a canned cycle. The two options are:

G98 returns to the point where the canned cycle was started. G99 returns to the R plane, which is the point where the controlled feedrate (not rapid) started.

The R plane is lower than the initial point. This can allow for shorter machining time because the machine is not travelling as far.

Often a series of holes will be drilled and both G98 and G99 return codes will be used in the process. The G99 code gets used when the machine is able to stick close to the part and move to the next hole. The G98 code is used when the machine needs to retract further, for instance if you needed to avoid a clamp or something else in the way of your cutter.

G98 retracts further and takes longer but is generally the safer option. You should be careful using both G98 and G99 though.

Pay close attention to the initial point (your Z height when starting the canned cycle) and your R plane (set with the R code in your canned cycle callout).

The initial point should be set at a level that is higher than any objects in the machine such as the part itself, clamps, fixtures, etc.

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

Plane selection [G17, G18, G19]

There are 3 codes that tell the machine which plane to work in:

  • G17 for the XY plane
  • G18 for the XZ plane
  • G19 for the YZ plane

Most programs will be written using the G17 code and work in the XY plane.

Modal M codes

In addition to the modal G codes listed above, there are also modal M codes available for controlling various machine functions.

These modal M codes work in the same way as the G codes. Once they are used, they will stay on until canceled or switched to another code in the same group.

Spindle commands [M03, M04, M05]

This group of modal commands controls the CNC spindle. They are:

  • M03 – spindle on, clockwise
  • M04 – spindle on, counterclockwise
  • M05 – spindle stop

Coolant [M07, M08, M09]

The next group of modal M codes controls the machines use of coolant.

  • M07 – mist coolant on
  • M08 – flood coolant on
  • M09 – coolant off

Mist coolant is usually a combination of coolant and compressed air but this can vary from machine to machine and sometimes you may find that it is only compressed air or not even available on your specific machine.

Flood coolant is the normal CNC coolant mode where the machine floods the cutting tool and workpiece with coolant.

M07 modal command turns on the mist coolant and it will be directed to the material cutting.

Other modal commands

There are many other modal commands which can be used for CNC programming. They vary by machine and not all machines will have each type of modal code.

The codes laid out here are the most common modal commands and will give anyone trying to learn CNC programming a good start towards learning the ins and outs of modal commands.

Frequently asked questions about modal codes

Modal vs one shot codes

While there are a lot of modal codes, not every CNC code is modal.

There are also one-shot (non-modal) commands as well. These codes only affect the line they are used on.

A couple of the commonly used one-shot codes are:

What happens if I don’t set a modal code?

When the CNC machine starts up it sets the machine to various default modes as set in the setup parameters of the machine.

It is never advised to rely on the startup modes of the machine to create your program. It is always better to call out any needed codes specifically in your program.

If you don’t set a modal command, the machine will remain in the default modes as set in the system parameters.

Want to learn more about CNC G Code?

Absolute and Incremental CNC Positioning Modes [G90 & G91]

a graphic of a cnc machine with text that says learn g code today cnc positioning modes

What is a CNC positioning mode?

In CNC programming, a positioning mode refers to the way the CNC machine will read and understand every new XYZ location it is given.

For CNC machines, there are two positioning modes available.

They are absolute positioning using the G90 G code and incremental positioning using the G91 code.

Want to learn more about CNC G Code?

G90 [Absolute Positioning]

Absolute positioning is the mode that most of your CNC programs will be written in. The alternative is incremental mode, and it is usually reserved for specific sections of a CNC program.

The G90 code sets the machine into absolute positioning mode. This means that all locations will be given relative to a fixed point called the origin.

When we discuss coordinates, we list them in the order XYZ. If you see (1,2,3), then X=1, Y=2, and Z=3. Sometimes this is simplified to only show the X and Y coordinates such as (4,5). In this example, X=4 and Y=5.

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

Now that you understand coordinate locations, let’s talk about the origin again.

The origin is the (0,0,0) location. This means X, Y & Z all equal zero at this one point.

In absolute positioning mode the origin (sometimes called the zero location) is a fixed point. This means it doesn’t move.

Using absolute positioning, you will set a location in your CNC machine as the origin and all locations in your CNC program will be given from that origin.

Each new machine location is given in parentheses () below. Notice how all locations are relative to the initial (0,0) location.

graph paper example of absolute positioning with multiple points as examples

Advantages and disadvantages of absolute positioning

Advantages

  • Easier to track the location of your cutting tool throughout the program
  • Changing one location doesn’t affect all of the following locations in the program

Disadvantages

  • Code can be hard to read for repetitive tasks such as drilling a large number of holes

G91 [Incremental Positioning]

While absolute positioning with G90 has a fixed origin or zero location, incremental positioning with G91 has a zero location that changes with each move.

Every time the cutting tool moves to a new location, that location becomes the new zero point.

Compare the two pictures below:

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

The numbers in parentheses () are the coordinates that the machine would be given to move to each new location in the two different positioning modes.

If at any time in the program we gave the machine a (0,0) coordinate in absolute positioning, the machine would return to the origin in the lower left corner.

If we did the same in incremental mode, the machine wouldn’t move. In incremental mode the origin changes each move and a (0,0) would tell the machine to move zero units in both directions.

Giving the CNC a (0,0) location in incremental mode would tell it to stay put.

Advantages and disadvantages of incremental positioning

Advantages

  • Easier to read code for repetitive features and patterns
  • Can reuse incremental sections of code easily either in the same program or as a subprogram

Disadvantages

  • Each location affects every location after it so changing one requires changing all others after it

Where you will find G90 and G91 in a CNC program

Setting the correct positioning mode is an important part of any CNC program.

For this reason, the positioning mode is often set in the safety lines of the program.

Safety lines are a line or lines of code that show up at the beginning of the program and at specific locations in the program. They are used to make sure the machine is prepared to run the next section of code.

They get used at the beginning of the program to set all the necessary modes before starting machining. They often show up again when a tool is changed or a new machining operation is being performed.

Check out the “sections” of a small CNC program below:

  • Program start
  • Machine exterior profile of piece
  • Drill pattern of holes
  • Add counterbores to holes
  • Program end

At the start of each of these sections you can expect to find safety lines that set the necessary modes needed for the upcoming operation. Doing this allows individual sections of the program to be re-run if needed.

Imagine you ended the program in incremental mode after using the counterboring canned cycle. Then you measure your part and realize the length and width are oversize.

Bummer!

So you decide to adjust your offsets and re-run the exterior profile section of the program.

If you don’t have safety lines that make sure the machine is set to absolute positioning mode, then the machine will remain in incremental mode and interpret the code very differently.

Setting your positioning mode is very important. Even if it doesn’t change often in the program, the codes get used frequently to make sure all the correct modes are set.

Which positioning mode is better?

Neither positioning mode is better, but they do have their own uses.

The body of most CNC programs will be written in absolute positioning mode. This is because it is easier to understand and visualize where the cutter is.

In absolute mode you only need to know where the origin and the new location are to understand your position in the machine.

In incremental mode you need to know every move the machine has made to know what position you are at. That can mean a lot of calculations to figure out where you are located.

This might make it seem like absolute positioning mode is the best and should be used for everything, but that is not always the case.

Incremental mode is great for patterns and features that repeat. This means incremental mode is often used when working with canned cycles.

Which codes will be affected by your choice of positioning mode?

Any code that uses XYZ movement as part of the code will be affected by your positioning mode.

All movement codes are affected by your choice of positioning mode:

Canned cycles are another set of commonly used codes that will be affected. They vary from machine to machine but generally canned cycles are the G73-G89 codes.

Another important code that is affected by your positioning mode is G28. The G28 zero return command can behave very differently from one mode to the other, so make sure you know how your machine will react before using it.

This isn’t a complete list of codes. There are many more that are affected by your choice of positioning mode, but this is a good start for anyone new to CNC machining.

As always, know your machine and check the manual if available to fully understand how it will react to any code you give it.

Frequently asked questions about CNC positioning modes

What happens if you don’t select a positioning mode?

On most CNC machines, there is a default positioning mode.

If you don’t use the G90 or G91 command, the machine will stay in the default mode.

The default mode is most often set to incremental mode for safety reasons.

It is not recommended to rely on the default modes of your machine to make sure you are in the correct positioning mode. You should always set the positioning mode you need with either a G90 or G91 code.

Can you use the G90 and G91 codes on the same line/block?

No, it is not possible to use the G90 and G91 commands on the same line.

Both G90 and G91 are modal commands from the same group. This means turning one on turns the other off.

With G90 and G91, only one code can be active at a time.

What could happen if you use the wrong positioning mode?

To put it simply, your machine could crash.

Moving to the (0,0) location in absolute mode with G90 will bring you to the part zero location. This could be any location you chose on your workpiece.

Moving to the (0,0) location in incremental mode with G91 will not move your machine at all.

These are two very different commands being executed based on the positioning mode.

Forgetting to set the correct positioning mode can cause the CNC machine to act in very unexpected ways for the programmer. Unexpected is never good when it comes to CNC machining.

Because being in the correct positioning mode is so important, the positioning mode is often set in the safety lines of the program.

Safety lines are used to make sure the machine is in all of the correct modes before running a section of the CNC program.

Want to learn more about CNC G Code?

Quick Guide to the G98 CNC Code [Return to Initial Plane]

a graphic of a cnc machine with text that says learn g code today G98 canned cycle return to initial plane

What does a G98 code do?

The G98 code is a modal command that tells the cutting tool to move back to the initial plane after finishing a canned cycle operation.

Often, CNC machines will have this command set to “on” by default.

G98 is a very important code because it allows you to avoid clamps, fixtures, and other obstacles that may be present when machining.

The alternative to G98 is G99. 

The G99 code also controls the movement of the machine after finishing a canned cycle.

Want to learn more about CNC G Code?

G98 vs G99

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

Both codes are used to perform similar actions.

The difference is that they lift the tool to different heights when finishing a canned cycle process.

G98 lifts the tool back to the initial Z position which the machine was at when starting the canned cycle. This is safer option if you are unsure

G99 travels back to the R plane instead. The R plane is closer to the part which can potentially cause problems if you aren’t paying attention.  

Parameters used with a G98 code

When using the G98 code you should consider all of the following items that can be included as part of the code:

The feed rate will depend on the material that is being cut and the diameter of the drilling tool that you are using.

The retract (R) plane or clearance level refers to the plane height where the tool can move safely from one hole to another. The R value should be set so the machine will clear any and all obstacles including clamps, fixtures and the part itself.

X and Y are the location of the hole. Z controls the depth of the hole.

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

G98 code format

Using the parameters above, the format for using a G98 code is:

G81 X0 Y0 R0 Z1 F5 G98

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

The rest of the codes are as listed in the parameters section above. The X, Y and F codes are not required. Often, the X and Y codes will be placed on the previous line.

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

What to think about when using a G98 code?

When using the G98 command, think about the obstacles that could be in the way of the movement of your cutter.

Using G98 will move your cutter to the initial plane which is the Z height at the start of the canned cycle. This is generally the safest location and should be set at a Z height above all obstacles in the machine.

Common obstacles in the machine are:

  • The workpiece
  • Clamps
  • Fixtures
  • Other tooling

You should also think about the speed of your program. 

For hobbyists, this isn’t as much of a concern. 

For industrial machines, the time it takes to move back to the initial plane instead of the R plane can add up and time is money in the machining industry.

G98 code example

N1 Z5.0 M08;

N2 G82 G99 Z-3.0 R1.0 P500 F50.0;

N3 X10.0;

N4 G98 X20.0;

N5 G99 X10.0;

N6 G80;

The first line (N1) brings the drill down to 5mm above the material surface and turns on the coolant with M08.

Then, the second line (N2) sets the counterbore canned cycle with G82 and tells the machine to return to the R plane where Z = 1.0.

The Z-3.0 is the depth of the counterbore and P500 is the dwell time when the machine reaches the bottom of the hole in milliseconds.

F is feed rate in mm/min.

Line 3 (N3) tells the machine to move the X axis of the machine to the X=10.0 location. Because the canned cycle is a modal code, another counterbore will be drilled to the same parameters as in line 2.

The next line (N4) drills the third hole at X=20mm. G98 tells the control to return to the initial plane. This is Z=5.0, the last Z location before starting the canned cycle. The machine will rapid back to Z=5.0 above the part surface after drilling the previous hole.

The machine drills the final hole 10mm in X from the last one, the G99 code tells the machine to rapid back to the R position instead of the initial Z position.

Lastly, on line 6 (N6) G80 turns the counterboring canned cycle off.

Frequently asked questions about the G98 code

Is G98 a modal command?

Yes, G98 is a modal command. 

This means it will stay on until canceled. It will only be in effect for running canned cycles though. G98 has no effect on straight line movement with either G00/G01 or circular movement with G02/G03.

What does the G98 code do on a lathe?

The G98 codes is completely different when running a CNC lathe.

G98 on a lathe sets the feed rate mode. G98 sets the machine to feed rate per minute.

This means if you use a F100 command and G98 is active, the feed rate will be set to 100 inches per minute.

G99 on a lathe sets the feed rate mode to feed rate per revolution.

Want to learn more about CNC G Code?

Every G Code for Your CNC [The Complete List]

G code, sometimes called g-code, is a simple programming language for CNC machines. 

Listed below are the codes that use the G word. These codes are preparatory commands.

This means that they set modes or prepare the machine for various actions. 

We have put together a complete list of G codes for your reference.

Not all codes are used often.

Below is a list of G codes. They have been organized into group types based on how the machine reacts to the individual code.

If you are new to G code, you should review the code groups and compare the codes within a single group. 

You will quickly see they handle the same function, such as one code to turn something on and one code to turn something off. 

This makes learning the codes twice as easy.

Table of Contents

CNC Movement and Travel

Moves the CNC around at the maximum possible speed. Often called rapid traverse.

Learn more about the G00 code

Move the CNC in a straight line to a specific location. The speed of this movement is determined by the feed rate (F).

Learn more about the G01 code

Move the CNC to specific location along along a specified arc or radius traveling clockwise. The speed of this movement is determined by the feed rate (F).

Learn more about the G02 code

Move the CNC to a specific location along a specified arc or radius traveling counterclockwise. The speed of this movement is determined by the feed rate (F).

Learn more about the G03 code

Tells the CNC not to move for a certain amount of time.

Learn more about the G04 code

Plane Selection

G17 CNC G Code

G17 - XY Plane Selection

The vast majority of movement in CNC programs will be relative to the XY plane. Plane selection affects how G02, G03, cutter compensation and some canned cycles are handled.

G18 CNC G Code

G18 - XZ Plane Selection

Plane selection affects how G02, G03, cutter compensation and some canned cycles are handled. G18 tells the machine you are machining radii around the Y axis.

G19 CNC G Code

G19 - YZ Plane Selection

Plane selection affects how G02, G03, cutter compensation and some canned cycles are handled. G19 tells the machine you are machining radii around the Z axis.

Unit Modes

All size and location commands will be in inches until changed.

Learn more about the G20 code

All size and location commands will be in millimeters until changed.

Learn more about the G21 code

Compensation Codes

Turns off any cutter compensation modes (G41 or G42). 

Learn more about the G40 code

Shifts the machine tool path to the left based on the radius of the cutting tool.

Learn more about the G41 code

G42 CNC G Code

G42 - Cutter Compensation Right

Shifts the machine tool path to the right based on the radius of the cutting tool.

Adjusts a tool’s location based on a specified tool height (H) offset.

Learn more about the G43 code

Turns off the tool length compensation set by a G43 or G44 command. 

Learn more about the G49 code

Work Offsets

Sets a specific location in the CNC as the zero location for the program.

Learn more about the G54 code

Sets a specific location in the CNC as the zero location for the program.

Learn more about the G55 code

Sets a specific location in the CNC as the zero location for the program.

Learn more about the G56 code

Sets a specific location in the CNC as the zero location for the program.

Learn more about the G57 code

Sets a specific location in the CNC as the zero location for the program.

Learn more about the G58 code

Sets a specific location in the CNC as the zero location for the program.

Learn more about the G59 code

Canned Cycles

G73 CNC G Code

G73 - High-Speed Peck Drilling Canned Cycle

Peck drill a hole by repeatedly drilling, then retracting a small amount, then drilling deeper. Often used to break up chips when drilling.

G74 CNC G Code

G74 - Left-Hand Tapping Canned Cycle

A tapping cycle for left-hand or reverse threads. The spindle must be running in reverse during this cycle. The machine will feed to the bottom of the hole and then reverse the spindle direction and back out of the hole.

G76 CNC G Code

G76 - Fine Boring Canned Cycle

Bores a hole then stops the spindle, retracts the tool from the surface and rapid retracts the tool out of the hole.

Drills a hole then rapid retracts out of the hole.

Learn more about the G81 code

G82 CNC G Code

G82 - Standard Drill with Dwell

Drills a hole, dwells at the bottom of the hole for a set amount of time and then rapid retracts out of the hole.

The machine will drill in pecks while rapid retracting out of the hole between each peck to help clear chips from the drill.

Learn more about the G83 code

G84 CNC G Code

G84 - Right-Hand Tapping Cycle

The standard tapping cycle with the spindle running clockwise. The CNC will feed to the bottom of the hole and then reverse the spindle direction and back out of the hole.

G85 CNC G Code

G85 - Reaming Cycle

The machine will feed to the bottom of the hole and then rapid retract out. 

G86 CNC G Code

G86 - Boring Cycle

The machine will feed to the bottom of the hole and then rapid retract out. The tool is not pulled away from the surface which can leave a tool mark on the hole.

G87 CNC G Code

G87 - Back Boring Cycle

Bores a hole from bottom to top. This is the reverse direction of the G86 code.

G88 CNC G Code

G88 - Boring Cycle with Dwell

Bores a hole and then dwells at the bottom for a set amount of time.

G89 CNC G Code

G89 - Back Boring Cycle with Dwell

The machine will feed to the bottom of the hole, pause for a set amount of time and then rapid retract out. The tool is not pulled away from the surface which can leave a tool mark on the hole.

Cancel Codes

G50 CNC G Code

G50 - Cancel Scaling

Turns off scaling mode.

G80 CNC G Code

G80 - Canned Cycle Cancel

Cancels any active canned cycles for drilling, boring, tapping, etc.

Positioning Modes

All movement locations are relative to a fixed zero such as the work offset (G54, G55, etc.) or machine home location.

Learn more about the G90 code

All movement locations are relative to the machine’s current position.

Learn more about the G91 code

Speeds and Feeds

G94 CNC G Code

G94 - Feed per Minute Mode

Sets the feed rate to units (inches/mm) per minute. The tool will move at a set rate.

G95 CNC G Code

G95 - Feed per Revolution Mode

Sets the feed rate to units (mm/inches) per revolution. The tool will move a set rate for every spindle rotation.

G96 CNC G Code

G96 - Constant Surface Speed

The spindle speed will vary to maintain the same surface speed of the material.

G97 CNC G Code

G97 - Constant Spindle Speed

The spindle will operate at a consistent RPM.

Plane Return

After a canned cycle the tool will retract to the start of the canned cycle.

Learn more about the G98 code

G99 CNC G Code

G99 - Return to Rapid Plane

After a canned cycle the tool will retract to a specified location. Can be useful for avoiding workholding fixtures.

Lesser Used G Codes

G09 CNC G Code

G09 - Exact Stop Check (Non-modal)

Causes the machine to come to a stop after each command. Useful for making sharp corners. It causes the machine to stop after executing the current line.

G10 CNC G Code

G10 - Programmed Offset Input

Adjusts offset values. Can change work offset, tool length offset and/or cutter compensation offsets.

G22 CNC G Code

G22 - Stored Stroke Limit

Turns on a set safety zone that will cause the machine to alarm out if it enters.

G23 CNC G Code

G23 - Stored Stroke Limit Cancel

Turns off the stored safety zone.

G27 CNC G Code

G27 - Zero Return Check

Rarely used. This command checks to see that the machine has moved to the zero return position.

The machine will move to a specified location and then to the zero return (home) position.

Learn more about the G28 code

G29 CNC G Code

G29 - Return From Reference Position

The machine will move to the position referenced in the last G28 command. It will then move to the location referenced with the G29 command.

G30 CNC G Code

G30 - Second Position Zero Return

Similar to G28 the machine will move to a specified location and then will move to a secondary reference point in the machine such as a tool changer location.

G31 CNC G Code

G31 - Skip Function

Often used with probing, it is used to move the probe until it hits an object.

G44 CNC G Code

G44 - Negative Tool Length Compensation

Rarely used. Adjusts a tool’s location based on a specified height (H) offset in the opposite of the normal direction.

G45 CNC G Code

G45 - Single Offset Increase

Rarely used.

G46 CNC G Code

G46 - Single Offset Decrease

Rarely used. 

G47 CNC G Code

G47 - Double Offset Increase

Rarely used.

G48 CNC G Code

G48 - Double Offset Decrease

Rarely used.

G51 CNC G Code

G51 - Scaling

Scales the machined part size by a scaling factor.

Scaling factors >1 make it bigger. Scaling factors <1 make it smaller.

G52 CNC G Code

G52 - Temporarily Shift Program Zero

Shifts the program zero location based on a location specified with the command.

G53 CNC G Code

G53 - Return to Machine Zero Position

Rapid moves the CNC to the machine reference position.

G60 CNC G Code

G60 - Single Direction Move

Forces the machine to approach each XY location from the same direction. Helps eliminate location errors caused by machine backlash.

G61 CNC G Code

G61 - Exact Stop Check (Modal)

The machine will come to a stop after each line of code. 

G64 CNC G Code

G64 - Normal Cutting Mode

Cancels both G60 and G61 modes.

G65 CNC G Code

G65 - Custom Macro Call

A macro is similar to a subprogram but it allows you to use variables in the program.

G66 CNC G Code

G66 - Custom Macro Modal Call

Makes the machine call a macro, similar to G65, with every new location given until turned off.

G67 CNC G Code

G67 - Cancel Custom Macro
Modal Call

Cancels any active custom macro modal calls.

G68 CNC G Code

G68 - Coordinate Rotation Mode

Rotates the machine coordinates at a given angle around a specified location.

G69 CNC G Code

G69 - Cancel Coordinate Rotation Mode

Turns off coordinate rotation mode.

G92 CNC G Code

G92 - Program a Work Offset

Sets a work offset based on a specific location in the machine.

Frequently asked questions

How many G codes are there?

We listed 70 G codes in our list alone but in reality there are many more. 

Some will have different meanings on lathes vs mills. Also, different CNC controller manufacturers will include their own special codes. For example, some will have codes such as G103 or they will switch the meaning of a code.

You should always know how your individual machine will react to a specific code. 

What other types of codes are used in CNC programming?

There are numerous codes other than G codes used in CNC programming. 

There are codes for locations, speeds, feeds and machine operations among others. For the most common codes, review our posts on CNC codes and M codes.

Who needs to know G code?

Anyone who works with a CNC machine would benefit from understanding G code. 

While it may not be a requirement for their job depending on where they work, these positions would benefit from understanding G codes:

Is learning G code hard?

Learning G code can be a daunting task when there are so many codes to understand. 

While it is true that there are a lot of codes that can be used, you should know that most shops will only use a small selection of codes which cuts down the number you need to understand before you are up to speed.

Don’t be overwhelmed. Figure out which codes are the most commonly used ones and start your learning there.

Quick Guide to the M30 CNC Code [Program End With Rewind]

a graphic of a cnc machine with text that says learn g code today M30 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.

Want to learn more about CNC G Code?

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?

Quick Guide to the M01 CNC Code [Optional Stop]

a graphic of a cnc machine with text that says learn g code today M01 optional stop

What idoes the M01 code do?

The M01 code is called an optional stop. 

It stops the current program in the middle of running but only if the optional stop switch or button is on.

If the optional stop switch is not on, then the machine will ignore any M01 codes it reads and continue running the program like normal.

haas cnc control panel with optional stop button highlighted

When the machine reads an M01 code and the optional stop switch/button is on, it will turn off the following functions:

  • Movement of all axes
  • Spindle rotation
  • Coolant flow
  • The machine pauses reading the program

When the M01 code is used, nothing is reset. In other words, any active modes or parameters will stay as they are. The program is merely paused when it reads the M01 code.

The machine will resume reading the program when the cycle start button is pressed after the program stop has occurred.

One thing to keep in mind is that the machine doesn’t turn on functions when cycle start is pushed. 

If the M01 code turns something off such as the spindle or coolant, it will need to be turned back on in the program.

Want to learn more about CNC G Code?

When should you use an optional stop with M01?

Optional stops are used for many reasons such as:

  • Manual tool changes
  • Checking the dimensions of the part
  • Checking the condition of the cutting tool
  • Clearing chips
  • Adjusting coolant lines
  • Adding tapping oil to a tap
cnc machine table with coolant lines and spindle shown
So many coolant lines to move

Program stops can be used for almost anything. 

Ideally the CNC programmer will list why the M01 code was used in the program and/or on the setup sheet.

How to resume program operation after using an M01 code

The M01 command stops the machine in the middle of reading the program.

However, it does not restart or reset the program. 

The CNC machine will keep all the conditions that were set before the stop occurred such as feed rate, spindle speed, unit mode, positioning mode, etc.

When the programmer wants to resume the program, he or she will have to make sure that they turn the spindle and coolant on again if needed in the program.

The CNC operator will resume the program by pressing the cycle start button.

fanuc cnc control panel with cycle start button highlighted

What are the benefits of using the M01 code?

Using the M01 code to put an optional stop in your CNC program allows you to check on the status of your machine or part. It also allows the operator to safely perform any needed manual actions.

Making the stop optional allows the machine to keep running if the stop isn’t needed for every run.

For instance, if you need to check the size of every 5th part then you could simply turn the optional stop switch on for one out of every five runs.

The other four runs can have the optional stop switch set to off and the machine will ignore the optional stop

What to watch out for when using an M01 code

Check the program comments and setup sheet to understand why the program stop has been used.

Most CNC machines are intended to run as much as possible because many businesses will look at it as “making chips means making money”. Because of this, it is usually safe to say that if the machine has been stopped on purpose, it is usually for an important reason.

If you aren’t sure why the M01 code is being used, find someone who does.

M00 [program stop] vs M01 [optional stop]

The M00 and M01 are very similar CNC codes.

They both stop the machine in the same way:

  • Machine functions off
  • Program is paused

Both codes required the cycle start button to be pushed after the stop occurs to make the CNC machine resume reading the program.

The difference between them is that the CNC will ignore the M01 code unless the optional stop button or switch is on. If the switch is off, the machine will ignore any M01 codes it reads and continue running the program like normal.

M01 [optional stop] vs M30 [program end]

The M01 and M30 codes both stop the machine, but the M01 stop is just a pause in the program. The M30 code ends the program.

Pressing cycle start after an M30 code will start the entire program over again.

Pressing cycle start after an M01 code will continue reading the program from the next line after the M01 code was used.

The M30 code is also not affected by whether the optional stop switch is on or off. The M30 code will end the program no matter what.

Want to learn more about CNC G Code?