Introduction to G-Code and M-Code in CNC Programming | G-Codes & M-Codes for CNC Turning & Milling | CNCLATHING



The G-code and M-code in CNC programming is for product designing and machines motion, to ensure exact shapes and dimensions be manufactured. If you want to learn CNC programs, you need to understand the meaning of each G-code and M-code for CNC turning and milling. Here is the introduction to the definition, difference and list of G-code and M-code, as well as how they work and write.

What is G-Code and M-Code in CNC Programming?

G-code, which is a kind of function used for CNC machining, refers to a list of preparatory codes begin with the letter “G” in CNC programming language, to position a tool and give commands. G-codes need to work with other codes, like M-code, for completing the desired tasks. 


M-code, the codes begin with the letter “M”, is a set of auxiliary command used for Miscellaneous function, combine with G-code to turn on or turn off various machine functions, such as coolant on/off, spindle speed on/off, tool change, open workstation door, and more.


What’s the Difference Between G-code and M-code of CNC Machine?

1)G (geometric) codes are the instruction that related to the motion or positioning of the tool relative to the workpiece, usually used in CNC turning for producing cylinder surfaces or CNC milling to fabricate profile of rectangular plates.
M (miscellaneous) codes are command for machine functions like speed, coolant, etc.
2)G-code is based on CNC part or product design, M-code is mainly related to switch machine off/on.
3)G-code activates the numerically controlled machine, M-code activates the PLC of the machine.


How to Write the G-Code and More Introductions

Different codes may write in vary formats. Here we take G00 and G01 as examples to introduce how to write it and what to note.
G00-Rapid positioning
Format: G00 X(U)Z(W)
(1) This command enables the tool to move quickly to the specified position according to the point position control mode. The workpiece can’t be processed during moving.
(2) All the programmed axes move at the speed defined by the parameters at the same time. When one axis finishes the programmed value, it stops, while the other axes continue to move.
(3) Coordinates that do not move don’t need to be programmed.
(4) G00 can be written as G0. Example: G00 X75 Z200

G01-Linear interpolation
Format: G01 X(U)Z(W)__F(mm/min)
(1) This command causes the tool to move to the specified position in line interpolation mode. Movement speed is commanded by F(Feed rate). All coordinates can be operated in linkage.
(2) G01 can also be written as G1. Example: G01 X40 Z20 F150


List of G-Codes & M-Codes for CNC Turning & CNC Milling

Part of g-codes and m-codes are the same meaning in CNC turning and milling, but other units are varying.
List of same G-codes for CNC turning and milling
G00: Rapid positioning
G01: Linear interpolation
G02: Circular/Helical Interpolation (clockwise)
G03: Circular/Helical Interpolation (counterclockwise)
G17: X-Y plane selection
G18: X-Z plane selection
G19: Y-Z plane selection
G20: Programming in inches
G21: Programming in mm

List of most common M-codes and meanings
M0: Program stop (press Cycle Start to continue)
M1: Optional stop (only executed if the switch on the CNC control is ON)
M2: End of program
M3: Spindle on clockwise
M4: Spindle on counterclockwise
M5: Spindle stop
M6: Change tool
M8: Coolant on
M9: Coolant off
M30: End program and press Cycle Start to run it again


You can check the full list of G-code and M-code for CNC lathe and Mill in FANUC/Haas on our site and download the file.