M-Codes & Auxiliary Functions in CNC Programming
M-codes — the miscellaneous or auxiliary functions — are the machine-side commands of a CNC program: the words that turn the spindle on and off, start and stop the coolant, change the tool, stop the program and declare its end. Where the G-codes of the language govern how the tool moves, the M-codes govern what the machine does around that movement, and a part is machined by the two working together — the G-codes directing the cut, the M-codes switching on the spindle that drives the cutter, the coolant that cools it and the functions that make the run safe and orderly. Every program this wiki describes carries M-codes as part of its structure, and this entry sets out the common ones and the part they play.
Motion and machine: the division of labour
The boundary between the two code families is clear and worth holding on to. G-codes prepare — they set the mode of motion and the state of the cut, telling the control how to interpret the coordinates that follow. M-codes switch — they operate the machine itself, the spindle motor, the coolant pump, the tool changer and the program’s own flow. A block that reads “feed to X50 Y25” is a G-code statement about where to move; a block that reads “spindle on” is an M-code statement about the machine state in which to move there. The two are so closely interleaved that a single program block can carry both — a G word directing the motion and an M word switching a function in the same line — but the mental division holds: G-codes shape the toolpath, M-codes run the machine. The machinist who reads a program fluently reads both at once, tracking the motion in the G-codes and the machine state in the M-codes.
The common M-codes
A small set of M-codes is common to virtually every control, and they are the ones a machinist meets daily. The program-flow codes control the run itself: M00 stops the program unconditionally, M01 stops it only when the operator’s optional-stop switch is on, M02 ends the program and M30 ends it and rewinds to the beginning, ready to run again. The spindle codes run the cutting rotation: M03 starts the spindle in the normal cutting direction for a right-hand tool, M04 reverses it and M05 stops it. The coolant codes switch the fluid: M08 turns the coolant on, M09 turns it off. The tool-change code, M06, makes the machine change to the tool named by the T word, and M19 orients the spindle to a fixed position — the function that a tool change or a probe needs. Around these core codes sit the machine’s own auxiliaries, and together they make the running state of the machine: the spindle turning, the coolant flowing, the tool in place, and the program stepping from block to block under the watch of the codes that can pause it.
The nature of the codes
The M-codes differ among themselves in a way that matters to reading and writing programs. Some are one-shot — they act in the block that carries them and are done: M06 performs the tool change and completes, M00 stops and waits, M30 ends the program. Others are latched — they switch a state on and leave it on until another code switches it off: M03 starts the spindle and it keeps turning through every following block until M05 stops it, just as M08 leaves the coolant running until M09 cancels it. Reading a program is largely tracking these latched states — the spindle and coolant set twenty blocks earlier are still on unless the program has turned them off — which is why the reading a block discipline of this group treats the machine state as part of every line’s meaning. The latched M-codes are the reason a program turns the spindle and coolant off deliberately at the end, and the one-shot codes are the reason the control panel shows the machine stopping, changing or rewinding at the moments the program intends.
The machine’s own M-codes
Beyond the common core, every machine has its own M-codes, and a machinist treats the machine’s manual as the authority for its particular list. A machine may define its own codes for the high-pressure coolant, the through-spindle air, the chip conveyor, the fourth-axis clamp or the pallet change, and two machines of the same kind can use the same M number for different functions. This is why a program is written for a machine, not for the abstract control: the G-code it generates is portable in its motion words, but its M-codes are tied to the iron that will run it. The practical rule is to read the machine’s manual before trusting any M-code that is not in the common set, and to check a new machine’s list when a proven program is moved to it — a program that ran one machining centre can stall, or worse, on another whose M-codes differ.
M-codes in the running program
The M-codes are placed in the program’s structure at the moments they belong, and their placing is part of the programming craft. The program opens by switching the machine to the state the job needs; it starts the spindle and the coolant just before the cutting blocks, and stops them just after; it uses M00 or M01 at the points where the operator must act — the check of a part between operations, the manual stage between tools — and it ends with M30, the spindle and coolant off and the program back at its beginning. A well-placed M-code makes a program safe and courteous to its operator: the optional stop that pauses the run only when the operator asks, the tool change at the change position, the coolant turned off before the tool retracts clear of the part. The M-codes are small words, but they are the machine’s voice in the program, and a program read with them in mind is a program whose machine the machinist can trust to do the right thing at the right time.