Home / All / What is G and M Code?

Table of Contents

What is G and M Code?

Shelly Yao

Ⅰ. Introduction

In CNC machining, machine tools rely on programs to control every step of their operation. These programs consist of a series of codes that instruct the machine when to move, where to cut, and how to process the material. Among these, G-code and M-code are the two most common and fundamental types of instructions. They act as “operational commands,” working together to control the machine tool in performing various machining operations. Understanding these two types of codes is the foundation for grasping the principles of CNC machining and achieving stable production.

Ⅱ. What is G-code for CNC Machines?

G-code is the programming language for CNC machines, also known as preparatory function codes, and is primarily used to control the machine’s motion.

Simply put, it is code that tells the machine “how to move,” such as:

  • Linear or circular motion
  • Rapid traverse or cutting feed
  • The path the tool should follow

Through G-code, the machine tool can perform various cutting operations along the set trajectory.

CNC machining process controlled by G-code, showing precision tool path programming for metal cutting and mechanical part manufacturing

Ⅲ. Common G-codes and Their Functions

The following is a list of common G-codes and their descriptions:

G Code Function Description
G00
Rapid Positioning
Moves the tool to the specified position at maximum speed without cutting; used for non-cutting movements
G01
Linear Interpolation
The tool cuts in a straight line at the set feed rate
G02
Clockwise Arc
G03
Counterclockwise Arc
G04
Pause/Delay
Program pauses for a specified duration
G17/G18/G19
Machining Plane Selection
Selects XY/ZX/YZ planes for arc machining
G28
Return to Reference Point
Tool automatically returns to the machine origin
G40/G41/G42
Tool Radius Compensation
Off / Left / Right
G43/G44
Tool Length Compensation
Positive / Negative
G90
Absolute Coordinate Programming
All coordinate values are relative to the workpiece origin
G91
Relative coordinate programming
All coordinate values are relative to the current position

Ⅳ. What are M Codes in CNC Machine Tools?

M codes are auxiliary function commands in CNC machine tools, primarily used to control various operational states of the machine.

Simply put, they tell the machine “when to enable or disable certain functions,” such as:

  • Spindle start or stop
  • Coolant on or off
  • Program pause or end

If G-codes determine “how the machine moves,” then M-codes determine “which auxiliary actions the machine performs.”

CNC machine M code programming interface showing machining control commands for precision mechanical processing

Ⅴ. Common M-codes and Their Functions

The following is a list of common M-codes and their descriptions:

M Code Function Description
M00
Program Stop
Stop spindle and feed
M03/M04
Spindle Start
Start spindle in clockwise/counterclockwise direction
M05
Spindle Stop
Stop spindle rotation
M06
Tool Change
Execute automatic tool change
M08/M09
Coolant On/Off
Turn coolant on/off
M30
Program End and Reset
End machining and return to program origin

Ⅵ. How G-Codes and M-Codes Work Together?

In CNC programming, G-codes and M-codes function like a coordinated pair of “actions” and “switches.” G-codes control the tool’s motion path and position, while M-codes control the machine tool’s operations.

The vast majority of systems follow an “M-code first, G-code second” execution sequence. First, M-codes activate auxiliary functions such as the spindle and coolant, followed by G-codes that control the tool’s cutting movements. After machining is complete, M-codes are used to perform final operations such as stopping the spindle and turning off the coolant, collectively ensuring efficient and stable automated machining.

Explanation of G-code and M-code in CNC machining, core programming language for precision mechanical processing and automated machine tool control

Ⅶ. Frequently Asked Questions (FAQ)

Q: How can I learn more about G-codes and M-codes?

A: There are two approaches. First, systematically study the definitions and execution rules of G-codes and M-codes by referring to the machine tool manual and documentation. Then, write test programs for simple parts to understand the interdependent logic of the commands through hands-on practice. Finally, use CNC simulation software to simulate machining operations and compare the combined application of the two types of codes.

 

Q:What categories of functional commands do G-codes and M-codes belong to within a CNC system?

A:G-codes are preparatory functional commands in a CNC system, used to define tool paths, machining cycles, and other motion-control-related instructions. M-codes are auxiliary functional commands in a CNC system, used to control machine tool switching operations, process control, and other non-motion-related operations.

 

Q:What is the main difference between G-codes and M-codes?

A: G-codes control how the tool moves, including its path and position, while M-codes function as control switches, such as turning the spindle on or off and activating the coolant system.

Ⅷ. Summary

G-codes and M-codes are the two core sets of instructions in CNC programming. G-codes enable precise machining of parts through commands such as linear interpolation and circular arc machining, while M-codes control the machine tool’s auxiliary movements and processes. By issuing commands to start and stop the spindle and activate or deactivate coolant, M-codes provide the necessary supporting conditions for cutting operations.

Working in tandem, these two sets of codes form the CNC machining command system, which is key to the efficient and stable production of precision parts.

Facebook
Twitter
LinkedIn

Choose Keywin for Quality

At Keywin, we pride ourselves on our precision and dedication to quality in CNC machining. Our expert team ensures every project meets the highest standards.
Stay Updated
Subscribe to our newsletter for the latest updates on our services and industry insights.
Stay Updated
Subscribe to our newsletter for the latest updates on our services and industry insights.
© 2024 Keywin. All rights reserved. Empowering your ideas with precision and quality.