G and M codes are two file formats integral to CNC, as well as other manufacturing methods like 3D printing. Most people may only have heard of G-code, but M-codes are equally as important. Without both, neither could perform the tasks required to accurately machine parts, but each perform very different functions.
Whereas the G-code activates the actual numerical control (NC) part of the CNC machine or CNC router, the M-code activates the PLC (programmable logic controller) of the machine.
Both G codes and M codes affect the CNC machining process, but control different aspects:
The “G” references geometry, controlling where the machine should move, co-ordinate-wise, to accurately form the part. It affects the actual design and machining of the part, telling the machine where to move to fabricate the part.
Instructions written in G-code include where and how to move, where to start, where to stop, or how quickly to move the spindle.
G codes comprise a number of other letters, and while the G part of the code tells the machine when to move and stop, other letters like X, Y and Z inform the machine of where to move within each of these three cartesian dimensions, and S denotes the speed of the spindle. There are more than a dozen other letters with other functions.
The “M” references miscellaneous or machine code, and covers all the major instructions not covered by the G-code. Rather than geometric alphanumeric movements, M-codes instruct the CNC machine to start and stop certain actions or programs outside the G-code’s domain.
These instructions could include when to use coolant, for example when the machine reaches a certain temperature, and turn off when the machine again reaches a certain lower temperature. Other examples include when open the machine doors, to change the spindle’s direction of spinning, or to change tools.
CAD-CAM software and generating G-code and M-code
Rather than entering software code by hand, CAM software can both design and prepare your eventual CNC-machined part for production and produce these files. Based on your design and CAD file, as well as your desired settings, this CNC software can generate your codes for machining.
Some experts may want to inspect and modify codes if they believe this could lead to a better quality part, but hobbyists and beginners will prefer to proceed with the software-generated codes.
For more information, we also have a guide to the best CNC software.