G & M Codes: The Secret Language of CNC Machines

Discover G and M codes in CNC programming. Learn how they control machine operations, improve precision, and simplify manufacturing.
Introduction
G and M codes are essential in CNC (Computer Numerical Control) programming, helping machines perform precise tasks and movements.
This guide explains what G and M codes are, why they matter, and how they are used in different industries.
What are G and M Codes?
G and M codes are the basic programming languages for CNC machines. The codes automate the processes by giving the CNC machine a standardized set of step-by-step instructions to follow without human intervention. This allows precise and repeatable cutting of complex 3D parts.
G codes control the machine's movements.
M codes manage extra functions, like turning the spindle or coolant on and off.
⠀These codes work together to ensure accurate and efficient machining operations.
Common G Codes List
G Code | Description |
---|---|
G00 | Rapid positioning |
G01 | Linear interpolation (controlled feed rate) |
G02 | Circular interpolation (clockwise) |
G03 | Circular interpolation (counterclockwise) |
G04 | Dwell (pause for a specified time) |
G17 | Select XY plane |
G18 | Select ZX plane |
G19 | Select YZ plane |
G20 | Input in inches |
G21 | Input in millimeters |
G28 | Return to home position |
G90 | Absolute programming |
G91 | Incremental programming |

Common M Codes List
M Code | Description |
---|---|
M00 | Program stop |
M01 | Optional stop |
M02 | Program end |
M03 | Spindle on (clockwise rotation) |
M04 | Spindle on (counterclockwise rotation) |
M05 | Spindle stop |
M06 | Tool change |
M08 | Coolant on |
M09 | Coolant off |
M30 | Program reset and end |
Key Difference Between G and M Codes
The main difference between G and M codes is their function.
G codes control how the machine moves, like the cutting path and speed.
M codes handle other machine functions, like turning the spindle or coolant on and off.
Why are G and M Codes Important for CNC Machining?
G and M codes are essential in CNC machining for several reasons:They give exact instructions, allowing for highly accurate manufacturing.
Precision: They provide precise instructions that enable highly accurate manufacturing.
Efficiency: Automating movements and operations reduces the need for manual intervention, speeding up production.
Flexibility: CNC programs can be easily modified to accommodate changes in design or specifications.
Repeatability: G and M codes ensure that parts can be produced with consistent quality across multiple runs.
How to Read and Write G & M Code Programs
Basic Structure of a CNC Program
A CNC program starts with a program number (e.g., N001), followed by G and M codes to control the machine's operations.
Each line represents one command, with the order of operations specifying tool movements, pauses, and setting changes.
Best Practices for Authoring Programs
Use Modular Macros
Create reusable subroutines for repetitive tasks to avoid writing the same code repeatedly.
Simulate First
Run your program in CAD/CAM software to catch errors before using the machine.
Test Gradually
Start with simple tests to check motions, then add complexity step by step.
Apply Descriptive Comments
Include clear comments to explain the purpose of each section, making the code easier to understand later.
Keep Code Neat
Use consistent formatting—like spacing and indentation—to make the code easier to read and troubleshoot.
Troubleshooting Common Issues with G & M Codes
Unexpected Tool Movements: These happen when G code commands or tool offsets are incorrect. Double-check your code and tool offset settings.
Tool Crashes: Tool crashes occur from incorrect tool paths or lack of clearance. Make sure the tool paths avoid obstacles and have enough space.
Inconsistent Cuts: Caused by incorrect feed rates, tool wear, or machine vibrations. Check the feed rate, inspect tools for wear, and ensure the machine is stable.
Error Messages: Pay attention to any error messages on the machine’s display and consult the manual for solutions.
Program Errors: Syntax issues like missing symbols or invalid commands can stop the program. Review the code carefully and use a code editor to spot errors.
Conclusion
G-codes and M-codes are important in CNC (Computer Numerical Control) machining. They allow CNC machines to efficiently manufacture complex metal parts through automated cutting and manufacturing according to the G and M code programs.
Precision CNC Machining Services at Unionfab
Unionfab is a leading manufacturing company that offers CNC machining services according to customer specifications and drawings. We utilize a variety of CNC machine tools, including 3 axis, 4 axis and 5 axis milling machines and lathes.
Contact us today to discuss your next CNC machining project and how we can help optimize your manufacturing process.