  WeChat
 Zhongshan Arify Automation Equipment Co., ltd.
    WAP
 Zhongshan Arify Automation Equipment Co., ltd.
    App
 Zhongshan Arify Automation Equipment Co., ltd.

Zhongshan Arify Automaton Equipment Co., ltd.
After-sales service +86-17328184228
Hotline +86-15976072388

Zhongshan Arify Automation Equipment Co., ltd.

An Introduction to G-Code and CNC Programming

人气:354Published time:2021/9/22 2:53:12

G-code is the operational language for CNC machining. It tells numerically controlled lathes and machining centers how to move tools in order to perform various cutting operations. It is equivalent to manual programming where each operation is spelled out line-by-line and is separate from M-code and T-code, codes that control the machine and tooling. G-code fits somewhere between conversational control, where the operator describes the part and how it should be machined, and CAM, or computer-aided manufacturing, programs, where software develops the necessary toolpaths, feed rates, and so on needed for the CNC machine to cut the material into the finished part.

Although G-code as a term is often used interchangeably with Numerical Control (NC), it is actually only one component and works in conjunction with other codes to perform the appropriate tasks. The standard version of G-code is known as RS-274D, although other versions do exist for CNC machines with compatibility issues.

This article will briefly describe G-code and its application to CNC machining. 


CNC machines are based around the Cartesian coordinate system, sometimes called rectangular coordinates. The three axes can be identified on a vertical milling machine, for example, by the right-hand rule. In placing one’s right-hand palm up on the machine’s table and extending thumb, forefinger and middle finger so they create three axes, the positive X-axis points in the direction of the thumb; the positive Y-axis, in the direction of the forefinger; and the positive Z-axis, in the direction of the middle finger. Similarly applied, the right-hand rule helps to identify axes on other CNC machines. Positive motions refer to the cutter relative to the workpiece. For instance, if the table was moved left while the tool remained stationary, this would still produce a positive machining action.

The intersection of these three axes marks the machine’s home position. Homing the machine will bring the three axes to this X0, Y0, Z0 position. The zero position can be moved to find, for instance, the corner of a workpiece.


Since G-codes are preparatory codes, in a CNC program they begin with the letter G and direct the machine. Typical actions G-code directs include:

  • Changing a pallet
  • Rapid movement
  • A series of controlled feed moves, resulting in a workpiece cut, a bored hole, or a decorative profile shape
  • Controlling  feed movement, in an arc or a straight line
  • Setting tool information


The “G” in G code signifies that a preparatory command follows. For example, G00 tells the machine to quickly move the tool to the specified position, say X22, which, if the machine is set in the metric mode, would move the tool to the right of the origin by 22mm. If the command gives a Y value, the rapid positioning command would move the tool the fastest way possible, not necessarily along a single diagonal line. Most G-code commands are modal, meaning they stay in effect until changed by another command. For instance, G21 sets the machine to metric. Some commands are non-modal to perform a temporary activity once.

A line of code for the above X move might look like this: G21 G00 X22.0

In addition to G-codes, CNC programming makes use of M codes for miscellaneous functions (such as M00 for program pause), S codes for spindle speed control, F codes for feed rates, and T codes for tool selection. A typical program will make use of all, or most of, these letters. All programs begin and end with a percent sign, and every program has a program number following the first percent sign, such as O0007.

G01 moves the tool in a straight line to the designated coordinate. It will travel at whatever speed was set by the last F code. G1 will do the same thing; leading zeros are not needed.

Feed rates for milling are given as feed rate/min. while turning operations are given as feed rate/rev. Similarly, spindle rates depend on the function; for example, milling is given in rev./min., while turning is expressed as feet or meters/min. Tools are stored in magazines or turrets with each tool given a numerical address.

There is a distinction made between absolute (G90) and incremental (G91) moves. An absolute move always references the origin. An incremental move starts from the current position of the tool. There are approximately one hundred G-codes, with separate codes for turning and milling. Many codes are the same for both machine types, though there is some variation among code for machines from different manufacturers.

G-Code Codes

Below are some common individual codes, that when combined, guide a machine’s movement.

  • G00: Rapid positioning

This code causes the machine to operate at a high speed.

  • G01: Linear interpolation

The machine will move in a straight line, performing the appropriate machining (milling, cutting, etc).

  • G02: Circular/Helical Interpolation

The machine will move clockwise in a circular or helical pattern, performing the appropriate machining process.

  • G03: Circular/Helical Interpolation

This code is the same as G02 but enables counterclockwise movement.

  • G17: X-Y plane selection
  • G18: X-Z plane selection
  • G19: Y-Z plane selection

These codes maneuver the machine onto different planes for coordinated motion.

  • G20: Programming in inches
  • G21: Programming in mm

Changes in programming units occur short-term with these particular codes.

The above codes are the same for both milling and turning, but other units may vary. For example, G34 in milling refers to a canned cycle for a bolt hole circle, whereas in turning it refers to variable lead thread cutting. In terms of software specifications, most g-code files can be created using CAM, but certain CNC machines rely on “conversational” programming, which either hides or bypasses the use of g-code completely.

Tool Compensation

Cutter radius offset accounts for the geometry of the tool and permits the programming of part dimensions for which the program determines the path the tool takes based on the tool's dimensions. It allows for tools of different radii to be used by the same program. Likewise, CNC lathes make use of tool nose radius compensation. In each case, the compensation corrects for the fact that the geometry of the cutting tool is not a sharp corner, but rather has some thickness or radius which needs to be accounted for when positioning the cutting tool using the G-code. 

Programmed functions

Codes G70 through G76 are known as fixed cycles and are used to drill, tap, rough turn, etc. as a single instance. G73, for example, calls up a chip-break routine designed to repeatedly dip the tool into the workpiece and then drive back or retract to remove or break up chips of material that have accumulated as part of the cutting action. The same operations are available in the G80-series of commands but these are repetitive, as might be used for drilling multiple holes in a bolt circle, and must be switched off in the program. G81, for example, calls up a drilling routine.


If more than one coordinate is given in the same line the controller will move the tool in a straight line to that point, a process called linear interpolation. The same idea applies to curves where the tool must coordinate its motion along two axes; and helical interpolation, which might be used to mill threads, where the machine must coordinate motion along all three axes.

For curves, G02 specifies a clockwise arc and G03, a counterclockwise arc. Either the centerpoint coordinates or the radius must be specified in addition to the ending coordinates. A function called G01 rounding is used for breaking sharp corners.


Individual line numbers may be called out with an “N” although numbering lines is not at all necessary. Comments may be added to any line with opening and closing parentheses.


This article presented a brief discussion of G-code and its application to CNC machining. For information on CNC machine tools, you can read other articles or contact directly.

Add:No. 9, Jixi, Wencheng Road East, Xiaolan Town, Zhongshan City, Guangdong Province, China.
Contact :Mr. Jiang
Cellphone :+86-15976072388
Copyright 2019 ZhongShan Arify Automaton Equipment Co.,Ltd    All rights reserved   FAE:baxi   Record n varchar:粤ICP备18075039号
Map  |  Dedcms