G-code and M-code
What is G-code?
G-code, or Geometric Code, is the standard programming language used to control CNC (Computer Numerical Control) machines. It plays a crucial role in automated manufacturing by directing machines on how to perform specific tasks such as cutting, drilling, milling, and more. A G-code program is composed of a series of commands, each written as a line of code, that tells the machine where to move, how fast to go, and what operations to execute.
For example, a command might instruct the machine to move its tool to a particular coordinate, start or stop the spindle, or change to a different tool. These commands allow for precise and repeatable movements, making G-code essential for producing accurate parts in industries like aerospace, automotive, and manufacturing. Understanding and writing G-code is fundamental for CNC programmers and operators, as it enables them to translate digital designs into physical components with a high degree of accuracy and consistency.
Why Use G-code?
CNC machines cannot operate independently—they require clear, detailed instructions to perform tasks accurately. G-code serves as the language that delivers these instructions, enabling CNC machines to automate and standardize manufacturing processes with high precision and repeatability. It translates digital designs, often created in CAD/CAM software, into specific machine actions such as movement, cutting, drilling, and tool changes. This conversion allows complex parts to be manufactured consistently and with tight tolerances. By using G-code, manufacturers can reduce the need for manual labor, streamline production, and maintain quality across large batches. Its use is essential for modern, automated manufacturing environments.
G-Code
G-Code | Description |
|---|---|
| G00 | Rapid traverse |
| G01 | Linear interpolation |
| G02 | Circular interpolation CW |
| G03 | Circular interpolation CCW |
| G04 | Dwell |
| G17 | X Y plane selection |
| G18 | Z X plane selection |
| G19 | Y Z plane selection |
| G28 | Return to reference position |
| G30 | 2nd, 3rd and 4th reference position return |
| G40 | Cutter compensation cancel |
| G41 | Cutter compensation left |
| G42 | Cutter compensation right |
| G43 | Tool length compensation + direction |
| G44 | Tool length compensation – direction |
| G49 | Tool length compensation cancel |
| G53 | Machine coordinate system selection |
| G54 | Workpiece coordinate system 1 selection |
| G55 | Workpiece coordinate system 2 selection |
| G56 | Workpiece coordinate system 3 selection |
| G57 | Workpiece coordinate system 4 selection |
| G58 | Workpiece coordinate system 5 selection |
| G59 | Workpiece coordinate system 6 selection |
| G68 | Coordinate rotation |
| G69 | Coordinate rotation cancel |
| G73 | Peck drilling cycle |
| G74 | Left-spiral cutting circle |
| G76 | Fine boring cycle |
| G80 | Canned cycle cancel |
| G81 | Drilling cycle, spot boring cycle |
| G82 | Drilling cycle or counter boring cycle |
| G83 | Peck drilling cycle |
| G84 | Tapping cycle |
| G85 | Boring cycle |
| G86 | Boring cycle |
| G87 | Back boring cycle |
| G88 | Boring cycle |
| G89 | Boring cycle |
| G90 | Absolute command |
| G91 | Increment command |
| G92 | Setting for work coordinate system or clamp at maximum spindle speed |
| G98 | Return to initial point in canned cycle |
| G99 | Return to R point in canned cycle |
Applications of G-code
G-code is widely used across various industries for precise and automated machining:
Manufacturing : Milling, turning, cutting, and drilling of parts
Aerospace : Precision machining of turbine blades, airframes, and structural components
Automotive : Production of engine blocks, transmission parts, and molds
Medical : Manufacturing surgical instruments, implants, and prosthetics
Electronics : PCB drilling, creation of enclosures, and electronic housings
Jewelry : Engraving and producing intricate patterns and detailed designs
3D Printing : Used to generate toolpaths for additive manufacturing processes
Benefits of Using G-code
G-code provides several advantages in CNC machining:
Precision : Enables extremely accurate control of machine movements and cuts
Repeatability : Allows the same program to be reused for identical, consistent parts
Automation : Reduces human labor by automating complex manufacturing tasks
Flexibility : Can be used to create a wide variety of shapes and geometries
Compatibility : Works across many types of CNC machines (mills, lathes, routers, etc.)
Debugging and Editing : Code can be easily read and modified for corrections or improvements
Limitations of G-code
While powerful, G-code has some limitations:
Low-Level Language : Not intuitive; often requires manual coding or CAM software
Lacks Intelligence : Executes fixed instructions without real-time decision-making
Dependent on CAD/CAM : Complex shapes require design and conversion from external software
Machine-Specific Syntax : Syntax may vary between machine brands (e.g., Fanuc vs. Haas)
Learning Curve : Can be challenging for beginners to learn and understand fully
What is M-code?
M-code, or Miscellaneous Code, is a critical part of CNC programming used to manage the non-motion functions of a CNC machine. While G-codes control movement—such as cutting paths, drilling, or positioning—M-codes handle auxiliary operations that support and complete the machining process. These operations include turning the spindle on or off, activating or stopping coolant flow, changing tools, opening or closing machine doors, and pausing or ending the program. M-codes are essential for maintaining a smooth and safe machining workflow, ensuring that every supporting action occurs at the correct time in coordination with tool movements. Without M-codes, a CNC machine could not perform its tasks effectively or safely, as critical steps such as tool changes or coolant application would be missed. Together with G-codes, M-codes create a complete set of instructions that allow CNC machines to operate autonomously with high precision, consistency, and reliability in manufacturing environments.
Why Use M-code?
M-codes are essential for managing a CNC machine’s support functions, ensuring programs run safely and efficiently. They control critical machine operations that complement the movement commands of G-code.
Key Reasons to Use M-code:
Machine Control: M-codes start and stop the spindle (M03, M05), switch coolant on or off (M08, M09), and handle tool changes (M06), all vital for proper machining.
Program Flow Control: Commands like M00 (program stop), M01 (optional stop), and M30 (program end and reset) help control pauses and completion, enhancing safety and control.
Automation Efficiency: Automating these support tasks reduces manual intervention, boosting productivity and consistency.
Safety and Precision: M-codes ensure critical steps—such as stopping the spindle before a tool change—are performed in the right order, preventing damage and injury.
| M-code | Description |
|---|---|
| M00 | Program Stop |
| M01 | Optional Stop |
| M02 | End of Program |
| M03 | Spindle On (Clockwise) |
| M04 | Spindle On (Counterclockwise) |
| M05 | Spindle Stop |
| M06 | Tool Change |
| M07 | Mist Coolant On |
| M08 | Flood Coolant On |
| M09 | Coolant Off |
| M10 | Chuck or Clamp On |
| M11 | Chuck or Clamp Off |
| M30 | End of Program & Rewind |
| M41 | Low Gear (Lathe) |
| M42 | Medium Gear (Lathe) |
| M43 | High Gear (Lathe) |
| M48 | Feed Rate Override Enable |
| M49 | Feed Rate Override Disable |
Applications of M-code
M-codes are used in CNC programming to control a variety of machine functions beyond tool movement, playing a crucial role in ensuring efficient, safe, and automated machining processes. Some common applications include:
Spindle Control : Turning the spindle on and off (
M03,M04,M05) to start and stop cutting operations or change rotation directions depending on the machining task.Tool Changes : Initiating tool changes automatically (
M06), which is essential in multi-tool CNC machines for performing various machining operations without manual intervention.Coolant Control : Managing the flow of coolant (
M08to turn on,M09to turn off) to keep cutting tools cool, reduce friction, and improve surface finish and tool life.Program Flow Management : Pausing the machine at specified points (
M00,M01) for operator checks or adjustments, and ending or restarting programs (M02,M30).Safety and Maintenance : Stopping all machine motion safely during emergency stops or maintenance checks, ensuring operator safety and preventing machine damage.
Auxiliary Machine Functions : Controlling other machine-specific operations such as turning on/off lights, activating clamping devices, or operating chip conveyors, depending on the CNC machine’s capabilities.
Benefits of Using M-code
Automation of Machine Functions
M-codes automate critical machine operations like spindle start/stop, tool changes, and coolant control, reducing the need for manual intervention and speeding up production.Improved Safety
By controlling operations such as emergency stops, spindle stops, and program pauses, M-codes help protect operators and prevent machine damage.Enhanced Precision and Consistency
Ensuring that machine auxiliary functions occur exactly when needed helps maintain consistent machining conditions and high-quality output.Simplified Program Control
M-codes allow easy management of program flow with commands to pause (M00), optionally stop (M01), or end and reset the program (M30), making the CNC program easier to operate and troubleshoot.Better Tool and Machine Management
Automated tool changes (M06) minimize downtime and human error, optimizing the machining process and tool usage.Supports Complex Multi-Tasking
M-codes enable the CNC machine to handle multiple functions simultaneously (e.g., turning spindle on while coolant flows), improving workflow efficiency.
Limitations of M-code
- Machine-Specific Variations
M-code commands can vary between different CNC machine brands and controllers, meaning that code written for one machine might not work exactly on another without modification. - Limited Function Scope
M-codes primarily control auxiliary machine functions but do not handle complex machining movements or logic, so they must be used alongside G-code for full CNC operation. - Lack of Standardization
Unlike G-code, which has more universal standards, M-codes can differ widely, causing compatibility issues when transferring programs between machines. - No Real-Time Decision Making
M-codes execute predefined commands without feedback or adaptation; they can’t adjust operations dynamically based on sensor input or changing conditions. - Dependence on Proper Sequencing
Improper or out-of-sequence M-code commands can lead to machine errors, unsafe conditions, or tool damage, requiring careful programming and verification. - Not User-Friendly
Understanding when and how to use specific M-codes requires training and experience, especially since improper use can halt production or cause machine faults.
How G-code and M-code Work in a CNC Program
G-code + M-code = Complete CNC Control
In a CNC program, G-codes control the movement of the machine tool, while M-codes control machine operations and functions (like spindle, coolant, and tool changes). Together, they form a complete set of instructions for automated machining.
How They Work Together: Step-by-Step
Setup Phase
The program begins with setup instructions:
Units (
G20for inches orG21for mm)Positioning mode (
G90for absolute orG91for incremental)Tool selection (
M06)Spindle and coolant (
M03,M08)
Movement Commands (G-code)
G-codes direct the machine where and how to move:
G00: Rapid positioning (move without cutting)G01: Linear cutting movement (with feed rate)G02/G03: Circular interpolation (clockwise or counterclockwise arcs)
Machine Control Commands (M-code)
M-codes are used throughout the program to control non-motion functions:
Start/stop spindle:
M03/M05Turn coolant on/off:
M08/M09Tool change:
M06Stop program:
M00,M30
Ending the Program
M-code ends the program and resets the machine:
M30: End of program and rewind
Example Program Using G and M Codes
G21 ; Set units to millimeters
G90 ; Use absolute positioning
M06 T01 ; Tool change to tool 1
M03 S1200 ; Start spindle clockwise at 1200 RPM
M08 ; Turn on coolant
G00 X0 Y0 ; Rapid move to start point
G01 X50 Y0 F100 ; Linear cut to X50 at 100 mm/min
G01 X50 Y50 ; Linear cut to Y50
G01 X0 Y50 ; Linear cut to X0
G01 X0 Y0 ; Return to start
M09 ; Coolant off
M05 ; Spindle stop
M30 ; End of program and reset
Related posts:
- 5-Axis Machining vs 3-Axis Machining – Essential Differences, Applications & Limitations
- Electrical Discharge Machining (EDM): Top 3 Types, Key Principles & Industries That Rely on It
- Laser Cutting vs. Waterjet Cutting: 7 Key Differences, Pros & Best Uses
- 3D Printing vs Machining: Powerful Comparison of Benefits, Limitations, and Applications
- How to Read Machining Drawings: 11 Essential Tips for Beginner
- 8 Common Mistakes in Machining and How to Avoid Them

