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

Get Instant Quote

How-to guide

CNC Machining Center Program: Codes, Skills and Common Instructions

This guide is for machinists and manufacturing engineers who write or edit programs at the control. It covers the address letters, G and M codes, and cutter compensation setup that decide whether a part comes off the table in tolerance. Read it once and you can read any Fanuc-style program on the floor.

Fanuc-style G-code±0.005 mm shop tolerance3-axis to 5-axis12-hour DFM review
CNC machining center program codes and common instructions reference
Quick answer

Key takeaways

Address letters come firstG, M, X, Y, Z, F, S, T, H and D each carry one job. Mixing them up is the most common cause of a crash on the first run.
G90 and G91 set the frameAbsolute or incremental. Decide before you write a single coordinate, and never switch modes mid-contour.
Cutter comp needs a lead-inG41 and G42 must be activated on a straight move at least half the tool diameter long, or the control alarms out.
G43 H offsets on Z onlyLength compensation applies to the Z axis. Calling it on X or Y does nothing useful.
Dry run before you cutSingle block, feed override down, rapid override down. Five minutes of simulation beats a broken insert.
Section 1

What every address letter in a CNC machining center program does

A CNC machining center program is a list of blocks. Each block holds one or more words, and each word is an address letter plus a number. The control reads left to right, so the order inside a block matters. Put the motion mode first, then the coordinates, then the feed. Writing F before G01 works on most controls, but it makes the block harder to read and harder to debug at the machine.

The letter set is small and stable across Fanuc, Mitsubishi, Siemens and most Chinese controls. A, B and C are rotary axes. D and H pick a compensation number from the offset table. E is a second feed function on some controls, rarely used. F is feed rate, S is spindle speed, T is tool number. G and M are the two function groups: G prepares a motion or a mode, M switches a machine function on or off.

Get the letters right and the program becomes readable. Get them wrong and you get either an alarm or, worse, a silent shift in position. A misplaced H value is the classic example. G43 H02 with H02 set to 0.000 mm means the tool runs 120 mm lower than expected, straight into the vise.

One habit pays off: keep the offset table and the program in sync. If you renumber tools, update every H and D call. We see more first-run crashes from stale offsets than from bad geometry.

  • 1
    G: mode and motionG00 rapid, G01 linear, G02/G03 arc, G17/G18/G19 plane select.
  • 2
    M: machine on/offM03 spindle forward, M05 stop, M08 coolant on, M30 end and rewind.
  • 3
    H and D: offset callsH for tool length, D for cutter radius. Both read from the offset page.
Section 2

G-codes and M-codes you actually use on a 3-axis or 5-axis machine

You do not need the full list. Roughly twenty G-codes cover most milling work. G00 moves fast, G01 cuts straight, G02 and G03 cut arcs. G17 selects the XY plane and should sit near the top of the program. G20 and G21 set inch or metric. G28 sends the machine home, G53 uses machine coordinates for a single move.

Cutter compensation lives in G40, G41 and G42: cancel, left, right. Tool length lives in G43 and G49. Work offsets use G54 through G59, with G54 the usual first choice. G73 and G83 are the two peck drilling cycles you will write most often. G81 is a simple drill cycle for shallow holes.

On the M side, M00 is a program stop for a manual check, M01 is an optional stop you enable at the panel. M03 and M04 set spindle direction, M05 stops it. M06 changes the tool. M08 and M09 control coolant. M30 ends the program and rewinds. That is enough to run a full job.

The order inside a block follows a convention that keeps the control and the operator happy: motion mode, then coordinates, then feed. G01 X50.0 Y20.0 F250 is clean. Putting F first is legal but noisy. Keep coolant and spindle calls at the start of a tool block, before the move that starts cutting.

  • 1
    Modal means stickyG01 stays active until another G-motion appears. You do not repeat it every line.
  • 2
    Cancel comp before retractG40 on a straight lead-out, then G00 Z100. Skipping this leaves the tool offset.
Section 3

How to set up cutter compensation without scrapping the part

Cutter compensation is where most first-run scrap comes from. The control shifts the tool path by the value in the D offset, so the same program can cut a 12 mm slot with a 10 mm cutter or a 12 mm cutter. That flexibility is the point. It only works if the lead-in is long enough for the control to apply the shift gradually.

Rule of thumb: the lead-in line must be at least half the cutter diameter. For a 10 mm end mill, 5 mm minimum, 8 to 10 mm is safer. Activate G41 or G42 on a G01 move, never on a G00. Some controls accept it on a rapid, most will alarm or apply the offset late. Late application means the first few millimeters of the contour are undersize.

The D value should match the real cutter radius, not the nominal one. Measure a test cut. If the slot comes out 0.06 mm undersize on both walls, the effective radius is 0.03 mm larger than nominal. Put that number in the D offset and the next part lands closer.

Cancel with G40 on a straight lead-out move, also at least half a diameter long, then retract in Z. Cancel too early and the last corner gets shaved. Cancel too late and the tool drags on the way out.

  • 1
    Lead-in lengthHalf the cutter diameter minimum. Use a full diameter when you can.
  • 2
    D offset sourceMeasured radius from a test cut, not the catalog number.
  • 3
    Never activate on G00Use G01 so the control applies the shift over a known distance.
Section 4

Programming skills that separate a clean run from a scrapped part

Start every program the same way. Safety block, G21 or G20, G17, G40, G49, G80 to cancel any cycle left over from the previous job. This costs six lines and prevents the classic failure where a modal G83 from the last program turns your first G01 into a drilling cycle.

Use subprograms for repeated features. A plate with 40 identical pockets is one subprogram and 40 call lines. If the pocket size changes, you edit one place. M98 calls the sub, M99 returns. Keep the sub number in the header comment so the next operator can find it.

Keep tool changes short and predictable. Retract to a safe Z that clears the tallest fixture by 20 mm or more, then G28 or a G53 move to the change position. On a 5-axis machine, park the rotary axes at zero before the change unless the post processor says otherwise.

Comment the program as if someone else will run it tomorrow, because someone will. A line that says (ROUGH OD, 0.4 MM STOCK) saves a phone call. Comments cost nothing at runtime.

Finally, match the program to the machine. A program written for a 500 × 500 × 450 mm machine may exceed the travel of a 500 × 310 × 200 mm one. Check travel before you post, not after.

  • 1
    Safety block firstG21 G17 G40 G49 G80. Every program, every time.
  • 2
    Subprograms for patternsOne pocket definition, many calls. Edit once when the size changes.
  • 3
    Check travel against the machineFixture height plus part height plus clearance must fit the Z stroke.
Section 5

When the program is not the problem

Not every out-of-tolerance part comes from bad code. Before you rewrite a working program, check the tool. A worn 10 mm end mill can measure 9.92 mm after a long run in 7075. That 0.08 mm shows up directly in the slot width. Measure the cutter, update D, and re-cut.

Check the offsets next. Thermal growth on a spindle running at 12,000 rpm for two hours moves Z by tens of microns. On a ±0.005 mm job, that matters. Warm up the spindle on the first part of the shift, then touch off again.

Check the workholding. A vise that lifts a thin plate on the finishing pass will spring back after unclamping, and the part measures different on the table than on the CMM. Support thin parts from below, or take lighter finishing passes.

If the geometry is right, the offsets are right and the tool is new, look at the post processor. A post that outputs G91 in a G90 program, or rounds arc endpoints, will produce a program that runs clean and cuts wrong.

  • 1
    Measure the cutterA 0.08 mm wear difference lands in the part on a profiling cut.
  • 2
    Warm up before touching offSpindle growth moves Z. Re-zero after 30 to 60 minutes of running.
  • 3
    Suspect the post lastIf geometry, offsets and tooling check out, review the post output.
Workflow

Step by step: from drawing to a running CNC machining center program

This sequence works for 3-axis work and scales to 4-axis and 5-axis positions with the same logic.

  • 1
    1. Read the drawing for datums and tolerancesMark the primary datum, then list which features carry tight tolerances. On a part with ±0.005 mm callouts, those features decide the operation order. Note any feature that cannot be reached in one setup.
  • 2
    2. Choose the workholding before the toolpathA 4,000 mm part on a 4,000 × 400 × 150 mm table needs support along its length. A 100 mm bracket may only need a vise. Workholding decides how many setups you need, and setups decide the program structure.
  • 3
    3. Set G21 or G20, then G17, then the work offsetMetric or inch first. Then the plane. Then G54 with the X, Y and Z zeros touched off on the datum. Skipping the plane call is fine on a 3-axis mill and wrong on anything with a rotary axis.
  • 4
    4. Write the tool list and match H and D numbersTool 1 uses H01 and D01. Keep the numbering aligned so a mid-program edit cannot leave a stale offset behind. Write the tool list at the top of the program as comments.
  • 5
    5. Program the roughing passes with a stepover you can trustFor aluminum, 40 to 60 percent of cutter diameter is common. For 4140 steel, drop to 25 to 40 percent. Keep the axial depth within the tool's flute length and leave 0.3 to 0.5 mm on walls for the finishing pass.
  • 6
    6. Add cutter comp on the finishing contourG41 or G42 on a G01 lead-in of at least half the cutter diameter. Set D from a measured test cut. Leave G40 on a straight lead-out before the Z retract.
  • 7
    7. Insert peck drilling where the hole is deepG83 with a peck of 2 to 3 mm for aluminum, 1 to 2 mm for steel. Set the R plane 1 to 2 mm above the surface. A full-retract peck clears chips and stops the drill from packing.
  • 8
    8. Dry run, then single block the first partRun with rapid override at 25 percent and feed override at 50 percent. Watch the distance-to-go display on every approach. Cut the first part in single block, then measure before releasing the run.
Reference

Common G-codes and M-codes at a glance

Fanuc-style controls. Check your own manual for exact behavior.

CodeFunctionWhen to use it
G00Rapid positioningAir moves between cuts
G01Linear interpolationAny straight cutting move
G02 / G03Circular interpolationFillets, bosses, radii
G17XY plane selectStandard 3-axis milling
G41 / G42 / G40Cutter comp left / right / offProfile cuts that need size control
G43 / G49Tool length comp on / offEvery tool change
G54–G59Work coordinate offsetsOne per vise or fixture position
G83Peck drilling cycleHoles deeper than 3 × Ø
M03 / M05Spindle on / offStart and end of each tool
M08 / M09Coolant on / offThrough-spindle or flood
M06Tool changeBetween operations
M30Program end and rewindLast block of the program

A readable program is a safer program

Write the safety block, keep H and D aligned with the tool list, and lead in and out of cutter comp on a straight move. Those three habits remove most first-run crashes. When a part still misses tolerance, measure the tool and the offsets before you rewrite the code.

FAQs

Frequently asked questions

What is the difference between G41 and G42?

G41 offsets the tool to the left of the programmed path when viewed from above, in the direction of travel. G42 offsets to the right.

For a climb milling cut on an external profile going clockwise, you use G41. For an internal pocket cut in the same direction, you use G41 as well, because the offset is relative to travel direction, not to the part.

Do I need cutter compensation if I program to the tool centerline?

No. Centerline programming works and is common on simple jobs. The trade-off is that any change in cutter size means rewriting coordinates.

Cutter comp keeps the geometry in the program and the size in the offset table. On a job that runs across several cutters or several machines, that separation is worth the extra setup.

How deep should a peck be in G83?

For aluminum, 2 to 3 mm per peck clears chips well. For 4140 or stainless, 1 to 2 mm is safer because the drill loads up faster.

If you hear the drill squeal on the retract, the peck is too deep or the feed is too high. Reduce the peck first, then the feed.

Why does my program alarm on the cutter comp lead-in?

Most controls require a linear move of at least the cutter radius to activate G41 or G42. An arc lead-in, or a lead-in shorter than the radius, triggers an alarm on Fanuc-style controls.

Move the activation to a G01 line at least half a diameter long and the alarm clears.

Can I use the same program on a 3-axis and a 5-axis machine?

Only if the 5-axis machine runs in 3-axis mode and the work offsets match. A true 5-axis program includes rotary axis positions and a different post output.

Check the travel of both machines against the part envelope before you move a program across. A 4,000 mm part will not fit a 500 × 500 × 450 mm machine.

What should be in the first block of a program?

A safety block that cancels anything left over from the last job: G21 or G20, G17, G40, G49, G80, and a G00 to a safe Z if the machine supports it.

Then the work offset and the first tool call. Six lines of setup prevent a modal carryover from turning a cutting move into something else.

Send us your drawing and we will review the setup

Upload a STEP file and we return a quotation with a free DFM analysis within 12 hours. Production can start within 24 hours, and parts ship in 3 to 5 days.

12-hour quote100% inspectionNo minimum order quantityNDA on request

Follow

More machining notes 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