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

Get Instant Quote

Machining fundamentals

CNC GCODE Programming: How Machine Instructions Become Metal Geometry

GCODE is the instruction set a CNC control reads line by line. This page explains what each code family does, how modal state carries from block to block, and where a hand-edited program beats CAM output. Written for design engineers, process engineers and buyers who review programs or quote cycle time.

RS-274 dialectModal stateG-codes vs M-codesHand-edit safety
Basics of CNC GCODE programming shown on a control screen
Block structure

How One Block Is Built in CNC GCODE Programming

A program is a stack of blocks. One block is one line, and one line normally ends with one motion or one machine action. The control reads left to right, executes the block, then moves to the next. Nothing in the block is a request the operator can negotiate at the panel.

A block carries a sequence number, one or more G-codes, axis words, feed and speed words, and sometimes an M-code. Letters are the address, numbers are the value. X50.0 means one thing; X50 without a decimal point means something else on older controls, and that difference has scrapped parts.

Comments sit in parentheses or after a semicolon, depending on the dialect. Operators trust comments more than they trust memory. A block that says (rough pass, 0.5 mm stock) tells the next shift what the programmer intended, and that is worth more than a tidy file.

Keep blocks short. A line with G01 G41 G43 X Y Z F S M08 is legal on most controls but hard to debug when a tool crashes. Splitting motion, compensation and coolant into separate blocks costs nothing in cycle time and saves an hour at the machine.

  • 1
    Address lettersG, M, X, Y, Z, I, J, K, F, S, T, N, H, D
  • 2
    Decimal ruleAlways write X50.0, never X50, on older controls
  • 3
    One action per lineEasier to restart mid-toolpath after a break
Code families

What G-codes and M-codes Actually Do

G-codes tell the machine where to go and how fast. The G00 group is rapid positioning, G01 is straight feed, G02 and G03 are clockwise and counterclockwise arcs. These four cover most milling and turning motion on any control.

Then come the setup codes. G54 through G59 select work offsets, G43 applies tool length compensation with an H register, G41 and G42 apply cutter radius compensation with a D register. G40 cancels it. A program that never calls G40 before a rapid move is a program waiting to cut a wall.

M-codes handle everything that is not motion. M03 and M04 start the spindle forward and reverse, M05 stops it, M08 and M09 control coolant, M06 changes the tool. M30 ends the program and rewinds. On a lathe, M03 with an S word sets spindle speed directly; on a mill with a VFD, the same command may need a dwell before the first cut.

The split matters when you read someone else's file. Motion problems show up as geometry errors. M-code problems show up as spindle faults, coolant floods or a tool that never changed. Knowing which family to suspect cuts diagnostic time in half.

  • 1
    G00 / G01 / G02 / G03Rapid, linear feed, CW arc, CCW arc
  • 2
    G43 / G41 / G42 / G40Tool length, cutter comp left, right, cancel
  • 3
    M03 / M05 / M08 / M09 / M06Spindle on, off, coolant on, off, tool change
State machine

A CNC control is a state machine. Most G-codes are modal, which means they stay active until another code in the same group replaces them. Write G01 once and every following block with axis words feeds at that rate, even if the G01 is not repeated.

That is why a single line can look harmless and still be dangerous. A block reading X120.0 Y45.0 with no G-code inherits whatever motion mode was last set. If the last mode was G00, the tool rapids into the part at full speed. If it was G01, the same block cuts.

Non-modal codes do the opposite. G04 dwell, G28 return to home and G53 machine-coordinate move apply only to the block that contains them. Mixing the two groups in your head is the most common source of crashes among new programmers.

The practical habit is simple. At the top of every tool section, reset the modes you depend on: G17 for the XY plane, G21 for millimeters, G90 for absolute positioning, G94 for feed per minute. Explicit reset costs four lines and removes all doubt about inherited state. On our 16 simultaneous 5-axis centers, that reset block is part of the standard post-processor template.

  • 1
    ModalStays active until replaced: G00, G01, G90, G21
  • 2
    Non-modalOne block only: G04, G28, G53
  • 3
    Safe resetG17 G21 G90 G94 at the start of each tool
CAM vs hand

When to Hand-Edit CAM Output

CAM software writes correct geometry and wasteful motion. It does not know that your stock is 2 mm oversize on one side, or that the operator will flip the part by hand. Hand-editing is normal in job shops; the question is where to draw the line.

Edit feed rates first. CAM posts conservative numbers because it cannot see the setup rigidity. If a 12 mm carbide end mill in 6061 aluminium runs at 0.08 mm per tooth in the post, an experienced programmer may push it to 0.12 mm per tooth and cut cycle time by a third. Verify with the tool supplier's data, not with feel.

Second, edit entry moves. Helical or ramp entries reduce tool load compared with plunging straight down. A CAM default that plunges into a pocket floor at 800 mm/min will chip corners on a 6 mm tool. Ramping at 3 degrees costs a few seconds and doubles tool life.

Do not hand-edit tool paths that carry tolerance. On work held to ±0.005 mm, changing a lead-in radius by hand can shift the finished surface. Edit speeds, coolant and retract heights by hand. Leave the compensated path to the post-processor, and re-post after any geometry change.

  • 1
    Safe to editFeed, spindle speed, coolant, retract height, dwell
  • 2
    Risky to editLead-in radius, cutter comp offset, arc endpoints
  • 3
    Always re-postAfter any change to the CAD model or stock size
Limits

Where CNC GCODE Programming Reaches Its Limits

GCODE describes motion; it does not describe intent. A program cannot tell the control that the wall is thin, that the material work-hardens, or that the fixture is only clamped on two edges. Those judgments live with the programmer and the setup operator.

Dialects differ. Fanuc, Siemens, Heidenhain and Haas each interpret some codes differently. G28 on a Fanuc mill returns through a reference point; on other controls the same number may do nothing or something else. A program moved between machines without a post check is a risk, not a shortcut.

GCODE also struggles with true 5-axis simultaneous motion written by hand. Five-axis toolpaths need vector math and collision checking that only CAM handles reliably. Where hand programming still wins on 5-axis work is in probing cycles, in-process measurement and short setup macros.

Know the boundary. For 3-axis profiles, drilling patterns and lathe turning, hand-written code is fast and transparent. For contoured 5-axis surfaces, complex undercuts or parts with 40+ tools, use CAM and treat the output as a draft to review, not a finished file.

  • 1
    Hand-code fits3-axis profiles, drilling, turning, probing macros
  • 2
    CAM fitsSimultaneous 5-axis, undercuts, deep 3D contours
  • 3
    Dialect riskCheck G-code meaning per control model, not per brand
Selection guide

Hand Programming vs CAM Output by Part Type

Use this to decide who writes the program.

Part or featureBetter methodWhyTypical tolerance
2.5D plate with holesHand codeShort, readable, fast to edit±0.05 mm
Lathe turning with threadsHand code or CAMCanned cycles cover most profiles±0.02 mm
3D contoured pocketCAMThousands of small moves, no manual gain±0.01 mm
Simultaneous 5-axis bladeCAM onlyNeeds vector math and collision check±0.005 mm
In-process probing cycleHand code macroControl-specific, CAM rarely posts itSetup dependent
One-off prototypeCAM with light editsGeometry changes often, re-post is cheap±0.01 mm

The Short Version

If the feature is flat, drilled or turned, hand-write it — the code is shorter and easier to restart. If it is a contoured 5-axis surface or needs collision checking, use CAM and review the post before it reaches the machine. Never hand-edit a compensated toolpath on a part toleranced to ±0.005 mm.

FAQs

Questions Engineers Ask About GCODE

Is GCODE the same as RS-274?

RS-274 is the original standard that GCODE grew from. Modern controls use dialects of it, so a program is rarely portable without a post-processor check.

The core motion codes are stable across brands. Setup codes, macros and canned cycles are where the differences appear.

Can I run the same program on a mill and a lathe?

No. Turning uses a different axis convention, and the X axis is usually a diameter rather than a radius. Canned cycles also differ.

A post-processor written for a lathe will not produce valid mill code, even from the same CAD model.

Why does my program rapid into the part?

Almost always a modal state problem. A block with axis words but no G-code inherits the last motion mode, and if that was G00 the machine rapids.

Add an explicit G01 or G00 at the start of each section and reset G90, G21 and G17 in the header.

Does every machine save the GCODE it ran?

Some controls keep a program directory and a run log; others only hold the active program in memory. It depends on the model and whether data logging was enabled.

If traceability matters for your part, ask the shop which control model runs it and whether program revision history is retained.

How much cycle time can hand-editing really save?

On aluminium roughing with a 12 mm cutter, raising feed per tooth from 0.08 mm to 0.12 mm can cut roughing time by roughly a third, if the setup is rigid enough.

On finishing passes the gain is small. Spend the editing time on entry moves and retract heights instead.

Do I need to read GCODE to buy machined parts?

No, but reading the header helps. Work offset, units and tool list tell you whether the shop planned the job or inherited it.

If you review first articles, knowing G41 and G43 will explain most dimensional surprises.

Send Us Your Drawing, Get a Program Plan

Upload a STEP file and we return a quotation with DFM notes within 12 hours. Production can start within 24 hours on approved parts.

12-hour quote100% inspection±0.005 mmNo minimum order

Follow

More Machining Notes

We publish setup notes, tooling trials and inspection data from the factory floor.

FacebookTikTokYouTubeLinkedInInstagramThreadsPinterest

Trusted by engineers and manufacturers worldwide

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