A look at the most common M Codes

CNC M Codes

CNC G Codes

As a complimentary partner to G Codes, the CNC M Codes direct the auxiliary functions of a CNC machine such as coolant and spindle control.


M00 - Program Stop

M00 Program Stop

The M00 command is used to stop the machine and the program.

A typical use would be to do a manual tool change or to add tapping oil to a tap before it feeds into the part. In this example, we use this MCODE to stop the machine for a manual tool change, although we have stopped the cutter using M05 as it is standard practice it is not necessary as the M00 would stop the spindle and render the machine safe open the door.

G00 Z10.0 M09;
G28 X0.0 Y0.0;
M05;
M00;
N2 T0202 (5MM DRILL);

There is no button to turn this function on or off like using the optional stop command M01. It will stop the machine each time the program reaches this block.

Care must be taken to start the spindle after this command, it does not ‘pause’ the program but stop everything, so everything must be started up again including spindle speed and feed rates.


M01 - Optional stop

M01 Optional stop is used to give the operator the choice to stop the machine at a given point in the program. On the machine controls will be an optional stop button, pressing this will stop the machine the next time an M01 command is read in the program.

Example:
G00 Z50.0;
G28 X0.0 Y0.0;
M05;
M01; (CHECK TOOL)

In the part program above, the M01 is used to check the tool is in good condition before an automated tool change. The most common use is at the end of a section before a tool change, this makes it easier to re-run one particular tool which is often needed for dimensional reasons.

If you wish the machine to stop without using the optional stop button on the controls an M00 command should be used instead.

Ensure the cutter is not touching the part when writing these commands into the program.


M03 and M04 M Codes

M03 and M04 M Codes

M03 – Spindle on in a clockwise direction.
M04 – Spindle on in a counterclockwise direction.

M03 is the command used to turn the spindle on in a clockwise direction. It can be inserted in its own block of the program or on the same block as other information.

M04 is the command used to turn the spindle on in an anti-clockwise direction.

The typical place to tell the machine to start the spindle is after defining the spindle speed using an S value, i.e. S1500 M03;

N1 T0101;
G21;
S700 M03;
G00 X175.0 Y25.0 Z10.0;

As we can see from the example program above, I have told the machine to turn the spindle on right after issuing a spindle speed command and before the machine starts to rapid into position with a G00 command. This turns the spindle on after telling the machine what spindle speed to use.

M03 is the usual direction for most cutters and drills as the cutting edge of the tool cuts in a clockwise direction. M04 is used when tapping without using tapping cycles and on a Lathe when the tool is loaded upside down for some operations.


M05 Spindle Stop

Once the cutter is away from the component we can safely stop the spindle with an M05 Command. Issuing this command stops the spindle (or workpiece on a Lathe) from rotating. This typically happens just before a tool change.

Example:
G28 X0.0 Y0.0;
M05;
M30;

In the example above we stop the spindle once the machine is in the home position using the G28 command and before the end of program.


M06 Tool Change

M06 Tool Change

Using the command M06 tells the machine to change the tool in the spindle if it has an automatic tool changer. It also is used on machines without an automatic system to tell the machine a new tool is now active. The declaration of T0101 tells the machine to select tool one (the first 01) and offset one (the second 01) while the ‘T’ stands for ‘tool’.

Example:
N2 T0202 (5MM END MILL);
M06;

The image above puts this into a program so you can see how it is used.


M08 and M09 Coolant Control

M08 and M09 Coolant Control

M08 is the G Code command to turn on the main spindle coolant, M09 turns off all coolant. Some larger CNC Machines have many different coolant systems, some might have overhead or shower coolant, high pressure through spindle coolant, slideway coolant and mist coolant. The CNC M Codes to operate these are often different depending on the machine. The main coolant is aimed directly at the cutting edge of the tool, this is always turned on by M08.

Example:
N1 T0101 (10MM ENDMILL);
G21;
S700 M03;
G00 X175.0 Y25.0 Z-10.0 M08;
Z-6.0;

It is typical to turn the coolant on before the tool touches the job, every machinist has their own preference, some turn it on right after the tool change. I prefer to activate M08 on the line before we start cutting, this is so I can watch the tool approach the job without coolant splashing on the window.

Depending on your machine, you may find it takes a few seconds for the pressure to build up before you have full flow, especially if your machine is older. In this case feel free to move the M08 command a few blocks earlier.

To turn all coolant off we use M09. On machines that only have one source of coolant we use M09 to turn it off. On the larger machines with many coolant systems, the M09 command turns off everything. Like the M08 command, it can be placed anywhere in the program as long as there is not another M Code on the same line.

M30 Program End Return, to Start

M30 CNC M codes Program End Return to Start

The command M30 stops the machine and tells it the program has finished, then the program is rewound back to the start.

On a lathe, if the continuous cycle is activated the machine will start making the next component without any further instruction from the operator.

G28 X0.0 Y0.0;
M05;
M30;
M30 is always the last command in a program


M98 Subprogram Call

M98 Subprogram Call

M98 is the M-Code we use to call a subprogram.

Subprograms are a quick way to reuse code that we need to write often. By using M98, we can run another program within our main program.

We can nest up to 4 subprograms inside each other, although this level of nesting is rarely needed. To return to the previous program, whether it's another subprogram or our main program, we use ,M99.


M98 P2000;
M98 calls the subprogram
P is the program number (O2000)
M99 returns to the previous program.

Read more about working with nested subprograms here.

CNC M Codes list

M Codes are an auxiliary command that each machine can have custom commands for. There is a standard set of codes that never change, these are listed below.

  • M00 Program stop
  • M01 Optional program stop
  • M03 Spindle on clockwise
  • M04 Spindle on counterclockwise
  • M05 Spindle off
  • M06 Tool change
  • M08 Coolant on
  • M09 Coolant off
  • M30 Program end, return to start
  • M98 Sub Program Call
  • M99 Sub Program End
  • Note, only one MCODE can be activated per block of code.

    Share this article

    To learn more about M Codes and G Codes, check out my Foundation course

    The quickest way to learn CNC Programming