Updated on: 18.05.2022

CNC programming with the aid of G-code

Subject area

CNC programming

G-Code

In the world of CNC machine tool programming, G-code or G-functions are of particular importance. In this article you will learn everything you need to know about G-code.

Manual CNC programming

In modern CNC programming, machining programmes are often created with the help of CAM software (Computer Aided Manufacturing). Starting from the 3D model of the workpiece, a machining sequence is created using predefined procedures and translated into a machine-specific NC programme. Even though the software takes over many programming tasks, a basic understanding of manual CNC programming facilitates the optimal design of the machining process. In manual programming, all motion sequences and machining parameters are described line by line in the form of individual command blocks.

What is the G-code or a G-function?

Within a CNC programme, the G functions determine which calculation logic is to be used for determining the tool path. The G-code thus determines how the machine axes are to be moved. For complex component geometries, several G codes often have to be combined in order to realise the desired movement sequence. As with other NC commands, the G code is a standardised function block that is called with the address letter "G". In this case, the G stands for Go and precedes all preparatory path conditions. Preparatory because the G commands precede the coordinate commands in a programming line. The term G code is also used synonymously in many cases for a manually created CNC programme.

  • An NC programme is a sequence of control commands and describes the machine movement
  • With the aid of the G-code / G-function, the path conditions are defined in an NC programme
  • The path conditions define how the machine axes are to be moved to approach a specific point
  • The most important commands are defined in the DIN 66 025 standard

Functions of the G-code

Even though different manufacturers provide additional or modified control commands, the basic structure is identical. The address letter G is followed by a two-digit number. This is used, among other things, to determine the type of interpolation, the direction of the tool radius compensation, the path dimension input or defines the cycle. A typical NC program is structured in blocks and is executed block by block from left to right or from top to bottom.

Structure of the G-code

Like every command set, G functions also begin with a set number with the address designation N. This is then followed by the actual G command for defining the path movement with which the coordinates named afterwards are approached.

...
N30 G03 G17 X165 Y70 R100 F150 $
...

Set no. 30 - Movement on circular path (radius: 100 mm) counterclockwise with a feed rate of 150 mm/min.

%
O123456 (MEIN_PROGRAM)
(T1 0.45 Schaftfräser)
N1 G17 ... G40 ...
...

The G-code programme is initialised with (%) and the desired programme is called up with the address letter O. Further comments, e.g. the tool used, make it easier for the operator to keep track. Possible reference points are reset to ensure that work is not carried out in the wrong reference system by mistake.

...
N2 T1 M06
N3 S7500 M03
...

N2 - The change to the tool with the number 1 (T1) is carried out with the switch function M06.

N3 - The spindle is started at a speed of 7500 rpm with the switching command M03.

...
N4 G54
N5 M08
N6 G00 X-0.05 Y-0.25
...

N4 - A predefined zero point is set as the reference point.

N5 - The cooling lubricant is switched on with the switching command M08.

N6 - A position above the workpiece is approached at rapid traverse.

...
N7 ...
N8 ...
...

The individual processing steps are worked through sentence by sentence.

...
N9 Z1
N10 M05
N11 M09
N12 G91 G28 Z0
N13 G91 G28 X0 Y0
N14 G90
%

N9 - Approaching a safe level.

N10 / N11 - Switch off spindle and cooling lubricant.

N12 / N13 - Move to the start position.

N14 - Switch to absolute positioning mode and reset the programme. The programme is terminated (%).

G-code commands and instructions

The following table provides an overview of all G commands according to DIN 66025.

G-CodeFunction
G00Positioning in rapid traverse (point control)
G01Linear interpolation
G02Circular interpolation (clockwise)
G03Circular interpolation (counterclockwise)
G04Dwell time
G06Interpolation on a parabolic path
G09Exacthalt
G17Selection of the machining plane X - Y
G18Selection of the machining plane X - Z
G19Selection of the working plane Y - Z
G33Thread cutting with constant pitch
G34Thread cutting with increasing pitch
G35Thread cutting with decreasing pitch
G40Reset all recalled tool offsets
G41Tool radius compensation (in feed direction left)
G42Tool radius compensation (in feed direction right)
G43Tool correction (positive)
G44Tool correction (negative)
G53Resetting the zero offset
G54 - G59Zero offset 1 - 6
G60Entry tolerance 1
G61Entry tolerance 2
G63Maximum feed
G70Dimension input [inch]
G71Dimension input [mm]
G80Resetting the recalled cycles
G81 - G89Drilling cycles
G90Absolute dimension input in relation to the current coordinate system (reference dimension)
G91Relative dimension input in relation to the current coordinate system (incremental dimension)
G92Shifting the reference point - Set memory
G94Feed rate [mm/min]
G95Feed [mm/rev]
G96Constant cutting speed
G97Spindle speed [1/min]

G-code types

Basically, 3 types of G-commands can be distinguished, which in turn are divided into different function groups. It is important to remember that G-codes from one function group overwrite each other.

  • Interpolation type: G00, G01, G02, G03, G06
  • Selection of the reference plane: G17, G18, G19
  • Tool compensation: G40, G41, G42, G43, G44
  • Zero offset: G92 , G53 - G59
  • Running-in behaviour: G08, G09, G60, G61, G62
  • Description of the work cycle: G80 - G89
  • Dimensions: G90, G91
  • Feed: G93, G94, G95
  • Spindle speed input: G96, G97

  • Dwell time: G04
  • Increase or decrease in speed: G08, G09
  • Thread specifications: G63
  • Shifting the reference point: G92

Conditions to which no fixed meaning is assigned in the standard,

Zero offset with G-code

The zero point is the essential reference value in an NC program. Depending on the workpiece and machining task, it may make sense to define a new reference point for the following machining steps. Especially if programme components (e.g. drilling patterns) are to be reused at another point, a zero point shift is essential. The G commands G54 - G59 can therefore be preset with different values in the control and called up in the programme.