← All guides
Difference between G-code and M-code guide - CNC programming screen showing G-code and M-code commands - CNC Dance tutorial

Difference Between G-Code and M-Code in CNC Programming Explained

Tutorials

If you are learning CNC programming one of the first questions you will ask is what is the difference between G-code and M-code. Both are programming languages used to control CNC machines but they serve different purposes. G-code prepares the control for geometric movements while M-code controls miscellaneous machine functions.

G-code also called geometric code tells the machine where to move, how fast to move, and what path to follow. M-code also called miscellaneous code controls functions like spindle on and off, coolant activation, tool changes, and program flow. They work together in the same program. A typical line of CNC code might contain both G-codes and M-codes such as G01 X1 Y1 F10 M08 which moves the tool in a straight line while turning on coolant.

This guide explains the difference between G-code and M-code in detail with examples of both and a quick reference table you can bookmark.

What Is G-Code

G-code is the primary programming language for CNC machines. It controls the geometry of the cut: where the tool goes, how fast it moves, and what type of motion to use. Each G-code command begins with the letter G followed by a number. G00 is rapid positioning, G01 is linear feed, G02 is clockwise arc, and G03 is counterclockwise arc.

G-codes are modal which means they stay active until another command in the same group replaces them. When you call G01 all subsequent moves are linear feed moves until you call G00, G02, or G03. This is why you do not need to repeat G01 before every feed move in the program. You call it once at the beginning of the cutting section and all XY positions that follow are executed as linear feed moves.

G-codes are organized into groups and only one code from each group can be active at a time. The motion group includes G00 G01 G02 and G03. The work offset group includes G54 G55 G56 G57 G58 and G59. Selecting a new code from the same group automatically cancels the previous one.

Common G-Code Commands

G00 is rapid positioning which moves the tool at the machine maximum speed. Use G00 for moving the tool to a starting position or retracting from the part. Do not cut material with G00 because the feed rate is uncontrolled.

G01 is linear interpolation or straight line feed. The tool moves in a straight line at the programmed feed rate F. Use G01 for all cutting moves including profiling, slotting, and facing. The format is G01 X Y Z F where X Y Z are the endpoint coordinates and F is the feed rate in inches per minute.

G02 and G03 are circular interpolation for cutting arcs and circles. G02 moves clockwise and G03 moves counterclockwise. Use them for radiused corners, circular pockets, full circles, and helical ramps. The arc can be programmed using the R method for radius or the IJK method for center point offsets.

G17 G18 and G19 select the plane for circular interpolation. G17 selects the XY plane which is the default for milling machines. G18 selects the XZ plane and G19 selects the YZ plane.

G20 and G21 select inches or millimeters as the unit of measurement. G20 sets inches and G21 sets millimeters. Most programs use one unit throughout and call the appropriate code at the beginning.

G40 G41 and G42 control cutter compensation. G41 offsets the tool left of the programmed path and G42 offsets right. G40 cancels compensation. Cutter compensation allows you to program the part geometry directly while the control adjusts for the actual tool diameter.

G43 and G49 control tool length offset. G43 activates the tool length offset stored in the H register. G49 cancels it. Tool length offset compensates for different tool lengths so the Z axis position is correct for each tool.

G54 through G59 are work offset registers that tell the machine where the part is located on the table. Each register stores a coordinate shift from machine home to part zero.

G90 and G91 select absolute or incremental positioning. G90 uses absolute coordinates measured from part zero. G91 uses incremental coordinates measured from the current tool position. Most programs use G90 for the entire program.

What Is M-Code

M-code controls miscellaneous machine functions that are not related to tool movement. These include spindle control, coolant control, tool changes, and program flow. M-codes are also modal and stay active until another code in the same group cancels them.

The most common M-codes are M03 spindle on forward, M04 spindle on reverse, and M05 spindle stop. M03 starts the spindle rotating clockwise at the speed set by the S code. M04 starts the spindle counterclockwise. M05 stops the spindle.

M06 is the tool change command. It tells the machine to stop the spindle, retract to the tool change position, exchange the tool in the spindle with the tool selected by the T code, and resume operation. The format is T01 M06 which changes to tool number 1.

M07 and M08 turn on coolant. M07 turns on mist coolant and M08 turns on flood coolant. M09 turns off all coolant. These are typically called at the beginning of a program and cancelled at the end.

M00 and M01 are program stop commands. M00 stops the program unconditionally until the operator presses start. M01 stops the program only if optional stop mode is enabled on the control. Use M00 for operator inspection between operations and M01 for optional stops during setup.

M02 and M30 end the program. M02 stops the program and resets to the beginning. M30 stops the program and rewinds to the beginning which is the standard for most production work. M30 also typically resets the spindle speed and feed rate to default values.

M98 and M99 control subprograms. M98 calls a subprogram and M99 returns from a subprogram to the main program. Subprograms allow you to reuse common sequences of code without rewriting them.

How G-Code and M-Code Work Together

G-code and M-code work together in the same program. A typical line of code might contain both G-codes and M-codes. The control processes all commands on the same line before moving to the next line.

Here is a typical program section showing both G-codes and M-codes:

N10 G90 G54 G00 X0 Y0 (G-codes for absolute mode, work offset, rapid) N20 S5000 M03 (S-code for speed, M-code for spindle on) N30 G43 H01 Z1 M08 (G-code for tool length, M-code for coolant on) N40 G01 Z-0.25 F50 (G-code for linear feed) N50 X1 Y1 F30 (G-codes for position and feed rate) N60 G00 Z1 (G-code for rapid retract) N70 M05 (M-code for spindle stop) N80 M09 (M-code for coolant off) N90 M30 (M-code for program end)

Lines N10 through N50 contain G-codes that control tool motion and positioning. Lines N20, N30, N70, N80, and N90 contain M-codes that control machine functions. Both types of codes are essential for a complete CNC program.

Quick Reference Table

G-Code Function M-Code Function
G00 Rapid positioning M00 Program stop
G01 Linear feed M01 Optional stop
G02 CW arc M02 Program end
G03 CCW arc M03 Spindle CW
G17 XY plane M04 Spindle CCW
G18 XZ plane M05 Spindle stop
G19 YZ plane M06 Tool change
G20 Inches M07 Mist coolant
G21 Millimeters M08 Flood coolant
G40 Cancel cutter comp M09 Coolant off
G41 Cutter comp left M30 Program end and rewind
G42 Cutter comp right M98 Call subprogram
G43 Tool length + M99 Return from subprogram
G49 Cancel tool length
G54-G59 Work offsets
G90 Absolute mode
G91 Incremental mode
G98 Retract to initial Z
G99 Retract to R plane

Key Differences

The main difference between G-code and M-code is their purpose. G-code controls tool motion and positioning. M-code controls machine functions that are not directly related to motion.

G-codes operate in the X Y Z coordinate system. They tell the machine where to go and how to get there. M-codes operate on machine components like the spindle coolant system and tool changer. They do not have coordinate values.

G-codes are organized into modal groups that control specific aspects of motion. Selecting a new G-code from the same group cancels the previous one. M-codes are also modal but the groups are different. M03 spindle on is cancelled by M05 spindle stop not by another M-code from a different group.

G-codes are more numerous than M-codes. There are hundreds of different G-codes defined in the ISO standard although most practical programs use only about 15 to 20 of them on a regular basis. M-codes have fewer total commands available and most programs use about 10 to 15 different M-codes on a regular basis.

Both G-codes and M-codes can be placed on the same line of code. The control executes them simultaneously when possible. For example G01 X1 F10 M08 starts a linear feed move and turns on coolant at the same time. This simultaneous execution makes CNC programming efficient and compact.

Which One to Learn First

If you are new to CNC programming start with G-code. Understanding how the machine moves and how to control that movement is the foundation of all CNC work. Learn G00 G01 G02 G03 G90 G91 and G54 through G59 first. These seven codes cover about 90 percent of what you need for basic programming.

After you understand motion control add M-codes for spindle and coolant control. M03 M05 M08 M09 and M30 are the most important. Then add M06 for tool changes and M98 M99 for subprograms as your programming becomes more advanced.

Most CAM software generates both G-codes and M-codes automatically. But understanding what each code does is essential for troubleshooting programs at the machine. When a program does something unexpected being able to read the code and understand which command is causing the problem saves hours of debugging time.

Practical Example

Here is a complete program that uses both G-codes and M-codes to drill four holes. This example shows how the two code types work together:

O1000 (DRILL FOUR HOLES) N10 G90 G54 G00 X0 Y0 (absolute mode, work offset, rapid position) N20 S3000 M03 (spindle speed 3000 RPM, spindle on CW) N30 G43 H01 Z1 M08 (tool length offset, retract height, coolant on) N40 G81 X1 Y1 Z-0.5 R0.1 F10 (drill cycle first hole) N50 X-1 Y1 (drill second hole) N60 X-1 Y-1 (drill third hole) N70 X1 Y-1 (drill fourth hole) N80 G80 G00 Z1 (cancel cycle, retract) N90 M05 (spindle off) N100 M09 (coolant off) N110 M30 (program end)

In this program G90 sets absolute mode which is a G-code. G54 selects work offset which is also a G-code. M03 starts the spindle which is an M-code. M08 turns on coolant which is another M-code. The program uses eight different G-codes and four different M-codes to complete the operation.

If you were to write this same program without canned cycles and without M-codes you would need about three times as many lines. The combination of G-codes for motion control and M-codes for machine functions makes CNC programs compact and efficient.

Common Misunderstandings

One common misunderstanding is that G-codes are more important than M-codes. Both are essential for a complete CNC program. A program with perfect G-code but no M-codes will not work because the spindle will not start and coolant will not flow. A program with correct M-codes but wrong G-code will cut in the wrong position and scrap the part.

Another misunderstanding is that all G-codes and M-codes are the same across all machines. While most basic codes like G00 G01 G90 M03 and M08 are standardized across all controls some codes vary between manufacturers. Haas uses different M-codes for some functions than Fanuc. Always check the programming manual for your specific machine control before using advanced codes.

Some beginners think they need to memorize every G-code and M-code before they can start programming. This is not true. You only need about 10 G-codes and 8 M-codes to write most programs. The rest you can look up as needed. Focus on learning the common codes first and add advanced codes when you need them for specific operations. When you do need to learn a new code look it up in the machine manual or in our Complete G-Code List reference.

G-codes and M-codes can also be used in combination with other programming features like variables, macros, and conditional statements. These advanced features are called parametric programming or macro programming. They allow you to write programs that can machine different part sizes by changing variable values instead of rewriting the entire program.

For example a macro program for drilling a bolt hole circle might use variables for the circle diameter, number of holes, and starting angle. The G-code uses these variables to calculate each hole position. The M-codes handle spindle control and coolant. The same program can drill a 4-hole pattern on a 2-inch circle or an 8-hole pattern on a 6-inch circle by changing only the variable values. These advanced features are available on most modern controls and allow you to write parametric programs that can machine different part sizes without modification.

Summary

G-code controls geometry and motion. It tells the machine where to move and what path to follow. M-code controls miscellaneous machine functions. It tells the machine to start the spindle, turn on coolant, change tools, and end the program. Both are essential for CNC programming and they work together in every single program you will ever write or run on a CNC machine.

Understanding the difference between G-code and M-code is one of the first steps in learning CNC programming. It is also one of the most useful because it helps you read any CNC program with confidence. Once you understand which code does what you can read any CNC program and understand what the machine will do. You will be able to identify which lines control motion, which lines control the spindle, which lines handle coolant, and where the program ends. This makes you a more effective programmer and a better troubleshooter at the machine. This skill is essential for troubleshooting programs at the machine and for making quick edits without returning to the CAM software.

For more CNC programming guides see our [G-Code for Beginners](

G90 G54 G00 X0 Y0
G01 Z-0.1 F10
G01 X1.0 F20
M30

/posts/g-code-for-beginners/) guide, our Common G-Code Mistakes guide, and our Complete G-Code List reference.

Tags:#g-code#m-code#programming#beginner#reference