Home › CNC Path › The ten codes that turn up in every program
Step 5 of 8 · 12 min

The ten codes that turn up in every program

A CNC program is written with a few dozen instructions, but 90% of the lines you will see in a workshop use the same ten or twelve. Understand these and you understand almost any program. Here is each one with what it does, an example line, and the failure it causes when it is used badly.

Before we start, two rules that settle half the doubts. The G codes (preparatory functions) tell the machine how to move; the M codes (auxiliary functions) switch things on and off: spindle, coolant, tool changer. And many G codes are modal: once you declare them they stay active line after line until another from the same group replaces them. That detail is the cause of more broken parts than you would think.


G00 — Rapid positioning

Moves the tool to the given point at the machine's maximum speed. It is not a cutting move. It is for approaching and for retracting.

G00 X50. Z2.

Typical mistake: using G00 with the tool inside material, or misjudging the path because you assume it travels in a straight line. On many controls the rapid moves each axis independently, so the real path can be a broken diagonal that takes the part or a clamp with it.

G01 — Linear interpolation

A straight move at the feed rate you set with F. This is the one that cuts.

G01 X50. Z-30. F0.2

Typical mistake: forgetting the F. If none has been declared earlier in the program, some controls alarm out and others start with the last feed in memory, which may belong to a different tool.

G02 and G03 — Circular interpolation

Arcs at feed rate. G02 clockwise, G03 counter-clockwise, looking at the plane from the positive axis perpendicular to it. The arc is defined either by radius (R) or by the centre in incremental coordinates (I, J, K).

G03 X30. Y20. R10. F0.15

Typical mistake: getting the direction the wrong way round, or giving an R that is impossible for the distance between the start and end points. With arcs over 180° the signed R gets confusing: in those cases I/J/K is safer.

G17, G18, G19 — Plane selection

Tells the control which plane the arcs and the cutter radius compensation work in. G17 = XY (normal on a mill), G18 = XZ (normal on a lathe), G19 = YZ.

G17

Typical mistake: leaving it at the default and programming an arc in a different plane. The control interpolates where it should not and the path comes out rotated 90°.

G20 and G21 — Inches or millimetres

G20 inches, G21 millimetres. It goes in the header, always.

G21

Typical mistake: the most expensive one on the list. A program in inches executed in millimetres moves the axes 25.4 times further than intended. If a program arrives from outside, this is the first line you look at.

G40, G41 and G42 — Cutter radius compensation

Lets you program the profile of the part instead of the path of the cutter centre: the control offsets the tool by its own radius. G41 compensates left, G42 right relative to the direction of travel. G40 cancels it.

G41 D01 X20. Y10.

Typical mistake: three of them, and all three common. Switching compensation on or off in the middle of an arc; approaching with a move shorter than the tool radius; and leaving G41 active when you finish. The last one bites you on the next operation.

G54 to G59 — Work offsets

These are the part zeros. G54 is the first and the most used; the others serve for several parts on the table or several clamped at once.

G54

Typical mistake: programming against G54 with the part set in G55. The machine does exactly what you ask, just in the wrong place. Check it before cycle start, every time.

G90 and G91 — Absolute or incremental

G90: every coordinate is measured from the work offset. G91: every coordinate is a move from wherever the tool is now.

G90 G54 G00 X0. Y0.

Typical mistake: the classic of the trade. A G91 left active from an earlier operation turns every following line into a relative move, and the errors add up. If you see the tool walking away in a staircase, look here.

M03, M04 and M05 — Spindle

M03 starts clockwise, M04 counter-clockwise, M05 stops. The S sets the speed.

M03 S1200

Typical mistake: the wrong direction with a right-hand cutting tool: the tool rubs instead of cutting, heats up and eventually breaks. In tapping it also destroys the thread.

M06 — Tool change

Executes the change. It normally goes with a T that selects the tool number.

T02 M06

Typical mistake: calling the change without having retracted the tool to a safe position, or with the spindle turning. Also forgetting the length offset (G43 H) after the change: the machine works with the previous tool's length.

M30 — End of program

Ends and rewinds to the top. M02 also ends, but it does not always rewind, depending on the control.

M30

Typical mistake: leaving the spindle or the coolant running because the end has no M05 and M09. A clean ending switches everything off and returns the axes to a position where you can clamp the next part.


The header that almost never fails

Most programs start the same way, declaring everything modal so nothing is inherited from the last one:

G21 G17 G90 G40 G80
T01 M06
G43 H01
G54 G00 X0. Y0.
M03 S1200
M08

Units, plane, absolute, compensation cancelled, canned cycles cancelled. Then tool, length offset, work offset, positioning, spindle and coolant. Get into this habit and you avoid most of the frights you have just read about.

Mind the dialect. Fanuc, Siemens, Haas, Heidenhain and Mazak share the base but differ in the details — canned cycles, offset syntax, mandatory decimal points. The above is the common trunk; the particulars are in your control's manual, and that manual needs opening.