CNC Turning: Writing a Complete G-Code Program (Using Appropriate G & M Codes)

CNC Turning: Writing a Complete G-Code Program (Using Appropriate G & M Codes)

Verified Sources
Nov 4, 2026

You’re asking for a CNC program to turn a specific component/profile from raw material MS 32×5032 \times 50 mm using cutting speed V=40V=40 m/min and feed f=0.1f=0.1 (assumed as 0.10.1 mm/rev), with suitable data for depth of cut, and using the figure-specified profile dimensions.

⚠️ Important limitation: In your prompt, the “component shown in the figure” is referenced, but the original figure/image is not included in the message. Without the actual profile dimensions (diameters/lengths/fillets/steps/tapers), I cannot produce the correct turning path (the correct sequence of XX–ZZ moves and any arc/taper logic).

Below is a complete, ready-to-use template course section showing how to write a turning program using appropriate G and M codes for a lathe (including speed/feed setup, stock setup, roughing/finishing cycles, threading optional, tool offsets, and safety moves). You then fill in the profile values directly from your figure.

What you must provide from the figure

To finalize the exact code, you need:

  • Turning sequence of diameters (major XX values) and their axial locations (major ZZ values)
  • Any taper definition (start/end diameter and lengths)
  • Any fillets/radii (or whether to approximate them with arcs)
  • Required finish diameter tolerances (to decide whether to use G70 finishing)

Typical lathe coordinate assumptions used in this course section

  • Tool moves in XX (diameter) and ZZ (axial distance).
  • Program starts at a safe clearance position.
  • Workpiece zero is at the facing/part datum (commonly Z=0Z=0 at the front face).
  • In ZZ, negative values move into the work.

type="warning" title="Figure is required to generate the final contour" content="Please paste/upload the original component/profile figure with all dimensions. Without the contour (step/taper/radius locations), any specific XX–ZZ toolpath would be a guess and would not match your component."

CNC Turning Program Structure (What “appropriate G & M codes” means)

For turning, the program typically includes:

  • keyword motion G-codes (commonly G00G00, G01G01, G02G02, G03G03)
  • keyword spindle control G/M-codes (M03M03, M04M04, M05M05)
  • keyword spindle-related M-codes (model-dependent, but M03/M05M03/M05 are common)
  • keyword coolant M-codes (M08/M09M08/M09)
  • keyword tool offset (often G54G54 work offset, plus tool nose radius compensation G41/G42G41/G42 if used)
  • keyword roughing cycle (G71G71 on many Fanuc-compatible controls)
  • keyword finishing cycle (G70G70)

Note: Exact syntax for G71G71 varies by controller family (Fanuc vs others). This section uses a common Fanuc-style G71G71 form; you may need minor edits to match your machine’s control manual.

Key machining calculations (from V=40V=40 m/min and f=0.1f=0.1)

1) Spindle speed from cutting speed

Cutting speed VV relates to spindle speed NN and workpiece diameter DD by:

V=πDN1000V=\frac{\pi D N}{1000}

So:

N=1000VπDN=\frac{1000V}{\pi D}

Using V=40V=40 m/min:

N=1000×40πD=40000πDN=\frac{1000\times 40}{\pi D}=\frac{40000}{\pi D}
  • If roughing starts near stock diameter D≈32D \approx 32 mm:
N≈40000π×32≈398 rev/minN \approx \frac{40000}{\pi \times 32} \approx 398\ \text{rev/min}

2) Feed per revolution

You gave feed =0.1=0.1; for turning, this is typically mm/rev:

f=0.1 mm/revf = 0.1\ \text{mm/rev}

3) Depth of cut assumption (suitable data)

Since you didn’t specify a depth-of-cut, pick a conservative roughing step. For example:

  • keyword dad_a (radial depth) = 0.5 mm
  • Number of roughing passes depends on how much diameter reduction the profile requires. For many classroom problems, da=0.5d_a=0.5 mm works well.

If your profile is stepped or includes tapers/radii but can be described with a contour path, a common strategy is:

  1. Describe the turning contour in a “profile definition” block (the X–Z points).
  2. Use G71G71 to rough to near-net shape using that contour.
  3. Use G70G70 to finish with a clean pass.

Mermaid overview of the contour workflow

Step-by-step: Turn the figure profile with a correct CNC program (template you can fill)

  1. 1
    Step 1

    Pick a work offset (e.g., G54G54), cancel modal stuff, and move to a safe XX (clearance) and ZZ (away from part). Use G00G00 for rapid.

  2. 2
    Step 2

    Use M03M03 (clockwise spindle) and M08M08 (coolant on) per your machine’s standard. Stop later with M05M05 and M09M09.

  3. 3
    Step 3

    Use N=1000VπDN=\frac{1000V}{\pi D} with V=40V=40 m/min and the relevant turning diameter DD (start with stock D≈32D\approx 32 mm). Program SS accordingly.

  4. 4
    Step 4

    Set F=0.1F=0.1 (mm/rev assumed). Select a roughing tool and a finishing tool (or use one tool with both cycles).

  5. 5
    Step 5

    Convert every step/taper/radius location into an ordered X–Z sequence in diameter coordinates. This becomes the X–Z ‘profile definition’ used by G71G71.

  6. 6
    Step 6

    Choose radial depth per roughing pass (e.g., 0.5 mm) as the ‘dd’ parameter in the G71G71 roughing block.

  7. 7
    Step 7

    Use G71G71 with start/end blocks so the control knows the region to machine.

  8. 8
    Step 8

    Use G70G70 to take the final finishing cuts to the exact contour.

  9. 9
    Step 9

    Move away with G00G00, stop spindle (M05M05), coolant off (M09M09), then end (M30M30).

Template CNC Program (Fanuc-like Lathe: G71 + G70)

How to fill the contour

In the contour definition block, you will replace the placeholder points (X=?,Z=?)(X=?, Z=?) with values from your figure:

  • XX = diameter (not radius)
  • ZZ = axial position from part face at Z=0Z=0 (negative into the part)

Note: Tool numbers (T1/T2), tool geometry offsets (like 01010101), and exact G71G71 parameter positions are machine/control dependent. Keep G00/G01/G71/G70/M03/M08/M05/M09/M30G00/G01/G71/G70/M03/M08/M05/M09/M30 consistent, but verify the controller’s exact G71G71 format.

Example skeleton (fill-in contour points)

1% 2O0001 (TURNING PROFILE - TEMPLATE) 3 4(--- SAFETY / SETUP ---) 5G21 (MM) 6G40 (CANCEL CUTTER COMP) 7G80 (CANCEL CYCLES) 8G54 (WORK OFFSET) 9T0101 (TOOL 1: ROUGH) 10G00 X40.0 Z5.0 11 12(--- SPINDLE SPEED & FEED ---) 13(Stock D=32mm, V=40 m/min => N ~ 398 rpm => set S ~ 400) 14S400 M03 15G97 (CONSTANT RPM - adjust if your control prefers G96) 16M08 (COOLANT ON) 17G00 X34.0 Z1.0 18F0.1 19 20(--- G71 ROUGHING PARAMETERS ---) 21(DEPTH OF CUT ASSUMPTION d=0.5mm RADIAL; adjust as needed) 22G71 U0.5 R0.2 23 24(START/END PROFILE RANGE) 25G71 P100 Q200 U0.1 W0.05 F0.1 26 27N100 (PROFILE START) 28(Replace these with figure-derived steps/taper/radii) 29G00 X32.0 Z0.0 30G01 X30.0 Z-10.0 31G01 X28.0 Z-25.0 32G01 X26.0 Z-40.0 33G01 X26.0 Z-50.0 34(If taper: use G01 with linear X change vs Z) 35(If radius/fillet: use G02/G03 arcs if your control supports it in the profile) 36 37N200 (PROFILE END) 38 39(--- FINISHING ---) 40T0202 (TOOL 2: FINISH) 41G70 P100 Q200 42 43(--- RETRACT & END ---) 44G00 X100.0 Z100.0 45M09 46M05 47M30 48%

type="tip" title="Best practice: keep the contour description in diameter coordinates" content="On most lathes, the XX axis in turning programs is diameter. If the figure gives radius or if you compute using radius, convert to diameter before programming XX (e.g., X=2rX = 2r)."

Tool motion details: when to use G01 vs G02/G03 vs taper

Linear moves (most profiles)

  • Use keyword G01G01 for straight changes in XX and/or ZZ.

Circular arcs (fillets/rounded corners)

  • Use keyword G02G02 and keyword G03G03 for radius/arc transitions (only if your profile includes arcs and your control’s profile-cycle supports them correctly).

Tapers

  • Use keyword G01G01 where XX changes linearly as ZZ advances.

From figure to working G-code (learning roadmap)

Extract X–Z geometry

1. Interpret the figure

List all diameters and axial lengths; decide Z=0Z=0 datum."

Speed and feed

2. Compute parameters

Compute NN from V=40V=40 and stock/starting diameter; set F=0.1F=0.1."

G71 + G70

3. Choose machining strategy

Use roughing cycle, then finish cycle."

Program G & M codes correctly

4. Write the program

Safety moves, spindle/coolant, contour blocks, rough and finish."

Collision check + trial run

5. Validate

Ensure safe clearance and verify toolpath ordering."

Example starting spindle speed for V=40 m/min (assuming turning diameter D)

Compute N=2˘00b1000VπDN=\u200b\frac{1000V}{\pi D}; actual S may be rounded to machine limits.

Common pitfalls and controller differences (read before finalizing)

Lathe Turning G71 Roughing + G70 Finishing (Fanuc-style) — Tutorial

Knowledge Check

Question 1 of 4
Q1Single choice

In turning programs on many lathes, what does the X-axis typically represent?