So far we have covered the CNC lathe’s structure, axes, coordinate system, cutting tools, cutting values, workholding, tool offsets and workpiece zero. Now we can move on to programming. CNC lathe programming is not just writing G and M codes one after another; the programmer first brings together the drawing, the X–Z coordinates, the tools, the operation sequence, the cutting speed and feed, the tool offsets and the safe approach/retract points, then turns this information into a program the control unit understands.
In this course, programming examples are mostly explained with FANUC-based ISO programming logic, because FANUC systems are common. But the aim is not to memorise every key of a panel; it is to teach the logic of the program: G00, G01, G02, G03, X, Z, S, F, T and many basic M codes work with the same or very similar logic on other control systems too. However, some canned-cycle formats, the tool-call scheme, the G-code groups, the chuck/tailstock/conveyor M codes and the work-zero method can vary by control unit and machine builder; the exact usage must be verified from the machine’s manual.
xF0x9Fx93xB7 IMAGE AREA — CNC program structure: program, block and word concepts
Suggested alt text: “CNC lathe program structure program block word”
1. What Is a CNC Program? Program, Block, Word
A CNC program is the set of commands that tells the control unit in what order and under what conditions the part is machined. It contains the program number, line numbers, unit/coordinate system, tool number, speed/cutting speed, spindle direction, coolant, motion commands, X–Z coordinates, feed and program-end commands. Three basic concepts: the program (all commands that machine the whole part), the block (each command line, e.g. G01 X40.0 Z-30.0 F0.20;) and the word (a letter + numeric value, e.g. G01, X40.0, F0.20). A block can consist of the N sequence number, G preparatory function, axis addresses, F feed, S speed, T tool and M auxiliary-function words.
A simple CNC lathe program can look like this:
% O1001 ; G21 ; (metric) G40 ; (comp. cancel) G99 ; (feed per rev) G50 S2500 ; (max speed) T0101 ; (tool + offset) G97 S900 M03 ; (constant speed, spindle CW) M08 ; (coolant on) G00 X52.0 Z2.0 ; (safe approach) G01 Z0 F0.20 ; X0 ; (facing) G00 X42.0 Z2.0 ; G01 X40.0 F0.25 ; Z-30.0 ; G00 X100.0 Z100.0 ; (safe retract) M09 ; M05 ; M30 ; %
2. Basic Addresses
In a CNC program each letter has a specific meaning:
| Address | Meaning | Example |
|---|---|---|
| O | Program number (identity) | O1001 |
| N | Line / sequence number | N10 |
| G | Preparatory function (motion/mode) | G01 |
| X | Diameter-direction coordinate (absolute) | X40.0 |
| Z | Length-direction coordinate (absolute) | Z-30.0 |
| U | Incremental move in X | U-4.0 |
| W | Incremental move in Z | W-20.0 |
| F | Feed (mm/rev or mm/min) | F0.20 |
| S | Spindle speed (rpm/cutting speed) | S900 |
| T | Tool and offset call | T0101 |
| M | Auxiliary function | M03 |
O is the program identity; two programs cannot be saved with the same number on one control. N numbers make the program easier to read, help find a line and mark start/end in cycles; usually given in tens (N10, N20, N30) so a line like N15 can be inserted later. X–Z are absolute, U–W incremental. F is read by the active feed mode (mm/rev in G99, mm/min in G98). S is speed or cutting speed by the active G96/G97. T0101 in the common scheme is station 1 + offset 1 (the exact structure varies by machine).
3. Block End, Percent Sign and Decimal Point
Program lines usually end with ; (block end). In some FANUC programs a % at the start and end marks the program limits in data transfer. The decimal point is very important: X40.0 = 40 mm; in some systems X40 is also read as 40 mm, but in some old parameter setups, without a decimal point the value is read in the smallest input unit. So as a safe habit, dimensions are written explicitly (X40.0, Z-30.0, F0.20); a decimal-point error causes very large coordinate mistakes.
4. Safe Start Line and Modal Codes
A safe start line is used at the beginning to cancel modes left from the previous program; e.g. G21 G40 G99; (metric, comp. cancel, feed per rev). G codes are of two kinds: modal (once written, effective until another code of the same group comes) and one-shot (effective only in the block where written). For example:
G01 X40.0 Z-20.0 F0.20 ; Z-40.0 ; (G01 still active) X50.0 ; (G01 still active)
G01 is written on the first line; even if not repeated on the next lines, the G01 mode stays active, so the moves continue as linear cutting. The absence of a G code on a line does not mean there is no motion mode. Codes in the same modal group replace each other: G00/G01/G02/G03 are one motion group; G96/G97, G98/G99, G40/G41/G42 are their own groups.
5. Basic G Codes
| G Code | Function |
|---|---|
| G00 | Rapid positioning (non-cutting) |
| G01 | Linear cutting move |
| G02 | Clockwise circular move (arc) |
| G03 | Counter-clockwise circular move |
| G04 | Dwell |
| G18 | X–Z working plane |
| G20 / G21 | Inch / Metric unit system |
| G28 | Reference return |
| G40 / G41 / G42 | Nose radius comp. cancel / left / right |
| G50 | Maximum spindle-speed limit |
| G96 / G97 | Constant cutting speed / constant speed |
| G98 / G99 | Feed per minute / per revolution |
G21 is metric (common in Turkey), G20 inch; accidentally running in G20 creates a serious size/crash risk, so it is stated clearly at the start. G18 is the lathe base plane X–Z; G02/G03 arc directions and compensation are read relative to it (usually the default on most lathes).
xF0x9Fx93xB7 IMAGE AREA — Difference between G00 rapid and G01 cutting move
Suggested alt text: “CNC lathe G00 rapid positioning and G01 linear cutting”
6. G00 — Rapid Positioning
G00 moves the tool rapidly from one point to another (non-cutting repositioning). Absolute: G00 X50.0 Z5.0;; incremental: G00 U10.0 W5.0;. G00 is not used for cutting: its purpose is not to remove material but to go to a safe point outside the raw stock (e.g. G00 X52.0 Z2.0;), and G01 is used when cutting starts. Entering the part with G00 can cause the tool to crash at high speed, the tip to break, the part to slip and the turret to be damaged.
Motion path: the physical path G00 follows can vary by control/parameter; the X and Z axes may move at different speeds and form a broken path. So it is not enough for only the start/end to be safe; the path between the two points must also be checked (chuck, part, tailstock, holder). Especially with a long boring bar/drill, moving X and Z on the same line rapidly can be risky; the safe method is sometimes to separate the axes:
G00 X100.0 ; G00 Z100.0 ;
Which axis moves first must be decided by the real machine geometry.
7. G01 — Linear Cutting (Straight, Facing, Taper, Chamfer)
G01 moves the tool linearly at the given feed. Format: G01 X... Z... F...;. With G01 you machine straight OD, facing, shoulders, tapers and chamfers.
(Straight OD: from X42 Z2, Ø40 x 30 mm) G01 X40.0 F0.20 ; (plunge to Ø40) Z-30.0 ; (cut to Z-30) (Facing: from X52 Z0, raw Ø50) G01 X0 F0.15 ; (OD to centre) (Taper: X30 Z-20 → X40 Z-50) G01 X40.0 Z-50.0 F0.15 ; (two axes synced) (Chamfer: 2x45°, X36 Z0 → X40 Z-2) G01 X36.0 Z0 F0.12 ; G01 X40.0 Z-2.0 ;
In a taper, X and Z change at the same time; the control synchronises the two axes. In a chamfer, due to diameter programming, a 2 mm radial chamfer corresponds to a 4 mm diameter change in X. Since G01 is modal, it may not be repeated on consecutive lines; in training it can be written explicitly on important moves for readability.
xF0x9Fx93xB7 IMAGE AREA — G02 and G03 arc directions (CW / CCW) in the X–Z plane
Suggested alt text: “CNC lathe G02 G03 circular move directions”
8. G02 and G03 — Circular Move (Arc)
G02 is clockwise, G03 counter-clockwise circular motion. To program an arc you need the end point, the radius or centre info and the direction. The direction is not guessed by looking at the part from the front; it is determined by looking from start to end in the X–Z plane (the turret being front/rear and the screen drawing can mislead). The safest method: mark the start/end point and the centre, follow the direction in the X–Z plane, verify with simulation.
(With R: if the radius is known) G03 X40.0 Z-25.0 R5.0 F0.12 ; (With I–K: centre incremental from the start point) G02 X... Z... I... K... F... ;
The R method is easy for simple arcs; but for arcs over 180° and when there are two possible solutions, care is needed with the sign rule. I and K are the distances of the arc centre from the start point in X (I) and Z (K); whether they are read as diameter or radius must be verified per control system. At the start, simple radii can be shown with R and complex arcs with I–K. Common mistakes: start/end not matching R, writing G03 instead of G02, not checking the active plane and ignoring nose radius compensation.
9. G04, G28 and G40
G04 (dwell): makes the tool/program wait a set time (G04 X1.0; or G04 P1000; — the unit is verified per control). Used for a short dwell at a groove bottom, for the spindle to reach speed or a chuck open/close delay; unnecessary long dwells increase time. G28 (reference return): G28 U0 W0; takes the current position as an intermediate point and sends the axes to reference; it is not safe in every case — if the tool is inside the part, in a hole with a long bar or near the tailstock, it can crash, so retract to a safe point first. G40 (comp. cancel): cancels any previous G41/G42 compensation at the start; G41/G42 select left/right nose radius compensation and are covered in detailed profile programming. In basic first programs, simple straight/perpendicular moves can be done in G40 mode.
10. Using Speed and Feed Codes in the Program
(Constant speed) G97 S900 M03 ; (900 rpm, spindle CW) (Constant cutting speed — limit first!) G50 S2500 ; (max 2500 rpm) G96 S180 M03 ; (180 m/min) (Feed per revolution) G99 ; G01 Z-30.0 F0.20 ; (0.20 mm/rev) (Feed per minute) G98 ; G01 Z-30.0 F150.0 ; (150 mm/min)
Setting a maximum-speed limit with G50 before using G96 is important for safety. G98 and G99 are the same modal group; writing one cancels the other.
11. Basic M Codes
| M Code | Function |
|---|---|
| M00 | Program stop (continue with cycle start) |
| M01 | Optional stop |
| M03 | Spindle clockwise |
| M04 | Spindle counter-clockwise |
| M05 | Spindle stop |
| M08 / M09 | Coolant on / off |
| M30 | Program end and rewind |
M00 stops the program (to check a part, measure); M01 stops only when optional stop is on. M03/M04 is the spindle direction — the wrong direction makes the tip enter the part with its back face and break; “clockwise” can be confused by which side you view the machine from, so the builder’s definition is used. M05 stops the spindle; do not measure the part or open the door before it fully stops. M08/M09 is coolant; on some machines M07/M08 can be different coolant types. M30 ends the program and rewinds.
Machine-specific M codes: chuck open/close, tailstock forward/back, chip conveyor, part catcher, bar feeder, driven tool and sub-spindle M codes vary by builder (M10 might be chuck clamp on one machine and different on another). So when a machine-specific M code is given, this note applies: this M code is an example; check the machine’s M-code table for the exact code.
xF0x9Fx93xB7 IMAGE AREA — The order of a tool section (call → speed → coolant → approach → cut → retract)
Suggested alt text: “CNC lathe program tool section order”
12. General Order of a Tool Section and Safe Approach
T0101 ; (call the tool) G50 S2500 ; (limit max speed) G96 S180 M03 ; (cutting speed + spindle dir.) G99 ; (feed mode) M08 ; (coolant on) G00 X52.0 Z2.0 ; (safe approach) G01 ... F... ; (cutting moves) ... G00 X100.0 Z100.0 ; (safe retract) M09 ; M05 ; (coolant off, spindle stop)
If the raw stock is Ø50, the tool is not sent straight to G00 X40 Z2; (X40 is inside the raw stock); the safe approach is G00 X52.0 Z2.0; or G00 X55.0 Z3.0;. The retract can be G00 X100.0 Z100.0;, but it must be verified that this coordinate is truly safe (stroke, tailstock, tool length, turret indexing envelope).
13. First Basic Program: Facing + Straight OD Turning
Raw Ø50, target Ø40, length 30 mm, face Z0, tool T0101:
% O1001 ; G21 ; G40 ; G99 ; G50 S2500 ; T0101 ; G97 S900 M03 ; M08 ; G00 X52.0 Z2.0 ; (safe approach: outside raw Ø50) G01 Z0 F0.20 ; (come to the face by cutting) G01 X0 F0.15 ; (facing: OD to centre) G00 X52.0 Z2.0 ; (back to a safe point) G01 X40.0 F0.20 ; (plunge to Ø40) G01 Z-30.0 F0.25 ; (cut 30 mm along the length) G00 X100.0 Z100.0 ; (safe retract) M09 ; M05 ; M30 ; %
Line by line: O1001 program number; G21 metric; G40 comp. cancel; G99 feed per rev; G50 S2500 speed limit; T0101 tool+offset; G97 S900 M03 constant speed + spindle CW; M08 coolant. G00 X52 Z2 safe approach 2 mm outside the raw diameter (1 mm radial); G01 Z0 come to the face by cutting; G01 X0 facing. Then plunge to X40 and cut along to Z-30, retract with G00 X100 Z100, end with M09/M05/M30. Warning: Ø50 to Ø40 in one pass is 5 mm radial stock; this program only shows the motion logic, in reality several roughing passes, a finishing allowance or G71 are needed.
14. Stepped-Pass and Profile Programs
Reducing Ø50 to Ø40 not in one pass but in several:
G00 X52.0 Z2.0 ; G01 X46.0 F0.25 ; Z-30.0 ; (rough pass 1) G00 X52.0 ; Z2.0 ; G01 X42.0 ; Z-30.0 ; (rough pass 2) G00 X52.0 ; Z2.0 ; G01 X40.4 ; Z-30.0 ; (final rough diameter) G00 X52.0 ; Z2.0 ; G01 X40.0 F0.12 ; Z-30.0 ; (finish)
Ø46, Ø42, Ø40.4 are roughing passes and Ø40 is finishing. This lengthens the code; later the G71 cycle will do these roughing passes automatically. But the student should first see what the tool does on each pass. Two-step and chamfered profile examples:
(Two steps: Ø40 [Z0..Z-20], Ø30 [Z-20..Z-50]) G00 X42.0 Z2.0 ; G01 X40.0 Z0 F0.15 ; Z-20.0 ; X30.0 ; Z-50.0 ; (Chamfered: 2x45° front chamfer, Ø40, Ø30) G00 X36.0 Z2.0 ; G01 Z0 F0.12 ; X40.0 Z-2.0 ; (chamfer) Z-20.0 ; X30.0 ; Z-50.0 ; (Radiused: X30 Z-20 → R5 → X40 Z-25) G01 X30.0 Z-20.0 F0.12 ; G03 X40.0 Z-25.0 R5.0 ;
These programs define the geometric profile; but as the tool enters radially from X40 to X30 it can remove 5 mm of stock, so the profile should not be machined in one pass without roughing. In the radius, the G03 direction must be checked against the real profile and not run without simulation. So programming and the machining plan are thought of together.
xF0x9Fx93xB7 IMAGE AREA — Full program and the resulting part profile (tidy N-numbered program)
Suggested alt text: “CNC lathe full program example and part profile”
15. A Tidier Program and Comment Lines
% O1002 (SIMPLE OD TURNING) N10 G21 G40 G99 ; N20 G50 S2500 ; N30 T0101 ; N40 G97 S900 M03 ; N50 M08 ; N60 G00 X52.0 Z2.0 ; N70 G01 Z0 F0.20 ; N80 X0 F0.15 ; N90 G00 X52.0 Z2.0 ; N100 G01 X46.0 F0.25 ; N110 Z-30.0 ; N120 G00 X52.0 ; N130 Z2.0 ; N140 G01 X42.0 ; N150 Z-30.0 ; N160 G00 X52.0 ; N170 Z2.0 ; N180 G01 X40.4 ; N190 Z-30.0 ; N200 G00 X52.0 ; N210 Z2.0 ; N220 G01 X40.0 F0.12 ; N230 Z-30.0 ; N240 G00 X100.0 Z100.0 ; N250 M09 ; N260 M05 ; N270 M30 ; %
The (SIMPLE OD TURNING) in parentheses is a comment line; the control does not run it as a command, it makes the program easier to read. Comments can hold info like (T01 OD ROUGHING), (MATERIAL C45), (RAW DIA 50); they inform the operator and ease maintenance. Since non-Latin character support may not exist on every control, plain Latin letters are mostly used in machine programs; very long comments can waste memory on old controls.
16. In-Block Order and Multiple Commands
A block can hold commands from different groups: G97 S900 M03; (speed mode + value + spindle direction) or G01 X40.0 Z-30.0 F0.20; (motion + coordinate + feed). But conflicting G codes from the same group are not written on the same line (G00 G01 X40; is wrong). Although many controls can read words in a different order, a standard, readable order is preferred: N... G... X... Z... U... W... R... F... S... T... M... ;. Each shop may have its own programming standard.
17. Relationship Between Basic Programming and Canned Cycles
In this lesson we write the roughing passes, retract and profile move one by one, so the student sees what the tool does on each pass. Canned cycles automate these moves: for example G71 automatically creates the roughing passes with a set pass depth, following the profile. But to use G71 correctly you must know G00, G01, coordinates and profile logic; so the basic motion codes are learned before cycles.
18. Safe Programming Habits and Common Mistakes
- Write the modes explicitly at the start; do not trust G codes left from the previous program.
- Set S and F again after a tool call; a new tool must not run with the old values.
- Use a G50 limit with G96; keep G00 outside the raw stock (do not cut with G00).
- Verify M codes from the real machine (especially chuck/tailstock/conveyor).
- Check the X and Z signs; one minus error can send the tool into the part.
- Verify the tool offset number; run the first time in single block and low rapid.
Common mistakes: confusing G00/G01 (cutting with a rapid move); expecting G00 while G01 is active (being modal, it moves at cutting feed); forgetting the F value; not checking the G98/G99 mode (F0.20 vs F200 are very different); confusing G96/G97 (S180 = 180 m/min or 180 rpm); choosing the wrong M03/M04 direction; writing X as radius (X20 for Ø40); forgetting the Z minus sign; choosing the wrong G02/G03 direction; not doing a safe retract at the end.
19. Before Running the Program
No example on this page should be run without being checked on the real machine first. Before running, verify the control’s G-code system, tool/offset numbers, workpiece zero, raw diameter, chuck jaws, tailstock position, spindle direction, S and F values, safe approach points and retract paths. The first run is done with graphic check, single block, low-speed motion, low rapid override and a controlled first chip. Programming knowledge does not replace the safety check at the machine.
Lesson Summary
- Program = blocks, block = words (letter + value). Basic addresses: O, N, G, X/Z (absolute), U/W (incremental), F, S, T, M.
- G00 rapid positioning (not cutting), G01 linear cutting, G02/G03 arc; G18 X–Z plane, G21 metric.
- Modal codes stay active until a new code comes; the decimal point and signs are critical.
- G50/G96/G97 and G98/G99 set the speed-feed modes; basic M codes manage the spindle, coolant and program flow.
- A tool section: call → speed/direction → coolant → safe approach → cut → safe retract. An example program is only run under control.
Assessment Test
- Which G code moves the tool rapidly (non-cutting) and is not used for cutting? A) G01 B) G00 C) G02 D) G04
- (True/False) G01 is modal; even without a G code on the next line, the linear-cutting mode stays active.
- Which unit system does
G21select? A) Inch B) Metric C) Feed per revolution D) Constant cutting speed - Which M code stops the spindle? A) M03 B) M08 C) M05 D) M30
- In which direction is G03 circular motion? A) Clockwise B) Counter-clockwise C) Linear D) Rapid positioning
- (True/False) When using G96, the max speed is limited with G50 to prevent a dangerous speed near the centre.
- What is the
(SIMPLE OD)in parentheses for? A) A cutting command B) A comment line, not run C) Calls a tool D) Stops the program
Show Answer Key
1) B – G00 is rapid positioning, not used for cutting. 2) True – G01 is modal. 3) B – G21 is the metric system. 4) C – M05 stops the spindle. 5) B – G03 is counter-clockwise. 6) True – G96 needs a G50 speed limit. 7) B – Text in parentheses is a comment; the control does not run it.
Open-Ended Questions
- Explain the concepts of program, block and word with an example.
- Explain the difference between G00 and G01 and why G00 is not used for cutting.
- What does a modal G code mean? Explain with an example.
- Write the general order of a tool section.
- Write a simple program draft that reduces Ø50 raw to Ø40 (roughing + finishing).
Summary and Next Lesson
In this lesson we learned manual programming with basic G and M codes: addresses, the block/word structure, G00/G01/G02/G03, speed-feed modes, M codes and full program examples. You can now turn coordinate, tool, cutting-value and setup knowledge into a real program. In the next lesson we move on to canned cycles (G71, G76, G74) and learn how to automate, with a single command, the roughing passes and thread/groove operations we wrote out one by one here.