The M02 code ends the program. It does not return to the beginning 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 M02 would tell the machine to not rewind. This was done for multiple reasons. Sometimes when the tape was rewound, the CNC would “eat” the tape. The tape would also get taped together to form a continuous loop for programs that were run over and over sometimes.
The M02 code turns off the following machine functions:
- The movement of all axes
- Spindle rotation
The other code that can be used to end a program is M30. The M30 code ends the program and rewinds 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.
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.
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.
Best practices for using the M02 code
You should always take some safety steps before stopping the program with the M02 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 M02 code.
It is also a good practice to turn off the coolant using the M09 code before stopping the program.
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 M02 or M30 code 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.