← Blog

G-code Essential Commands and CNC Programming

12 min read

MetronQ Team
CNC controller screen showing lines of G-code
Designed by Magnific

G-code is the standard programming language of CNC machines: a set of commands that tell the machine tool where, how fast and along which path the tool should move. Below you will find G-code explained from the basics, a list of G- and M-codes with descriptions, a sample program line by line and the principles of CNC programming. The list of G- and M-codes is available as a PDF in the "Downloads" box.

G-code for CNC: what it is

G-code is the language for programming numerically controlled machines (CNC, Computer Numerical Control), in which commands controlling tool motion and machine functions are written. It is based on ISO 6983 and DIN 66025, which define a standard G-code shared by most controls, from a milling machine in a small shop to a machining centre.

The name comes from the letter G that starts the most important commands, although a program also contains the addresses M, X, Y, Z, F, S and T. Its roots go back to the 1950s and the RS-274 standard; today Fanuc, Siemens, Heidenhain and the open-source LinuxCNC each have their own dialect, but the core compatible with DIN 66025 is shared. Among CNC programming languages, G-code is what English is among natural languages.

The purpose of G-code is simple: turn the geometry from the drawing into planned tool movements and commands for the spindle, coolant and tool magazine. CNC machining means the machine is controlled by a program rather than by hand wheels. A finished G-code program runs the same way the first and the thousandth time, which makes machining repeatable and precision production predictable.

CNC programming: how a CNC program is created

CNC programming means translating the geometry of a part into G-code instructions the control understands. The process engineer plans operations and CNC tools, then a program is written with the planned tool movements and machine functions, and after simulation it goes to the machine, where the operator watches the first part.

CNC machines are programmed in three ways. By hand: the programmer writes G-code in an editor, which works well for simple parts. At the machine: conversational controls (Heidenhain Klartext, Mazatrol) generate code from forms. From a CAM system: CNC software and CAD/CAM systems calculate toolpaths from a 3D model. In every case programming the tool movements ends in the same text file, so reading and correcting G-code remains a core skill for anyone operating CNC machines.

G-code structure: block, line and word

A CNC program is a plain G-code text file that the control reads block by block. Each line, called a block, describes one step: a movement, a machine function or a change of settings. A block consists of words, and each word is an address, that is a letter, and a numerical value.

The G-code instruction N30 G01 X50.0 Y20.0 F200 means: block 30, linear move to X=50, Y=20 at a feed rate of 200 mm/min. You read it from left to right like a sentence, and the servo drives then execute the motion to within thousandths of a millimetre.

Interpreting G-code commands

Knowing the addresses makes G-code commands easy to interpret: G selects the type of motion or operating mode, M switches machine functions, X, Y and Z give coordinates, F the feed, S the spindle speed, T the tool and N the block number. Modality matters: most G commands stay active until another command from the same group cancels them, so after one G01 the following blocks may contain coordinates only.

The coordinate system and the machine zero point

The machine zero point is a fixed reference point set by the manufacturer, usually at axis end. The machine measures its coordinate system from it after every start-up and reference run. The workpiece zero point, by contrast, is the zero from which the part dimensions are conveniently measured, for example a corner or the axis of a hole.

Because the machine zero lies far from the part, a zero point offset is used: G54-G59 store the distance between machine zero and workpiece zero, measured with a probe during setup. G53 temporarily switches back to machine coordinates, G28 returns to the reference point, and the older G92 sets the zero in the program. G90 dimensions absolutely from the active zero, G91 incrementally from the current position.

Confusing machine zero with workpiece zero, or entering a wrong offset, ends in scrap or a collision, so CNC program settings are always checked before the start. Controlling moves in Z, first a safe height, then plunging, protects the tool and the part, which is why a return to machine zero is programmed in Z first.

Essential G commands: tool movements

The essential G commands are four movements with which every course in CNC programming starts. G00 is rapid traverse, positioning CNC tools outside the material, G01 a linear move at a programmed feed, and G02 and G03 circular interpolation clockwise and counterclockwise. These motion commands are enough to control tool movement for most contours.

Circular interpolation lets the tool mill arcs with no calculations by the operator, while the G01 cutting move guides the tool along edges. G04 is a dwell, for example at the bottom of a hole. Less common are G05 and G06 (high-speed machining, spline), G07 (virtual axes) and G09 (exact stop): these are specific to the control and the type of CNC machine.

CNC milling: a tool performing a cutting move in the material
Designed by Magnific

List of G-codes: CNC G-codes and what each one does

The list of G-codes in most controls covers a few groups: movements (G00-G04), units and dimensioning (G20/G21, G90/G91), zeros (G28, G53, G54-G59, G92), tool compensation (G40-G43), canned cycles (G76-G87) and feed modes (G94-G97). The table lists the G commands with descriptions in line with ISO 6983 and DIN 66025.

Code Function
G00 rapid traverse outside the material
G01 linear move at cutting feed
G02 / G03 circular interpolation CW / CCW
G04 dwell
G09 exact stop in the block
G20 / G21 inches / millimetres
G28 return to reference point
G33 threading with feed synchronised to spindle
G40 / G41 / G42 cancel / left / right cutter radius compensation
G43 tool length compensation
G53 machine coordinates
G54-G59 workpiece zero offset
G76 fine boring (mills), threading cycle (lathes)
G79 parametric cycle (selected controls)
G80 cancel canned cycles
G81 / G82 / G83 drilling cycle: simple / with dwell / peck
G84 tapping
G87 back boring
G90 / G91 absolute / incremental dimensioning
G92 set zero in the program
G94 / G95 feed in mm/min / mm/rev
G96 / G97 constant cutting speed / constant spindle speed
G98 / G99 return to initial / retract plane

Cutter radius and tool length compensation

G41 and G42 offset the path by the cutter radius, so you program the part contour rather than the tool centre; G40 cancels the compensation, and G43 adds tool length compensation from the tool table. After regrinding a cutter, editing the program comes down to changing one value in the table.

Drilling and tapping cycles

A drilling cycle is approach, drilling and retract written in one block. G81 is for shallow holes, G82 adds a dwell at the bottom, and G83 pecks the drill to break chips. G84 is rigid tapping, G98 and G99 set the return height, and G80 ends the cycle.

Units, feed and spindle speed

G20 and G21 switch between inches and millimetres. G94 and G95 decide whether the feed rate is per minute or per revolution, and G96 and G97 are lathe territory: constant cutting speed changes the spindle speed with the diameter.

M-codes: CNC machine functions

M-code is the other half of the language: commands that switch machine functions unrelated to geometry, such as the spindle, coolant and tool changes. All CNC machines understand the core M-codes, although manufacturers assign numbers above M30 freely, so their meaning should always be checked in the manual of the specific control.

Code Function
M00 / M01 program stop / optional stop
M03 / M04 spindle on clockwise / counterclockwise
M05 spindle stop
M06 tool change
M08 / M09 coolant on / off
M30 end of program with rewind

Modern CNC machines have tool magazines, so tool changes are automatic: the block T05 M06 picks up tool number 5 with no operator involvement.

G-code example: a CNC program line by line

The sample program below has 17 blocks: it sets millimetres, absolute dimensioning and the G54 zero, contours a rectangle 2 mm deep with a 10 mm end mill, changes tools, drills one hole with the G81 cycle and ends with M30. It is a typical finished G-code program for a milling machine.

%
O0006 (PLATE 60x40)
N10 G21 G90 G54          (mm, absolute, zero G54)
N20 T01 M06              (end mill 10 mm)
N30 S3000 M03            (3000 rpm clockwise)
N40 G00 X-10.0 Y-10.0 Z5.0
N50 G01 Z-2.0 F100       (plunge 2 mm)
N60 G01 X70.0 F300       (bottom edge)
N70 Y50.0                (right side, G01 still active)
N80 X-10.0               (top edge)
N90 Y-10.0               (back to start)
N100 G00 Z50.0           (retract)
N110 T02 M06             (drill 6 mm)
N120 S1500 M03
N130 G00 X30.0 Y20.0     (above the hole)
N140 G81 Z-12.0 R2.0 F80 (drilling cycle)
N150 G80 G00 Z50.0       (end of cycle)
N160 M05 M09             (spindle and coolant off)
N170 M30                 (end of program)
%

This short program shows what matters most: settings at the start, tool movements along the contour, a canned cycle and a clean ending. It is a good machine code example to start learning G-code programming with.

Generating G-code and verifying the NC code

Writing G-code by hand makes sense for simple parts, but for complex shapes CAD/CAM systems take over: they calculate toolpaths from the 3D model, and a post processor translates them into the control's dialect. Simpler tools can generate G-code too: the DraftSight G-Code Generator creates programs from a 2D drawing, and 3D printing slicers generate G-code for printers.

Whatever the source, the result is a text file that can be corrected by hand. Before the start it is worth checking it in a simulator: graphical verification of the NC code catches collisions and wrong offsets more cheaply than a broken cutter. Running G-code without simulation is acceptable only for trivial programs; the first part runs at reduced feed.

CNC machine operator panel with feed override dials
Designed by Magnific

Lathes, mills and laser cutting: CNC machining with G-code

G-code controls motion wherever repeatable machine motion control matters. Mills and lathes are classic machining, but the same language runs laser cutting, plasma cutting, waterjet cutters and grinders, and even industrial robots and 3D printers, each in its own dialect.

The application of a CNC machine decides which command groups are used: on a lathe G96 and turning cycles dominate, on a plasma cutter motion is limited to two axes, and a machining centre uses the full range. When choosing the right CNC machine for production, check the control and the available post processor.

The potential of CNC machines: machine optimisation and process automation

CNC optimisation starts in the code: shorter G00 moves, a well-thought-out sequence of operations, fewer tool changes and matched feeds can cut cycle time by tens of per cent with no investment. The full potential of CNC machines is unlocked only by process automation: bar feeders, probes and robots loading parts.

Every minute of spindle time saved has a real value, as we explain in the most expensive minute in CNC machining. Programming and spindle time also translate directly into the price of the part, which is the basis of automatic CNC quoting in MetronQ, calculated for the machines a shop enters and calibrates itself: machines and calibration.

Frequently asked questions about G-code

How does G-code for CNC differ from M-code?

G-code commands describe geometry and motion: where, how fast and along which path the tool moves. M-code controls the machine's devices: spindle, coolant and tool magazine. For example, G00 and G01 are the rapid and cutting moves, while M03 starts the spindle, M06 changes the tool and M30 ends the program. A complete CNC program uses both in turn.

Are G-codes the same on a lathe and a milling machine?

Not entirely: the core is shared (G00-G03, G90, G91), but lathes work in the X and Z axes, use G96 and G97 and their own turning cycles, while mills have drilling cycles and radius compensation in the XY plane. A program from a mill will not run on a lathe without rework, even with the same control manufacturer.

How does an automatic tool change work?

A block such as T03 M06 tells the machine to put the current tool back in the magazine and pick up tool number 3. The control adds the length compensation from the table (G43), so every tool has the correct zero in Z. As a result one program drills, mills and taps in a single setup.

What sets the spindle speed and the feed?

The S address sets the spindle speed, for example S3000, and M03 or M04 starts it. The F address sets the feed rate, in mm/min with G94 or mm/rev with G95. On lathes G96 keeps the cutting speed constant regardless of diameter, and G97 returns to constant spindle speed. The block N30 S3000 M03 starts the spindle at 3000 rpm.

In CNC machining, do you need to know G-codes if you have CAD/CAM?

Yes, at least passively. The CAM system will generate the program, but at the machine something always needs correcting: the feed, the zero offset, the order of blocks. The post processor's result is a text file. An operator who understands G-codes catches a post processor error before it costs a cutter or a part.

What is program-level CNC machine optimisation for?

For shorter cycle times and lower tool costs: fewer empty moves, fewer tool changes and feeds matched to the material. Shorter G00 paths and a well-planned sequence of operations can cut cycle time by tens of per cent with no investment. In series production even a small program improvement is multiplied by the number of parts.

Downloads

G-code and M-code list - PDF cheat sheet

The most common G- and M-codes with descriptions on one page: block structure, motion, work zeros, compensation, canned cycles and machine functions, plus a checklist before running a program. A4, portrait.

MetronQ quotes parts from the 3D model or the drawing. Try it in the demo, no account

TopicsCNC programmingG-codetechnology

Sources

  1. 1.ISO 6983-1:2009 (program format and address words)
  2. 2.DIN 66025 (NC program structure)
  3. 3.P. Smid, "CNC Programming Handbook", 3rd ed., Industrial Press, 2008 (G and M codes, canned cycles)
  4. 4.LinuxCNC documentation, linuxcnc.org (RS-274/NGC dialect).

Read next

Quote your own part

Upload a 3D model or a drawing and see the price from the MetronQ engine. No account.

Quote a part in the demoCreate a free account