G-code Fundamentals

Programming|Process Desk|

G-code is the language that tells a CNC machine what to do, and its fundamentals are the foundation of every program this wiki’s programming group describes. When a machining centre cuts a part, it is not thinking or deciding — it is obeying a program, line by line, and that program is written in G-code: a low-level, word-address language in which each line, called a block, is one precise instruction about where to move, how fast, in what mode and with which machine function. G-code is old, terse and unforgiving, but it is also the common tongue of every CNC machine in the shop — the mill, the lathe and the router all understand the same families of codes, whatever the maker calls their control — and understanding its fundamentals is what lets a machinist read, write, check and improve the programs the machines run. This entry sets out what G-code is and how it works.

The machine’s literal language

G-code is a literal language, and the literalness is its most important fact. A machine does not interpret intent: it executes exactly what the program says, in the order the program says it, and it will cut through a fixture, run into the chuck or drive the tool into the table with the same obedience it shows cutting the part. Where a human reading “move the tool to the hole” would add judgement, the control has none — it must be told the safe height, the feed, the spindle speed and the mode for every move. This is why G-code is written the way it is: nothing is left to the machine to decide, and the program is made complete, explicit and safe before it runs. It is also why the structure of a program and the discipline of reading its blocks matter so much — the language gives the machine no margin, so the person who writes it must supply the judgement the machine lacks.

The words that build a program

The vocabulary of G-code is small, regular and easy to recognise. A program is a list of blocks, each block a list of words, and each word is an address letter with a value: G words are the preparatory codes that set how the tool is to act and move, M words switch the machine’s auxiliary functions, X, Y and Z (and the rotary axes) give positions, F sets the feed, S the spindle speed, T the tool, and the offset registers are called by their own words. The G codes fall into families that this wiki treats in their own entries: the motion codes — rapid, straight-line feed and the circular and helical arcs — the codes that select coordinate systems and offsets, the compensation and units codes, and the canned cycles that package repeated patterns of motion. Many of these codes are modal, staying active until another code of their kind replaces them, so a program sets a mode once and then runs many blocks under it; the machine’s control panel shows the state these modes have built up as the program runs.

Where G-code comes from

G-code reaches the machine by two roads, and both end in the same language. A program may be written by hand — as the machinist writes a simple job or adjusts a proven one, typing blocks directly at the machine or in an editor — or it may be generated by CAM software, which turns the part’s geometry into toolpaths and then, through a postprocessor, writes them out as G-code for the specific machine. The two roads meet in the program itself: whether a block was typed by a person or printed by a computer, the machine reads exactly the same literal language, and the machinist who can read G-code can check a CAM program, catch what the software has done wrong, and make the small edits that prove a program true. The fundamentals of the language are thus the shared ground of the hand programmer and the CAM programmer, and the rest of this group — the modes, the cycles, the macros, the proving out — is built on them.

Why the fundamentals matter

The fundamentals matter because everything else in programming rests on them, and because the machine will not forgive their absence. A program is a contract between the person who wrote it and the machine that runs it, written in a language the machine reads literally; the machinist who understands that language can write a new job without a CAM system, read a CAM program with a critical eye, find the line that is wrong when a part comes out wrong, and communicate with every machine in the shop in the same tongue. The entry on reading a program block gives the skill of reading a single line; the entry on part program structure gives the shape of a whole program; and this entry gives the ground they stand on — what G-code is, how it is built and where it comes from. With the fundamentals in place, the codes, the modes and the cycles that follow in this group are not a list to memorise but a language to read, and the machine’s literal obedience becomes, in the hands of a machinist who speaks its language, a precise and dependable instrument for making parts.

Related