CNC Turning: Writing a Complete G-Code Program (Using Appropriate G & M Codes)
You’re asking for a CNC program to turn a specific component/profile from raw material MS mm using cutting speed m/min and feed (assumed as 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 – 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 values) and their axial locations (major 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 (diameter) and (axial distance).
- Program starts at a safe clearance position.
- Workpiece zero is at the facing/part datum (commonly at the front face).
- In , 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 – 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 , , , )
- keyword spindle control G/M-codes (, , )
- keyword spindle-related M-codes (model-dependent, but are common)
- keyword coolant M-codes ()
- keyword tool offset (often work offset, plus tool nose radius compensation if used)
- keyword roughing cycle ( on many Fanuc-compatible controls)
- keyword finishing cycle ()
Note: Exact syntax for varies by controller family (Fanuc vs others). This section uses a common Fanuc-style form; you may need minor edits to match your machine’s control manual.
Key machining calculations (from m/min and )
1) Spindle speed from cutting speed
Cutting speed relates to spindle speed and workpiece diameter by:
So:
Using m/min:
- If roughing starts near stock diameter mm:
2) Feed per revolution
You gave feed ; for turning, this is typically 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 (radial depth) = 0.5 mm
- Number of roughing passes depends on how much diameter reduction the profile requires. For many classroom problems, mm works well.
Strategy: Use G71 Roughing + G70 Finishing (recommended)
If your profile is stepped or includes tapers/radii but can be described with a contour path, a common strategy is:
- Describe the turning contour in a “profile definition” block (the X–Z points).
- Use to rough to near-net shape using that contour.
- Use 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)
- 1Step 1
Pick a work offset (e.g., ), cancel modal stuff, and move to a safe (clearance) and (away from part). Use for rapid.
- 2Step 2
Use (clockwise spindle) and (coolant on) per your machine’s standard. Stop later with and .
- 3Step 3
Use with m/min and the relevant turning diameter (start with stock mm). Program accordingly.
- 4Step 4
Set (mm/rev assumed). Select a roughing tool and a finishing tool (or use one tool with both cycles).
- 5Step 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 .
- 6Step 6
Choose radial depth per roughing pass (e.g., 0.5 mm) as the ‘’ parameter in the roughing block.
- 7Step 7
Use with start/end blocks so the control knows the region to machine.
- 8Step 8
Use to take the final finishing cuts to the exact contour.
- 9Step 9
Move away with , stop spindle (), coolant off (), then end ().
Template CNC Program (Fanuc-like Lathe: G71 + G70)
How to fill the contour
In the contour definition block, you will replace the placeholder points with values from your figure:
- = diameter (not radius)
- = axial position from part face at (negative into the part)
Note: Tool numbers (T1/T2), tool geometry offsets (like ), and exact parameter positions are machine/control dependent. Keep consistent, but verify the controller’s exact 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 axis in turning programs is diameter. If the figure gives radius or if you compute using radius, convert to diameter before programming (e.g., )."
Tool motion details: when to use G01 vs G02/G03 vs taper
Linear moves (most profiles)
- Use keyword for straight changes in and/or .
Circular arcs (fillets/rounded corners)
- Use keyword and keyword for radius/arc transitions (only if your profile includes arcs and your control’s profile-cycle supports them correctly).
Tapers
- Use keyword where changes linearly as advances.
From figure to working G-code (learning roadmap)
Extract X–Z geometry
1. Interpret the figureList all diameters and axial lengths; decide datum."
Speed and feed
2. Compute parametersCompute from and stock/starting diameter; set ."
G71 + G70
3. Choose machining strategyUse roughing cycle, then finish cycle."
Program G & M codes correctly
4. Write the programSafety moves, spindle/coolant, contour blocks, rough and finish."
Collision check + trial run
5. ValidateEnsure safe clearance and verify toolpath ordering."
Example starting spindle speed for V=40 m/min (assuming turning diameter D)
Compute ; 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
In turning programs on many lathes, what does the X-axis typically represent?