Learn CNC G-Code programming from basic commands to advanced techniques. This comprehensive guide covers units, modes, linear/circular moves, pocket milling, drilling cycles, and optimization tips with real code examples and explanations.
Introduction to CNC G-Code
G-code (often RS-274) is the standard text-based programming language that directs CNC machines. It tells the machine how to move tools, control spindles, and execute cuts. Each G-code program consists of lines with commands (like G00, G01, G02, etc.) and parameters (coordinates, feed rates, spindle speed) to define the toolpathcnccode.com. In modern CNC workflows, CAM software often generates G-code, but understanding and editing G-code is crucial for precision and troubleshooting. This guide covers essentials from units and modes to real-world examples (pocketing, drilling, arcs), programming strategies, common mistakes, and performance tips.
Units, Modes, and Coordinate Systems
Before cutting, set the units and coordinate modes. G20/G21 select inches or millimeters (e.g. G21
for mm). Absolute mode (G90
) uses a fixed origin (usually machine or work zero), while incremental mode (G91
) moves relative to the current position. For example,
gcodeG21 (Units = millimeters)
G90 (Absolute positioning from zero)
G17 (Select XY plane for circular moves)
Here, G17
selects the XY plane (G18 = XZ, G19 = YZ). Programs also reference a work offset (e.g. G54) to locate the part’s origin. A typical first line might be G21 G90 G17
cnccode.com. Remember to specify feed rate (F) and spindle speed (S) before cutting. For example, F150
sets feed to 150 mm/min and S1200
starts the spindle at 1200 RPM.
Fundamental G-Codes and M-Codes
CNC motion is controlled by G-codes (geometry commands) and M-codes (misc functions). Key G-codes include:
- G00 – Rapid non-cutting move (moves as fast as possible).
- G01 – Linear interpolation at a set feed (cutting straight lines).
- G02 – Circular interpolation, clockwise arc (in current plane).
- G03 – Circular interpolation, counterclockwise arc.
- G04 – Dwell (pause) for a specified time.
- G20/G21 – Unit selection: G20 for inches, G21 for millimeters.
- G90/G91 – Mode: G90 for absolute coordinates, G91 for incremental.
- G17/G18/G19 – Plane selection (XY/XZ/YZ).
- G43 – Tool length offset (with a D-number) for accurate Z positioning.
- G54 – Work coordinate system #1 (set part zero).
- M03/M04 – Spindle on (CW/CCW).
- M05 – Spindle stop.
- M06 – Tool change.
- M30 – Program end and rewind.
For example, a CNC starter program might include:
gcodeG21 G90 G17 (mm units, absolute mode, XY plane)
G00 X0 Y0 Z5 (rapid to start position, 5mm above part)
M03 S1000 (spindle on, CW, 1000 RPM)
G01 Z-5 F100 (plunge to 5mm depth at 100 mm/min)
This aligns with common usage: “G00 Rapid positioning, G01 Linear movement, G02/G03 arcs, M03 spindle on, M05 spindle stop, M30 program end”cnccode.com.
Example: Rectangular Pocket Milling
As a practical example, consider milling a 30 × 20 mm pocket 5 mm deep. The code below sets up the machine, plunges into the material, and cuts the pocket perimeter. Comments explain each step:
gcodeO1001 (Rectangular Pocket Example)
G21 G90 G17 (Units = mm, Absolute mode, XY plane)
G00 X-15 Y-10 Z5 (Rapid to 15mm left, 10mm down, 5mm above origin)
M03 S1200 (Spindle on clockwise, 1200 RPM)
G01 Z-5 F150 (Feed down into material to Z = -5mm at 150 mm/min)
G01 X15 Y-10 F200 (Cut along bottom edge to X = +15)
G01 X15 Y10 (Cut right side up to Y = +10)
G01 X-15 Y10 (Cut top edge back to X = -15)
G01 X-15 Y-10 (Cut left side down to starting corner)
G00 Z5 (Retract rapidly to Z = +5)
M05 (Spindle stop)
M30 (End program)
This program uses G00
for rapid moves and G01
for cutting moves. We start at one corner and trace the rectangle in a clockwise loop. The spindle is turned on with M03
(and later off with M05
)cnccode.com. Multiple passes or a trochoidal strategy could be added for deep pockets, but this illustrates the core sequence.
Example: Drilling Canned Cycle (G81)
Drilling multiple holes is simplified with a canned cycle (G81). G81 drills to a specified depth and retracts automatically. In this example, we drill four holes in a 50 × 50 mm plate at 10 mm depth, retracting to an R-plane at 2 mm above the part. We use G98
so each hole retracts to the original Z position after drilling.
O1002 (Drilling Cycle Example)
G21 G90 G17 (mm units, absolute mode, XY plane)
M03 S800 (Spindle on, 800 RPM)
G00 X10 Y10 Z5 (Rapid to first hole XY pos, above part)
G81 R2 Z-10 F120 (Drill to Z = -10mm, retract to Z = 2mm, feed 120)
X40 Y10 (Next hole at 40,10)
X40 Y40 (Next hole at 40,40)
X10 Y40 (Next hole at 10,40)
G80 (Cancel drilling cycle)
G00 Z5 (Rapid retract to safe Z)
M05 (Spindle stop)
M30 (End program)
Here, G81
repeats at each specified XY coordinate until G80
cancels it. Each hole is drilled to -10 mm and the tool retracts to either the initial Z (because of G98) or the R-plane (if G99 were active). This canned cycle saves coding multiple G01 plunge/retract moves. Note: forgetting G80
or mixing up G98/G99 can cause unexpected behavior.
Example: Circular Interpolation (Arcs)
Circular moves use G02 (clockwise) or G03 (counterclockwise) with center offsets (I, J). For instance, to mill a 50 mm diameter circle pocket, one approach is to start on the circle edge and command two 180° arcs:
O1003 (Circular Pocket Example)
G21 G90 G17 (mm, absolute, XY plane)
M03 S1000 (Spindle on, 1000 RPM)
G00 X25 Y0 Z5 (Rapid to rightmost edge of circle, 5mm above)
G01 Z-5 F100 (Plunge to Z = -5mm at 100 mm/min)
G02 X-25 Y0 I-25 J0 F150 (Clockwise half-circle to left edge)
G02 X25 Y0 I25 J0 F150 (Complete circle back to start)
G00 Z5 (Retract)
M05 (Spindle off)
M30 (Program end)
In the G02 commands, I
and J
specify the relative center of each arc (here at the origin). G02/G03 modes remain active until cancelled or plane changed. Note that CNC controllers often require the correct plane (G17) and careful IJK values. Circular interpolation demands more calculations (e.g. for backlash at quadrant points), but it achieves precise round pockets or arcs. Always double-check clockwise (CW) vs counterclockwise (CCW) direction.
Programming Strategies and Optimization
Efficient G-code isn’t just about working code — it’s about good structure and speed. Follow these strategies:
- Use Subprograms for Repetition: Factor repeating sequences into subprograms (O-calls) with
M98/M99
. This reduces code size and errorscnccode.com. - Tool Length Offsets: Always apply
G43 H#
(with a tool offset number) for Z-axis compensation. This ensures correct tool tip height without hardcoding Z movescnccode.com. - Consistent Formatting: Write clear code with comments
( )
or;
to explain moves. Consistent syntax (spaces, line breaks) prevents misinterpretation. - Minimize Air Cuts: Combine linear moves when possible. For example, use a single
G01 Xx Yy
instead of separate X and Y lines. Group axes in one block to shorten cycle time. - Optimal Speeds and Feeds: Set correct
F
andS
for the material and tool. Running too fast or too slow wastes time or wears tools. Always verify with manufacturer guidelines. - Safe Retract Heights: Use a designated R-plane (via G98/G99) and safe Z heights to avoid crashes during rapid movescnccode.com.
- Simulation and Verification: Before cutting, simulate the G-code in software (like NC Viewer or CAMotics) to catch errors. CNC experts stress “Always simulate your code before running”cnccode.com.
- Back Up Programs: Save proven G-code files. This allows reuse of optimized routines and reduces recoding time.
By applying these tips (and often using macros or variables for flexibility) you can greatly enhance machining efficiencycnccode.com.
Common Programming Mistakes
Avoid these pitfalls that can cause scrapped parts or errors:
- Wrong Coordinate Mode: Mixing up absolute (
G90
) and incremental (G91
) can send the tool to unexpected positions. Always confirm which mode is active. - Forgetting Units: A single wrong G20/G21 changes all dimensions. Verify you’re in the intended unit system.
- Modal Command Left On: CNC codes are modal (stay active until changed). For example, forgetting to cancel a drilling cycle (
G80
) or leavingG17/G18
set can affect subsequent moves. - Spindle and Tool Mistakes: Not stopping the spindle (
M05
), or using the wrong tool number (T
) causes accidents. Always program the correctTxx M06
andM03/M05
for tool changes. - Poor Feed/Plunge Rates: Plunging too quickly can break tools or overload the machine. Use conservative feed (F) for plunge moves and heavy cuts.
- Ignoring G98/G99: As shown above, G98 vs G99 alters retract behavior. Using the wrong one can pull the tool too high or not high enough.
- No Safety Checks: Skipping a dry-run or probing can miss offsets and zeroing errors. Always verify work and tool offsets first.
Being aware of these errors will improve your CNC programming accuracy and safetycnccode.com.
Conclusion
Mastering G-code unlocks the full power of CNC machining. The more fluent you become with commands like G00/G01/G02, canned cycles (G81, etc.), and M-codes, the more efficient and versatile your machining will be. Keep practicing with real examples (pocketing, drilling, arcs), use subprograms and compensation wisely, and always simulate before cutting. With patience and care, “G-Code mastery is your gateway to CNC excellence”cnccode.com, leading to faster cycles, better surface finish, and fewer errors. Bookmark this guide and continue learning — CNC programming skills grow with experience and sharing knowledge on platforms like cnccode.com.
Leave a comment