G-Code Basics: CNC Program Structure and Common G Commands

Updated

In short

G-code is the programming language of CNC machines, with its fundamentals defined in ISO 6983: a program consists of blocks of address words (N, G, X, Y, Z, F, S, T, M), and G commands set the motion type and modes. Key commands are G00 rapid, G01 linear interpolation, G02/G03 circular arcs, G90/G91 absolute and incremental coordinates and G54–G59 work offsets.

G-code is the text-based control language of CNC machines, used to define tool paths, speeds and auxiliary functions. The basic syntax is defined in ISO 6983-1 (the US equivalent is RS-274), but each control maker (Fanuc, Siemens, Heidenhain, Haas, Mazak and others) has its own extensions and differences. This article describes the most common meanings on Fanuc-style milling controls; always check the specific machine documentation before running a program.

Program structure

A program consists of blocks (lines). Each block is made of words – a letter (address) followed by a number.

AddressMeaning
OProgram number (Fanuc)
NBlock (sequence) number
GPreparatory function (motion type, modes)
X, Y, ZLinear axis coordinates
A, B, CRotary axis angles
I, J, KArc centre coordinates (incremental from start point)
RArc radius or canned cycle retract plane
FFeed rate (e.g. mm/min)
SSpindle speed (rpm) or surface speed (G96)
TTool number
H, DTool length and radius offset number
MMiscellaneous function (see M-codes)

Most common G commands (Fanuc milling)

CodeFunction
G00Rapid positioning (non-cutting)
G01Linear interpolation at feed rate F
G02 / G03Circular interpolation clockwise / counter-clockwise
G04Dwell
G17 / G18 / G19Plane selection XY / ZX / YZ
G20 / G21Inch / millimetre input (ISO 6983 specifies G70/G71; Fanuc uses G20/G21)
G28Return to machine reference point
G40 / G41 / G42Cutter radius compensation off / left / right
G43 / G49Tool length compensation on / cancel
G54–G59Work coordinate systems (work offsets)
G80Cancel canned cycle
G81 / G83Drilling cycle / peck drilling cycle
G84Tapping cycle
G90 / G91Absolute / incremental positioning
G94 / G95Feed per minute / feed per revolution

Lathe controls differ in places: for example, Fanuc lathe G-code system A uses G98/G99 for feed per minute and per revolution, incremental moves are often written with addresses U and W, and G96/G97 switch constant surface speed on and off (see CNC turning).

Modal and non-modal commands

Most G-codes are modal – they remain active until replaced by another code from the same group (e.g. G01 stays active until G00, G02 or G03 is programmed). Non-modal codes (e.g. G04, G28) act only in their own block. A safety block setting a known state is therefore usual at the start of a program, e.g. G17 G21 G40 G49 G80 G90.

Simple program example

O1001
N10 G17 G21 G40 G49 G80 G90
N20 T1 M06
N30 G54 G00 X0 Y0 S3000 M03
N40 G43 H01 Z50.0 M08
N50 G00 Z2.0
N60 G01 Z-1.0 F200
N70 G01 X50.0 F600
N80 G02 X60.0 Y10.0 R10.0
N90 G01 Y40.0
N100 G00 Z50.0 M09
N110 M05
N120 G28 G91 Z0
N130 M30

Here tool 1 is loaded, work offset G54 selected, spindle and coolant switched on, the tool descends, mills a line and an arc, retracts, the spindle stops and the program ends. The example is illustrative – real speeds and feeds are chosen for the tool and material (see cutting speed and rpm, feed per tooth).

Manual programming or CAM

Simple operations (facing, hole patterns, simple contours) can be programmed by hand or with conversational programming on the control. Complex contours, 3D surfaces and 5-axis machining are generated in CAM systems, which use a post-processor to tailor G-code to the specific control.

Safety tips

  • Before running a new program, simulate it and run it with reduced rapid override or in single-block mode.
  • Check the work offset and tool length and radius offsets.
  • On an unfamiliar control, do not rely on code meanings from other machines.

More on coordinates in CNC machine axes and coordinates. CNC machining services can be found or requested on the CNC Pasaulis marketplace.

Frequently asked questions

What is G-code?

The programming language of CNC machines, in which G commands set the motion type and modes and other addresses give coordinates, feed, speed and tools.

What is the difference between G00 and G01?

G00 is rapid positioning at maximum machine speed without cutting. G01 is a linear move at programmed feed F, used for cutting.

What does G54 mean?

The first work coordinate system – a zero point whose location on the machine table is stored in the control's offset table.

Are G-codes the same on all machines?

The basic codes are similar, but there are many differences between makers and between milling and turning controls. Always check the machine documentation.

What is a modal code?

A code that stays active in following blocks until replaced by another code of the same group, such as G01 or G90.

Looking for someone to do this job? Find a manufacturer on the marketplace or post a request.

Services offered I need a service