G-codes

What are G-codes?

G-codes are CNC-controlled machining command codes used to define machine movements, toolpaths, cycles, and coordinate systems. They form the backbone of CNC machining in machine shops, where precision, repeatability, and efficient production require controlled numerical programming.

G-codes are part of a standardized, ISO-based programming system used in milling machines, CNC lathes, multi-function lathes, 5-axis machines, and other automated tools. In practice, a G-code program determines how the machine moves the cutting tool relative to the workpiece and what parameters are used to remove material.

G-codes are closely connected to concepts such as toolpath, cutting speed, feed rate, depth of cut, work offsets, and tool compensation. Without correctly structured G-code, it is not possible to achieve the required tolerances, surface finish, or process reliability.

Structure of G-codes in a CNC program

A G-code program consists of blocks or lines, each containing a series of letters and numbers. A typical line might include the following addresses:

  • G (movement or operation command)
  • X, Y, Z (coordinates)
  • F (feed rate)
  • S (spindle speed)
  • T (tool)
  • M (M-codes, such as spindle start or coolant commands)

For example, in a milling machine, the program can move the tool at rapid speed to the starting point, switch to a cutting movement with a specified feed, and make circular moves using interpolation.

Motion commands in G-codes

Motion commands are the most essential part of G-codes in machining. They determine how the tool moves along different axes.

Common motion codes include:

  • G00: Rapid move, which moves the tool to the destination point at the machine’s maximum speed without cutting.
  • G01: Linear movement at a specified feed rate (F-value). Used for actual cutting operations.
  • G02: Circular movement clockwise (circular interpolation).
  • G03: Circular movement counterclockwise (circular interpolation).

In circular movements, you usually define the radius (R) or the center point offset (I, J, K). These are used especially for pocket milling, contour milling, and lathe arc profiles.

Machining cycles and fixed G-codes

Machine shops widely use pre-programmed cycles that automate several consecutive movements. This speeds up programming and reduces the risk of errors.

Common cycles include:

  • G73: Peck drilling cycle, deep hole drilling with chip breaking.
  • G81: Drilling cycle, simple drilling
  • G82: Drilling cycle with dwell
  • G83: Deep hole drilling cycle with chip removal.
  • G84: Tapping cycle, righthand thread
  • G85: Boring cycle with feed in and out.

These cycles are especially useful in mass production, where, for example, hole patterns or multiple similar features are machined repeatedly.

Managing coordinate systems and planes with G-codes

Coordinate system management is critical in CNC machining. G-codes define the active work plane and work coordinate system.

Commonly used codes include:

  • G17: XY -plane selection (most common in milling).
  • G18: XZ -plane selection (common in turning).
  • G19: YZ -plane selection.
  • G54–G59: Work coordinate systems, i.e., user-defined zero points.

Versatile machine shops often use several coordinate systems during a single setup. This is common in pallet systems and FMS solutions.

Compensations and programming styles

In precision CNC machining, tool diameter and wear are compensated for using various codes.

Key codes are:

  • G41: Tool radius compensation to the left.
  • G42: Tool radius compensation to the right.
  • G40: Cancel tool radius compensation.

Additionally, the programming mode is defined as follows:

  • G90: Absolute programming relative to the zero point.
  • G91: Incremental programming relative to the previous point.

Compensations allow adjustments to part dimensions without needing to post-process new NC code from the CAM software. This is important when machining with tight ISO tolerances or accounting for tool wear during a production run.

G-codes in different control systems

Although G-codes are based on an international standard, different controllers such as Fanuc, Siemens Sinumerik, Heidenhain, or Haas may use varying extensions and parameters. For example, built-in cycles, macro programming, and subroutine handling may differ.

For this reason, machinists and CAM programmers need to be familiar with the CNC control in use, the post-processor, and machine-specific limitations. Incorrectly written G-code can cause collisions, dimensional errors, or damage the machine.

G-codes and CAM programming

In modern manufacturing, G-codes are most often generated with CAM software. The CAM system calculates toolpaths from the CAD model, and the post-processor converts them into machine-specific G-code.

Nevertheless, it remains important for machinists to understand G-codes. Being able to read the program enables error detection, optimization, and manual changes directly at the machine, for example in MDI mode.

Summary

G-codes are programming commands for CNC machines that control movements, cycles, planes, and coordinate systems. They are the foundation of CNC machining in machine shops and manufacturing facilities. When used properly, G-codes enable precise, efficient, and repeatable machining along with a controlled production process. Mastery of G-codes is an essential skill for every CNC machinist and CAM programmer.