Starting with CNC programming can feel overwhelming. Many online guides use complex language that makes basic code hard to understand. However, you only need to master a few core concepts to get started.
This guide explains the basic functions of G-code and M-code in CNC machining. It covers how G-codes control programmed machine movements and how M-codes manage auxiliary machine functions.
Note: The exact meaning and availability of G-codes and M-codes can vary between CNC controllers and machine manufacturers. Always check the programming manual for your specific machine before running a program.
 |
| CNC machining center showing a G-code program |
1. The Core Differences Between G-Code and M-Code
What is G-Code and How It Controls Tool Motion
CNC machines rely on G-code for physical movement. G-code commands tell the CNC control how to position and move the machine axes, such as X, Y, and Z, during an operation. The program commands the machine axes to follow straight lines or controlled arcs. Depending on the command used, G-code can control positioning, linear motion, circular motion, and other machining functions.
G90 G01 X50.0 Y25.0 Z-5.0 F150 ; Moves tool to precise X, Y, Z coordinates at a controlled speed
The Role of M-Code in Machine Hardware Functions
M-codes control auxiliary machine functions. Depending on the machine and controller, they can start or stop the spindle, control coolant, perform tool changes, or activate other machine functions.
M03 S1500 ; Turns the hardware spindle on clockwise at 1500 RPM
M08 ; Turns the coolant pump hardware on
2. Essential G-Code Commands for Manual Programming
Mastering G00 and G01 for Rapid and Linear Cuts
G00 commands rapid positioning to a specified location. G00 is normally used for non-cutting moves. Rapid motion into material can cause a collision or damage the tool and workpiece. Use G01 for linear interpolation instead. G01 commands linear movement at the programmed feed rate and is commonly used for controlled cutting moves.
G00 X100.0 Y50.0 ; Rapid motion to safe position above the part (no cutting)
G01 Z-10.0 F200.0 ; Linear feed downward into the metal to start the cut
How G02 and G03 Control Circular Interpolation
Making arcs requires circular motion commands. In the active programming plane, G02 commands clockwise circular motion, while G03 commands counter-clockwise circular motion. Depending on the controller and programming method, an arc can be defined using an R value or center-point parameters such as I, J, and K. G02 and G03 are commonly used to machine circular profiles, radii, and other curved paths.
G02 X20.0 Y20.0 R5.0 F120.0 ; Cuts a clockwise arc with a 5mm radius
G03 X10.0 Y25.0 R5.0 F120.0 ; Cuts a counter-clockwise arc with a 5mm radius
3. Vital M-Code Commands for Workshop Safety
Controlling Spindle Rotation and Direction with M03 and M04
M03 commonly starts the spindle in the clockwise direction. M04 commonly starts the spindle in the opposite direction. The required rotation depends on the machine setup and cutting operation. An S command is commonly used to program the spindle speed. Before starting a cutting cycle, verify the spindle direction and programmed speed for the selected tool and operation.
M03 S2000 ; Commonly starts the spindle clockwise at 2000 RPM
M04 S800 ; Commonly starts the spindle counter-clockwise at 800 RPM
Managing Coolant Systems with M07 and M08 to Protect Tools
M08 commonly activates flood coolant, depending on the machine configuration. On many CNC machines, M07 controls an auxiliary coolant function such as mist coolant. Its exact function can vary by machine and controller. M09 is commonly used to turn off coolant functions, depending on the controller configuration.
M08 ; Commonly activates flood coolant
M09 ; Commonly turns off coolant functions
4. How to Structure a Standard CNC Program : The Safety Block
Setting Up Safe Units and Coordinate Systems
Many CNC programs begin with a safety block that establishes important operating modes before machining starts. This block can cancel selected active modes and establish the programming conditions required for the operation. It can also set the measurement units using G20 for inches or G21 for millimeters and establish absolute programming mode with G90. These commands help establish known programming modes instead of relying on modal states left from a previous program.
G21 G90 G40 G80 ; Sets millimeters, absolute mode, and cancels selected compensation and canned-cycle modes
Ending the Program Correctly Using M30
A CNC program normally ends with a program-end command. M30 commonly marks the end of a CNC program and performs a program reset or rewind function. The exact behavior can vary between CNC controllers and machine configurations.
M30 ; Marks the end of the program and performs the controller's program reset or rewind function
5. Common CNC Programming Mistakes and Fixes
How to Avoid Blending Rapid Motion with Feed Motion
Using rapid motion where a controlled feed move is required can cause a collision or damage the tool, workpiece, or machine. A common error involves omitting the G01 command after a G00 position line. Because G00 remains active until another motion command is selected, a following Z move may also be executed as rapid motion. Always command the correct motion mode before moving the tool into the workpiece. A feed rate alone does not change G00 into G01.
WRONG WAY (Dangerous Plunge):
G00 X50.0 Y50.0
Z-5.0 ; Machine plunges into metal at maximum rapid speed!
RIGHT WAY (Safe Action):
G00 X50.0 Y50.0
G01 Z-5.0 F100.0 ; Safely feeds into the workpiece
Understanding Modal Commands and Active Modes
Many CNC commands are modal, meaning they remain active until another command changes or cancels the active mode. If a coordinate is programmed without a new motion command, the control may continue using the currently active motion mode. Always write explicit motion commands when changing from straight lines to circular arcs.
G01 X10.0 F150.0 ; Machine cuts in a straight line
X20.0 ; Machine cuts to X20.0 because G01 stays active automatically
G00 Z5.0 ; Cancels G01 mode by switching to rapid mode
Conclusion
Understanding the basic functions of G-code and M-code is an important first step in learning CNC programming. G-codes are used for programmed motion and machining functions, while M-codes control auxiliary machine functions.
Before running any CNC program, verify the active modes, coordinate system, tool offsets, spindle settings, and machine-specific commands. Program behavior can vary between controllers, so the machine documentation should always be checked before running a new or modified program.
Machining Tuto
Professional metal turner and machinist with 7 years of hands-on experience, specializing in conventional turning and advanced mechanical machining. Dedicated to sharing accurate technical tutorials, precise formulas, and practical guides for both manual and CNC machining operations.