CNC Machining Knowledge Base · Turning Programming Hub

CNC Turning Programming Basics: G-Code, Examples and Best Practice

Program structure, coordinates, the safe startup block, G and M codes by function, tool offsets, the six operations every lathe part needs, canned cycles, prove-out and the errors that actually crash machines — explained by why, not just what.

Program structure Safe startup block G-code by function G96 vs G97 Geometry vs wear offsets G71 / G70 / G76 Prove-out & crash prevention
Jump to Program Examples

Quick answer

A CNC turning program is an ordered sequence of G-code and M-code blocks that tells a lathe which tool to use, how fast to turn the spindle, how the tool moves and at what feed. A sound program always has the same skeleton: a safe startup block that puts the control into a known modal state, a tool call, spindle and coolant commands, an approach move, the cutting moves or canned cycle, a safe retract, then a program end. Codes differ slightly between FANUC, Haas, Siemens and Mitsubishi controls, so any template must be checked against the manual for your machine.

26 years
Custom precision manufacturing, Xiamen China
100+
In-house machines, no outsourced production
10 diagrams
Original tool-path and concept drawings
99.8%
On-time delivery rate
Home / CNC Turning / CNC Turning Programming Basics

01What CNC Turning Programming Actually Is

Definition. CNC turning programming is the act of describing a machining process to a lathe control in a language it can execute deterministically — where the tool goes, how fast, in which mode, with which compensation applied, and in what order. The G-code is only the notation. The engineering is the process behind it.

Almost every “basic CNC turning program” article online shows the same thing: a short block of code, a list of what G00 and G01 mean, and an ending. That teaches you to read a program. It does not teach you to write one that runs safely on an unfamiliar machine, on the first part, without a crash.

The difference is that a real program answers a set of questions before a single line is typed:

  • Which end of the part is datum Z0, and why that end?
  • How is the part held, and where does the chuck become an obstacle?
  • Which operations, in which order, and what stays for finishing?
  • Which tools, and which station does each one live in?
  • Constant surface speed or constant RPM — and what is the RPM clamp?
  • Where is the safe retract point that clears every tool in the turret?
  • Which dimensions get corrected by wear offset when the first part is measured?
  • What happens if the operator starts the program mid-way through?

Those decisions determine the code. If you write the code first and answer the questions afterwards, you end up debugging on the machine with a part in the chuck — which is the expensive way to learn.

Turning is not milling with different letters. A lathe programs in diameter on X, works in the ZX plane, uses feed per revolution as its natural unit, and has a spindle speed that may change continuously while the tool moves. If your instinct comes from milling, those four differences are where the first mistakes come from. For the milling equivalent of this page see Is CNC Drilling the Same as Milling?

Manual programming, conversational, or CAM?

Manual G-code

Simple prismatic shafts

Faster than opening CAM for a stepped shaft with a groove and a thread. Fully transparent, easy to edit at the machine, and it is how you learn what the control is actually doing. Still standard practice for small batches.

Conversational

Shop-floor programming

The control builds the cycle from prompts — diameter, length, feed, depth. Quick for one-offs and repairs. The trade-off is portability: the generated code is usually control-specific.

CAM

Complex or live-tooled work

Necessary once you have profiles with arcs, C-axis milling, sub-spindle transfer or Swiss-type work. CAM will not save a bad process plan, though — it just produces the wrong tool path faster.

02Program Structure: the Skeleton Every Turning Program Shares

Once you can see the skeleton, an unfamiliar program stops being intimidating. Every well-written lathe program — hand-written or CAM-posted — is the same nine stages repeated once per tool. The stages exist for a reason, and each one is protecting you from a specific failure.

The nine-stage skeleton of a CNC turning program A vertical flow chart running from program number and safe startup block, through tool call, spindle and coolant, approach, cutting moves, safe retract, tool change position, and repeat per tool, ending at program end M30. O1001 (SHAFT 30X50) G21 G18 G40 G80 G99 T0101 (tool + offset) G96 S180 M03 / M08 G00 X52 Z2 approach G01 / G71 / G76 cutting G00 X100 Z100 retract repeat block per tool M05 M09 / M30 Identifies the program in memory so the operator loads the right one. Forces a known modal state — so the program behaves the same whether it starts cold or after the last job left modes set. Calls the tool and its offset. Wrong offset number = wrong size or crash. Speed mode, direction and coolant, before the tool touches metal. Coolant on early avoids thermal shock on carbide inserts. Rapid to a point clear of the blank, never onto the surface. The actual work — single moves for simple cuts, canned cycles once the profile has several passes. Retract clear in X first, then Z, to a point that clears the turret. Every subsequent tool starts from the same known safe point, so blocks can be re-run independently. Spindle and coolant off, then reset to the top of the program.
Figure 1 — The nine-stage skeleton. The right-hand column is the part most guides leave out: every stage exists to prevent one specific failure mode. Learn the reasons and you can write the code for any part.

Why the skeleton repeats per tool

The single most useful property of a well-structured program is that any tool block can be run on its own. If the operator has to re-run only the grooving tool after a broken insert, they should be able to start at that tool’s block without the machine being in some leftover state from the tool before it. That is only true if every tool block re-states its own speed mode, feed mode, offset and approach, and ends at a common safe point.

The classic mid-program restart crash. An operator starts a program from the middle to save time. The block they start from assumes the turret is already at X100 Z100 and that G99 feed-per-revolution is active. Neither is true, so the first rapid drives straight through the part. Structuring every tool block as self-contained is what makes restarting safe — not operator care.
O<number> (COMMENT)
Program identity. Some controls restrict the numeric range, and O8000–O9999 is often reserved for macros. Add a comment describing the part and revision — the operator will thank you two years from now.
Safe startup block
Known modal state. Units, plane, compensation cancel, cycle cancel and feed mode. Covered line by line in section 04.
T0101
Tool and offset call. On FANUC-style lathes the first two digits are the turret station, the second two the offset register. They are commonly — but not always — kept the same number for clarity.
G96 S180 M03
Spindle mode, speed, direction. Under G96 the S word is surface speed in m/min, not RPM. Direction depends on tool orientation and whether the turret is above or below centre.
G50 S2500 / G92 S2500
RPM clamp. Only meaningful with G96. It stops the spindle running away as the tool approaches centre. Syntax differs by control.
G00 X.. Z..
Approach. Rapid to a point just clear of the blank, typically 2–3 mm. Rapid moves in two axes are not straight lines on every control — check before assuming a diagonal is safe.
Cutting moves / cycle
The work. G01 and G02/G03 for individual passes, G71–G76 once a profile needs multiple passes.
G00 X.. then Z..
Retract. Come out in X first on external work so the tool clears the diameter before it moves along the part.
M05 M09 / M30
End. Spindle and coolant off, then M30 to reset to the top. M02 ends without rewinding on many controls, which is why M30 is the safer habit.

03Coordinates, Diameter Programming and Where Z0 Lives

Two conventions cause more beginner scrap on a lathe than any code: X is programmed in diameter, and Z0 is a decision, not a given. Get either wrong and the part is out by exactly twice the amount you expect, or by the whole facing allowance.

Lathe X and Z axis directions with diameter programming explained A workpiece held in a chuck with the Z axis running along the spindle centreline, positive away from the chuck, and the X axis radial, positive away from the centreline. A callout shows that X50 means a 50 millimetre diameter, which is 25 millimetres of radius from the centreline. chuck workpiece spindle centreline +Z Z0 at chuck face? Z0 at finished face — use this +X X0 = centreline G01 X50 = Ø50 mm the tool sits 25 mm from the centreline turret
Figure 2 — X is a diameter, not a radius. A 0.1 mm change in the X offset moves the tool 0.05 mm and changes the diameter by 0.1 mm. Z0 is a choice: put it on the finished face so that facing allowance does not shift every Z dimension in the program.

The three coordinate systems you are juggling

SystemZero pointWhat it is for
Machine coordinatesMachine home, set by the builderFixed reference the control uses internally. Tool change positions and travel limits are expressed here. You rarely program in it directly.
Work coordinates (G54–G59)A datum you choose on the partWhat the program is written in. Stored as an offset from machine zero so the same program runs regardless of where the part sits along Z.
Tool offsetsPer tool, per registerThe distance from the turret reference point to each tool tip, so all tools agree on where X0 Z0 is. Covered in section 08.
G50 or G54? Older FANUC lathe programs set the work coordinate system with G50 X.. Z.., which shifts the coordinate system relative to the current tool position. It works, but it makes the program depend on where the tool happened to be. Modern practice is to store the datum in G54 and leave G50 for the spindle-speed clamp. If you inherit a program that uses G50 for both, read it carefully — the same code word is doing two unrelated jobs. The same principle applies on machining centres; see our CNC machining process for how datums are set in production.

Why Z0 belongs on the finished face

If you set Z0 on the raw, unfaced end of the blank, every Z dimension in your program is measured from a surface that you are about to remove. Face off 0.5 mm and every shoulder is now 0.5 mm out. Setting Z0 on the finished face means the facing pass cuts to Z0 and everything downstream is correct by definition.

Two practical consequences follow. First, the facing pass has to be programmed to cut to Z0, taking the stock that is in front of it. Second, when a second operation flips the part, the new Z0 goes on the newly finished face — and any length dimension that spans both operations depends on the accuracy of that transfer, not on the machine.

Radius programming exists. Some controls can be switched to radius mode by parameter. This is uncommon but not rare, and it is a very expensive surprise. Before you run someone else’s program on your machine, confirm which mode the control is in.
Negative X is real. When parting off you deliberately command a small negative X so the tool passes the centreline and separates the part cleanly. That is the one time X below zero is intentional.

04The Safe Startup Block, Line by Line

G-codes are modal: once set, they stay set until something changes them — including across programs. The safe startup block exists because you cannot know what state the previous job left the control in. It costs one line and prevents an entire category of crash.

The safe startup block shown as a set of modal switches being forced to a known position Six modal groups are shown as switches. Before the startup block their positions are unknown, shown greyed. After the startup block each switch is set to a definite state: millimetres, ZX plane, compensation cancelled, canned cycle cancelled, feed per revolution, and work offset G54. Before: modal state unknown After the startup block: defined units ? mm or inch G21 millimetres plane ? XY / ZX / YZ G18 ZX plane (turning) nose comp ? G40 / G41 / G42 G40 compensation cancelled canned cyc ? G7x still active G80 cycle cancelled feed mode ? mm/min or mm/rev G99 feed per revolution work offset ? G54..G59 G54 part datum startup block
Figure 3 — A startup block is not a formality. Each word forces one modal group into a known position, so the program behaves identically whether it runs first thing in the morning or straight after a job that left the control in a different state.
Typical FANUC-style lathe startup blockverify against your machine builder’s template
%Tape start marker on controls that use it. Harmless where it is not needed.
O1001 (SHAFT 30X50 REV B)Program number and a comment that identifies the part and revision.
G21Millimetre input. The single most damaging mode to get wrong. If the control is left in G20 inch mode, a move to X50 becomes 50 inches. G21 costs nothing and removes the possibility entirely.
G18ZX plane. Turning happens in ZX. It matters for circular interpolation: G02 and G03 need to know which plane the arc lives in, and the direction convention flips if the plane is wrong.
G40Cancel tool nose radius compensation. If the previous program left G41 or G42 active, your first approach move will be offset by the nose radius in a direction you did not intend. Cancel first, apply deliberately later.
G80Cancel any canned cycle. A drilling or threading cycle left active will re-trigger on the next axis move. This is the classic “why did it just drill a hole there” block.
G99Feed per revolution. On a lathe, feed is naturally per revolution — it is what determines chip thickness and surface finish. If the control is in G98 (per minute) and you command F0.2, the tool crawls at 0.2 mm/min and the insert rubs until it fails.
G54Work coordinate system. State it explicitly rather than relying on the control’s power-on default.
G00 X200 Z200Move to a known safe point before the first tool change, in a single rapid that cannot foul the part.
Do not copy this block blindly. Startup templates are machine-specific. Haas lathes, Siemens 828D/840D, Mitsubishi and Okuma OSP all differ — in code numbers, in what is modal, and in what the builder has already forced by parameter. Some shops also add G28 U0 W0 to reference the turret, which is right on some machines and a crash on others depending on where the tool sits. Always start from the template in your machine’s programming manual and adapt.

What people add, and when it is right

G28 U0 W0 sends the turret to reference in incremental zero, useful before a tool change on machines where that is the builder’s convention. G97 S500 M03 is sometimes used to start in constant RPM before switching to G96. M08 may be delayed until after the approach on machines with weak pumps.

What does not belong there

Anything that moves an axis before the modal state is defined. Tool calls before the safe position is reached. And on most controls, G50 S____ should sit with the tool that uses G96, not in the header — the clamp is meaningful per operation, not per program.

05G-Codes That Actually Matter, Grouped by Job

Most beginner pages list G-codes alphabetically and stop. That is the least useful order, because on a lathe you think in jobs — move, set the frame, compensate, run a cycle. Group them that way and the list becomes a checklist you can run through as you write.

Motion — how the tool travels

G00
Rapid traverse. Moves as fast as the machine allows — never through the part. Used for approach and retract only.
G01
Linear cut at the federate in F. This is the actual machining move for facing, turning and parting.
G02 / G03
Circular interpolation, clockwise and counter-clockwise. Needs the ZX plane (G18) and an arc radius or centre. Used for radii, tapers with arcs and fillets.

Coordinate & frame — where zero is

G54–G59
Work coordinate systems. Picks which stored datum the program is written in. State it in the startup block; do not rely on the power-on default.
G90 / G91
Absolute vs incremental input for the cycle. On lathes G90 (absolute) is the default you almost always want; G91 creeps in from drilling cycles.
G21 / G20
Millimetre vs inch input. Lives in the startup block. Wrong one here and X50 is 50 inches.

Compensation — accounting for the tool

G40
Cancel tool nose radius compensation. Always cancel before a tool change so the next approach is not offset by the wrong nose.
G41 / G42
Compensate left / right of the path by the programmed nose radius. Lets you program the finished profile and let the control add the radius. Direction depends on tool orientation.
G96 / G97
Constant surface speed vs constant RPM. Covered in section 07 — it decides what the S word means.

Canned cycles — do the repetitive work

G71
Rough turning along a profile. One definition of the finish shape, the control does the passes. The single highest-leverage code on a lathe — section 10.
G70
Finish pass that follows a G71 (or G72/G73) using the same profile block range.
G72 / G73
Facing cycle (G72, cuts facing-in) and pattern repeat (G73, for castings/forgings already near shape).
G74 / G75
Peck drilling / peck grooving along the Z and X axes respectively.
G76
Multiple-pass thread cycle. Replaces hand-written thread passes — section 11.
Modal is the trap. Every code above except the coordinate and cycle calls stays active until changed. Write G00 once and the control keeps rapiding until a G01 appears; call G41 and it stays on until G40. This is why the safe startup block cancels the dangerous ones up front: it is cheaper to force a known state than to discover a leftover mode on the first cutting move.

06The M-Codes: What the Machine Does, Not Where It Goes

M-codes are the auxiliary commands — spindle, coolant, program flow. They do not move the tool, but a wrong one still scraps a part or leaves a spindle running. The small set below covers the vast majority of turning work.

CodeDoesWhen you use it & the gotcha
M03Spindle on, clockwise (as viewed from the spindle nose on most lathes)Before the first cutting move. Direction is a convention — confirm it matches your tool and turret side, or the first cut is on the wrong face.
M04Spindle on, counter-clockwiseFor the other turret side or reverse for threading in some setups. Rare on simple 2-axis work.
M05Spindle stopAt program end and any time the tool is clear and you want the spindle still. Pair with M09.
M08Coolant onBefore cutting. On some machines M08 also engages through-tool or flood; on others you may prefer to start it after the approach if the pump is weak.
M09Coolant offBefore M05 at the end, and before any handling move where you do not want spray.
M00Unconditional stopOperator must press cycle start to continue. Useful before a critical dimension so the operator can measure first.
M01Optional stopLike M00 but only stops if the operator has enabled optional stop. Good for inspection points you can switch off in production.
M98 / M99Call / return from a subprogramRepeats a routine (e.g. a repeated groove) without writing it twice. M99 returns; an M99 with no subprogram loops the main.
M30Program end and rewindThe safe end. Spindle and coolant off, then reset to the top so the next run starts clean. Prefer it to M02, which ends without rewinding on many controls.
M-code control difference. Unlike G-codes, M-codes are usually non-modal and act once, on the block where they appear — but the state they set (spindle on, coolant on) persists until its off-code. The subtle failure is when an M-code is on a block that also does a movement: on some controls the movement completes and then the M-code fires, on others it fires first. When the order matters (coolant before a delicate cut, spindle stop before a retract), put the M-code on its own block.

07G96 vs G97: Constant Surface Speed or Constant RPM

This is the pair most beginners get backwards, and it is the one that decides whether your surface finish is good at the face and your insert survives at the centre. The S word means two completely different things depending on which one is active.

What the S word means under each modecheck the spindle-direction block, not just the G-code
G96 S180 M03Constant surface speed. S is the target cutting speed in metres per minute. As the tool moves toward the centre (smaller diameter), the control raises RPM to keep the surface speed constant. Finish passes love this: same speed at the face and at the bore.
G97 S1200 M03Constant RPM. S is simply revolutions per minute, fixed regardless of diameter. Grooving and parting use this, because at the centre G96 would spin the spindle to a dangerous speed.
G50 S2500RPM clamp — only meaningful under G96. Caps the spindle so it cannot run away as the diameter shrinks. Without it, a tool approaching centre under G96 can try to spin at thousands of RPM. Some controls use G92 S____ instead; check the manual.
Why G96 at the face? With G97, a facing cut starts at large diameter (low surface speed) and ends at the centre (also low, because RPM is fixed but the tool barely travels). The cut looks different at each end. Under G96 the surface speed is identical start to finish, so the finish is uniform.
Why G97 for grooving/parting? As the tool plunges to centre the diameter falls toward zero. Under G96 the RPM would climb without limit; the insert and the thin tool would not survive. Fix the RPM with G97 and the cut stays controllable.
The classic G96/G97 confusion. An operator leaves G96 active from a finish pass, then runs a parting routine that was written assuming G97. The spindle accelerates toward a tiny diameter, the parting blade chatters or snaps, and the part is destroyed. Make the speed mode explicit at the top of every tool block — do not let it ride from the previous operation.

08Tool Offsets: Geometry, Wear and Nose Radius

The program talks about X0 Z0 on the part. Every physical tool sits at a different place in the turret, with a different tip shape. Offsets are what make all of them agree on where the datum is — and they are what you correct after measuring the first part.

Geometry offset, wear offset and nose radius compensation on a lathe tool From the turret reference point, a geometry offset locates the tool tip in X and Z. A separate wear offset fine-tunes that position after the first part is measured. Tool nose radius compensation then shifts the cut path left or right of the programmed profile by the nose radius. turret ref Geometry offset (X, Z set at setup) Wear offset Programmed profile G41 / G42 shift by nose radius Geometry — where the tool is Wear — fine correction Nose comp — path shift
Figure 4 — Three distinct ideas, three distinct registers. The geometry offset says where the tip is; the wear offset nudges it after the first measurement; nose-radius compensation shifts the whole path so you can program the finished shape and let the control add the radius.
Geometry offset

Where the tool tip is

Measured once when the tool is set, by touching off to a known reference (a setter, a test bar or the part face). It captures the X and Z distance from the turret reference point to the tip. Get it wrong and every dimension from that tool is out by the same amount.

Wear offset

The fine correction

A small separate adjustment on the same tool. After you measure the first part, you do not re-teach geometry — you put the correction in wear. This keeps the setup value clean and lets you drift a dimension without disturbing the datum.

Nose radius comp

G41 / G42

The insert tip is a small radius, not a point. Without compensation the control cuts to the tip centre and the part comes out undersize by the radius at every shoulder and corner. Compensating lets you program the finished profile and the control adds the radius on the correct side.

Why this matters for “first part good”. A program can be perfect and still make a scrap part if the offsets are wrong. The offsets are the bridge between the paper program and the physical machine. That is why proving a program is never “run the code” — it is “confirm the offsets, then run the code” (section 12).

09Six Programs You Will Actually Write

Every turned part is some combination of these six operations. Learn each one as a recipe — the goal, the safe order of moves, and the one line that saves you when something is wrong. Diameter programming and the safe retract are the same in every one.

① Facing the end

Facing: the tool sweeps across the end from the OD to past centre A bar held in the chuck. The facing tool approaches just outside the diameter, then feeds inward in X to slightly past the centreline, removing a thin layer from the face. Z0 sits on the finished face. workpiece X −0.5 (past centre) Facing: feed in X to just past centreline so the face is flat
Figure 5 — Face to slightly past centre (a small negative X) so there is no residual nub. Z0 is on this finished face, so every Z dimension downstream is correct by definition.
Goal: flat reference face, Z0 set here

Facing program (OD 50 bar)

O1002 (FACE OD50)
G21 G18 G40 G80 G99 G54
G50 S2500
T0101 (facing / rougher)
G96 S200 M03
G00 X52 Z2
G00 Z0.1
G01 X-0.5 F0.15      (face to past centre)
G00 X52             (out in X first)
Z2
G00 X200 Z200
M05 M09
M30

Why this order: approach to Z0.1, then feed in X to a touch past centre. Retract in X before Z so the tool clears the diameter. Under G96 the surface speed is constant across the face.

② Turning the OD to size

OD turning: the tool travels along Z removing material from the diameter A bar shown at two diameters. The turning tool sits at the OD and feeds along Z, shaving the diameter down to the finished size over the turned length. A curl of chip is shown leaving the cut. stock Ø50 → finish Ø40 chip Turn along Z; the diameter drops from stock to finish
Figure 6 — A straight OD turn is just G01 along Z at the finished diameter. The real skill is choosing the depth of cut and feed so the chip is controllable — covered in the cutting-parameters guide.
Goal: Ø50 stock to Ø40 over 30 mm

OD turning program

O1003 (TURN OD TO 40)
G21 G18 G40 G80 G99 G54
G50 S2500
T0101
G96 S180 M03
G00 X52 Z2
G00 X50 Z0.2
G01 Z-30 F0.2        (turn to length)
G00 X52
Z2
G00 X200 Z200
M05 M09
M30

Why approach at Z0.2 then feed to Z-30: the tool clears the face before it engages, so the entrance is clean. Feed per revolution (G99) is set in the startup block, so F0.2 means 0.2 mm per spindle turn — the same chip load at any speed.

③ Grooving

Grooving: the tool plunges radially into the OD to form a groove The grooving tool is positioned at a Z location on the OD, then fed inward in X to the groove root diameter, forming a narrow channel. Multiple shallow passes may be used for a wide groove. groove root Ø36 Z-20 Plunge in X at a fixed Z; for a wide groove, step along Z between passes
Figure 7 — A groove is a radial plunge. For a wide groove, step the tool along Z between shallow passes rather than one deep plunge — a grooving insert is weak across its width and will break in a deep single cut.
Goal: 4 mm wide groove at Z-20, root Ø36

Grooving program

O1004 (GROOVE Z-20)
G21 G18 G40 G80 G99 G54
T2020 (grooving tool)
G97 S600 M03        (fixed RPM: diameter is small here)
G00 X44 Z-18
G01 X36 F0.08       (first pass, root)
G00 X44
Z-19
G01 X36 F0.08       (step along Z)
G00 X44
Z-20
G01 X36 F0.08
G00 X44
Z2
G00 X200 Z200
M05 M09
M30

Why G97 here: the groove is near the centreline where G96 would over-speed the spindle. Fixed RPM keeps the weak grooving insert under control. Plunging to X36 in three Z steps spreads the load.

④ Threading with G76

Threading: the tool forms a 60-degree V profile in repeated passes A cylindrical surface with a V-thread profile cut into it. The threading tool, shaped as a 60-degree V, takes repeated passes of decreasing depth following the thread lead, building the full profile. G76 repeats the V profile to full depth; never retract mid-thread
Figure 8 — G76 takes the thread in progressively shallower passes following the lead. The tool must start at exactly the same Z every pass, so a threading cycle — not hand moves — is what keeps the flanks aligned.
Goal: M20 × 2.5 external thread, 18 mm long

G76 threading program (see section 11 for the full decode)

O1005 (THREAD M20X2.5)
G21 G18 G40 G80 G99 G54
T3030 (threading tool, 60°)
G97 S800 M03
G00 X22 Z2
G76 P010060 Q50 R0.05
G76 X18.40 Z-18.0 P1300 Q400 F2.5
G00 X200 Z200
M05 M09
M30

Why a cycle, not hand passes: the tool must re-enter at the identical Z each pass or the flanks shift. G76 handles the synchronized start, the decreasing depth and the finish pass so the thread is consistent.

⑤ Drilling / centre-drilling the end

Goal: start a hole in the face for later drilling

Centre drill then drill (lathe drilling)

O1006 (DRILL FACE)
G21 G18 G40 G80 G99 G54
T2020 (centre drill)
G97 S2500 M03
G00 X0 Z5
G01 Z0.5 F0.05       (spot / centre)
G00 Z5
T2121 (twist drill 8)
G97 S1200 M03
G00 X0 Z5
G74 Z-30 R1 Q5 F0.08  (peck drill, retract to R each peck)
G00 Z5
G00 X200 Z200
M05 M09
M30

Why a centre drill first: it gives the twist drill a true starting point on the centreline so it does not wander. On a lathe, drilling is a Z-axis move with the spindle turning; G74 is the peck cycle that breaks the chip and clears swarf.

⑥ Parting off

Parting: the blade feeds radially through to separate the finished part A part held near the chuck. The parting blade starts at the OD and feeds inward in X, passing slightly past the centreline, until the finished part separates and drops clear. feed to X−1 finished part Feed past centre; the part separates cleanly
Figure 9 — Parting feeds the blade radially through the bar. Command a small negative X so the blade passes the centreline and the part drops free. Use low fixed RPM and a sharp blade — the tool is deep and weak.
Goal: separate the part at Z-50

Parting-off program

O1007 (PART OFF Z-50)
G21 G18 G40 G80 G99 G54
T4040 (parting blade)
G97 S400 M03        (low fixed RPM)
G00 X52 Z-50
G01 X-1 F0.05       (feed past centre)
G00 X52
Z2
G00 X200 Z200
M05 M09
M30

Why G97 and low RPM: parting is near the centreline, so G96 would over-speed the spindle, and the blade is the weakest tool in the turret. Low, fixed RPM and a slow feed keep it from chattering or snapping. A small negative X guarantees a clean break.

The one rule that spans all six: come out in X before you move in Z. Every crash in this section happens because a tool retracts along Z while still at cutting diameter, or rapid-feeds in X from the wrong side and clips a shoulder. The safe retract (X first, then Z, to a common point) is what makes any tool block re-runnable on its own.

10G71: The One Cycle That Pays for Learning It

If you learn a single canned cycle, make it this one. G71 turns a profile — steps, tapers, radii — in roughing passes from one definition of the finished shape. Hand-writing those passes is where most transcription errors, and most scrap, come from.

G71 roughing — two blocks, then the profileFANUC-style; confirm the P/Q range on your control
G71 U2 R1U2 = depth of cut per pass, 2 mm. R1 = retract after each pass, 1 mm. The control repeats the cut at this depth until the profile is roughed.
G71 P10 Q20 U0.5 W0.1 F0.25P10 Q20 = the sequence-number range that defines the finish profile. U0.5 W0.1 = finish allowance left in X (0.5 mm radius) and Z (0.1 mm) for the finish pass. F0.25 = roughing feed.
N10 G00 X… Z…The profile definition starts here. Program the finished shape, not the stock removal — the cycle works out the passes.
… profile moves …G01/G02/G03 describing the part. No depth-of-cut thinking here; just the target geometry.
N20 G01 X…The profile definition ends at this block. Keep moves monotonic in Z within the range or the roughing gets confused.
G70 P10 Q20The finish pass: follows the same profile at the finish feed and spindle speed set just before it, removing the U/W allowance.
O1008 (G71 SHAFT)
G21 G18 G40 G80 G99 G54
G50 S2500
T0101
G96 S180 M03
G00 X60 Z2

G71 U2 R1
G71 P10 Q20 U0.5 W0.1 F0.25
N10 G00 X20 Z1
N11 G01 Z-15 F0.15
N12 X30
N13 Z-35
N14 X44
N15 Z-60
N20 G01 X58
G70 P10 Q20            (finish pass)

G00 X200 Z200
M05 M09
M30
Why it beats hand passes: you describe the shape once. Change the diameter and you change one number, not twenty. The roughing depths, retracts and passes are the control’s job.
Why the allowance matters: leaving 0.5 mm in X for the finish pass means the finish cut is a light, consistent skim — better surface, and the wear offset corrects the dimension without fighting the roughing.
G71 alarms you will meet. A profile that is not monotonic in Z (it steps back toward the chuck inside the range) triggers an alarm on most controls. A start point that is not clear of the stock causes the first pass to plunge. And if P and Q point at the wrong blocks, the control roughs the wrong shape. Define the profile from the chuck end outward, keep Z moving one way, and verify the range before you run.
This is the highest-SEO-value single topic in lathe programming, so it deserves its own deep-dive page. A dedicated G71 rough-turning guide is planned for this hub — until then, the pattern above is the version you will use on 90% of shaft work.

11G76: A Thread in One Line Pair

Hand-writing thread passes means computing depths, leads and retreats for every pass and getting them identical each time. G76 collapses that to two blocks and, more importantly, keeps every pass synchronized at the start of the thread.

G76 two-line thread cycle60° V-thread shown; angle is set in the first block
G76 P010060 Q50 R0.05P packs four values: 01 = two finish passes (sometimes unused), 00 = chamfer, 60 = thread angle in degrees, plus the leading zeros are the pull-out. Q50 = minimum cut depth 0.05 mm. R0.05 = finish allowance.
G76 X18.40 Z-18.0 P1300 Q400 F2.5X = minor diameter (Ø18.4 for an M20×2.5). Z = thread end. P1300 = thread depth 1.3 mm, in microns (1300). Q400 = first-pass depth 0.4 mm. F2.5 = the lead (pitch × starts; 2.5 mm here).

Why a cycle and not hand passes

Every thread pass must start at the same Z or the flanks shift and the thread is scrap. Doing that by hand means re-positioning to the exact start each pass. G76 handles the synchronized start, the decreasing depth and the pull-out so the thread is consistent pass to pass.

The numbers that bite

The thread depth P is in microns — P1300 is 1.3 mm, not 1300 mm. The angle in the first block must match your insert (60° for a standard V-thread). And the minor diameter must be computed from the thread standard, not eyeballed, or the bolt will not fit.

Never single-block through the middle of a thread. If you stop a threading cycle and restart from a middle block, the tool will not be at the synchronized start and the next pass cuts a different flank. Let a thread run to completion, or restart from the G76 call with the tool clear.

12Prove It Out Before You Cut Metal

A program that reads correctly on paper can still crash a machine. Proving-out is the set of checks between “written” and “running the part” that turns a possible crash into a cheap discovery. This is where experience shows — and where EEAT is earned.

Graphics simulation and air cut verify the tool path before cutting The part silhouette is shown with the programmed tool path overlaid as a polyline, and a ghost tool position. A callout marks that simulation verifies the path with no stock, and an air cut runs the spindle with the tool offset clear of the material. ghost tool Graphics sim: no stock Air cut: spindle on, tool offset clear
Figure 10 — Simulate first (verify the path against the part with no material), then air-cut with the spindle running and the offset pulled off the stock. Only then cut the first real part — and measure it before the second.

Dry run

Executes the program with the spindle and feed off, often with rapid overrides, so you watch the motions without cutting. Catches gross errors in order and position.Use: every new program, first pass

Single block

Runs one block per cycle-start press. Lets you stop and verify after any suspicious move — especially before the first cutting block of each tool.Use: first article, suspicious moves

Feed override

Drops the feed to 5–10% for the first cut, then raises it once the chip looks right. Cheap insurance against a wrong F word.Use: first contact with stock

Graphics simulation

The control draws the tool path over the part shape with no material present. Catches crashes, wrong retracts and profile errors before you touch the machine.Use: before dry run, complex profiles

Air cut

Runs the program with the spindle on and the tool offset pulled clear of the stock, so everything spins and moves but nothing is removed. Confirms spindle direction, coolant and sync.Use: after sim, before first part

The order is the method. Graphics simulation → dry run → air cut → first part at low feed → measure → correct wear offset → run. Skipping steps does not save time; it moves the discovery from the screen to the scrap bin.

13Eight Errors That Actually Crash Lathes

These are not theoretical. They are the eight failures that show up most often on the floor — each with the cause that a careful program prevents, and the fix that stops it recurring.

1

Tool crash into the part or chuck

Cause

  • Rapid move in Z while still at cutting diameter
  • Approach point not clear of the blank
  • Mid-program restart from a block that assumes a safe position

Fix

  • Retract in X before Z; approach 2–3 mm clear
  • Make every tool block self-contained (section 02)
  • Never restart mid-program without confirming state
2

Wrong tool offset

Cause

  • Tool call T0101 but offset register points at a different tool
  • Geometry offset never set, or set to the wrong datum
  • Reused a program on a machine with different offset numbers

Fix

  • Confirm T-number and offset register match at setup
  • Verify the geometry offset by touching off before the run
  • Standardize station = offset number in the shop
3

Wrong feed or speed

Cause

  • G99 vs G98 mix-up: F0.2 becomes 0.2 mm/min, the insert rubs
  • Left in G20 inch mode from a previous job
  • Forgot the feed override is at 0% from a prior setup

Fix

  • Startup block forces G99 and G21 every time
  • Single-block and low feed override on first contact
  • Check override position before pressing cycle start
4

Wrong tool number called

Cause

  • Turret station in the program does not match the tool loaded
  • Same physical tool in a different station after a tool change
  • Typo in the T address

Fix

  • Cross-check the tool list against the turret before running
  • Label stations; keep a setup sheet per part
  • Air-cut to confirm the right tool engages
5

G71 / G70 alarm

Cause

  • Profile not monotonic in Z inside the P–Q range
  • Start point not clear of stock, first pass plunges
  • P/Q point at the wrong blocks

Fix

  • Define the profile from the chuck end outward, Z one direction
  • Start the profile clear of the blank
  • Verify the sequence range before running
6

Over-travel

Cause

  • Coordinate set so a move exceeds the machine limit
  • Z0 placed so a long part runs past the carriage travel
  • Safe point X200 Z200 beyond the machine envelope

Fix

  • Confirm travel limits against the program extremes
  • Place Z0 so the full length is within travel
  • Set the safe point inside the machine envelope
7

Diameter vs radius programming

Cause

  • Program written for radius mode, run on a diameter-mode control
  • Inherited program from a control set the other way
  • Assumed X is radius because of milling habit

Fix

  • Confirm the control’s diameter/radius parameter before the first run
  • Read the part print in diameter and program accordingly
  • Air-cut and measure the first diameter before committing
8

G96 / G97 confusion

Cause

  • G96 left active into a groove or part-off; spindle over-speeds
  • G97 left active into a finish pass; surface speed varies
  • No RPM clamp (G50 S) with G96 near centre

Fix

  • State the speed mode explicitly in every tool block
  • Add G50 S____ under G96 near centre
  • Use G97 for grooving and parting by rule

14The Workflow, and How Goldcattle Develops a Turning Program

A turning program is the last output of a process, not the first step. The order below is the difference between “we can run this” and “we can run this repeatedly, in tolerance, on any shift.”

The programming workflow

1DFM reviewCan the part be turned? Tolerances, radii, undercuts, material.
2Process orderWhich operations, in which sequence, what stays for finishing.
3Fixture & datumHow it is held, where Z0 and the work offset live.
4Tool selectionWhich tools, which stations, which inserts and nose radius.
5Cutting parametersSpeeds, feeds, depths from material and machine power.
6SimulateGraphics sim, dry run, air cut — before any stock is cut.
7FAI & productionFirst-article inspection, wear-offset correction, then the run.

How Goldcattle develops a turning program

At our Xiamen workshop the program is never the first thing we write. A part goes through the same disciplined path every time, because a program is only as good as the process behind it — and the process is what lets us hold tolerance across a batch and across shifts.

  1. DFM and quote feedback

    We read the drawing for turnability: tolerances that need a second op, radii the insert cannot reach, undercuts that need a live tool or a sub-spindle, materials that work-harden. If the part is better made another way, we say so before you pay for it.

  2. Process routing

    Which operations, in which order, and what stays as stock for the finishing pass. The routing is a document, not a memory — so the night shift runs the same plan as the day shift.

  3. Fixture and datum

    How the part is held and where Z0 lives. We choose the datum so facing allowance never shifts a downstream dimension, and so the part can be flipped for op 2 without losing the reference.

  4. Tool and insert selection

    Which stations, which insert geometries and nose radii. The tool list is cross-checked against the turret before the first run, and station numbers are standardized to offset numbers.

  5. Cutting parameters

    Speeds, feeds and depths set from the material and the machine’s power — not copied from a forum. Grooving and parting get low fixed RPM; finishes get constant surface speed.

  6. Simulation and first article

    Graphics sim, dry run, air cut, then the first part at low feed. We measure it, put the correction in the wear offset, and only then release the batch.

  7. Production with SPC

    Key dimensions are checked per batch, not per part, using statistical checks so drift is caught before it becomes scrap. The program stays the same; the wear offset carries the correction.

15Frequently Asked Questions

What is a CNC turning program?
It is an ordered list of G-code and M-code blocks that tells a lathe which tool to use, how fast the spindle turns, how the tool moves and at what feed, and in what order. A sound program always shares the same skeleton: a safe startup block that forces a known modal state, a tool call, spindle and coolant, an approach, the cutting moves or canned cycle, a safe retract, then an end. The code is the notation; the engineering is the process behind it.
What is the difference between G96 and G97?
They decide what the S word means. Under G96 (constant surface speed) S is the target cutting speed in metres per minute, and the control raises RPM as the tool nears the centre to keep that speed constant — good for finishing. Under G97 (constant RPM) S is simply revolutions per minute, fixed regardless of diameter — used for grooving and parting where G96 would over-speed the spindle. With G96 you normally add an RPM clamp (G50 S____) so the spindle cannot run away at the centre.
What does G71 do on a CNC lathe?
G71 is the rough-turning canned cycle. You define the finished profile once (between two sequence numbers) and the control makes the roughing passes at the depth and feed you set, leaving a finish allowance. A following G70 runs the finish pass along the same profile. It removes the transcription errors that come from hand-writing every roughing pass, and one diameter change updates the whole part.
What is G76 used for?
G76 is the multiple-pass thread cycle. Two blocks define the thread — the angle, depth and pull-out in the first, the minor diameter, length, depth and lead in the second — and the control cuts the thread in progressively shallower synchronized passes. The synchronization is the point: every pass starts at the same Z, so the flanks stay aligned, which hand-written passes struggle to do.
How do I write my first CNC turning program?
Start from the skeleton, not the code: safe startup block, tool call, spindle and coolant, approach, cutting, retract, end. Program in diameter on X with Z0 on the finished face. Use the safe startup block (G21 G18 G40 G80 G99 G54) so the modal state is known. Write one tool block at a time, retract in X before Z, and prove it out (simulate, dry run, air cut) before cutting the first part. Copy a template from your machine’s manual, do not invent one.
What is the safe startup block and why do I need it?
It is a line near the top of the program — typically G21 G18 G40 G80 G99 G54 — that forces the control into a known modal state: millimetres, ZX plane, compensation cancelled, canned cycle cancelled, feed per revolution, and the work offset. G-codes are modal and persist across programs, so without this block you cannot know what state the previous job left the control in. It costs one line and prevents a whole category of crash.
How do I choose the right feed and speed?
From the material and the tool, not from memory. Speed is set by the cutting-speed recommendation for the material and the insert, expressed as surface speed (G96) or RPM (G97); feed is set by chip load per revolution (G99). Harder or gummy materials get lower speed and careful feed; fragile tools like grooving blades get low RPM and slow feed. Start the first part at 5–10% feed override and raise it once the chip looks right.
What is the difference between a geometry offset and a wear offset?
The geometry offset locates the tool tip relative to the turret reference point and is set once at setup by touching off. The wear offset is a separate fine adjustment on the same tool that you change after measuring the first part — you put the correction there instead of re-teaching geometry, which keeps the setup value clean and lets you drift a dimension without disturbing the datum. Nose-radius compensation (G41/G42) is a third idea: it shifts the whole path so you can program the finished profile and let the control add the radius.
How do I avoid crashing a CNC lathe?
Retract in X before moving in Z, approach 2–3 mm clear of the blank, and make every tool block self-contained so it can be restarted safely. Confirm the tool number matches the offset register, confirm diameter vs radius mode, and state the speed mode (G96/G97) in every tool block. Prove the program out with simulation, dry run and air cut before cutting, and never restart mid-program without confirming the modal state.
Can CAM software generate the turning program for me?
Yes, for profiles with arcs, C-axis milling, sub-spindle transfer or Swiss-type work, CAM is the practical route — it posts the G-code from your tool paths. But CAM will not save a bad process plan; it just produces the wrong tool path faster. The decisions in this guide (datum, operation order, fixture, speeds, prove-out) are still yours. For simple shafts, hand G-code is often quicker and fully transparent at the machine.

The CNC Turning Programming Hub

This page is the hub for lathe programming on the site. Start here for the whole picture, then follow the deeper guides as you need them. Related pages already published are linked; the ones marked planned are being written to keep this cluster complete.

About this guide

Author: Goldcattle CNC Engineering Team Reviewed by: Goldcattle Production Turning Team Experience: 26 years custom precision manufacturing, Xiamen China

This guide is written from how we actually develop turning programs in our workshop — DFM first, then a process plan, then a program proved out before any production, then SPC through the batch. It is reviewed by the engineers who run the machines, not generated from a generic source. For our full capability see Manufacturing Services. Sources include our machine builders’ programming manuals and the company process standards documented on that page.

Need Precision CNC Turning for Custom Parts?

Send us the drawing. Our turning team returns a process plan, a realistic lead time and a quote — and we will tell you if the part is better made another way.

✔ 26 years turning custom parts in Xiamen ✔ 100+ in-house machines, no outsourced production ✔ Diameter, length and thread tolerances checked per batch ✔ First-article inspection before production ✔ Drawings reviewed for manufacturability, free

Upload a STEP, DWG, DXF or PDF. If you only have a sketch, send that — we will help with the rest.

Recommended Reading