G-code is the universal programming language for CNC machines. It’s used to control movements, speeds, tool changes, and other operations. While G-code can seem overwhelming at first, having a complete guide makes it much easier to understand and reference. Let’s cover the most important G-code commands, what they do, and how they’re used.
What is G-Code?
G-code (Geometric Code) is a series of instructions that tell your CNC machine how to perform specific actions. Each command begins with a letter, such as G
, M
, or F
, followed by numbers that define parameters. G-code enables precise control of:
- Tool movement (X, Y, Z axes)
- Cutting paths and patterns
- Tool changes and offsets
- Spindle and feed rates
Let’s break down the essential G-code commands you’ll use in CNC machining.
Common G-Code Commands
G00 – Rapid Positioning
Moves the tool quickly to a specified position without cutting.
Example:
G00 X10 Y20 Z5
Moves the tool to X=10, Y=20, Z=5 as fast as possible.
G01 – Linear Interpolation
Moves the tool in a straight line at a controlled feed rate for cutting.
Example:
G01 X50 Y50 F200
Cuts a straight line to X=50, Y=50 at 200 units per minute.
G02 – Circular Interpolation (Clockwise)
Cuts an arc or circle in a clockwise direction.
Example:
G02 X20 Y20 I10 J0
Cuts a clockwise arc to X=20, Y=20 with a center offset I=10, J=0.
G03 – Circular Interpolation (Counterclockwise)
Cuts an arc or circle in a counterclockwise direction.
Example:
G03 X20 Y20 I10 J0
Cuts a counterclockwise arc to X=20, Y=20 with a center offset I=10, J=0.
G20/G21 – Units
Sets the units for measurements.
G20
: InchesG21
: Millimeters
Example:
G21
Sets units to millimeters.
G28 – Return to Home Position
Moves the tool to the machine’s predefined home position.
Example:
G28
Sends the tool to its home position.
G90 – Absolute Positioning
Coordinates are relative to the origin (0,0,0).
Example:
G90
Sets absolute positioning mode.
G91 – Incremental Positioning
Coordinates are relative to the tool’s current position.
Example:
G91
Sets incremental positioning mode.
G17/G18/G19 – Plane Selection
Specifies the cutting plane.
G17
: XY planeG18
: ZX planeG19
: YZ plane
Example:
G17
Cuts in the XY plane.
G40/G41/G42 – Tool Compensation
Adjusts for the tool’s radius during cutting.
G40
: Cancel compensationG41
: Left compensationG42
: Right compensation
Example:
G41 D1
Activates left compensation using tool diameter 1.
G43/G49 – Tool Length Compensation
Accounts for the tool’s length to ensure proper cutting depth.
G43
: Apply compensationG49
: Cancel compensation
Example:
G43 H1
Applies tool length offset for tool 1.
G54-G59 – Work Offsets
Sets alternative coordinate systems for different workpieces.
Example:
G54
Activates work offset 1.
Common M-Code Commands
M03 – Spindle On (Clockwise)
Starts the spindle in a clockwise direction.
Example:
M03 S1200
Starts the spindle at 1200 RPM.
M05 – Spindle Off
Stops the spindle.
Example:
M05
Turns off the spindle.
M06 – Tool Change
Initiates a tool change.
Example:
M06 T2
Changes to tool 2.
M30 – End Program
Marks the end of the program and resets the machine.
Example:
M30
Ends the program.
360 Key Takeaways
- G-code commands control every aspect of CNC machining, from movement to tool changes.
- Common commands like
G00
,G01
, andM03
are essential for basic operations. - Tool compensation (
G41
,G42
) and length adjustments (G43
) ensure precise cuts. - M-codes handle auxiliary machine functions like spindle control and program termination.
- Familiarity with these commands makes it easier to write, debug, and modify G-code programs.
Conclusion
This guide to G-code provides a solid foundation for CNC programming. Whether you’re writing code manually or modifying code generated by CAM software, understanding these commands is crucial. Keep this guide handy as a reference, and with practice, you’ll master G-code programming in no time.