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:
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
| System | Description | Typical Use |
|---|---|---|
| **Machine (M)** | Fixed origin point, defined by the manufacturer | Absolute reference, never modified |
| **Work (W)** | Part origin, defined by the operator | All programmed movements |
| **Part (P)** | Local system on the part | Sub-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)
| Code | Function | Group | Modal |
|---|---|---|---|
| G00 | Rapid positioning | 01 | Yes |
| G01 | Linear interpolation | 01 | Yes |
| G02 | Circular interpolation clockwise | 01 | Yes |
| G03 | Circular interpolation counterclockwise | 01 | Yes |
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
| Code | Function | Group |
|---|---|---|
| G17 | XY plane | 02 |
| G18 | XZ plane | 02 |
| G19 | YZ plane | 02 |
| G40 | Cutter radius compensation cancel | 07 |
| G41 | Cutter radius compensation left | 07 |
| G42 | Cutter radius compensation right | 07 |
| G43 | Tool length compensation positive | 08 |
| G44 | Tool length compensation negative | 08 |
| G49 | Tool length compensation cancel | 08 |
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:
| Code | Cycle | Key Parameters |
|---|---|---|
| G81 | Simple drilling | X, Y, Z, R, F |
| G82 | Drilling with dwell | X, Y, Z, R, P, F |
| G83 | Peck drilling | X, Y, Z, R, Q, F |
| G84 | Rigid tapping | X, Y, Z, R, F (pitch = F/Speed) |
| G85 | Boring with feed retract | X, Y, Z, R, F |
| G86 | Boring with spindle stop | X, Y, Z, R, F |
| G80 | Fixed 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
| Code | Function | Note |
|---|---|---|
| G20/G21 | Inches/millimeters | G21 = mm (default in Canada) |
| G28 | Return to machine reference point | Often via an intermediate point |
| G90/G91 | Absolute/incremental | G90 = absolute (default) |
| G94/G95 | Feed per minute/revolution | G95 = mm/rev (turning) |
| G96/G97 | Constant cutting speed/constant RPM | G96 = m/min, G97 = RPM |
| G50 | Maximum 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.
| Code | Function | Type |
|---|---|---|
| M00 | Program stop | Non-modal |
| M01 | Optional stop (button) | Non-modal |
| M02 | End of program | Non-modal |
| M03 | Spindle on, clockwise | Modal |
| M04 | Spindle on, counterclockwise | Modal |
| M05 | Spindle stop | Non-modal |
| M06 | Tool change | Non-modal |
| M08 | Coolant on | Modal |
| M09 | Coolant off | Non-modal |
| M30 | End of program with rewind | Non-modal |
| M98 | Sub-program call | Non-modal |
| M99 | Sub-program return | Non-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:
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
| Code | Cycle | Description |
|---|---|---|
| G71 | Longitudinal roughing | Multi-pass, follows the profile |
| G72 | Transverse roughing | Face roughing |
| G73 | Pattern roughing | Follows the profile with retracts |
| G70 | Finishing | Re-passes the profile with finishing feeds |
| G74 | Axial grooving | Pecking on Z |
| G75 | Radial grooving | Pecking on X |
| G76 | Multi-pass threading | Automatic threading |
| G92 | Simple threading | Threading in one block |
G71 is the most commonly used cycle. The typical syntax:
G71 U... R...
G71 P... Q... U... W... F...
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...
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:
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
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:
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
Pitfalls to Avoid
Exam Tips
Summary
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