M-Codes: CNC Machine Miscellaneous Functions and Their Meaning

Updated

In short

M-codes are the miscellaneous (machine) functions of a CNC program that control spindle rotation, coolant, tool changes, program stops and program end. The most common are M03/M04/M05 (spindle), M06 (tool change), M08/M09 (coolant), M00/M01 (stops) and M30 (program end), but many M-codes are machine-builder specific.

M-codes (miscellaneous functions) are CNC program commands that control machine devices rather than the tool path: spindle, coolant, tool changer, door interlocks, pallet changer and so on. Basic meanings are described in ISO 6983, but some codes are left unassigned and machine builders allocate them to their own functions. Always check the specific machine manual.

Most common M-codes (Fanuc-style controls)

CodeFunctionNotes
M00Program stopUnconditional; resume with Cycle Start
M01Optional stopActive only when the Optional Stop switch is on
M02End of programOlder form; often does not rewind
M03Spindle on clockwiseUsed with S
M04Spindle on counter-clockwiseE.g. for left-hand tools
M05Spindle stop
M06Tool changeOn machining centres, with T address
M07Mist (minimum quantity) coolantMachine dependent
M08Coolant onFlood coolant
M09Coolant off
M19Spindle orientationCommon but not universal
M29Rigid tappingFanuc; may differ elsewhere
M30End of program and rewindThe most common program end code
M98Subprogram callFanuc, with P address
M99Return from subprogramIn a main program – repeat from start

Other codes (e.g. through-spindle coolant, air blast, chuck open/close on lathes, bar feeder, pallet change) vary between builders and even between models.

Lathe specifics

  • Tool changes on lathes are usually made with the T address (e.g. T0101 – tool 1 with offset 1) without M06.
  • M03/M04 direction is defined looking from the spindle, so the correct choice depends on tool position (front or rear of centreline).
  • Chuck, sub-spindle, live tool and C-axis functions are controlled by machine-specific M-codes (see mill-turn machining).

Several M-codes in one block

Many older Fanuc controls allow only one M-code per block, while newer ones may allow several (parameter dependent). The safest practice is one M-code per block or following the machine manual. Whether an M-code executes before or after the motion in the same block also depends on the control.

Example

N10 T5 M06 (load tool 5)
N20 G54 G90 G00 X0 Y0
N30 S2500 M03 (spindle clockwise)
N40 G43 H05 Z25.0 M08 (length offset and coolant)
...
N200 M09
N210 M05
N220 M01 (optional stop for inspection)
...
N500 M30

G commands and program structure are described in G-code basics.

Practical tips

  • M01 for the first part – insert M01 after each tool so the operator can check the result, then switch it off for the batch.
  • Stop the spindle before measuring – M05 and M09 before M00 whenever the operator must work inside the machine.
  • Do not copy programs between different machines without checking M-codes – the same number may mean an entirely different function.
  • CAM post-processor must be configured for the specific machine so correct M-codes are output (see CAM programming).

Subprograms

Repeated operations, such as milling the same contour in several places, are often moved into a subprogram. On Fanuc controls it is called with M98 P2001 (subprogram O2001) and ends with M99. Some controls allow a repeat count (e.g. with an L address or digits in the P address). Siemens and Heidenhain controls call subprograms with different syntax – by name or with CALL commands.

M-codes and automation

In automated cells M-codes also control external devices: robot signals, pallet changes, probes, door opening and measuring cycle start. Such codes are defined by the machine builder or integrator and documented in the machine manual, so their meanings are not universal.

CNC machining services can be found or requested on the CNC Pasaulis marketplace.

Frequently asked questions

What is the difference between M00 and M01?

M00 always stops the program, while M01 stops it only when the optional stop switch on the control is on.

What is the difference between M02 and M30?

Both end the program, but M30 normally also rewinds to the start. Modern programs mostly use M30.

What does M06 do?

On a machining centre M06 changes to the tool given by the T address. On lathes the tool is usually changed with the T address alone.

Are M-codes the same on all machines?

No. Basic codes are similar, but many functions are builder-specific, so the machine manual must be checked.

What are M98 and M99 used for?

On Fanuc controls M98 calls a subprogram and M99 returns control to the main program.

Looking for someone to do this job? Find a manufacturer on the marketplace or post a request.

Services offered I need a service