G-code is the language of CNC machines. Key commands and practices include:
-
Basic movement codes:
-
G00: Rapid move (non-cutting move to a position at maximum speed). Use for quick positioning above the part.
-
G01: Linear interpolation (straight-line cutting move) at a specified feed rate. This is the workhorse for controlled cuts.
-
G02/G03: Circular interpolation (arc moves) – G02 for clockwise arcs, G03 for counterclockwise. Specify center or radius to cut circles or curves.
-
-
Units and distance mode:
-
G20/G21: Sets units (G20 = inches, G21 = millimeters). Always include one of these at the start to avoid unit confusion.
-
G90/G91: Absolute vs. incremental positioning. G90 means coordinates are absolute from the program’s origin; G91 means relative (each move adds to the previous position). Know which mode you’re in when coding arcs or repetitive moves.
-
-
Miscellaneous commands (M-codes):
-
M03/M04/M05: Control the spindle (M03 = spindle on clockwise, M04 = counterclockwise, M05 = stop). Pair them with S commands (S1000, for example) to set spindle speed.
-
M06: Tool change. Use this to call a specific tool (e.g. T1) and then manually or automatically change to that cutter.
-
M08/M09: Coolant control (M08 on, M09 off) if your machine uses flood or mist coolant. M00/M01 for program pause or optional pause. M30 ends the program and rewinds.
-
-
Best practices:
-
Comment your code: Use parentheses (or semicolons, depending on the controller) to annotate sections of G-code. For example,
(Starting pocket cut)
or(Material is aluminum)
. Comments don’t affect machining but help you or others understand the program. -
Line numbers: Though not always required, adding N numbers (N10, N20, etc.) at the start of lines can make it easier to restart or edit.
-
Check sequence: Always begin with a setup block (setting units, offsets, and initial spindle speed). For example:
G21 G17 G90 G40
(mm, XY plane, absolute, cancel cutter comp). End with a safe retraction andM30
. -
Simulate or dry-run: Before cutting, run the program in a simulator or with the spindle off to catch any major errors. Verify tool changes and ensure the right tool is loaded.
-
Use incremental moves for loops: If machining repetitive patterns or cycles, G91 (incremental mode) can simplify code. For instance, a drilling cycle can repeat G81 commands with incremental Y-moves.
-
Safety moves: Raise the Z-axis high (clearance plane) before rapid XY moves. For example, before G00 to a new cut location, lift up to a safe height. Many programs insert a G00 Z10.0 (go to 10 mm above part) before XY moves.
-
Consistent formatting: Keep one command per line (e.g. “G01 X10 Y10 F150”). This clarity prevents mistakes. Lowercase or uppercase (G00 vs g00) is tolerated by most controllers, but be consistent.
-
By mastering these commands and following good programming habits, you ensure reliable CNC operation. Well-written G-code is self-explanatory, reduces downtime, and makes debugging much easier. Whether hand-coding small jobs or reviewing CAM outputs, these practices lead to smoother runs and better finished parts.