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

Get Instant Quote

CNC programming explained

Master GCODE CNC Programming: The Art of the Language

To master GCODE CNC programming you have to read the program the control reads, not the CAM preview. This page covers block structure, modal commands, feed and speed values, and the points where a hand edit is safer than trusting the post. It is written for engineers and buyers who review programs, quote parts, or debug a first article.

ISO 6983 word addressModal vs non-modalFeeds and speedsFirst-article proofing
Operator reviewing master GCODE CNC programming on a machine control
Program structure

How a GCODE program is built

A G-code file is a list of lines, and each line is one block. A block holds one or more words. A word is a letter plus a number: G for preparatory functions, M for machine functions, X Y Z for coordinates, F for feed, S for spindle speed, T for tool number. The control reads the block left to right, executes it, then moves to the next line. That order matters. If a G43 tool length offset sits on the same line as the Z move toward the part, the machine applies the offset before it plunges. Put the offset in its own block and the geometry stays predictable.

Programs usually carry a safety header before any cutting move. A typical header cancels cutter compensation with G40, cancels tool length offset with G49, selects absolute positioning with G90, sets the work plane with G17, and picks units with G20 or G21. Skip the header and a program that ran yesterday can behave differently today because the control kept a modal state from the previous job. The header is cheap insurance. It costs four or five blocks and removes a whole class of crashes.

Comments in parentheses or after a semicolon are ignored by the control but read by people. Good programmers label the tool, the operation, and the feature. On a 12-tool setup for an automotive bracket, a comment that says which face and which datum each section machines saves more time than any optimization. When a job returns six months later, the comments are the only documentation left on the floor.

  • 1
    One operation per sectionGroup blocks by tool and feature so a restart lands on a clean boundary.
  • 2
    Header every programG40, G49, G90, G17, and a unit call before the first move.
  • 3
    Document the datumState work offset and stock allowance in the comment block.
  • 4
    Keep the end block explicitRetract, cancel offsets, stop spindle, then M30.
Modal behavior

A modal command stays in effect until another command in the same group replaces it. G01 is modal: once you call a feed move, every following X Y Z block is also a feed move until a G00 or G02 or G03 appears. The same applies to feed rate, spindle speed, plane selection, and units. New programmers write a full G01 F command on every line out of caution. That is safe but slow to read. Experienced programmers rely on modal state and write only the coordinates that change.

The risk is state carried across operations. If a roughing section ends with G02 arc mode active and the finishing section starts with bare coordinates, the control may read those coordinates as an arc and alarm out, or worse, cut a curve nobody asked for. The fix is a reset block at every operation boundary. Call G01 or G00 explicitly, restate the plane, and restate the feed before the first cutting move of the new section.

Tool length and cutter compensation are also modal in practice. G41 and G42 stay active until G40 cancels them. If a program ends a contour without G40 and the next tool starts cutting, the control offsets that tool by the previous radius. On a 6 mm end mill working a stainless housing, a 3 mm unrequested offset is scrap. Always cancel compensation on a lead-out move while the tool is still in the cut, never in the air.

Cutting data

Feeds and speeds from the material side

Feed and speed come from the material and the tool, not from a habit. Surface speed sets the spindle: aluminium 6061 runs comfortably at 300 to 500 m/min with carbide, while 316 stainless sits nearer 100 to 150 m/min, and titanium TC4 lower still. Feed per tooth then sets the table feed. A three-flute 10 mm cutter at 0.05 mm per tooth running 8,000 rpm feeds 1,200 mm/min. Change one number and the others move.

Chip thinning matters on light radial cuts. When radial engagement drops below about half the cutter diameter, the chip gets thinner than the feed per tooth suggests, and the edge rubs instead of cutting. Increase feed per tooth to compensate, or reduce spindle speed. Rubbing work-hardens stainless and burns aluminium. It also shortens tool life faster than a heavier, correct chip load.

Roughing and finishing want different values. Rough with the largest safe axial and radial engagement the holder and machine allow, then finish with a light radial step and a feed that produces Ra 0.8–1.6 μm on most steels. On our 16 simultaneous 5-axis centers, we keep finishing passes at a constant chip load and verify with a first article before a run goes past the sample stage. The program is only half of it. The setup, the holder, and the stock condition carry the rest.

  • 1
    Start from surface speedMaterial grade and cutter coating pick the range.
  • 2
    Then set feed per toothMultiply by teeth and rpm for the table feed.
  • 3
    Watch radial engagementBelow half the diameter, raise the chip load.
  • 4
    Finish lightSmall radial step, steady load, then measure.
CAM and the post

Where CAM output is enough and where it is not

A post processor translates toolpaths into the dialect your control speaks. That dialect is not universal. Fanuc, Siemens, Heidenhain, and Haas differ in canned cycle syntax, in how they handle high-speed look-ahead, and in what M codes they accept for pallet and probe functions. A post tuned for one machine will usually run on a similar machine of the same builder, and usually will not run cleanly on a different control without edits.

For 2.5D work on a three-axis mill, CAM output is normally good enough to run after a visual check and a single-block dry run. Complex 5-axis surfacing is different. The post has to convert a tool vector into rotary axis positions, and machines with a table-and-head configuration can reach the same point through more than one solution. A post that picks the wrong solution will spin the table through 180 degrees mid-cut. That is a collision, not a toolpath.

Manual edits are worth making when the change is small, local, and easy to prove: adding a dwell, splitting a deep pocket into two depths, inserting a probe check between operations, or slowing a lead-in on a thin wall. Rewriting a whole surfacing strategy by hand is not worth it. Repost instead. The line is whether the edit removes a known risk without changing the geometry the toolpath was verified against.

Shop floor proofing

Proving a program before it cuts metal

The first proof is graphical: run the simulation with the actual stock model, the actual holder, and the actual fixture. Most crashes in a new program come from the holder or the fixture, not the cutter. Simulating only the tool hides that. Load the holder geometry and the vise and the simulation earns its keep.

The second proof is on the machine with no stock. Run at feed override down, in single block, with the rapid override low, and watch the distance-to-go readout on every approach. On a 4,000 mm travel machine, an approach that looks short on screen can be 300 mm in reality. Distance-to-go tells you the truth before the tool reaches the part.

The third proof is the first article. Cut one part, inspect it fully, and compare the report to the drawing. On tight work we hold ±0.005 mm and measure with the same instrument the drawing calls out. If the first article passes and the setup is locked, the program is released. If it drifts across three parts, the issue is usually thermal or workholding, not the G-code itself.

  • 1
    Simulate the whole setupStock, holder, and fixture, not just the cutter.
  • 2
    Dry run in single blockWatch distance-to-go at every approach.
  • 3
    Inspect the first article fullyCompare against drawing tolerances, then release.
  • 4
    Watch the first three partsDrift points to thermal or clamping, not code.
Judgment calls

When to trust CAM output and when to edit by hand

Use this as a triage list, not a rule book.

SituationCAM output is enoughHand edit or repost
3-axis pocket and contourYes, after a dry runOnly to adjust a lead-in
Deep pocket, long reachNo, split the depthInsert two depth passes
5-axis surfacing on a table-head machineNo, verify the solution branchRepost with an axis-limit check
Thin wall under 1.5 mmNo, load is unsteadyReduce radial step and feed
Probe check between operationsRarely posted cleanlyAdd a probe block by hand
Same part on a different controlNo, the dialect differsRepost for that control
Adding a dwell for a finishYes, if the post supports itAdd one G04 block

The short version

If the part is 2.5D and the post is tuned to the machine, run the CAM output after a dry run. If the cut is 5-axis surfacing, thin-walled, or moving to a different control, repost and prove it on a first article instead of editing line by line.

FAQs

Questions engineers ask about G-code

Is G-code the same on every CNC machine?

The core word address format follows ISO 6983, so G00, G01, G02, G03, and the coordinate words mean the same thing on most controls. The differences sit in canned cycles, high-speed look-ahead, probe and pallet M codes, and macro syntax.

A program written for a Fanuc control often needs edits before it runs cleanly on a Siemens or Heidenhain machine. The geometry survives. The cycles and the auxiliary functions usually do not.

Can a CAM post processor replace a programmer?

A post handles the repetitive translation from toolpath to control dialect, and it does that well. What it cannot judge is the setup on the floor: holder clearance, stock condition, whether the part will move in the vise, and whether the machine can hold the tolerance.

In practice the programmer defines the strategy and the post executes it. A bad strategy posted perfectly is still a bad program.

Why does my program alarm only on the second part?

Modal state is the usual cause. The first part runs from a known state left by the previous program. The second part starts from the state the first part ended in, which may include an active cutter compensation, a changed plane, or an arc mode.

A reset header at the top of the program removes the dependency on whatever ran before it.

How do I pick a feed rate for a material I have not cut before?

Start from surface speed for that material and cutter coating, convert to rpm for the tool diameter, then apply a conservative feed per tooth. Cut one part and listen and look at the chip. A chip that is thin and powdery means the load is too light.

Raise the feed per tooth before you raise the spindle speed. Rubbing damages the edge faster than a heavier, correct chip.

Does a tighter tolerance change the G-code?

It changes the finishing strategy more than the syntax. Tighter work usually means a smaller radial step, a steadier chip load, and sometimes a spring pass to release deflection.

On work held to ±0.005 mm, the setup and thermal stability matter as much as the program. Code alone will not hold that band on a machine that is warming up.

What should be in a program header?

At minimum: cancel cutter compensation, cancel tool length offset, select absolute positioning, set the work plane, select units, and state the work offset. Add the tool number and a short comment naming the part and the operation.

The header costs a few blocks and prevents a program from inheriting a state nobody intended.

Send the drawing, get a program plan

Upload your CAD and we return a quotation with a free DFM analysis within 12 hours, plus a machining plan that states the setup, the tooling, and the inspection points.

12-hour quoteFree DFM analysis100% inspection

Follow our work

More from the shop floor

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