M CNC processing code basics
M codes tell a CNC machine when to switch something on or off: spindle, coolant, tool change, program end. This page explains the logic behind those commands, where they sit in a program block, and which ones you should leave alone on a production part. Written for engineers and buyers who review CAM output before it goes to the shop floor.

In this article
- 1
- 2
- 3
- 4
- 5
- 6
- 7
What an M code actually does
M codes are machine functions. They do not cut metal. They switch things on and off: spindle rotation, coolant flow, tool changer arms, pallet clamps, chip conveyors, program stop. A G code moves the tool along a path; an M code decides whether the machine is ready for that path. If the spindle has not reached speed, the G code that follows will rub instead of cut.
The letter M is followed by a two or three digit number, from M00 to M99. On most controls the range is fixed by the machine builder, not by the ISO standard. M03 and M04 mean spindle forward and reverse almost everywhere. M08 and M09 mean coolant on and off. Beyond that, the mapping gets loose. M29 is rigid tapping on a Fanuc mill, but on a Brother drill-tap center it may mean something else entirely.
A block can hold one M code with a move, or an M code on its own line. M codes that start a physical action usually go on their own block so the control can wait for confirmation. That confirmation comes from a limit switch or a pressure sensor, not from a timer. When the signal never arrives, the program stalls at that line and the operator sees an alarm instead of a crash.
Ordering inside a block matters more than most people expect. M03 S8000 on the same block is fine on a mill, but M03 and a rapid move on the same block can start the cut before the spindle reaches 8,000 rpm. Put the M code first, then the move. Two extra characters cost nothing.
- 1M codes are non-motionThey control machine state, not tool position.
- 2Numbering is builder-specificOnly a small set is near-universal.
- 3Handshake, not guessworkThe control waits for a real signal before continuing.
Where M codes sit next to G codes
A CNC program is two languages sharing one line. G codes describe geometry and feed: G00 rapid, G01 straight feed, G02 arcs clockwise, G81 drilling cycle. M codes describe the machine around that geometry. A drilled hole needs both. G81 gives the depth and feed rate; M08 brings coolant to the tip; M09 shuts it off before the tool retracts past the fixture.
This split explains why two shops running the same part get different cycle times. The G code may be identical while the M code sequence differs. One shop changes tools with M06 and a pre-staged carousel; another changes tools mid-cut because it forgot to add the coolant-off block before the retract. Same geometry, different stopwatch.
The practical rule: if the line changes the position of the cutting edge, it is G code. If it changes what the machine is doing while the edge is stationary, it is M code. When a program behaves strangely, read the M codes first. Geometry errors usually show up as a wrong dimension; M code errors show up as a stalled cycle, a broken tool, or a scrapped batch.
- 1G = pathPosition, feed, cutter compensation, canned cycles.
- 2M = stateSpindle, coolant, tool changer, clamps, stops.
- 3Interleave deliberatelyStart the state before the move that needs it.
The commands you will see on almost every job
M00 is a program stop. The machine halts and waits for the operator to press cycle start. Use it for a manual measurement or a chip clear on a deep pocket. M01 is an optional stop that only triggers when the panel switch is on. M01 is the safer choice on a proven program because a forgotten M00 stops the job at 2 a.m.
M03 and M04 start the spindle forward and reverse. M05 stops it. On a mill, M03 is clockwise seen from the spindle nose, which suits right-hand cutters. On a lathe, M03 usually turns the spindle toward the operator. M19 orients the spindle to a fixed angle so a boring bar or a probe can enter a known position.
M06 changes the tool. The number that follows the T word selects it. On a 16-station carousel, M06 with a pre-call like T07 M06 keeps the next tool ready and trims seconds per pocket. M08 floods coolant, M07 is mist, M09 shuts both off. On deep pockets in 6061 or 304 stainless, M08 alone often leaves chips packed in the corner; a through-spindle option helps more than a higher flow rate.
M30 ends the program and rewinds it to the top. M02 ends it without the rewind on older controls. M98 calls a subprogram and M99 returns from one. Those two keep a family of similar parts on one page of code instead of four hundred lines. On a 4,000 mm gantry job, M98 loops a roughing pattern along the length and cuts the file size hard.
- 1M00 vs M01Prefer M01 unless the stop must always trigger.
- 2M08 timingTurn coolant on before the tool enters the cut.
- 3M30Ends the program and resets it for the next cycle.
- 4M98 / M99Subprogram loops keep repeated geometry compact.
Where M code behavior stops being portable
Post-processor output assumes a control family. A program written for a Fanuc 0i may run on a Haas with edits, but the same file pushed to a Siemens 828D or a Heidenhain TNC needs a different post. The G code geometry often survives; the M code sequence usually does not. Tool change logic, pallet calls and probe macros are the first things to break.
Machines with a bar feeder, a pallet pool or a robot loader add M codes that exist nowhere else. M50 might close a collet on one lathe and open a door on another. Before you send a program to a new machine, ask for the builder's M code list. It is a one-page PDF and it saves a scrapped first article.
Some commands are locked behind an option. Rigid tapping with M29 needs the synchronized spindle option. High-pressure coolant through the tool needs a pump and a rotary union rated for it. If the option is missing, the control throws an alarm at that block instead of quietly degrading, which is the better failure mode.
Control age matters too. A 1998 lathe may only support two-digit M codes and no M19 orientation. A 2024 5-axis center may accept M codes for thermal compensation and tool breakage detection. The same part number, two very different programs.
- 1Post-processor firstMatch the post to the control before the first cut.
- 2Ask for the M code listOne page prevents a scrapped first article.
- 3Options gate commandsNo synchronized spindle, no M29.
How M code choices show up in part quality
Coolant timing changes surface finish. Start M08 too late and the first 2 mm of a cut runs dry, which leaves a bright rub mark on 6061 and a smear on 304 stainless. Stop it too early and chips sit in the flute on the retract, dragging a scratch down the wall. On a finish pass targeting Ra 0.8–1.6 μm, that scratch is the whole failure.
Spindle dwell before the cut affects bore roundness. A boring bar entering before the spindle reaches commanded speed cuts a slightly oval first 3 mm to 5 mm. Adding a dwell of 0.5 s to 1 s after M03 costs almost nothing in cycle time on a 20-minute part and removes the taper. On a ±0.005 mm bore, that dwell is not optional.
Tool change sequence affects repeatability. If the carousel pre-stages the next tool while the current one is still cutting, the spindle has to wait at the change position. If it does not, the change takes longer but the thermal load on the spindle is steadier. For a run of 10,000 parts, the steadier option holds size better across the shift.
Program stops affect the operator, not the metal. A well-placed M01 after a roughing pass lets the operator check a dimension before the finish pass commits. A misplaced M00 in the middle of a deep pocket stops the tool in the cut, where it will leave a witness mark when it restarts.
- 1Coolant lateDry rub mark on the first few millimeters.
- 2No spindle dwellSlightly oval entry on a bored hole.
- 3Stop in the cutWitness mark where the tool restarts.
How to review an M code sequence before the first cut
Run these checks in order. Each one takes a few minutes on a program that already posts cleanly.
- 1List every M code in the fileSort the program and count occurrences of each M number. An M08 with no matching M09 is the most common single-line error we see.
- 2Check start order at the top of each toolSpindle on and up to speed before the first G01, coolant on before the tool enters the material. M03 S8000, then the move.
- 3Verify tool change positionM06 needs clearance. Confirm the Z retract clears the fixture and the part by 20 mm or more on a 750 × 1,150 × 550 mm travel machine.
- 4Match M codes to the specific machineCompare against the builder's list. Delete or rewrite anything above M09 that you cannot confirm, especially pallet and probe calls.
- 5Dry run with the spindle disabledRun the program with the tool offset raised and feed override at 5% to 10%. Watch for stalls at M06 and clamp commands.
- 6Cut the first article and log the sequenceRecord which M codes triggered and how long each wait lasted. That log becomes the template for the next job on the same machine.
M code vs G code at a glance
The columns below separate what the control moves from what the control switches. Use it when you review a posted program.
| Item | G code | M code |
|---|---|---|
| Controls | Tool path and feed | Machine state |
| Typical example | G01 X50.0 F250 | M08 coolant on |
| Units affected | mm or inch | None |
| Where it appears | Inside the cut | Before or after the cut |
| Standard coverage | Broad ISO agreement | Builder-specific above M09 |
| Common fault | Wrong dimension | Stalled or crashed cycle |
When to edit M codes and when to leave them alone
If you are proving a new program on a machine you know, edit the M code sequence freely: reorder the spindle and coolant calls, add dwells, and tune the tool change position. If the program is already running to a qualified first article on a machine you do not own, do not touch the M codes. Change the geometry in CAM instead and let the post handle the machine state. Geometry errors scrap one part; a broken tool change sequence scraps the fixture and the spindle.
M code questions engineers ask
Is M code the same as G code?
No. G codes control the tool path: position, feed rate, arcs, drilling cycles. M codes control machine state: spindle, coolant, tool changer, clamps, program stop.
They share the same program lines. A drilled hole needs G81 for depth and feed, plus M08 and M09 for coolant timing.
Do all CNC machines use the same M codes?
Only a small set is close to universal: M03, M04, M05, M08, M09, M30. Above M09 the numbering is set by the machine builder, not by an international standard.
Ask for the builder's M code list before running a program on an unfamiliar machine. It is usually one page.
What does M30 do at the end of a program?
M30 ends the program and rewinds it to the first block, ready for the next cycle. M02 ends the program on older controls without the rewind.
On a production run, M30 saves the operator from manually resetting the file between cycles.
Can I edit M codes in a CAM post?
Yes, and that is the right place to do it. Post-processor edits apply to every part from that machine, so the change is consistent instead of hand-patched into individual programs.
Hand edits in the control are fine for proving a job. They get lost on the next post.
Why did my program stop at an M code?
Most likely the control is waiting for a signal that never arrived. A tool changer, a clamp or a door sensor did not confirm its position, so the block sat there.
Check the alarm text and the machine builder's M code list. A missing option, such as synchronized spindle for M29, also stops the program at that line.
Does M code choice affect part tolerance?
Indirectly, yes. Coolant timing changes surface finish, and a spindle dwell before the cut affects bore roundness on the first few millimeters.
The geometry comes from the G code, but the M code sequence decides whether the machine is stable when that geometry is cut.
Send us your drawings and we will review the program
Upload a STEP file and we return a quotation with a free DFM analysis within 12 hours. Our team runs 127 CNC machines across three plants, from one prototype to 10,000+ part runs, with 100% inspection before shipment.
12-hour quote100% inspectionNo minimum order quantityNDA on request