GreatLight CNC Machining Factory logo
CNC Machining
Rapid Prototyping
Materials
Industries
News
About GL

Get Instant Quote

CNC Programming

Basics of G Code Knowledge for CNC Milling

This page explains how a milling program is actually structured, which codes matter on the shop floor, and how to read a block before you run it. It is written for design engineers, manufacturing engineers, and buyers who receive programs and need to judge whether the code, the setup, and the part geometry agree.

G and M codesModal statesWork offsetsCutter compensation
Basics of GCODE for CNC new immigrants
How to use this page

What the basics of G code knowledge actually cover

A milling program is a short list of words read in order. Most mistakes come from state, not from syntax.

Structure

How a milling block is built

A G code program is a sequence of blocks. One block is one line, and one line is one set of instructions the control executes before it reads the next. A block usually contains a line number, one or more G codes, axis words with coordinates, feed and speed words, and sometimes a comment in parentheses. The order inside the block is not free-form, but most modern controls accept the common order: N, G, X Y Z, I J K, F, S, T, M.

Each instruction is a word, and a word is a letter plus a number. G is a preparatory function and sets a mode. M is a miscellaneous function and switches something on or off, such as spindle rotation or coolant. X, Y, and Z carry coordinates, and the coordinate values themselves depend on the active plane, the active units, and the active work offset.

The letter alone tells you almost nothing. G01 with a feed word cuts in a straight line. The same G01 with no feed word on the line keeps the last feed value, because feed is modal. That single fact explains most of the strange moves people see when they hand-edit a file.

Comments in parentheses are ignored by the control, but they carry real information for the operator. Tool names, stock size, and setup notes often live there. When a program is transferred between machines, comments are frequently the first thing lost, and the first thing an operator needs.

  • 1
    ModalStays active until changed by another code in the same group.
  • 2
    Non-modalApplies to one block only and then clears.
  • 3
    GroupOnly one code per group can be active at a time.
  • 4
    CancelG80 cancels canned cycles; G40 cancels cutter comp.
Codes

The G and M codes that decide the cut

Motion codes come first in most conversations. G00 positions at rapid, G01 feeds in a straight line, G02 and G03 cut arcs clockwise and counterclockwise, and G02/G03 need either I, J, K offsets from the start point or an R value for the radius. On a Fanuc-style control the two arc formats are not interchangeable in every case, and mixing them inside one program invites an alarm.

Plane selection matters more than people expect. G17 selects the XY plane, G18 selects XZ, and G19 selects YZ. Arc direction and cutter compensation both reference the active plane. A program written for G17 and run with G18 active will still move, which is exactly why the error is hard to spot until the part is scrap.

Units are set by G20 for inches and G21 for millimeters. This is a modal state, and it should appear near the top of every program. A program in millimeters run under G20 will attempt to travel roughly 25 times farther than intended. Nothing in the code itself looks wrong.

Canned cycles compress repetitive work. G81 drills a simple hole, G83 adds pecking for deep holes, G73 pecks with chip breaking, and G84 taps. Each cycle stores its own parameters for depth, retract plane, and feed. G80 returns the control to normal motion and should close every cycle before the next tool change.

Compensation codes change the geometry the control actually cuts. G41 offsets the tool to the left of the programmed path, G42 to the right, and G40 cancels. G43 applies tool length compensation with an H register, and G49 cancels it. G54 through G59 select work coordinate systems, with G54 being the first vise or fixture position on most machines.

M codes handle everything that is not motion. M03 starts the spindle clockwise, M05 stops it, M08 turns coolant on, M09 turns it off, M06 changes the tool, and M30 ends the program and rewinds. M00 is a mandatory stop that waits for the operator, and M01 is an optional stop that only triggers when the panel switch is on.

  • 1
    G00/G01Rapid positioning and linear feed.
  • 2
    G02/G03Clockwise and counterclockwise arc interpolation.
  • 3
    G43/G49Apply and cancel tool length offset.
  • 4
    G54–G59Select the work coordinate system.
Reference

Common milling codes and what they change

Group column shows which codes cancel each other. Only one code per group can be active.

CodeFunctionGroupTypical use
G00Rapid positioning01Move between features
G01Linear feed01Contour and face cuts
G02 / G03Arc interpolation01Fillets, bosses, radii
G17 / G18 / G19Plane selection02Sets arc and comp plane
G20 / G21Inch / metric units06Set once at program top
G40 / G41 / G42Cutter compensation07Control part size with offsets
G43 / G49Tool length comp on / off08Set Z zero per tool
G54–G59Work coordinate systems12One per vise or fixture
G80Cancel canned cycle09Close cycle before tool change
G81 / G83Drill / peck drill09Holes and deep holes
G84Tapping cycle09Synchronized rigid tapping
M03 / M05Spindle on CW / stop—Start and stop the cut
M06Tool change—Next tool in the program
M08 / M09Coolant on / off—Flood or mist control
M30End program and rewind—Return to program start
State

Modal state and why hand edits go wrong

A control only knows what it has been told. Every modal value persists across blocks until another code in the same group replaces it. Feed rate, spindle speed, plane, units, work offset, cutter compensation, and coolant all behave this way. When an operator deletes a block to save a few seconds, they may also delete the line that set the state for everything after it.

The classic failure is a missing G80. A canned cycle stays active, so the next positioning move is interpreted as another hole. The machine goes to the coordinate and drills, even though the drawing shows a clear face. Programs from CAM usually handle this correctly; hand edits usually do not.

Tool length offset is the other common trap. G43 with an H value establishes Z zero for that tool. If the H number does not match the tool loaded in the spindle, the first Z move can be wrong by the full offset difference. On a machine with 100 mm of offset error, the tool reaches the part before the control expects it.

A safe habit is to read the program backwards from the tool change. For each tool, confirm the H register, the work offset, the spindle direction, and the coolant state. That check takes under a minute and catches most of the errors that make it to the machine.

Setup

Work offsets, tool offsets, and the numbers behind them

Work offsets store the distance from machine home to the part zero. G54 is normally the first operation, G55 the second, and so on. The values live in the control and are set by touching off edges, a probe, or a gauge. The program does not carry them, which is why the same file can run correctly on one machine and cut air on another.

Tool length offsets are stored per tool in the H registers. On a machine with a tool presetter, the numbers come from the presetter; on a machine without one, they come from touching each tool to a reference surface. Either way, the H number in the program must match the physical tool. This is the single most common cause of a crash during first-run prove-out.

Diameter offsets are used with G41 and G42. They let you adjust the finished size of a contour without editing the geometry. A wear offset of 0.02 mm changes the part by 0.04 mm on a diameter, which is often enough to move from a rework to a passing part. Keep the geometry offset at zero and use wear for adjustment.

Coordinate systems should be documented in the setup sheet, not only in the program. When a job is set up by a different operator on the second shift, the sheet is what prevents a G55 job from being run in a G54 fixture. We keep the setup sheet and the program revision number together for this reason.

Judgment

When to edit code and when to leave it alone

Editing a proven program on the machine is fast and risky in equal measure. Changing a feed rate at the control is normal practice. Changing coordinates, adding blocks, or reordering tools is not. Once the geometry changes on the machine, the CAM file and the running program no longer match, and the next revision loses whatever was fixed.

The right time to edit at the control is during prove-out and for small process adjustments: a feed override, a wear offset, a coolant change, a retract height. The right time to go back to CAM is any change to the toolpath, the tool sequence, or the geometry. That rule keeps the source file honest.

Some parts should not be programmed by hand at all. A five-axis contour with a tilting head, a deep cavity with a long reach tool, or a thin wall in titanium all depend on simulation and collision checking. Writing that code manually is possible, but the risk of a collision is far higher than the time saved.

For simple parts, hand-written code still has a place. A face op, a two-hole plate, or a quick fixture plate can be written and proven in a few minutes. The judgment call is whether the part has more than about three distinct features. Past that point, CAM is faster and safer.

FAQs

Common questions about G code for milling

Do I need to understand G code to work with a machine shop?

You do not need to write it. You do need to read enough to check that the program matches the drawing: tool sequence, work offsets, units, and whether cutter compensation is used. That level of reading catches most quoting and setup questions before they become scrap.

What is the difference between G code and M code?

G codes prepare motion and control states such as units, plane, offsets, and cycles. M codes switch machine functions on and off, like spindle rotation, coolant, tool change, and program end. A block can contain both.

Why does the same program cut a different size on another machine?

Work offsets and tool length offsets live in the control, not in the file. If the new machine has different G54 values or a different H register for the same tool, the geometry moves. Always re-touch off and verify the offset numbers before running a transferred program.

When should cutter compensation be used instead of adjusting the program?

Use G41 or G42 whenever the finished size may need adjustment. It lets an operator change a wear offset and correct the part without touching the geometry. If the contour is programmed on the tool centerline, every size change becomes a program edit.

Can a hand-written program match CAM output for a complex part?

For a few features, yes. For a five-axis contour, a deep cavity, or a part with tight wall thickness, CAM with simulation is more reliable. The toolpath, the stock model, and the collision check all need to agree, and that is difficult to verify by eye.

What tolerance and finish can a well-prepared program hold?

Our milling work holds ±0.005 mm (±0.0002 in) on controlled features, with surface finish from Ra 0.2–0.8 μm on fine finishes to Ra 1.6–3.2 μm as machined. The program sets the path; the machine, the tool, and the setup set what is achievable.

Send us the drawing and we will check the program path

Upload your files for a quoting response and free DFM analysis within 12 hours. Every part is inspected before shipment, and uploads stay confidential.

12-hour quoteFree DFM analysis100% inspectionNDA on request

Trusted by engineers and manufacturers worldwide

Tesla Ford Motor Company BYD Auto Denso Magna International Boeing Airbus Medtronic KUKA FANUC