CNC
Computer Numerical Control automates machine tools. Learn machine axes and coordinate systems, G-code and M-code syntax, work offsets, tool compensation, feeds and speeds for milling, CAM workflows and precision.
A CNC machine follows a program line by line, moving axes with servo motors and ball screws to micrometre precision. It turned machining from a craft into a repeatable, programmable process.
Engineers today mostly generate programs with CAM software, but understanding G-code is essential for editing, debugging and running machines safely.
An analogy
Like turn-by-turn navigation for a cutting tool: 'go here fast, now cut there slowly along this arc, change tools, stop'.
Machine anatomy and axes
A CNC machine has a controller, servo drives, motors, ball screws and linear guides, spindle, tool changer and feedback encoders. Axes follow the right-hand rule: Z along the spindle, X usually the longest travel. 3-axis mills cover most parts; 5-axis machines tilt the tool for complex surfaces (impellers, moulds).
G-code and M-code
G-codes command motion and modes (G00 rapid, G01 linear, G02/G03 arcs, G17 plane, G90/G91 absolute/incremental, G54 work offset). M-codes switch machine functions (spindle, coolant, tool change, program end). Many codes are modal — they stay active until changed.
Offsets and compensation
Work offsets (G54–G59) tell the machine where the part zero is. Tool-length offsets (G43 H) account for each tool's length; cutter-radius compensation (G41/G42 D) lets you program the part outline while the controller offsets by the tool radius — and adjust for wear.
CAM workflow
CAD model → CAM (Fusion, Mastercam, NX CAM, hyperMILL) → choose tools and strategies (adaptive roughing, contour finishing, drilling) → simulate for collisions → post-processor generates machine-specific G-code → set up and prove out on the machine.
Accuracy and smart machining
Accuracy depends on ball-screw pitch error, backlash, thermal growth and servo tuning; controllers compensate with pitch-error tables and thermal models. Look-ahead and jerk limiting keep high-speed contouring smooth. Probing, in-process measurement and digital twins enable lights-out production.
Formulas
- Milling spindle speed:
S = 1000 V_c / (π D) - Milling feed rate:
F = f_z · z · S - Positioning resolution:
Δ = screw pitch / encoder counts per rev - Circular interpolation:
G02/G03 X Y I J (I, J = centre offset from start)
Worked example: Milling feed and speed
Given: Ø10 mm carbide end mill, 4 flutes; V_c = 100 m/min, f_z = 0.05 mm
Find: S and F.
- S = 1000 × 100/(π × 10) = 3183 rpm
- F = 0.05 × 4 × 3183 = 637 mm/min
Answer: S ≈ 3180 rpm, F ≈ 640 mm/min (used in the program above)
Worked example: Axis resolution
Given: Ball screw pitch 5 mm; Encoder 10 000 counts/rev
Find: Position resolution.
- Δ = 5/10 000 = 0.0005 mm
Answer: 0.5 µm per count
Mill a 50 × 30 mm rectangular pocket outline (ISO/Fanuc style)
%
O1001 (RECT CONTOUR 50x30, 10 MM END MILL)
G21 G17 G90 G40 G49 G80 (mm, XY plane, absolute, cancel comps/cycles)
T01 M06 (tool 1: 10 mm 4-flute end mill)
G54 (work offset: part zero)
S3180 M03 (spindle 3180 rpm clockwise)
G00 X-10. Y-10. (rapid to start, outside part)
G43 H01 Z5. (tool length comp, clear height)
M08 (coolant on)
G01 Z-3. F300 (plunge 3 mm deep)
G41 D01 X0. Y0. F640 (cutter comp left, feed 640 mm/min)
G01 Y30.
G01 X50.
G01 Y0.
G01 X0.
G40 G01 X-10. Y-10. (cancel comp, leave part)
G00 Z50. M09 (retract, coolant off)
M05 (spindle stop)
G91 G28 Z0. (home Z)
M30 (end, rewind)
%G00 = rapid, G01 = linear feed, G41/G40 = cutter-radius compensation on/off, G43 = tool-length offset, G54 = work coordinate system, M03/M05 = spindle on/off, M06 = tool change, M08/M09 = coolant, M30 = program end.
Arc and drilling cycle
G00 X20. Y0.
G01 Z-2. F200
G03 X0. Y20. I-20. J0. F500 (CCW quarter arc, centre at X0 Y0)
G00 Z5.
G81 G98 X10. Y10. Z-15. R2. F120 (drill cycle: to Z-15, retract to R2)
X30. (repeat hole at X30 Y10)
G80 (cancel canned cycle)Canned cycles (G81 drill, G83 peck drill, G84 tap) repeat at each new XY position until G80 cancels them.
Common mistakes
- Wrong: Running a new program at full speed first time. — Right: Simulate, then dry-run with single-block and reduced rapids; check offsets.
- Wrong: Forgetting to cancel cutter comp or canned cycles. — Right: Use a safe-start line (G40 G49 G80) and cancel modes explicitly.
Applications
- Job shops & prototyping: 3-axis mills and lathes for one-offs and small batches.
- Aerospace & medical: 5-axis machining of blisks, implants and structural parts.
- Mass production: Transfer lines and machining centres with pallet changers and robots.
Key takeaways
- S = 1000V/πD; F = f_z·z·S.
- G-codes move, M-codes switch functions; many are modal.
- Offsets (G54, G43, G41) separate program from setup.
- Simulate and prove out before production.
Going further
- 5-axis kinematics: Tool-centre-point control (TCPM/RTCP) keeps the tip on path while rotary axes tilt.
- Adaptive clearing: Constant tool engagement toolpaths allow full-flute depth at high feeds.
- Industry 4.0: MTConnect/OPC UA stream machine data for OEE monitoring and predictive maintenance.