CNC Programming Basics

A Complete Beginner's Guide to G-Code Programming

📚 Comprehensive Guide ⏱️ 25 min read 🔧 Updated Jan 2025

What You'll Learn

  • Understanding CNC machine coordinate systems
  • Essential G-codes and M-codes for milling and turning
  • How to structure a complete CNC program
  • Feeds, speeds, and tooling basics
  • Setting up work offsets and tool compensation
  • Common canned cycles for drilling and threading

💡 Quick Reference: Jump to our Interactive G-Code Reference for instant code lookups.

1. What is CNC?

CNC (Computer Numerical Control) is a manufacturing process where pre-programmed computer software dictates the movement of factory tools and machinery. CNC machines can control a range of complex machinery, from grinders and lathes to mills and routers.

Types of CNC Machines

  • CNC Mills - 3-axis, 4-axis, and 5-axis vertical and horizontal machining centers
  • CNC Lathes/Turning Centers - For cylindrical parts, including swiss-type machines
  • CNC Routers - Typically for wood, plastic, and foam
  • CNC Plasma/Laser Cutters - Sheet metal cutting
  • EDM Machines - Wire and sinker electrical discharge machining

Common CNC Controllers

Different manufacturers use different control systems, but the basic G-code language is similar across all:

  • Fanuc - Most common worldwide, orange controls
  • Haas - Common in North America, based on Fanuc
  • Mazak - Mazatrol conversational or G-code
  • Siemens - European standard (840D, 828D)
  • Heidenhain - TNC conversational programming

2. Coordinate Systems & Machine Axes

Cartesian Coordinate System

CNC machines use a 3D coordinate system with X, Y, and Z axes:

  • X-axis - Horizontal movement (left/right when facing the machine)
  • Y-axis - Horizontal movement (toward/away from you)
  • Z-axis - Vertical movement (up/down)

Right-Hand Rule

Point your right thumb toward positive X, index finger toward positive Y - your middle finger points toward positive Z (up).

Machine vs Work Coordinate Systems

Machine Coordinate System (MCS): Fixed origin point set by the machine manufacturer. You never change this.

Work Coordinate System (WCS): Your part's origin point. You set this with work offsets (G54-G59):

  • G54 - Work offset 1 (most commonly used)
  • G55 - Work offset 2
  • G56-G59 - Additional work offsets

Absolute vs Incremental Positioning

  • G90 (Absolute) - All coordinates measured from work zero. X2.0 means "go to X=2.0"
  • G91 (Incremental) - Coordinates measured from current position. X2.0 means "move 2.0 in X from here"

Example: Absolute vs Incremental

G90 G00 X1.0 Y1.0    (Move to X1, Y1 in absolute mode)
G00 X2.0 Y2.0        (Move to X2, Y2)
G00 X0 Y0            (Return to origin)

G91 G00 X1.0 Y1.0    (Move 1" right, 1" forward from current position)
G00 X1.0 Y1.0        (Move another 1" right, 1" forward - now at X2,Y2)
G00 X-2.0 Y-2.0      (Move back 2" left, 2" back - now at origin)

3. G-Codes (Preparatory Functions)

G-codes prepare the machine for a specific operation. They control movement, set modes, and activate cycles.

Motion Commands (Most Important)

Code Function Example
G00 Rapid positioning (no cutting) G00 X2.0 Y3.0
G01 Linear feed move (cutting) G01 X2.0 F10.0
G02 Circular interpolation CW G02 X2.0 Y2.0 I1.0 J0
G03 Circular interpolation CCW G03 X0 Y0 I-1.0 J0

Circular Interpolation (Arcs)

For cutting arcs and circles:

  • I - Distance from start point to arc center in X
  • J - Distance from start point to arc center in Y
  • K - Distance from start point to arc center in Z
  • R - Arc radius (alternative to I, J, K)

Example: Cutting a Circular Pocket

G00 X0 Y0               (Rapid to center)
G01 Z-0.125 F5.0        (Plunge to depth)
G01 X0.5 F10.0          (Move to start of arc)
G03 X0.5 Y0 I-0.5 J0    (Cut full circle CCW)
G00 Z1.0                (Retract)

Plane Selection

  • G17 - XY plane (default for milling)
  • G18 - XZ plane (for lathe or side milling)
  • G19 - YZ plane

Units and Modes

  • G20 - Inch programming
  • G21 - Metric programming (millimeters)
  • G90 - Absolute positioning
  • G91 - Incremental positioning

Canned Cycles (Drilling)

Canned cycles automate common operations like drilling and tapping:

Code Cycle Use Case
G81 Drill cycle Simple through holes
G82 Spot/counterbore Spotting, counterboring with dwell
G83 Peck drill cycle Deep holes, chip evacuation
G84 Tapping cycle Threaded holes
G85 Boring cycle Precision holes
G73 High-speed peck Faster peck drilling
G80 Cancel cycle Exit canned cycle mode

Example: Drilling Pattern with G81

G90 G54 G00 X0 Y0
G43 H1 Z1.0            (Tool length comp, rapid above part)
S1200 M03              (Spindle on 1200 RPM)

G81 Z-0.65 R0.1 F6.0   (Drill cycle: Z depth, R clearance, F feed)
X1.0 Y0                (Drill at X1, Y0)
X2.0                   (Drill at X2, Y0)
X2.0 Y1.0              (Drill at X2, Y1)
X1.0                   (Drill at X1, Y1)
G80                    (Cancel drill cycle)
G00 Z1.0 M05           (Retract, spindle off)

Cutter Compensation

  • G40 - Cancel cutter compensation
  • G41 - Cutter comp left (tool moves left of programmed path)
  • G42 - Cutter comp right (tool moves right of programmed path)
  • G43 - Tool length compensation (H-code)
  • G49 - Cancel tool length compensation

💡 Pro Tip: Cutter compensation (G41/G42) lets you program to the part edge and let the control offset by the tool radius. This makes diameter changes easy without reprogramming.

4. M-Codes (Miscellaneous Functions)

M-codes control machine functions like spindle, coolant, and program flow.

Program Control

Code Function
M00 Program stop (must press cycle start to continue)
M01 Optional stop (only if M01 switch is ON)
M02 Program end
M30 Program end and reset to start

Spindle Control

  • M03 - Spindle on clockwise (CW) - normal milling
  • M04 - Spindle on counter-clockwise (CCW) - for tapping or left-hand tools
  • M05 - Spindle stop
  • S____ - Spindle speed in RPM (e.g., S2000 = 2000 RPM)

Coolant Control

  • M08 - Coolant ON (flood or mist, depending on machine)
  • M09 - Coolant OFF

Tool Changes

  • M06 - Tool change (often combined with T-code: T1 M06)
  • T____ - Select tool number (e.g., T1 = Tool 1)

Example: Tool Change Sequence

G00 G90 G54 G40 G49 G80    (Safety block)
T2 M06                     (Change to Tool 2)
G43 H2 Z1.0                (Tool length comp offset 2)
S3000 M03                  (Spindle 3000 RPM clockwise)
M08                        (Coolant ON)
(... machining operations ...)
M09                        (Coolant OFF)
M05                        (Spindle OFF)
G00 Z1.0                   (Retract)

5. Program Structure

Typical CNC Program Format

A well-structured program follows this general format:

%
O0001 (PART NAME - OPERATION DESCRIPTION)
(TOOL 1 - 0.500 4-FLUTE END MILL)
(TOOL 2 - #3 CENTER DRILL)
(TOOL 3 - 0.201 DRILL)
(MATERIAL: 6061 ALUMINUM)
(STOCK: 4.0 X 3.0 X 1.0)

(--- TOOL 1: FACE AND PROFILE ---)
N10 G90 G20 G17 G40 G49 G80     (Safety line - absolute, inch, XY plane, cancel comp)
N20 T1 M06                      (Load Tool 1)
N30 G54                         (Work offset 1)
N40 S2000 M03                   (Spindle 2000 RPM CW)
N50 G00 X0 Y0                   (Rapid to start position)
N60 G43 H1 Z1.0                 (Tool length comp, rapid above part)
N70 M08                         (Coolant ON)

(Face operation)
N80 G01 Z0 F20.0                (Feed to surface)
N90 G01 X4.0 F12.0              (Face across)
N100 G00 Z1.0                   (Retract)

N110 M09                        (Coolant OFF)
N120 M05                        (Spindle OFF)
N130 G00 Z1.0                   (Safe Z height)
N140 G28 G91 Z0                 (Return to machine home Z)
N150 G28 X0 Y0                  (Return to machine home XY)

(--- TOOL 2: CENTER DRILL ---)
N200 T2 M06                     (Load Tool 2)
N210 G90 G54
N220 S1500 M03
N230 G43 H2 Z1.0 M08

N240 G81 Z-0.2 R0.1 F4.0        (Spot drill cycle)
N250 X1.0 Y1.0                  (Hole 1)
N260 X3.0                       (Hole 2)
N270 G80                        (Cancel cycle)

N280 M09 M05
N290 G28 G91 Z0
N300 G28 X0 Y0

M30                             (Program end and reset)
%

Program Components

  • % or O-number - Program delimiter/number
  • (COMMENTS) - Parentheses for notes, tool lists, setup info
  • N-numbers - Optional line numbers (N10, N20, etc.)
  • Safety Block - G90 G20 G17 G40 G49 G80 ensures clean state
  • Tool Sections - Organize by tool for clarity
  • M30 - Always end with program end and reset

⚠️ Important: Always include a safety block (G90 G20 G40 G49 G80) at the start of your program to cancel any active modes from previous programs.

6. Feeds & Speeds

Spindle Speed

Spindle speed is specified with the S word in RPM:

Calculate RPM from Surface Speed

RPM = (SFM × 3.82) / Tool Diameter (inches)

Or in metric: RPM = (Vc × 1000) / (π × D)

Where:

  • SFM = Surface Feet per Minute (from material/tooling charts)
  • Vc = Cutting speed in meters/minute
  • D = Tool diameter

Example: 1/2" end mill in aluminum

Aluminum SFM = 800 (typical for carbide)

RPM = (800 × 3.82) / 0.5 = 6,112 RPM

S6000 M03    (Rounded to 6000 RPM)

Feed Rate

Feed rate is specified with the F word:

Calculate Feed Rate

Feed Rate (IPM) = RPM × Chip Load × Number of Flutes

Example: 0.5" 4-flute end mill at 6000 RPM, chip load 0.003"

IPM = 6000 × 0.003 × 4 = 72 IPM

F72.0

💡 Use our Speeds & Feeds Calculator for automatic calculations with 60+ materials!

Typical SFM Values (Carbide Tooling)

Material SFM Range Chip Load (per tooth)
Aluminum 800-1200 0.002-0.006"
Mild Steel 250-400 0.002-0.004"
Stainless Steel 150-250 0.001-0.003"
Cast Iron 200-350 0.003-0.005"
Brass 500-800 0.002-0.005"
Plastic 600-1000 0.003-0.008"

7. Tool Management

Tool Numbers and Offsets

  • T-code - Tool number (T1, T2, T3, etc.)
  • H-code - Tool length offset register (usually matches T number)
  • D-code - Tool diameter offset register (for cutter comp)

Tool Change with Compensation

T5 M06                (Change to Tool 5)
G43 H5 Z1.0           (Apply length offset 5, rapid to Z1.0)
G01 G41 D5 X1.0 F10.0 (Apply diameter comp left, offset 5)

Setting Tool Offsets

Length Offsets (H): Measure from spindle gauge line to tool tip

Diameter Offsets (D): Enter actual tool diameter or measured radius

💡 Tip: Use a tool presetter or indicator to set accurate tool lengths before running your program. Incorrect offsets are a common cause of crashes!

8. Basic Milling Operations

8.1 Facing

Removing material from the top surface to create a flat reference:

G00 X-0.5 Y0              (Start off the part edge)
G01 Z0 F20.0              (Feed to surface)
G01 X4.5 F15.0            (Face across)
G00 Z1.0                  (Retract)

8.2 Pocketing

Removing material inside a boundary:

(Rectangular pocket 2" x 1" x 0.5" deep)
(Using 0.5" end mill, helical entry)

G00 X1.0 Y0.5             (Center of pocket)
G01 Z0 F20.0
G01 Z-0.125 F5.0          (First depth)
G01 X0.5 F12.0            (Move to start cutting)
G01 X0.5 Y1.0             (Cut perimeter)
G01 X1.5
G01 X1.5 Y0
G01 X0.5
(Repeat at deeper Z levels)

8.3 Drilling with G81

G90 G54
G00 X0 Y0
G43 H3 Z1.0               (Tool 3 length comp)
S1200 M03 M08

G81 Z-0.75 R0.1 F6.0      (Drill through 0.75" thick part)
X1.0 Y1.0                 (Hole position 1)
X3.0 Y1.0                 (Hole position 2)
X3.0 Y3.0                 (Hole position 3)
X1.0 Y3.0                 (Hole position 4)
G80                       (Cancel drill cycle)

8.4 Tapping with G84

(Tapping 1/4-20 threads)
(Tap drill already completed)

T4 M06                    (Tap)
G97 S500 M03              (500 RPM for tapping)
G00 X1.0 Y1.0
G43 H4 Z1.0

G84 Z-0.6 R0.1 F25.0      (F = RPM / TPI, 500/20 = 25 IPM)
X1.0 Y1.0                 (Tap hole 1)
X3.0 Y1.0                 (Tap hole 2)
G80

📐 For tap drill sizes, see our Tap Drill Chart or use the Tap Drill Calculator.

9. Basic Turning Operations (Lathe)

Lathe programming is similar but uses the XZ plane (G18) where:

  • Z-axis - Parallel to spindle (length of part)
  • X-axis - Perpendicular to spindle (diameter)
  • Note: X values are often diameter, not radius

9.1 Facing Operation

G00 G18 G54 G40           (Safety, XZ plane)
T0101                     (Tool 1, offset 1)
G97 S800 M03              (800 RPM CW)
G00 X2.1 Z0.1             (Rapid to start position)

G01 X-0.062 F0.010        (Face to center, feed in IPR)
G00 Z0.1                  (Retract Z)
G00 X4.0                  (Retract X)

9.2 Turning (OD)

(Turn OD to 1.500" diameter)
G00 X1.6 Z0.1             (Rapid to start)
G01 Z-3.0 F0.012          (Turn along length)
G00 X4.0                  (Retract)
G00 Z0.1                  (Return Z)

9.3 Threading with G76

(Thread 1.0-8 UNC)
G97 S400 M03              (Lower RPM for threading)
G00 X1.1 Z0.2

G76 P010060 Q0050 R0.020
G76 X0.8376 Z-1.5 P0812 Q0200 F0.125
(P = depth, Q = first pass, F = lead (1/TPI))

⚠️ Note: Threading code varies significantly between controls (Fanuc, Haas, Mazak). Always consult your machine's programming manual.

10. Complete Programming Examples

Example 1: Simple Milled Part

Part: 4" x 3" x 1" aluminum plate with 4 holes

%
O1001 (4X3 PLATE - 4 HOLES)
(T1 - 1.0 FACE MILL)
(T2 - #3 CENTER DRILL)
(T3 - 0.25 DRILL)
(MATERIAL: 6061 AL)

(TOOL 1 - FACE)
N10 G90 G20 G17 G40 G49 G80
N20 T1 M06
N30 G54 S1200 M03
N40 G00 X-0.5 Y1.5
N50 G43 H1 Z0.1 M08
N60 G01 Z0 F20.0
N70 G01 X4.5 F15.0
N80 G00 Z1.0
N90 Y0.5
N100 G01 Z0 F20.0
N110 G01 X-0.5 F15.0
N120 G00 Z1.0 M09 M05
N130 G28 G91 Z0
N140 G28 X0 Y0

(TOOL 2 - SPOT)
N200 T2 M06
N210 G90 G54 S2000 M03
N220 G00 X0.5 Y0.5
N230 G43 H2 Z1.0 M08
N240 G81 Z-0.15 R0.1 F6.0
N250 X0.5 Y0.5
N260 X3.5
N270 X3.5 Y2.5
N280 X0.5
N290 G80
N300 M09 M05
N310 G28 G91 Z0
N320 G28 X0 Y0

(TOOL 3 - DRILL)
N400 T3 M06
N410 G90 G54 S2500 M03
N420 G00 X0.5 Y0.5
N430 G43 H3 Z1.0 M08
N440 G83 Z-1.1 Q0.25 R0.1 F8.0
N450 X0.5 Y0.5
N460 X3.5
N470 X3.5 Y2.5
N480 X0.5
N490 G80
N500 M09 M05
N510 G28 G91 Z0
N520 G28 X0 Y0

M30
%

Example 2: Bolt Hole Circle

6 holes on a 3" diameter bolt circle:

(BOLT HOLE CIRCLE - 6 HOLES ON 3" DIA)
G90 G54 G00 X0 Y0
G43 H1 Z1.0
S1500 M03 M08

G81 Z-0.5 R0.1 F5.0
X1.5 Y0                (0°)
X0.75 Y1.299          (60°)
X-0.75 Y1.299         (120°)
X-1.5 Y0              (180°)
X-0.75 Y-1.299        (240°)
X0.75 Y-1.299         (300°)
G80

M09 M05
G28 G91 Z0
G28 X0 Y0

11. Safety & Best Practices

Program Safety Checklist

  • ✓ Always include safety block (G90 G20 G40 G49 G80)
  • ✓ Verify work offsets before running
  • ✓ Check all tool lengths in offset table
  • ✓ Simulate program in graphics mode first
  • ✓ Run program in single block mode initially
  • ✓ Keep hands clear of work envelope
  • ✓ Verify spindle direction before cutting
  • ✓ Check for adequate tool clearance
  • ✓ Use feed rate override for first run
  • ✓ Have emergency stop location memorized

Common Programming Mistakes

⚠️ Crash Hazards

  • Wrong work offset - Always verify G54-G59
  • Missing tool length comp (G43) - Tool will crash into part
  • G91 incremental mode left active - Next move will be relative
  • Forgetting cutter comp cancel (G40) - Tool won't follow programmed path
  • Negative Z in rapid (G00) - Should feed (G01) when cutting
  • Tool too long for clearance - Check Z-axis travel limits

Debugging Tips

  • Single Block Mode - Execute one line at a time
  • Dry Run Mode - Machine runs program with spindle off, no feed
  • Graphics Simulation - Visualize toolpath on control screen
  • Prove Out Air Cut - Run program Z=+1.0 above part first
  • Feed Override - Reduce feed to 25-50% for first run

Documentation

Always include in program header:

  • Part name/number
  • Tool list with descriptions
  • Material type
  • Stock size
  • Work holding method (vise, fixture, etc.)
  • Special instructions

Next Steps

Now that you understand CNC programming basics, here are some resources to continue learning:

Keep Learning

CNC programming is a skill that improves with practice. Start with simple programs, gradually add complexity, and always prioritize safety. Use graphics simulation and dry runs when trying new techniques.

Remember: Even experienced programmers check their work carefully. A few extra minutes of verification can prevent hours of repair and rework.