Understanding how to write G code is the foundational skill for anyone serious about CNC machining, whether you are operating a milling machine, a lathe, or a 3D printer. This numerical language serves as the direct instruction set that tells a computer-controlled tool exactly where to move, how fast to travel, and how deep to cut. While modern CAD/CAM software can automate much of the code generation, the ability to write and edit G code manually separates a casual hobbyist from a skilled machinist who can troubleshoot errors and optimize programs for efficiency and precision.
The Core Structure of G Code Programs
Every G code program, often called a "program" or "file," is built from a series of lines known as "blocks." Each block typically contains a specific word, which is a letter followed by a number or value, such as G01 or X10. The letter identifies the type of command, while the number provides the data. For example, G indicates a geometric motion, X specifies a coordinate position, and F sets the feed rate. The sequence and combination of these words within a block determine the exact action the machine takes at that moment.
Essential G Codes for Linear and Circular Motion
Rapid and Linear Movement
The most fundamental commands relate to movement. G00, commonly called rapid positioning, commands the tool to move at the maximum safe speed to a specified location without cutting material. This is used for positioning the tool above a workpiece or moving between machining operations. In contrast, G01 is used for linear interpolation, meaning controlled, straight-line cutting at a speed defined by the F word. Mastering the distinction between G00 and G01 is critical for preventing crashes and ensuring efficient material removal.
Arc Cutting Commands
Creating curved surfaces requires G02 and G03. G02 commands clockwise circular interpolation, while G03 commands counter-clockwise circular interpolation. To execute an arc, the programmer must specify the endpoint of the arc using coordinates like X and Y, and also define the center of the arc using I and J offsets (for the X and Y plane) or the full circle command. Incorrectly defining the direction or center will result in a part that does not match the design, so these codes demand precision.
Setting the Stage: Work Coordinate Systems and Units
Before a toolpath can be calculated, the machine must understand where the workpiece is located and how large the units are. G90 and G91 dictate the coordinate mode, with G90 setting absolute positioning (coordinates relative to a fixed origin) and G91 setting incremental positioning (coordinates relative to the tool's current location). Similarly, G20 and G21 determine the unit of measurement, with G20 setting inches and G21 setting millimeters. Using the wrong setting is a common source of severe machine collisions, making these safety-critical lines the foundation of any program.
Controlling the Tool and Spindle
Movement is only half of the equation; the machine must also manage the tools and rotation. M-codes, or miscellaneous functions, handle the auxiliary machine operations. M03 turns the spindle on clockwise, M04 turns it counter-clockwise, and M05 stops it. M08 typically activates coolant to flush away chips and cool the tool, while M09 disables it. M00 and M01 are program stop codes, with M01 being an optional stop that only halts if a specific switch is enabled, allowing for operator checks during long production runs.