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 with links for more info on each code.
You can expect that these codes will work the same on most machines:
- M00 [program stop]
- M01 [optional stop]
- M02 [program end, no rewind]
- M03 [spindle on, clockwise]
- M04 [spindle on, counterclockwise]
- M05 [spindle off]
- M06 [tool change]
- M08 [flood coolant on]
- M09 [coolant off]
- M19 [spindle orient]
- M30 [program end with rewind]
- M97 [local subprogram call]
- M98 [subprogram call]
- M99 [subprogram end]
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.
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.
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.
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.
The M97 code will jump to a new line in the current program.
The M98 code 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.