Chapter VII

CNC Programming and Setup

Red Seal Practice study guide with diagrams.

CNC Programming and Setup

Introduction to Computer Numerical Control (CNC)

CNC programming is the set of coded instructions that control the movements of a computer numerical control machine tool. For the Red Seal exam, you must master not only the syntax of the codes, but also the logic of setup, coordinate systems, tool compensations, and fixed cycles. This chapter covers the essentials to succeed on questions in this domain, which represents approximately 15% of the machinist trade exam.

Fundamental Principles of CNC Machines

A CNC machine executes a program composed of blocks (lines) containing G-codes (preparatory functions, geometry) and M-codes (auxiliary functions, machine commands). The controller interprets these codes sequentially, unless jump or repeat functions are programmed.

The three main axes are defined according to the right-hand rule:

X: horizontal axis (often the length on a lathe, the width on a milling machine)
Y: horizontal axis perpendicular to X (milling machine only)
Z: vertical axis (aligned with the main spindle)

On a CNC lathe, the X-axis is radial (diameter) and the Z-axis is longitudinal. Additional axes (B, C, U, V, W) are rotary or secondary axes.

Coordinate Systems and Reference Points

SystemDescriptionTypical Use
**Machine (M)**Fixed origin point, defined by the manufacturerAbsolute reference, never modified
**Work (W)**Part origin, defined by the operatorAll programmed movements
**Part (P)**Local system on the partSub-programs, repetitions

The machine reference point (often noted as machine Z0 or X0) is detected by limit switches or encoders. The homing procedure must be performed at startup to establish the absolute position.

Origin offsets are stored in the machine registers (G54 to G59). Each register contains the coordinates of the part origin relative to the machine origin. G54 is the default offset; G55 to G59 are additional offsets for multiple setups.

G-Codes: Preparatory Functions

Basic G-Codes (Group 01)

CodeFunctionGroupModal
G00Rapid positioning01Yes
G01Linear interpolation01Yes
G02Circular interpolation clockwise01Yes
G03Circular interpolation counterclockwise01Yes

These codes are modal: once programmed, they remain active until another code from the same group is called. For example, after a G01, all subsequent blocks with X, Y, Z coordinates will perform linear interpolation at the programmed feed rate (F).

G00 moves the tool at the machine's maximum speed, without machining. The path is not necessarily a straight line: each axis moves at its own maximum speed, which can produce diagonal paths. Common trap: a G00 with a programmed F feed rate does not use that feed rate; the speed is the machine rapid speed.

G01 requires a feed rate (F) in units per minute (mm/min or in/min). Without a programmed F, the machine uses the last memorized F or signals an error.

G02/G03 require the definition of the radius (R) or the I, J, K parameters (coordinates of the arc center relative to the starting point). The syntax is:

G02 X... Y... R... F...

G02 X... Y... I... J... F...

For an arc greater than 180°, the radius R must be negative (R-). A full circle (360°) cannot be programmed with R; you must use I, J, K.

Plane Selection and Compensation G-Codes

CodeFunctionGroup
G17XY plane02
G18XZ plane02
G19YZ plane02
G40Cutter radius compensation cancel07
G41Cutter radius compensation left07
G42Cutter radius compensation right07
G43Tool length compensation positive08
G44Tool length compensation negative08
G49Tool length compensation cancel08

Cutter radius compensation (G41/G42) allows you to program the theoretical part profile without accounting for the actual tool radius. The controller offsets the tool perpendicular to the profile. The direction is determined by looking in the direction of feed: G41 = tool to the left of the profile, G42 = tool to the right.

Important rule: compensation must be activated on a movement block (G00 or G01), never on an arc block. Likewise, cancellation (G40) must be done on a linear block. Entry and exit transitions use lead-in arcs (G02/G03) or straight lines, depending on machine parameters.

Tool length compensation (G43/G44) uses the H register corresponding to the tool number (H01 for T01, etc.). The register value is the tool length measured from the reference point. G43 adds the value, G44 subtracts it. G49 cancels the compensation.

Fixed Cycles (Group 09)

Fixed cycles simplify repetitive operations (drilling, tapping, boring). They are defined by a G-code and parameters:

CodeCycleKey Parameters
G81Simple drillingX, Y, Z, R, F
G82Drilling with dwellX, Y, Z, R, P, F
G83Peck drillingX, Y, Z, R, Q, F
G84Rigid tappingX, Y, Z, R, F (pitch = F/Speed)
G85Boring with feed retractX, Y, Z, R, F
G86Boring with spindle stopX, Y, Z, R, F
G80Fixed cycle cancel

The R parameter is the retract plane (Z position where the tool moves in rapid), Z is the final depth, Q is the depth of each pass (G83), P is the dwell time in milliseconds (G82).

Drilling example:

G90 G54 G00 X50. Y25. S1200 M03

G43 H01 Z10. M08

G99 G81 X50. Y25. Z-20. R2. F150

X75. Y40. (repeats the cycle at the new position)

G80 (cancellation)

G98 (return to initial plane) and G99 (return to R plane) control the retract position between holes. G98 is necessary if obstacles (clamps, stops) are above the R plane.

Other Important G-Codes

CodeFunctionNote
G20/G21Inches/millimetersG21 = mm (default in Canada)
G28Return to machine reference pointOften via an intermediate point
G90/G91Absolute/incrementalG90 = absolute (default)
G94/G95Feed per minute/revolutionG95 = mm/rev (turning)
G96/G97Constant cutting speed/constant RPMG96 = m/min, G97 = RPM
G50Maximum speed limit (turning)Used with G96

M-Codes: Auxiliary Functions

M-codes control the machine's logic functions. They are executed once (non-modal), unless otherwise indicated.

CodeFunctionType
M00Program stopNon-modal
M01Optional stop (button)Non-modal
M02End of programNon-modal
M03Spindle on, clockwiseModal
M04Spindle on, counterclockwiseModal
M05Spindle stopNon-modal
M06Tool changeNon-modal
M08Coolant onModal
M09Coolant offNon-modal
M30End of program with rewindNon-modal
M98Sub-program callNon-modal
M99Sub-program returnNon-modal

M00 stops the machine for manual intervention (inspection, part change). M01 is identical, but only if the optional stop button is activated on the panel. M02 ends the program without rewinding; M30 ends and rewinds to the beginning.

M06 triggers a tool change. The tool must be called with T (e.g., T03) before or in the same block. The typical sequence is:

T03 M06 (tool change to tool 3)

G43 H03 Z10. (length compensation with H03 register)

Turning Programming

Coordinate Systems in Turning

On a CNC lathe, the axes are:

X: radial, programmed in diameter (double the radius value)
Z: longitudinal, positive toward the tailstock

The part origin is typically placed at the front face of the part (Z0) and on the axis of rotation (X0). Radial dimensions are programmed in diameter, which corresponds to the dimensions on the engineering drawing.

Turning Cycles

CodeCycleDescription
G71Longitudinal roughingMulti-pass, follows the profile
G72Transverse roughingFace roughing
G73Pattern roughingFollows the profile with retracts
G70FinishingRe-passes the profile with finishing feeds
G74Axial groovingPecking on Z
G75Radial groovingPecking on X
G76Multi-pass threadingAutomatic threading
G92Simple threadingThreading in one block

G71 is the most commonly used cycle. The typical syntax:

G71 U... R...

G71 P... Q... U... W... F...

U: depth of cut (radius)
R: retract after each pass
P: number of the first profile block
Q: number of the last profile block
U: finishing allowance in X (diameter)
W: finishing allowance in Z

The profile is defined between blocks P and Q, with G00/G01/G02/G03 movements. The profile blocks must not contain F (the feed rate is defined in the cycle).

G76 for threading:

G76 P... Q... R...

G76 X... Z... P... Q... F...

P: number of finishing passes, thread angle (60°), finish type
Q: minimum depth of cut
R: finishing allowance
X: final thread diameter
Z: final position
P: thread height (radius × 1000)
Q: first pass depth
F: thread pitch

Constant Cutting Speed (G96)

In turning, G96 maintains a constant cutting speed (m/min) by adjusting the spindle speed according to the diameter. The formula is:

V = π × D × N / 1000

where V = cutting speed (m/min), D = diameter (mm), N = spindle speed (RPM).

G50 limits the maximum spindle speed to avoid excessive speeds at small diameters. Example:

G96 S180 M03 (cutting speed 180 m/min)

G50 S2500 (maximum speed 2500 RPM)

Milling Programming

Milling Cycles and Helical Interpolation

In milling, the fixed cycles are identical to those for drilling, but you also use:

G12/G13: circular interpolation with pocket milling (some controllers)
G68/G69: coordinate system rotation
G51/G50: scaling (scale factor)

Helical interpolation (G02/G03 with a simultaneous Z movement) is used for milling holes or threads. The syntax:

G02 X... Y... Z... I... J... F...

The helix pitch is determined by the ratio between the Z movement and the number of revolutions.

Sub-programs and Repetitions

Sub-programs (M98/M99) allow you to repeat identical sequences. The syntax:

M98 P1234 L5 (calls sub-program 1234, 5 times)

The sub-program ends with M99. Sub-programs can be nested (up to 4 levels depending on the controller).

Setup and Implementation

Setup Procedure

101.Machine inspection: check oil levels, compressed air, spindle and slideway condition.
102.Machine homing: perform homing on all axes.
103.Workpiece mounting: install the chuck, vise, or specialized fixture. Check parallelism and squareness with a dial indicator.
104.Tool mounting: install tools in the magazine or turret. Measure each tool (length and radius) and enter the values in the H and D registers.
105.Establishing the part origin: use a probe, dial indicator, or trial cut to define G54.
106.Dry run: run the program with reduced feed rate (feed override button at 10-25%) and without a part or with a dummy part.
107.Trial machining: machine the first part with allowances, check dimensions, and adjust compensations.

Tool Measurement

Tool length is measured from the reference point (spindle face or turret) to the tool tip. This value is entered in the corresponding H register. Tool radius (end mill) or nose radius (turning tool) is entered in the D register for G41/G42 compensation.

Measurement methods:

Off-machine pre-setting: uses an optical or contact pre-setter
On-machine measurement: uses a measuring probe or a trial cut on a known surface
Trial cut measurement: machine a surface and measure with a micrometer, then calculate the offset

Program Verification

Graphical simulation on the controller or on external software (CAM) allows you to detect path errors, collisions, and over-travel. Program proofreading by a second machinist is a recommended practice.

Programming Calculations

Speed and Feed Calculations

Spindle speed:

N = (V × 1000) / (π × D)

where N = RPM, V = m/min, D = mm.

Feed rate:

F = f × N (milling, f = feed per tooth × number of teeth)

F = f × N (turning, f = feed per revolution)

Example: 12 mm end mill, 4 flutes, V = 80 m/min, feed per tooth = 0.05 mm/tooth.

N = (80 × 1000) / (π × 12) = 2122 RPM

F = 0.05 × 4 × 2122 = 424 mm/min

Arc Coordinate Calculations

For an arc of radius R connecting two points, the center can be calculated by:

I = X_center - X_start

J = Y_center - Y_start

K = Z_center - Z_start

If the radius R is used, the value must be positive for an arc < 180° and negative for an arc > 180°.

Allowance and Compensation Calculations

Radius compensation offsets the tool by a distance equal to the tool radius (D register) relative to the programmed profile. If the actual radius differs from the nominal radius, the adjustment is made by modifying the D register value.

Example: 10 mm end mill (5 mm radius) machines a profile. If the part is 0.1 mm too large, increase the D register by 0.05 mm (because the compensation acts on the radius, not the diameter).

Canadian Standards and Safety

Applicable Standards

CSA Z434 (Computer numerically controlled machine tools) is the main Canadian standard for the design, installation, and safe use of CNC machines. It covers electrical, mechanical, and functional safety requirements.

The Canadian Electrical Code, Part I (CE Code) (C22.1) applies to electrical installations of machine tools. Rule 8-200 concerns equipment grounding requirements.

The Hazardous Products Act and WHMIS (Workplace Hazardous Materials Information System) apply to cutting fluids and chemicals used in machining.

CNC Programming Safety

Verify that edit mode is disabled before execution
Use the emergency stop button in case of abnormal behavior
Never open protective doors during the cycle
Check the limit switches and software limits before the first trial
Ensure the program is saved before any modification
Use single block mode for the first execution

Pitfalls to Avoid

149.Confusing G00 and G01: G00 does not cut and ignores the F feed rate. A G00 with a depth of cut causes tool breakage.
150.Forgetting G43: without length compensation, the tool can collide with the part or fixture.
151.Programming an arc > 180° with positive R: the controller generates an error or an incorrect arc. Use negative R or I, J, K.
152.Forgetting G80 after a fixed cycle: the cycle remains active and applies to subsequent blocks, causing unintended drilling.
153.Confusing H and D registers: H is for length, D is for radius. An inversion causes erroneous offsets.
154.Using G96 without G50: spindle speed can exceed the mechanical limit at small diameters.
155.Programming in inches (G20) by mistake: check the unit system before execution. In Canada, the metric system (G21) is the default standard.
156.Forgetting M30: without an end of program, the controller may execute stray blocks or loop indefinitely.
157.Not checking the retract plane (R): an R plane that is too low causes collisions with clamps or obstacles.
158.Modifying a program during execution: modifications must be made in edit mode, then reloaded.

Exam Tips

Memorize the code groups: questions often focus on code modality (e.g., "Which code cancels G01?" → G00 or G02/G03, because they are in the same group 01).
Master speed calculations: calculation questions are common. Practice with the formulas N = (V × 1000) / (π × D) and F = f × N.
Understand fixed cycle logic: know how to identify the Q, R, P parameters and their units (Q in mm, P in ms).
Know the differences between turning and milling: in turning, X is in diameter; in milling, X and Y are Cartesian coordinates.
Read questions carefully: multiple-choice questions often contain wording traps (e.g., "Which is NOT a modal code?").

Summary

CNC programming uses G-codes (geometry) and M-codes (machine functions), organized into modal groups.
Coordinate systems include the machine origin (M), work origin (W/G54-G59), and part origin (P).
Tool compensations (G41/G42 for radius, G43/G44 for length) are essential for accurate machining.
Fixed cycles (G81-G89) simplify drilling, tapping, and boring; they must be cancelled with G80.
In turning, G71/G70 for roughing/finishing, G76 for threading, and G96/G50 for constant cutting speed.
Setup includes homing, tool mounting, measuring lengths/radii, and establishing G54.
Speed and feed calculations are fundamental: N = (V × 1000) / (π × D), F = f × N.
Safety is governed by CSA Z434 and the Canadian Electrical Code, Part I (CE Code) (Rule 8-200).
Common errors include forgetting G43, confusing G00/G01, arcs > 180° with positive R, and omitting G80.

Mastering CNC programming requires an integrated understanding of codes, calculations, and setup procedures. For the Red Seal exam, focus on general principles and practical applications, rather than the specifics of any particular controller (Fanuc, Siemens, Haas). The questions focus on universal concepts, applicable to all CNC machines conforming to Canadian standards.

Ready to test this chapter?

Practice with exam-aligned questions and timed simulations.

Start Practicing Free