Learning to program a CNC machine can feel overwhelming at first. You're staring at lines of code, trying to figure out what each command does, and hoping you don't crash a spindle into the workpiece. One of the fastest ways to build confidence is to memorize the common M-codes every beginner CNC machinist should memorize early on. These codes control the machine's auxiliary functions things like turning the spindle on, activating coolant, and stopping the program. Without a solid grasp of M-codes, you'll struggle to write or even read basic CNC programs. This guide breaks down the essential M-codes you need to know, explains what each one does, and gives you practical tips so you can use them correctly from day one.
What Are M-Codes and How Are They Different from G-Codes?
M-codes are machine-level commands in CNC programming that control functions outside of tool movement. While G-codes handle motion feed rates, rapid moves, arcs, and positioning M-codes handle everything else. Think of G-codes as "where and how the tool moves" and M-codes as "what the machine does while or after the tool moves."
Common M-code functions include starting and stopping the spindle, turning coolant on and off, calling a tool change, and ending a program. If you want a deeper breakdown of how these two types of commands compare, check out our explanation of the difference between G-code and M-code commands.
Why Should Beginners Memorize M-Codes Instead of Just Looking Them Up?
Looking up every M-code in a reference table slows you down, especially when you're proving out a program at the machine. When you have the most common M-codes memorized, you can read through a program faster, spot errors before they cause problems, and make edits at the control without second-guessing yourself. Speed and accuracy at the machine both improve when these codes are second nature.
Memorizing M-codes also helps you understand what's happening during each line of a program. If the spindle isn't turning when it should, you'll know to check for M03 or M05 instead of hunting through the code blindly. That kind of confidence comes from repetition and practice, not from a laminated cheat sheet taped to the control panel (though that helps too).
What Are the Most Common M-Codes Every Beginner CNC Machinist Should Know?
Here are the M-codes you'll encounter most often on a typical 3-axis CNC mill or lathe. Different machine builders may define some M-codes differently, so always check your machine's programming manual. But these are the standards used across most Fanuc, Haas, and similar controllers.
M00 – Program Stop
M00 stops the program completely. The spindle stops, coolant turns off, and the machine waits for the operator to press the cycle start button to continue. Use it when you need to check a part, flip a workpiece, or perform a manual operation mid-program.
M01 – Optional Stop
M01 works like M00, but only if the operator has turned on the "Optional Stop" switch at the control. This is useful during production runs you can leave it in the program for setup and troubleshooting, then skip it during full production by keeping the switch off.
M02 – End of Program
M02 ends the program and does not reset to the beginning. On older machines, this was the standard way to finish a program. Some newer controllers treat it the same as M30, but it's worth knowing the distinction.
M03 – Spindle On (Clockwise)
M03 starts the spindle rotating clockwise, which is the normal direction for most cutting operations. It's almost always paired with an S word to set the spindle speed, like M03 S1200 (spindle on, clockwise, at 1200 RPM).
M04 – Spindle On (Counter-Clockwise)
M04 starts the spindle in reverse. You'll use this less often, but it's needed for left-hand threading, certain boring operations, and some specialty tooling.
M05 – Spindle Stop
M05 stops the spindle. You'll use this before a tool change, before measuring a part, or at the end of a machining cycle. Always make sure the spindle has fully stopped before performing any manual operations near the workpiece.
M06 – Tool Change
M06 commands the machine to execute a tool change. On a machine with an automatic tool changer (ATC), this swaps the current tool for the one called in the T word. For example, T03 M06 loads tool number 3 into the spindle.
M08 – Flood Coolant On
M08 turns on the flood coolant system. Most milling operations require coolant to control heat, clear chips, and extend tool life. If your machine has both flood and mist coolant options, M08 typically refers to the flood setting.
M09 – Coolant Off
M09 shuts off all coolant. You'll include this before a tool change or at the end of a program so coolant isn't spraying while the operator opens the door or handles the part.
For a more complete list of M-codes, including ones used on multi-axis and more advanced machines, see our guide to advanced M-code functions for multi-axis CNC machines.
M30 – End of Program and Rewind
M30 is the most common way to end a CNC program. It stops the machine, turns off coolant and spindle, and rewinds the program back to the beginning so it's ready to run again. On most modern controllers, this is your go-to end-of-program command.
How Do M-Codes Appear in a Real CNC Program?
Seeing M-codes in context helps make them click. Here's a short example of a typical CNC milling program using the M-codes listed above:
O1001– Program numberT01 M06– Load tool 1M03 S2500– Spindle on, clockwise, 2500 RPMG00 X1.0 Y1.0– Rapid move to positionG01 Z-0.5 F20.0– Feed into the partM08– Coolant on(...cutting moves...)M09– Coolant offG00 Z1.0– RetractM05– Spindle stopM30– End program, rewind
If you want a refresher on how to read and interpret M-codes line by line, our walkthrough on how to read M-codes in CNC programming covers that in detail.
What Common Mistakes Do Beginners Make with M-Codes?
One frequent mistake is forgetting to include M03 before a cutting move. The machine will happily plunge a tool into the workpiece without the spindle running. This damages the tool, the part, and potentially the spindle.
Another mistake is using M02 instead of M30 at the end of a program. On some controllers, M02 doesn't rewind the program, which means the next time the operator hits cycle start, nothing happens or the program picks up from the wrong line.
Beginners also sometimes place M06 (tool change) without first commanding M05 (spindle stop). Most modern controllers handle this automatically, but older machines may not, and it's safer to be explicit in your code. Writing clean, predictable code protects you and the machine.
Lastly, don't assume every machine uses the same M-code for the same function. A M-codes reference chart specific to your machine builder is something you should always have access to. Fanuc, Haas, Siemens, and Mazak all have slight variations.
What Tips Help You Learn M-Codes Faster?
- Practice reading real programs. Pull up posted code from CAM software or from your shop's existing programs and trace through each M-code by hand.
- Write simple programs by hand. Even a basic facing operation with five or ten lines of code forces you to use M-codes actively instead of passively reading them.
- Use the single-block mode on the machine. This lets you step through a program one line at a time, which is one of the best ways to see M-codes fire in real time.
- Keep a personal M-code reference card near your machine until the codes become automatic. Repetition over a few weeks will commit them to memory naturally.
- Ask experienced machinists which M-codes they use most on your specific machine. Shop-floor knowledge fills in the gaps that manuals don't always cover.
Do All CNC Machines Use the Same M-Codes?
No, and this is a detail many beginners overlook. The M-codes covered in this article M00 through M30 are widely standardized, especially on machines running Fanuc-based controls (which includes Haas and many others). But machine builders often add custom M-codes for features specific to their equipment. For example, a bar feeder on a CNC lathe might use M50 or M51, while a probing cycle on a mill might use M19 for spindle orient. Always verify against your machine's programming manual before using an unfamiliar M-code in a program.
Even within the same shop, two machines from different builders might behave slightly differently with the same M-code. When in doubt, dry-run the program with the spindle off and the tool removed to see what happens.
Quick-Reference M-Code Checklist for Beginners
- M00 – Program stop (waits for cycle start)
- M01 – Optional stop (only if switch is on)
- M02 – End of program (no rewind on some controls)
- M03 – Spindle on, clockwise
- M04 – Spindle on, counter-clockwise
- M05 – Spindle stop
- M06 – Tool change
- M08 – Flood coolant on
- M09 – Coolant off
- M30 – End of program and rewind
Print this list and keep it next to your machine's control panel. Practice reading and writing these codes daily. Within a couple of weeks, you won't need the list anymore and that's when you know you've built a real foundation for everything else in CNC programming. Style your reference sheets or training materials with a clean techno font to make them easy to read at a glance on the shop floor.
Difference Between G-Code and M-Code Commands Explained | G-Code Tutorial
How to Read M-Codes in Cnc Programming: a Complete Beginner's Guide
Advanced M-Code Functions for Multi-a
M-Code Reference Chart for Grbl Controller Setup and Configuration
Barcode Maker Code Standards Compliance for Small Business Inventory Management
Simple Arduino Sensor Code Snippets for Beginners