Back to GSM Code List

M30: end of program and return to the first block

M30 ends the program: the control stops the feed, normally stops the spindle and the coolant, and puts itself back on the first block, ready to machine the same part again. It is the last line of practically every milling and turning program. Its relative M02 also ends, but classically it does not rewind. What is worth being clear about is that M30 is not a safety block: retracting the tool, stopping the spindle and shutting the coolant have to be written before it.

For reference only: syntax changes with the control, the post-processor and the machine parameters. Check the official manual and run a simulation before cutting. Legal notice.

Family
Program
Machine
Milling, Turning, Mill-turn, Laser/cut
Risk
Low
Example
M30

Syntax by control

Fanuc

M30

End of program and rewind to the first block. What modal state is left afterwards (G90 or G91, the active work offset, the plane, the units) is decided by machine parameters, so you cannot take it for granted that the control goes back to G90 on its own.

Haas

M30

It ends, rewinds and also increments the two parts counters of the control, which is how a production run is tallied. With Setting 118 on, an M99 in the main program increments those counters too.

Siemens 840D / 828D

M30

M30 and M2 end the program the same way on most Siemens machines. M17 is the end of a subprogram, not of a program. The state the control is left in at the end is defined in the machine data.

Heidenhain TNC (Klartext)

END PGM PIEZA1 MM

In Klartext the end of the program is the END PGM line, which the control itself writes when the program is created. M30 is accepted in ISO format programs and on many TNCs it can be written next to the final line.

Machines with automation

M30

On machines with a bar feeder, a robot or a pallet changer, the M30 can also be the signal that triggers the next cycle. When parts have to be chained with no intervention, plenty of shops use an M99 in the main program instead.

Address letters used with it

LetterWhat it setsUnit
No address lettersM30 goes alone in its block. It takes no arguments of any kind and it is usually the last line of the file, followed by the end of program character where the control uses one.no units
Parts counterIt is not programmed, but it is part of what it does: many controls add a part to the counter every time they run an M30. It is the number you look at to know how many of the run are done.parts
Modal state after the M30Which codes are left active after the end (absolute or incremental, work offset, units, plane) depends on control parameters. It is a machine setting, not a property of the code.machine setting

Worked example

The complete closing block of a milling program. The M30 is the last line, but what makes the ending safe is the blocks before it: cancelling cycles and offsets, shutting the coolant, retracting in Z, sending the axes to the reference point and stopping the spindle. This block is copied as it stands onto the end of any program and it avoids half the scares of the next start.

G99 G81 X120. Y40. Z-12. R3. F220
( Last hole of the last operation: the program reaches the close with a canned cycle active and the coolant running. )
G80 G40
( Cancels any canned cycle and cutter compensation. Finishing with G41 active means the next tool starts offset by half a cutter. )
G00 Z50. M09
( Retracts the tool in Z and shuts the coolant, with the machine still under program control. )
G91 G28 Z0. M05
( Z to the reference point in incremental and spindle stop. Under G90 this block would drop to the part Z0 first, which is the face. )
G91 G28 Y0.
( Sends Y to the reference point as well to leave the table out so the next part can be loaded comfortably. On some machines a G53 to a particular position is preferred. )
G49
( Cancels the length offset with the machine already at the reference point, to leave the control in a predictable state. )
G90
( Puts the control back in absolute. Whether the M30 restores it depends on a parameter, and starting the next program in incremental is a crash. )
M01 (REVISAR ACABADO CADA 10 PIEZAS)
( Optional stop before the close: if the operator has it switched on, he can look at the part without editing the program. )
M30
( End of program. The control goes back to the first block, adds a part to the counter and is ready to start again. )
Open the simulator to check the example

Practical keys to using M30 in the workshop

What M30 does and what it does not

It does three things: it stops execution, it leaves the pointer on the first block so cycle start runs the same part again and, on most machines, it cuts the spindle and the coolant and increments the parts counter. It does not do what plenty of people assume: it does not retract the tool, it does not send the axes to the reference point, it does not open the door and it does not guarantee that cutter compensation, length offset or incremental mode are cancelled. All of that is machine parameters or blocks you have to write. M30 is the full stop at the end of the sentence, not the summary.

The closing block worth writing every time

The sequence that works on any machine is short: cancel the cycle and cutter compensation with G80 and G40, retract in Z and shut the coolant with M09, go up to the reference point with G91 G28 Z0. and stop the spindle with M05, cancel the length with G49, go back to G90 and finish with M30. If the machine has to end up in a comfortable loading position, you add a G53 to that position or a G28 in Y. Writing that block is not fussiness: it is what lets the next program start from a known state instead of inheriting the last one.

M30 in production: counters and chaining parts

In a run, the M30 is also the point where the control adds a part. Those counters are there to cut production at a quantity, to call for an insert change every so many parts or simply to know how many you have done. On automated machines, with a bar feeder or a robot, the end of the program can chain straight into the next part: some shops handle it with the signal the M30 itself sends the automation and others replace the M30 with an M99 that restarts the program in a loop, normally with the block delete slash so it can be broken.

Mistakes that cost you a part (or a tool)

  • Trusting the M30 to retract the tool: it moves no axis. If the program ends with the cutter two millimeters off the part, that is where it stays until somebody raises it by hand.
  • Assuming the M30 puts the control back in G90: it is a machine parameter. Ending in G91 makes the first positioning move of the next program read as incremental.
  • Assuming it cancels G41, G43, G52 or the active work offset: on many machines it does not, and the next tool starts with the inherited compensation or shift.
  • Writing M30 inside a subprogram: the whole program is taken as finished in the middle of the part, with the tool wherever it was.
  • Forgetting the M30 at the end of the main program: the control keeps reading whatever is behind it in memory, usually the subprogram, and runs it as if it were new code.
  • Using M02 expecting it to rewind: in the classic behavior it does not, and although many modern controls have made them the same, it still depends on the machine.
  • Counting on the M30 to shut the coolant and stop the spindle on every machine: normally it does, but it is a PLC matter and writing M05 and M09 first costs two blocks.

Frequently Asked Questions (FAQ)

What is the difference between M30 and M02?

Both end the program. The classic difference is that M30 rewinds the pointer to the first block and leaves the control ready to repeat the part, while M02 ends where it is. On many modern controls they behave the same, but since that depends on the machine, the sensible thing is to use M30 every time.

Does M30 stop the spindle and shut the coolant?

On most machines yes, because the PLC provides for it. Even so it is not an obligation of the code, and it depends on the builder. The shop habit is to write M05 and M09 in the closing blocks, which also makes clear to whoever reads the program what state is expected at the end.

Does M30 cancel the G54, the G43 or cutter compensation?

It depends on control parameters. There are machines set up to return to the initial state after end of program and others that keep the modals. Since you cannot know without looking at the machine, the practical answer is to cancel what matters by hand with G40, G49 and G90 before the M30.

Do the axes have to go to the reference point before M30?

It is not compulsory, but it is strongly recommended: leaving Z up and the table in a comfortable position lets you load the next part without fighting the tool, and it means the next program always starts from the same place. G91 G28 Z0. is enough in most cases.

How do I make the program repeat on its own with nothing to touch?

By replacing the M30 with an M99 in the main program, which restarts from the first block. It only makes sense on machines where the next part comes in on its own, with a bar feeder or with automation, and it should be written with the block delete slash in front so the operator can break the loop from the panel.

Related codes

Other codes in Program

See the full list of G and M codes

Related tools