An M code is a CNC command for machine functions rather than a direct axis-motion command. M stands for miscellaneous. M codes start and stop the spindle, run coolant, change tools, pause or end the program, and fire air devices like vises, doors, and grippers. G codes move the tool. M codes run everything around that motion. Some M codes, such as a tool-change command, can trigger a machine-controlled motion sequence.
This guide gives you a working M code list, real program examples, and the differences between Fanuc and Haas. It also covers the part most lists skip: pneumatic M codes, the commands that let a mill clamp, unclamp, and load parts on its own. We build in-machine automation for CNC mills, so that is where we spend most of our time.
What Is an M Code in CNC Programming?
An M code is a miscellaneous function. It tells the control to switch something on the machine, not to command a cutting path. The format is the letter M followed by digits, commonly two or three, like M03 or M116.
The motion side of the control does not run M codes. The machine logic does. On a Fanuc control, the CNC sends the M code number and a strobe signal to the machine's PMC (its built-in PLC). The ladder logic, written by the machine builder, decides what that number does. Haas works the same way with its own control logic.
Here is how an M code sits in a block:
N20 S1200 M03 ;
S1200 sets the speed. M03 starts the spindle forward at that speed.
Some M codes leave a machine state active. M03 keeps the spindle running until you stop it with M05 or command another supported spindle state. Others act once. M06 changes the tool and is done.
The G and M word format goes back to the EIA RS-274 standard, later adopted as ISO 6983. The standard set the core meanings. Builders filled in the rest, and that is why M code lists drift from machine to machine.
Takeaway: an M code is a switch, and the machine builder decides what most of those switches are wired to.
G and M Codes: How They Work Together
G and M codes share every program but do different jobs. G codes are preparatory functions. They set motion, plane, units, offsets, and canned cycles. M codes are miscellaneous functions. They run the spindle, coolant, tool changer, program flow, and accessories.
Other words fill in the details:
- S sets spindle speed in RPM when RPM mode is active
- F sets the feed rate
- T picks the tool
- H and D call length and radius offsets
One common mistake: M03 does not set spindle speed. It only starts the spindle. The S word sets the RPM in RPM mode. If you omit an S value, the control may use a previously commanded speed, so program the intended speed explicitly.
| G codes | M codes | |
|---|---|---|
| What they control | Tool motion and geometry | Machine functions |
| Examples | G00, G01, G02, G43, G54, G84 | M03, M05, M06, M08, M30 |
| Who defines them | Mostly the control maker | Mostly the machine builder |
| Same across machines? | Core codes, mostly yes | Core codes yes, the rest varies widely |
| Per block | Several, if from different groups | Usually one |

Here is a short program that uses both. Read the comments top to bottom and you can follow the whole cycle.
%
O1001 (FACE TOP OF PART)
G90 G17 G20 G40 G80 G94 ; (absolute, XY plane, inch, cancels, feed/min)
T1 M06 ; (call tool 1, change tool)
G54 G00 X-0.5 Y0. ; (work offset, rapid to start)
S3000 M03 ; (3000 RPM, spindle forward)
G43 H01 Z1.0 M08 ; (length offset, coolant on)
G01 Z0. F20. ; (feed down to Z0)
X4.5 F40. ; (face across the part)
G00 Z1.0 M09 ; (rapid up, coolant off)
M05 ; (spindle stop)
G53 G00 Z0. ; (Z to machine home)
M30 ; (end program and reset)
%
That last block, M30, matters more than it looks. It is also where many published lists get it wrong.
Takeaway: G codes decide where the tool goes, and M codes decide what the machine does while it gets there.
CNC M Code List: The Most Common M Codes
Many of these CNC M codes are shared across vertical mills. Others, including M07 and the Haas local-call code M97, are control- or builder-specific. Learn the common functions first. Then check your builder's manual for the exact behavior.
| Code | Function | Shop notes |
|---|---|---|
| M00 | Program stop | Stops program execution; machine outputs follow the builder's logic. Press cycle start to continue |
| M01 | Optional stop | Stops only when the optional stop switch is on |
| M02 | Program end | Ends the program. Rewind behavior depends on the control |
| M03 | Spindle on, forward (clockwise) | Program the intended S value explicitly |
| M04 | Spindle on, reverse (counterclockwise) | Left-hand tools and some tapping |
| M05 | Spindle stop | Use before manual access and non-rotating spindle-gripper handling |
| M06 | Tool change | Paired with a T word, such as T5 M06 |
| M07 | Mist or shower coolant on | Function varies by builder |
| M08 | Flood coolant on | |
| M09 | Standard coolant off | Commonly cancels M07 and M08; other coolant systems may need separate commands |
| M19 | Spindle orient | Positions the spindle at a set angle |
| M30 | Program end and reset | Commonly rewinds to the top, ready for the next cycle; verify the control setup |
| M97 | Local subprogram call | Haas: jumps to a line number in the same program |
| M98 | Subprogram call | Calls a separate program |
| M99 | Subprogram return or loop | Returns to the main program, or loops |
The sections below cover each group with real code.
Program Stop and End Codes (M00, M01, M02, M30)
M00 stops the program every time it is read. On Haas mills, the spindle, coolant, and programmed motion stop. Other machines follow their builder's stop logic. Use it for a manual check, a part flip, or a chip clear, following the machine's access procedure.
M01 is the same stop, but only when the operator turns on optional stop. Shops use it at the end of each tool during prove-out, then switch it off for production.
M02 and M30 both end the program, but they are not the same. M30 commonly ends the program and resets it to the top, so the next cycle start runs it again. M02 ends the program, and on some controls it does not rewind. On some Fanuc controls, settings in parameter 3404 affect end-of-program behavior; check the manual for the exact control series and builder setup.
A clean stop for a manual tool change looks like this:
G00 Z1.0 M09 ; (retract, standard coolant off)
M05 ; (spindle stop)
M00 ; (program stop)
Some controls stop the spindle on M00 anyway. Program M05 first. It makes the intent clear to the next person who reads the code.
Takeaway: use M30 at the end of production programs, and save M02 for when you know exactly how your control treats it.
Spindle Codes (M03, M04, M05, M19)
M03 is the most used M code in any shop. It starts the spindle forward, which is clockwise when viewed from the spindle looking toward the part. That is the direction standard right-hand end mills, drills, and taps cut in.
Program the intended spindle speed explicitly. Put the S word in the same block or a block before M03:
S2500 M03 ;
You can also start the spindle in MDI the same way. Type S1000 M03 and press cycle start to run it at 1,000 RPM.
M04 runs the spindle in reverse. You will use it for left-hand tools, some back-boring setups, and certain tapping cycles. M05 stops the spindle. Use it before M00 and before a manual tool check. For spindle-mounted devices, follow the device's operating instructions: gripper handling requires a stopped spindle, while a chip fan may require commanded rotation.
M19 orients the spindle. It stops the spindle at a fixed angle. Boring cycles use it to shift the boring bar away from the wall before retracting. Spindle-mounted grippers and chip fans also depend on correct orientation during tool changes.
Takeaway: M03 starts the spindle, S sets the speed, and M05 stops it. Match any device-specific rotation or orientation command to the accessory's instructions.
Tool Change Code (M06)
M06 runs the tool changer. Pair it with a T word:
T5 M06 ;
On most Haas mills, M06 sends the spindle to the tool change position on its own. Some other machines want Z at machine home first, so programmers add G53 G00 Z0. or G28 G91 Z0. before the tool change. Check your manual. If you use G91 for a return move, restore the intended distance mode before subsequent positioning.
The tool changer is also how automation gets into the spindle. A spindle gripper or chip fan sits in the carousel like any other tool. M06 loads it. The next device-specific command actuates or runs it.
Takeaway: M06 brings any tool into the spindle, including tools that hold parts instead of cutting them.
Coolant Codes (M07, M08, M09, M88, M89)
M08 turns on flood coolant. M09 turns off standard coolant. Those two are close to universal, but M09 does not necessarily stop every coolant system.
M07 is less consistent. On some builds it is mist. On Haas mills it is shower coolant. Through-spindle coolant has no standard code. On a Haas mill it is M88 to turn it on and M89 to turn it off. On other builders it can be any number the builder picked.
Coolant on and off also shapes automation. Through-spindle air or coolant can power some spindle-mounted devices. In that case, the coolant M code is the trigger.
Takeaway: M08 and M09 travel well between machines, and every other coolant code needs a check against the manual.
Subprogram Codes (M97, M98, M99)
Subprograms let you write a routine once and call it many times. That makes them the backbone of unattended cycles.
-
M98 calls a separate program, such as
M98 P2000 -
M97 calls a line number inside the same program on Haas controls, such as
M97 P100 - M99 returns to the line after the call. At the end of a main program, M99 loops it back to the top
A program that ends in M99 instead of M30 runs over and over. That can be what you want for a bar feeder or pallet cell. It can also be how a machine keeps cutting air when nobody is watching, so use it on purpose.
Takeaway: M97, M98, and M99 turn one block of code into a repeatable cycle, and that is the first step toward running lights-out.
Fanuc M Codes vs Haas M Codes
Fanuc writes the control. The machine builder writes most of the M codes. That one fact explains how Fanuc M codes can differ from one machine to the next, and almost every M code mix-up in a shop with more than one brand.
On a Fanuc control, the CNC passes the M number to the PMC ladder, and the builder's ladder decides what happens. Builder-specific assignments are not confined to codes above M79. The permitted number of M codes per block also depends on the control and enabled options. Haas specifies one M code per block and states that M codes take effect at the end of the block.
The same number can mean different things on different machines:
| Code | Typical Fanuc-based mill | Haas mill |
|---|---|---|
| M10 / M11 | Clamp / unclamp (builder assigns what) | 4th axis brake engage / release |
| M19 | Spindle orient (builder dependent) | Spindle orient |
| M29 | Rigid tapping mode on many builds | Set output relay with M-FIN |
| M70 / M71 | Builder assigned | Workholding clamp / unclamp |
| M80 / M81 | Builder assigned | Auto door open / close |
| M97 | Not a standard Fanuc code | Local subprogram call |
It gets worse across machine types. On a Haas lathe, M10 and M11 are chuck clamp and unclamp. On a Haas mill, the same codes lock the 4th axis brake.
This is why a generic M code cheat sheet can hurt you. Several popular online lists mix codes from different builders into one table, and the result does not match any real machine. Use those lists to learn the idea. Use your builder's manual to write the program.
Doosan, DN Solutions, Hurco, Brother, Mazak, and DMG Mori machines follow the same rule. Some run Fanuc controls with builder ladders. Others run their own controls. Either way, many core functions are familiar, and the accessory codes belong to the builder.
Takeaway: learn the common functions, but confirm the codes and behavior in the manual for that exact machine, including coolant, tool-change and program-end behavior.
Pneumatic M Codes: How CNC Machines Control Air Devices
There is no universal pneumatic M code. No standard says M52 opens a vise or M80 opens a door. Each machine maps spare M codes to output relays, and those relays drive air valves.
That is why "pneumatic M codes" returns so many unrelated lists. The useful question is how your machine turns an M code into air pressure. That chain is the same on almost every mill:
- The program reads the M code. The control sends it to the machine logic.
- The logic turns on an output. On Haas, that is a relay on the I/O board. On Fanuc builds, it is a PMC output the builder or installer maps.
- The output drives a solenoid valve. The valve shifts and sends air down one line.
- The air moves the device. A vise clamps, a door lifts, or gripper jaws close.
- The program waits or checks. A dwell allows time to pass. A correctly implemented M-FIN or input signal can confirm the required device state.
Step 5 is where most homemade setups go wrong. An air vise that takes two seconds to clamp will not be clamped if the program starts cutting at 0.2 seconds. A dwell alone cannot detect a failed clamp, blocked door or lost air supply.

Takeaway: a pneumatic M code commands an output. Allow for movement time and verify completion wherever the next operation depends on it.
Common Pneumatic Functions and Their M Codes
Haas publishes its full mill list, so it makes a clear reference. Fanuc-based machines do the same jobs, but the numbers come from the builder or installer.
| Function | Haas mill M codes | Typical Fanuc-based build |
|---|---|---|
| Latch a relay on / off | M51 to M55 on, M61 to M65 off | Spare output M code in the PMC ladder |
| Relay that waits for a finish signal | M21 to M25 (wait for M-FIN) | M code with the FIN signal through the PMC |
| Relay on an add-on 8M bank | M59 / M69 with a P code (P90 to P97 on the first bank) | Extra I/O module |
| Workholding clamp / unclamp | M70 / M71 | Builder assigned |
| Fixture clamp input on / off | M90 / M91 | Builder assigned input check |
| Auto door open / close | M80 / M81 | Builder option or spare relay |
| Tool air blast on / off | M73 / M74 | Builder assigned |
| Auto air gun on / off | M83 / M84 | Builder assigned |
| Vise air chip blast on / off | M116 / M117 | Builder assigned |
Three details on Haas are worth knowing before you wire anything.
- M51 to M55 latch. Each code turns a relay on and leaves it on. M61 to M65 turn the matching relay off. M52 turns its assigned relay on. M62 turns that relay off.
- RESET clears the M51 to M55 outputs. The corresponding I/O-board user relays turn off. Do not assume every add-on 8M relay behaves the same way; reset behavior varies with bank, options and software configuration.
- M21 to M25 wait. Each one closes a relay and holds the program until an external M-FIN signal arrives. Use a correctly implemented completion handshake when the next move depends on the device finishing.
Treat that RESET behavior as a design input. Choose the required safe state for each device, then verify the complete electrical and pneumatic arrangement. A relay turning off does not by itself guarantee that a vise stays clamped during air loss, an alarm or an E-stop. Valve type, retained pressure, mechanical retention and the machine's safety design all matter.
Takeaway: on Haas, verify what RESET does to the exact output you use, and test what the connected device actually does in that state.
Pneumatic Vise and Auto Door M Code Example
Here is an illustrative load-sequence excerpt for a mill with a pneumatic vise and an auto door for a robot or cobot. The relay numbers, positions and dwell times are examples. Your wiring and validated integration set the real ones.
(EXAMPLE ONLY: RELAYS, POSITIONS AND TIMES ARE INSTALLATION-SPECIFIC)
G90 G20 G94 ; (absolute, inch, feed per minute)
M05 ; (spindle stop)
M09 ; (standard coolant off)
G53 G00 Z0. ; (Z to machine home)
G53 G00 X-10. Y0. ; (table to verified load position)
M53 ; (example relay on: open auto door)
G04 P3.0 ; (allow door travel time)
M52 ; (example relay on: open vise)
G04 P2.0 ; (allow jaws to open)
M21 ; (signal robot to load, wait for M-FIN)
M62 ; (example relay off: close vise)
G04 P2.0 ; (allow clamp time)
M63 ; (example relay off: close auto door)
G04 P3.0 ; (allow door travel time)
(EXCERPT ENDS: VERIFY CLAMP, DOOR AND ROBOT CLEARANCE BEFORE MACHINING)
Notice the order. The spindle stops first. The door opens before the vise. The vise closes before the door. The air moves have example dwells, and the robot handoff uses M21. In production, the required confirmations must be satisfied before the machining routine is called.
The same pattern works with our pneumatic vises and with aftermarket CNC auto-doors. Swap the relay numbers to match your install, and prove it in single block before you run it at speed.
Takeaway: stop, open, wait, confirm, close, wait. Confirm the required machine and robot states before cutting.
M Codes for Spindle Grippers and In-Machine Automation
A spindle gripper turns the mill into its own part loader. No robot arm. No external cell. The gripper lives in the tool carousel. M06 loads it. G codes move it. Pneumatic M codes open and close it.
This sequence is based on the Haas VF-2YT template used in our spindle gripper programming tutorial. The example uses the M55/M65 output pair for the gripper and M52/M62 for the air vise. It explicitly selects the tray work offset before lowering.
(EXAMPLE EXCERPT: VERIFY OFFSETS, CLEARANCES AND RELAY FUNCTIONS)
G90 G20 G94 ; (absolute, inch, feed per minute)
M05 ; (spindle stop)
T17 M06 ; (load gripper from pocket 17)
G43 H17 ; (verified gripper-and-part length offset)
G55 G00 X0. Y0. ; (position above selected tray pocket at safe Z)
G01 Z0. F50. ; (lower to stock in the tray)
M55 ; (close gripper)
G04 P1.0 ; (allow gripper actuation time)
G53 G01 Z0. F400. ; (lift part to verified machine Z0 clearance)
M52 ; (open vise)
G04 P2.0 ; (allow jaws to open)
G54 G01 X0. Y0. F400. ; (move over the vise)
G01 Z-0.01 F20. ; (set part on the verified jaw step)
M62 ; (close vise)
G04 P2.0 ; (allow clamp time)
(EXCERPT ENDS: CONFIRM CLAMP BEFORE M65 RELEASE AND Z RETRACT)
Look at the pairs. In this installation, M55 closes the gripper and M65 opens it. M52 opens the vise and M62 closes it. Each pair sets and clears one Haas output. The example vise is arranged to clamp with its output off; verify retention under reset and loss of air rather than assuming the relay state guarantees it.
Do not command cutting rotation with the spindle gripper loaded. Any spindle-orientation operation must follow the instructions for the exact gripper configuration. Spindle grippers are built for vertical machining centers, where the carousel and machine axes do the handling. That setup is how a single VMC can handle CNC machine tending through a full tray of blanks with no robot.

Takeaway: with a spindle gripper, the "robot" is your mill, and its controller is a handful of M codes.
Safe Sequencing Rules for Pneumatic M Codes
Many problems in air-driven setups come down to timing, missing confirmation or an incorrect assumption about device state. These rules address those issues.
- Stop the spindle for non-rotating gripper handling. Program M05 and follow the gripper's approved orientation procedure. Other accessories, such as chip fans, may have different rotation requirements.
- Allow time for every air move. Use a suitable dwell where required by the device sequence. Time the real device and account for operating variation.
- Confirm when it matters. Use a correctly implemented M-FIN handshake or input check when the next operation depends on completed actuation. A dwell is not proof of clamping.
- Design and verify the safe state. Check the complete valve, actuator and retention arrangement under reset, alarm, E-stop and loss of air. Relay-off does not automatically mean safe.
- Prove it in single block. Verify each assigned output under the installation's commissioning procedure, then test the sequence with suitable overrides and clearances.
- Hold the rated air pressure. Our vises run from 40 to 175 PSI. Clamping force changes with pressure, so set a regulator for the application and monitor the required pressure.
- Plan for air loss. Know what each device does when shop air drops. Confirm the retention behavior of the exact GimGripper4™ configuration and test it with the actual part and load.
Takeaway: reliable pneumatic sequencing combines appropriate timing, state confirmation and verified retention behavior.
How to Add or Assign a Custom M Code
Need an air output your machine does not have? There are three common ways to add one.
Use a spare user relay. Compatible Haas mills provide user M-code outputs reached with M51 to M55 and M61 to M65, but installed relay hardware and availability depend on the machine and options. The optional 8M relay bank provides more outputs, called with M59 and M69 plus a P code.
Have the builder or installer map an output. On Fanuc-based machines, a spare output gets tied to an M number in the PMC ladder. This is a ladder edit. Leave it to the builder, the dealer, or an experienced integrator.
Call a macro from an M code. Some Fanuc control series provide parameter assignments for M-code subprogram or custom-macro calls, including parameter ranges 6071 to 6079 and 6080 to 6089. Availability and meaning depend on the exact control series and options. Check its parameter manual before making changes. This is useful when one M code needs to run a whole sequence, like open door, open vise, and dwell.
Once you have the output, you still need a valve. Our MCodeAir™ programmable air solenoid kit gives you two programmable air outputs that fire from an M code. Each can run two-way or on/off. Haas, Doosan, and other machines with onboard relays work out of the box. Fanuc, Brother, DN, and Mazak machines connect through a 24V converter.
Shops in California and nearby western states can also have our team install, wire, and test the outputs on site as part of a turnkey setup.
Takeaway: check available outputs first; the missing pieces may be relay hardware, a valve, interface electronics and a verified wiring plan.
Common M Code Mistakes and How to Avoid Them
The same handful of errors shows up again and again. Each one is easy to fix once you know it.
- Copy codes from a generic list. Check the builder's manual for the exact machine, including the behavior of familiar codes.
- Put two M codes in one block. Haas allows one. Fanuc support depends on the control and options. Split them onto separate lines unless the manual says otherwise.
- Treat M02 and M30 as the same. Use the documented production-program ending for the control, commonly M30.
- Expect M03 to set speed. Program the intended S value first or in the same block.
- Assume an air move has finished. Allow suitable time and verify the required device state before a dependent operation.
- Forget M05 before manual access. Stop the spindle on purpose and follow the machine's access procedure.
- Wire relays without thinking about RESET. Verify the output's reset behavior and the connected device's actual safe state.
- End a main program with M99 by accident. It can loop continuously. Use M30 unless you intend the loop and have verified the control behavior.
Takeaway: many M code problems are timing or assumption problems. Correct the program and verify the machine's actual response.
Frequently Asked Questions
What does M stand for in M code?
M stands for miscellaneous. M codes are miscellaneous functions that operate machine features such as the spindle, coolant, tool changer, and accessories. They do not directly specify an axis toolpath, although commands such as a tool change can trigger machine-controlled movement.
What is the difference between G code and M code?
G codes control motion and geometry, such as rapid moves, cutting moves, offsets, and canned cycles. M codes control machine functions, such as spindle on, coolant on, tool change, and program end. Every CNC program uses G and M codes together.
Are M codes the same on every CNC machine?
No. Many core functions, such as spindle start, spindle stop and program end, use familiar numbers across machines. The exact behavior and accessory assignments still depend on the control and machine builder. Check the manual for the exact machine rather than relying on a generic number range.
What is the M03 code in CNC?
M03 starts the spindle forward, which is clockwise when viewed from the spindle toward the part. Program the intended speed explicitly with the S word, as in S2500 M03. A previous S value may remain active if no new speed is given. M04 runs the spindle in reverse and M05 stops it.
What does M05 do?
M05 stops the spindle. Program it before a program stop (M00), before a manual tool check, and for non-rotating spindle-gripper handling. Follow the machine's access procedure and the accessory's instructions.
What is M09 in CNC?
M09 turns standard coolant off. It commonly cancels M07 (mist or shower) and M08 (flood). Some machines use separate codes to turn off through-spindle coolant, such as M89 on Haas mills.
What is the difference between M02 and M30?
Both end the program. M30 commonly resets the program to the top so the next cycle start runs it again. M02 rewind behavior depends on the control and its settings. Confirm both commands in the exact machine's manual.
Can you put more than one M code on one line?
Usually not. Haas allows one M code per block. Fanuc support depends on the control series and enabled options. Put each M code on its own line unless your manual says otherwise.
What are pneumatic M codes?
Pneumatic M codes are M codes mapped to outputs that drive air solenoid valves. They clamp vises, open doors, run air blasts, and open or close grippers. There is no standard numbering. On compatible Haas mills, M51 to M55 set user relay outputs and M61 to M65 clear them.
M Codes Are the Switchboard for Automation
Every automated mill runs on the same few ideas. G codes move the axes. Core M codes run the spindle, coolant, and tools. Pneumatic M codes clamp, open, and load. Get the timing, confirmation and retention right and one VMC can run a full tray of parts on its own.
If you are planning that step, start with the hardware your M codes will drive: CNC spindle grippers, air vises, and a solenoid kit to tie them to your control.






















