Author: Mehdi Hassanzadeh
Table of Contents
- Introduction
- 1. What Exactly Is G-Code and What Does It Do?
- 2. A Brief but Accurate History of G-Code
- 3. The Standard Structure of a G-Code Program
- 4. Common G-Codes for CNC Milling/Routing/Plasma
- 5. Common M-Codes
- 6. Practical G-Code Examples
- 7. Important Industrial Notes That Commonly Cause G-Code Errors
- 8. Recommended Resources for G-Code Study and Reference
Introduction
In the CNC industry, G-code (short for Geometric Code) is the most common programming language for directing the movements and commands of a machine tool; that is, it tells the controller where to go (X/Y/Z …), by which path (linear/circular), at what speed (Feed/Spindle), and with which auxiliary operations (spindle on/off, coolant, tool change, and so on). This G-code language is written as address-carrying words (Word Address), such as G1 X50 F300 or M3 S12000.
1. What Exactly Is G-Code and What Does It Do?
A G-code program consists of lines (Block/Line). Each line can contain several “words”:
- G: Motion & Geometry commands, such as G0/G1/G2/G3
- M: Miscellaneous machine commands, such as M3 (spindle clockwise) and M8 (coolant)
- X Y Z A B C: Axis coordinates (linear/rotary)
- F: Feed rate
- S: Spindle speed (RPM), or a dependent parameter on some controllers
- T: Tool number
- I J K / R: Arc parameters for circular motion
- N: Line number (optional)
- (Comment): Remarks
On many controllers, a large number of G-code commands are Modal; that is, the mode stays active until replaced by another code. For example, if you issue G1 once, subsequent lines also run as linear moves until the mode changes.
2. A Brief but Accurate History of G-Code
The roots of G-code trace back to the history of numerical control (NC):
- 1949-1950: Early ideas for numerical control in machining, projects related to Parsons and collaboration with MIT.
- 1952: Early prototypes of NC machines publicly demonstrated at MIT and developed further.
- Early 1960s: Development of a standardizing language in the United States under the RS-274 family, associated with the EIA.
- 1963: Publication of one of the early standard versions of RS-274 in the United States.
- 1979: Completion/finalization of RS-274-D as an important standard version in this family.
- ISO standardization: The ISO 6983 standard covers the program format and the definition of “address words” at the international level, and is also known in the industry as “ISO programming / G-code programming.”
Key point: despite the standards, implementations differ between controllers; therefore, a program that runs well on one machine may need changes to run on a different manufacturer’s controller.
3. The Standard Structure of a G-Code Program
A common format, especially in the Fanuc/Haas style and similar controllers, typically includes:
- Safety Line / Initialization: unit, plane, distance mode, canceling cycles and compensations
- Selecting a Work Offset, such as G54
- Calling the tool with T… and changing the tool with M6
- Setting the spindle speed with S… and turning it on with M3/M4
- Safe (Rapid) movements, followed by cutting (Feed) movements
- Ending: turning things off, returning, and M30
4. Common G-Codes for CNC Milling/Routing/Plasma
Note: some G-code details vary between controllers; the following represent the “common core.”
4.1 Motion
- G0: Rapid movement
- G1: Linear movement with feed (Linear feed)
- G2 / G3: Circular movement clockwise/counterclockwise (Arc CW/CCW)
- G4: Dwell
4.2 Units and Distance Mode
- G20 / G21: Inch / Millimeter
- G90 / G91: Absolute / Incremental coordinates
4.3 Plane Selection for Arcs
- G17 / G18 / G19: XY / ZX / YZ plane
4.4 Coordinate System and Part Offset
- G54 to G59, and on some controllers G59.1 to G59.3: selecting Work Coordinate Systems
- G53: movement in machine coordinates, with restrictions on some controllers
4.5 Tool Compensation and Cycles
- G40: cancel cutter radius compensation (Cutter comp cancel)
- G41 / G42: left/right cutter compensation (Left/Right cutter comp)
- G80: cancel canned drilling cycles
- G81 / G82 / G83: simple drilling / drilling with dwell / peck drilling (common on many controllers, though parameter details may vary)
5. Common M-Codes
- M3 / M4 / M5: spindle clockwise / counterclockwise / stop
- M7 / M8 / M9: mist coolant / flood coolant / coolant off
- M6: tool change (on many controllers)
- M0 / M1: program stop (optional/conditional)
- M2 / M30: end of program; M30 is usually paired with a reset/return to the beginning of the program
6. Practical G-Code Examples (Understandable and Close to Real Use)
Example 1: A Common “Safety Line” Pattern for Starting a Program (millimeters, 3-axis milling)
% O1000 (DEMO) G21 G17 G90 G40 G49 G80 (mm, XY plane, absolute, cancel comps/cycles) G54 (work offset)
Example 2: Cutting a 50×50 Square with a 2mm Depth
T1 M6 S12000 M3 G0 X0 Y0 Z10 G1 Z-2 F300 G1 X50 F600 G1 Y50 G1 X0 G1 Y0 G0 Z10 M5 M30 %
Example 3: An Arc (Circular Move) with G2/G3 — a Conceptual Example
If the plane is G17, arcs lie in the XY plane and are usually defined with I/J, the arc center relative to the starting point:
G17 G0 X0 Y0 G1 X10 Y0 F500 G2 X10 Y10 I0 J5 (example: CW arc with relative center)
7. Important Industrial Notes That Commonly Cause G-Code Errors
- Modal commands: if you set G90 or G91 incorrectly, the output can be disastrous.
- Work Offset: always be clear which offset you are working with, such as G54 and so on.
- Differences between controllers: even when the basics are shared, the details of cycles, macros, compensations, and some M-codes are manufacturer-specific.
- Safety: before actual execution, a Dry Run/Single Block and limiting Feed/Spindle on the first run are recommended industrial practice.
8. Recommended Resources for G-Code Study and Reference
- LinuxCNC documentation: a good, clear reference for many G/M codes and concepts, including G0 and the coordinate system.
- The ISO 6983-1 standard and the ISO page for the definition of the format and address words.
- The NIST report on RS274NGC, with references to RS274-D and differences between implementations.
- The history of NC (Parsons/MIT) for the historical context of CNC and the formation of these languages.