THE ULTIMATE CNC G-CODE EXAMPLES & PROGRAMMING LIBRARY 2026
FANUC, Haas, CNC Milling, Turning, Drilling, Tapping, Subprograms, Macro B and Professional CNC Programming Examples
The Complete CNC Program Examples, G-Code Reference and Practical Programming Guide
Welcome to the Ultimate CNC G-Code Examples & Programming Library 2026.
This reference is designed for CNC operators, programmers, machinists, manufacturing engineers, students, and technicians who want to understand CNC programming through practical examples.
Instead of memorizing isolated commands, you will learn how G-codes, M-codes, coordinates, offsets, cutting parameters, subprograms, and macro logic work together.
Inside this library, you will find examples covering:
- CNC milling
- CNC drilling
- Peck drilling
- Tapping
- Circular interpolation
- Slot milling
- Rectangular profiles
- Tool length compensation
- Work coordinate systems
- Subprograms
- FANUC-style Macro B
- Bolt-circle calculations
- CNC turning
- Spindle speed modes
- Professional programming practices
- Common programming errors
- Program verification
Important: Read Before Using Any Example
The programs in this guide are educational examples, not production-ready machine programs.
Most milling examples use common FANUC-style ISO conventions. Some commands are shared with Haas controls, but compatibility must be checked against the exact machine.
The turning examples use general FANUC-style concepts. Turning cycles and formats differ significantly by controller and series.
Every example requires verification of:
- Controller model and software
- Installed control options
- Machine configuration
- Axis travel and direction
- Tooling and tool offsets
- Workholding and fixtures
- Stock dimensions
- Work coordinate system
- Feedrates and spindle speeds
- Toolpath clearance
- Collision risk
- Machine-specific M-codes
Before any physical execution, use an appropriate controller-aware simulator, verify the setup, and follow your machine’s approved prove-out procedure.
Never copy an unfamiliar CNC program directly into a production machine and press Cycle Start.
PART I — CNC PROGRAMMING FOUNDATIONS
SECTION 1 — BASIC CNC PROGRAM STRUCTURE
A CNC program typically contains:
- Program identification
- Initial modal-state commands
- Work coordinate selection
- Tool selection
- Spindle commands
- Tool length compensation
- Positioning
- Cutting operations
- Retraction
- Program termination
Example 1 — Basic FANUC-Style Milling Program
%
O1001 (BASIC MILLING EXAMPLE)
G21 G17 G90 G40 G49 G80
G54
T01 M06
S1500 M03
G00 X10. Y10.
G43 H01 Z50.
M08
G01 Z-2. F100.
G01 X60. F250.
G00 Z50.
M09
M05
M30
%
Explanation
O1001 identifies the program.
G21 selects metric units.
G17 selects the XY plane.
G90 selects absolute positioning.
G40 cancels cutter radius compensation.
G49 cancels tool length compensation.
G80 cancels canned cycles.
G54 selects a work coordinate system.
T01 M06 requests a tool change on compatible machines.
S1500 M03 requests clockwise spindle rotation at 1500 RPM.
G00 performs rapid positioning.
G43 H01 activates the designated tool length compensation.
M08 activates coolant on compatible machines.
G01 performs feed-controlled linear interpolation.
M30 ends the program.
Important: This example assumes the programmed coordinates, tool length, stock, and fixture clearances have been independently verified. Its startup and retract sequence is not a universal safe-start template.
SECTION 2 — G00 RAPID POSITIONING
G00 is used for rapid positioning.
Example 2
G90 G00 X50. Y25.
This commands positioning to X50 and Y25 in the active coordinate system.
The exact rapid trajectory may depend on the controller and machine.
Professional note: A safe destination does not guarantee a safe rapid path.
SECTION 3 — G01 LINEAR INTERPOLATION
G01 moves an axis or multiple axes at a programmed feedrate.
Example 3
G90 G01 X80. Y20. F300.
The tool moves toward X80 Y20 at a programmed feedrate of 300 units per minute when the active feed mode is feed per minute.
The actual units depend on the unit-selection mode.
SECTION 4 — G02 CLOCKWISE ARC
G02 commands clockwise circular interpolation in the selected plane.
Example 4 — Quarter-Circle Arc
Assumptions:
- XY plane active
- Incremental arc-center offsets supported
- Starting point X10 Y0
- Arc center X0 Y0
G17 G90
G00 X10. Y0.
G03 X0. Y10. I-10. J0.
This example uses G03, not G02, because the movement from the positive X axis toward the positive Y axis is counterclockwise.
To move clockwise from X0 Y10 to X10 Y0:
G02 X10. Y0. I0. J-10.
This distinction is important when troubleshooting arc direction.
SECTION 5 — G03 COUNTERCLOCKWISE ARC
Example 5
G17 G90
G00 X10. Y0.
G03 X0. Y10. I-10. J0.
The example describes a quarter-circle arc with a radius of 10 units.
Verify the controller’s arc-center convention before use.
SECTION 6 — G90 ABSOLUTE POSITIONING
G90 commands absolute positioning on many milling controls.
Example 6
G90
G01 X10. F200.
X20.
X30.
The programmed X destinations are:
10
20
30
Each coordinate refers to the active coordinate system’s origin.
SECTION 7 — G91 INCREMENTAL POSITIONING
G91 commands incremental positioning.
Example 7
G91
G01 X10. F200.
X10.
X10.
G90
Each X10 move adds 10 units to the current X position.
The final G90 restores absolute positioning.
Professional shortcut: Explicitly restore the positioning mode expected by the calling program.
SECTION 8 — G54 WORK COORDINATE SYSTEM
G54 commonly selects a work coordinate system.
Example 8
G90 G54
G00 X0. Y0.
The destination is X0 Y0 in the G54 coordinate system.
Its physical machine location depends on the stored work offset.
SECTION 9 — G55 SECOND WORK OFFSET
Example 9
G90 G55
G00 X0. Y0.
G55 can reference a different work setup or fixture location.
Common application
Machine identical parts at separate fixture locations using different work offsets.
The work offsets must be measured and verified.
SECTION 10 — G43 TOOL LENGTH COMPENSATION
Example 10
G90 G00 X20. Y20.
G43 H01 Z50.
On compatible controls, G43 activates positive tool length compensation using the selected H offset.
Incorrect tool length data can create serious collision hazards.
PART II — CNC DRILLING PROGRAM EXAMPLES
SECTION 11 — G81 SIMPLE DRILLING
Example 11 — Three-Hole Drilling Pattern
%
O1101 (G81 DRILLING)
G21 G17 G90 G40 G49 G80
G54
T01 M06
S1200 M03
G00 X10. Y10.
G43 H01 Z50.
M08
G98 G81 Z-12. R3. F120.
X30. Y10.
X50. Y10.
G80
G00 Z50.
M09
M05
M30
%
What this example demonstrates
The first G81 block defines the drilling cycle.
The following XY positions repeat the drilling operation.
G80 cancels the cycle.
On many FANUC-style mills, G98 selects return to the initial level during a canned cycle. Verify the exact machine’s behavior and initial level before use.
SECTION 12 — G82 DWELL DRILLING
G82 adds a dwell at the bottom of a drilling cycle on many compatible controls.
Example 12
G90 G98 G82 X20. Y20. Z-8. R3. P1000 F100.
G80
Controller note: The units and interpretation of the P dwell parameter vary by controller and programming format. Do not assume P1000 always means one second.
SECTION 13 — G83 PECK DRILLING
G83 is commonly used for deeper holes where chip evacuation is important.
Example 13
G90 G98 G83 X20. Y20. Z-30. R3. Q5. F120.
G80
Common FANUC-style interpretation:
Z-30. — Final depth
R3. — R plane
Q5. — Peck increment
F120. — Feedrate
Peck behavior, retract behavior, and supported parameters depend on the controller.
SECTION 14 — MULTIPLE G83 HOLES
Example 14
G90 G98 G83 X10. Y10. Z-25. R3. Q4. F100.
X30. Y10.
X50. Y10.
X70. Y10.
G80
This pattern avoids repeating the full drilling-cycle definition for every hole.
The actual toolpath must be checked for fixture and stock clearance.
SECTION 15 — G84 TAPPING
G84 is commonly associated with tapping cycles.
Example 15 — Metric Pitch Calculation
Assume:
Thread pitch = 1.5 mm
Spindle speed = 500 RPM
For feed-per-minute tapping:
Feed = Pitch × RPM
Feed = 1.5 × 500 = 750 mm/min
Illustrative cycle:
G90 G84 X20. Y20. Z-15. R3. F750.
G80
Important: This is not a complete machine-ready rigid-tapping program. Spindle synchronization, required modes, speed commands, feed conventions, and cycle syntax are controller-specific.
SECTION 16 — G73 HIGH-SPEED PECK DRILLING
Some FANUC-style milling controls support G73 as a chip-breaking or high-speed peck-drilling cycle.
Example 16
G90 G73 X20. Y20. Z-18. R3. Q3. F150.
G80
G73 behavior can differ from G83.
Check the official cycle description for your control.
SECTION 17 — LINEAR HOLE PATTERN
Suppose five holes are spaced 20 mm apart.
Starting X = 10 mm.
The positions are:
10
30
50
70
90
Example 17
G90 G98 G81 X10. Y20. Z-10. R3. F100.
X30.
X50.
X70.
X90.
G80
This is a compact approach to repeated drilling.
SECTION 18 — RECTANGULAR HOLE PATTERN
A four-corner hole pattern can be programmed with four positions.
Example 18
G90 G98 G81 X10. Y10. Z-10. R3. F100.
X70. Y10.
X70. Y50.
X10. Y50.
G80
The example demonstrates coordinate reuse within a modal drilling cycle.
PART III — MILLING GEOMETRY EXAMPLES
SECTION 19 — STRAIGHT SLOT CENTERLINE
Example 19
G90 G01 X10. Y20. F250.
X90. Y20.
This represents a straight tool-center movement.
It does not define a complete slot machining process.
Actual slot width depends on tool diameter and cutting strategy.
SECTION 20 — RECTANGULAR TOOLPATH
Example 20
G90 G01 X10. Y10. F250.
X90. Y10.
X90. Y60.
X10. Y60.
X10. Y10.
This describes a rectangular tool-center path.
Important: It is not automatically a finished rectangular part contour. Cutter diameter, compensation, depth, lead-in, stock, and fixture conditions must be accounted for.
SECTION 21 — SQUARE PROFILE
Example 21
G90 G01 X0. Y0. F200.
X50. Y0.
X50. Y50.
X0. Y50.
X0. Y0.
The tool-center path forms a 50 × 50 square.
This is useful for learning coordinate programming.
SECTION 22 — CIRCULAR CONTOUR USING TWO ARCS
Assume the starting point is X10 Y0 and the intended circle center is X0 Y0.
Example 22
G17 G90
G00 X10. Y0.
G03 X-10. Y0. I-10. J0.
G03 X10. Y0. I10. J0.
This describes a full circle using two semicircular moves.
Actual machining requires appropriate tool engagement, depth control, and compensation.
SECTION 23 — SPIRAL TOOLPATH CONCEPT
A spiral milling strategy gradually changes the radial position as the tool rotates around a center.
Conceptually:
Radius = Initial Radius + Growth Per Revolution
X = Center X + Radius × COS(Angle)
Y = Center Y + Radius × SIN(Angle)
The path may be generated by CAM or a controller-supported macro.
Spiral machining requires attention to tool engagement, interpolation capability, and stock removal.
SECTION 24 — HELICAL INTERPOLATION CONCEPT
Helical interpolation combines circular movement with axial movement.
A conceptual example:
G17
G03 X-10. Y0. I-10. J0. Z-1.
G03 X10. Y0. I10. J0. Z-2.
Assumptions:
- The first arc begins at X10 Y0
- Arc-center offsets are incremental
- The controller supports simultaneous circular and Z interpolation
This is a geometric illustration only, not a complete helical-boring routine.
SECTION 25 — FACE MILLING PATTERN
A basic raster face-milling strategy moves across a surface in parallel passes.
Concept:
PASS 1 — X positive
STEP OVER — Y positive
PASS 2 — X negative
STEP OVER — Y positive
PASS 3 — X positive
The stepover depends on tool diameter, engagement, surface-finish requirements, and material.
A complete production routine should be generated or verified for the actual tool and stock.
SECTION 26 — Z-LEVEL ROUGHING
Z-level roughing removes material at multiple depths.
Example depth sequence:
Z = -2 mm
Z = -4 mm
Z = -6 mm
Z = -8 mm
This approach can be used for pockets and contours.
The correct depth per pass depends on the tool, material, machine rigidity, and cutting strategy.
SECTION 27 — FINISHING ALLOWANCE
Professional programs often separate roughing from finishing.
Example:
Target wall position = X50.000
Roughing allowance = 0.300 mm
Finishing pass removes the remaining allowance.
The actual roughing tool-center position depends on which side of the wall is machined and whether cutter compensation is used.
PART IV — CNC SUBPROGRAM EXAMPLES
SECTION 28 — M98 SUBPROGRAM CALL
On many FANUC-style controls, M98 calls a subprogram.
Example 28 — Main Program
%
O1200 (MAIN PROGRAM)
G21 G17 G90 G40 G49 G80
G54
T01 M06
S1000 M03
G00 X20. Y20.
G43 H01 Z50.
M98 P2000
M05
M30
%
Subprogram
%
O2000 (SUBPROGRAM)
G90 G00 Z10.
G01 Z-2. F100.
G01 X40. F200.
G00 Z50.
M99
%
Explanation
The main program calls O2000.
The subprogram executes its movements.
M99 returns control to the caller on compatible controls.
Important: The subprogram assumes a specific tool, spindle, work offset, entry position, and verified clearances. The example demonstrates call structure rather than a validated machining operation.
SECTION 29 — M98 REPEAT COUNT
Some FANUC-style controls allow a repeat count to be included in the subprogram call.
Example 29
M98 P30002000
On controls using the common eight-digit P convention, this can represent:
Repeat count = 30
Subprogram number = 02000
Critical compatibility note: M98 repeat-count encoding is controller- and format-dependent. Other controls use different syntax, and some may interpret this value differently or reject it.
Always verify the exact manual before use.
SECTION 30 — M99 SUBPROGRAM RETURN
Example 30
O2100
(EDUCATIONAL SUBPROGRAM)
M99
M99 commonly returns from a subprogram.
The behavior can differ when the program is executed directly or when optional return addresses are used.
SECTION 31 — NESTED SUBPROGRAMS
A nested subprogram calls another subprogram.
Concept:
MAIN PROGRAM
↓
SUBPROGRAM A
↓
SUBPROGRAM B
↓
RETURN TO A
↓
RETURN TO MAIN
Potential benefits:
- Reusable operations
- Reduced duplicate code
- Modular program structure
Potential problems:
- Missing programs
- Incorrect return behavior
- Excessive nesting
- Unexpected modal state
SECTION 32 — HAAS M97 LOCAL SUBPROGRAM
On supported Haas controls, M97 can call a local subprogram using a sequence number.
Example 32 — Structural Illustration
O1300
M97 P100
M30
N100
(LOCAL SUBPROGRAM BODY)
M99
This example illustrates the call-and-return relationship.
It contains no cutting movement and is not a complete machining program.
Verify the applicable Haas control documentation for local subprogram rules.
SECTION 33 — M198 EXTERNAL PROGRAM CALL
M198 is associated with external subprogram calls on some controls.
Its syntax and storage behavior vary.
Typical troubleshooting questions:
- Is external program execution supported?
- Is the program available?
- Is the media configured correctly?
- Is the program reference valid?
- Is the machine using the required option?
Do not assume M198 is supported on every FANUC-style machine.
PART V — FANUC MACRO B PROGRAMMING
SECTION 34 — MACRO VARIABLES
FANUC Macro B uses variables to store values and perform calculations.
Illustrative assignments:
#100 = 50.
#101 = 25.
#102 = [#100 + #101]
After these assignments:
#102 = 75
Variable availability and persistence depend on the controller series and installed macro options.
SECTION 35 — MACRO ARITHMETIC
Example 35
#100 = 20.
#101 = 5.
#102 = [#100 * #101]
#103 = [#100 / #101]
Results:
#102 = 100
#103 = 4
Always validate divisors and input ranges.
SECTION 36 — PARAMETRIC POSITION CALCULATION
Example 36
#100 = 10. (START X)
#101 = 20. (SPACING)
#102 = 3. (INDEX)
#103 = [#100 + #101 * #102]
Calculated result:
#103 = 70
This formula can generate repeated feature positions.
SECTION 37 — IF / GOTO LOGIC
Example 37 — Educational Macro Logic
#100 = 5
IF [#100 GT 10] GOTO 100
#101 = 1
GOTO 200
N100 #101 = 2
N200
Because #100 is 5, the condition is false and #101 becomes 1.
Macro syntax and permitted control-flow constructs must be verified for the controller.
SECTION 38 — WHILE LOOP
Example 38
#100 = 1
WHILE [#100 LE 5] DO1
#101 = [#100 * 10]
#100 = [#100 + 1]
END1
The loop processes index values 1 through 5.
This is an arithmetic demonstration and does not command machine movement.
SECTION 39 — MACRO INPUT VALIDATION
A professional macro should reject invalid inputs.
Conceptual logic:
IF HOLE_COUNT <= 0:
STOP WITH EXPLANATION
IF RADIUS <= 0:
STOP WITH EXPLANATION
IF REQUESTED_DEPTH EXCEEDS LIMIT:
STOP WITH EXPLANATION
This is pseudocode, not executable CNC syntax.
On supported controls, validated alarm-generation mechanisms can be used to stop a program with an informative message.
SECTION 40 — BOLT CIRCLE FORMULA
For N equally spaced holes:
Angle Step = 360 / N
Angle = Start Angle + Index × Angle Step
X = Center X + Radius × COS(Angle)
Y = Center Y + Radius × SIN(Angle)
Example:
Center = X0 Y0
Radius = 40 mm
Hole count = 8
Angle step = 45 degrees
SECTION 41 — BOLT CIRCLE COORDINATES
For an eight-hole pattern with a 40 mm radius:
| Hole | Angle | X | Y |
| 1 | 0° | 40.000 | 0.000 |
| 2 | 45° | 28.284 | 28.284 |
| 3 | 90° | 0.000 | 40.000 |
| 4 | 135° | -28.284 | 28.284 |
| 5 | 180° | -40.000 | 0.000 |
| 6 | 225° | -28.284 | -28.284 |
| 7 | 270° | 0.000 | -40.000 |
| 8 | 315° | 28.284 | -28.284 |
Coordinates are rounded to three decimal places.
SECTION 42 — BOLT CIRCLE MACRO CALCULATION
Example 42 — FANUC Macro B-Style Calculation
#100 = 0. (CENTER X)
#101 = 0. (CENTER Y)
#102 = 40. (RADIUS)
#103 = 8. (HOLE COUNT)
#104 = 0. (START ANGLE)
#105 = 0. (INDEX)
WHILE [#105 LT #103] DO1
#106 = [#104 + #105 * 360. / #103]
#107 = [#100 + #102 * COS[#106]]
#108 = [#101 + #102 * SIN[#106]]
#105 = [#105 + 1]
END1
This calculates hole positions but does not drill them.
On supported FANUC Macro B controls, trigonometric functions commonly use degrees.
A production implementation must validate the hole count, radius, coordinates, drilling cycle, clearances, and machine configuration.
SECTION 43 — GRID PATTERN FORMULA
For a rectangular grid:
X = Start X + Column × X Spacing
Y = Start Y + Row × Y Spacing
Example:
4 columns
3 rows
X spacing = 20 mm
Y spacing = 15 mm
Total holes:
4 × 3 = 12
A nested-loop macro can calculate these positions.
SECTION 44 — NESTED LOOP CONCEPT
FOR EACH ROW:
FOR EACH COLUMN:
CALCULATE X
CALCULATE Y
PROCESS FEATURE
This is pseudocode.
Actual CNC syntax depends on the controller’s macro language.
SECTION 45 — PARAMETRIC PART FAMILIES
Instead of storing a separate program for every similar part, a macro can accept parameters such as:
- Part length
- Part width
- Hole diameter
- Hole spacing
- Hole count
- Machining depth
- Finishing allowance
This approach can reduce duplicate code and simplify controlled program maintenance.
PART VI — CNC TURNING PROGRAMMING
SECTION 46 — CNC LATHE PROGRAM STRUCTURE
A CNC lathe program commonly includes:
- Work offset
- Tool selection
- Spindle mode
- Spindle speed
- Feed mode
- Tool geometry offsets
- Approach movement
- Cutting movement
- Retraction
- Program termination
Turning syntax is particularly controller-specific.
Tool call formats, canned cycles, diameter/radius programming, and feed conventions must be verified.
SECTION 47 — G96 CONSTANT SURFACE SPEED
G96 is commonly used for constant surface speed in turning.
The controller adjusts spindle RPM as the cutting diameter changes.
For metric units:
RPM = (1000 × Cutting Speed) / (π × Diameter)
Example:
Cutting speed = 180 m/min
Diameter = 60 mm
RPM ≈ 955
As diameter decreases, the calculated RPM increases.
A suitable spindle speed limit must be established according to the machine and process.
SECTION 48 — G97 CONSTANT RPM
G97 commonly selects fixed spindle speed in turning.
Example 48
G97 S1200 M03
On a compatible lathe, this requests clockwise spindle rotation at 1200 RPM.
Verify the machine’s spindle-direction conventions and operating conditions.
SECTION 49 — FEED PER REVOLUTION
Feed per revolution is widely used in turning.
Example:
Feed = 0.20 mm/rev
Spindle speed = 1000 RPM
Equivalent linear feed at constant RPM:
0.20 × 1000 = 200 mm/min
The G-code used to select feed per revolution depends on the controller and machine type.
Important: G98/G99 meanings in turning may differ from G98/G99 canned-cycle return meanings in milling.
SECTION 50 — G71 ROUGH TURNING CYCLE
G71 is associated with rough turning on many FANUC-style lathes.
However, there are multiple G71 programming formats.
Some controls use one-line formats.
Others use two-line formats.
Parameters and profile definitions vary.
Conceptual process
- Define roughing allowance.
- Define depth of cut.
- Define contour profile.
- Execute repeated roughing passes.
- Leave specified finishing stock.
Do not copy G71 syntax from an unrelated controller generation.
SECTION 51 — G70 FINISHING CYCLE
On many FANUC-style lathes, G70 is used to finish a previously defined profile.
A common workflow is:
G71 — Roughing
G70 — Finishing
The exact profile reference and cycle format depend on the control.
SECTION 52 — G72 FACING ROUGHING
G72 is associated with facing roughing cycles on many FANUC-style turning controls.
It is useful for removing material primarily in the facing direction.
As with G71, syntax and parameters vary.
SECTION 53 — G76 THREADING CYCLE
G76 is a commonly used threading cycle on many CNC lathes.
Potential parameters include:
- Thread pitch
- Thread depth
- Finishing allowance
- Number of finishing passes
- Infeed behavior
- Thread geometry
Critical: G76 formats vary significantly between controller generations.
A two-line G76 program from one machine must not be assumed compatible with another.
SECTION 54 — THREAD PITCH AND LEAD
For a single-start thread:
Lead = Pitch
For a multi-start thread:
Lead = Pitch × Number of Starts
Example:
Pitch = 2 mm
Starts = 2
Lead = 4 mm per revolution
Threading synchronization and cycle programming must follow the specific controller documentation.
PART VII — PROFESSIONAL CNC PROGRAMMING SHORTCUTS
SECTION 55 — USE MODAL COMMANDS INTENTIONALLY
Modal commands remain active according to the controller’s rules.
This can reduce repeated code.
However, excessive reliance on implicit modal state makes debugging harder.
Professional programs balance compactness with clarity.
SECTION 56 — REPLACE REPETITION WITH SUBPROGRAMS
If an operation is repeated at multiple locations, a reusable subprogram may simplify maintenance.
Potential benefits:
- Less duplicated code
- Easier corrections
- Consistent operation logic
- Smaller program size
Every subprogram must define or document its required entry state.
SECTION 57 — REPLACE FIXED COORDINATES WITH FORMULAS
For repeated geometry, use calculations.
Examples:
Bolt circles
Hole grids
Linear arrays
Radial slots
Part families
Variable spacing
Mathematical generation can reduce manual coordinate errors when properly validated.
SECTION 58 — USE CANNED CYCLES WHERE APPROPRIATE
Canned cycles can simplify common operations.
Examples:
G81 — Drilling
G82 — Dwell drilling
G83 — Deep-hole drilling
G84 — Tapping
Always confirm the supported cycle format and behavior.
SECTION 59 — CONTROL RETRACT HEIGHTS
Excessive retract distances increase cycle time.
Insufficient retract distances increase collision risk.
The correct retract strategy depends on:
- Fixture geometry
- Stock geometry
- Tool length
- Holder dimensions
- Machine kinematics
- Operation sequence
Optimization must never sacrifice verified clearance.
SECTION 60 — MINIMIZE UNNECESSARY TOOL CHANGES
Operation planning can reduce repeated tool changes.
Possible improvements:
- Group compatible operations
- Plan tool order
- Reuse verified tools
- Coordinate multiple fixtures
The best sequence also depends on setup stability, part quality, and inspection requirements.
SECTION 61 — USE MEANINGFUL PROGRAM COMMENTS
Example 61
O1500 (ALUMINUM PLATE – OP10)
(TOOL 01 – 6MM DRILL)
(WORK OFFSET – G54)
(VERIFY SETUP BEFORE EXECUTION)
Comments improve readability and reduce ambiguity.
They do not replace setup verification.
SECTION 62 — VERIFY TOOL NUMBERS AND OFFSETS
Common mistakes include:
- Wrong tool number
- Wrong H offset
- Wrong D offset
- Incorrect measured tool length
- Incorrect tool diameter
- Tool replacement without data verification
The tool table and physical setup must agree.
SECTION 63 — CHECK PROGRAM MODAL STATE
Important modes include:
- Units
- Plane
- Absolute/incremental positioning
- Feed mode
- Work offset
- Tool length compensation
- Cutter compensation
- Canned-cycle state
- Spindle mode
Many unexpected movements originate from an earlier modal command.
SECTION 64 — CHECK SUBPROGRAM ENTRY CONDITIONS
Before calling a subprogram, establish its requirements.
For example:
Expected tool
Expected work offset
Expected coordinate mode
Expected feed mode
Expected entry position
Expected safe clearance
A reusable subprogram is only reliable when its assumptions are explicit.
SECTION 65 — VALIDATE MACRO INPUTS
Professional macros should reject:
- Zero hole counts
- Negative radii where invalid
- Excessive depths
- Invalid tool dimensions
- Coordinates outside approved ranges
- Unsupported parameter combinations
Never calculate motion from unchecked inputs.
PART VIII — CNC TROUBLESHOOTING EXAMPLES
SECTION 66 — WHY DOES G02 OR G03 ALARM?
Check:
- Active plane
- Arc direction
- Start and end points
- I/J/K or R values
- Controller arc conventions
- Geometry consistency
A geometrically impossible arc cannot be repaired by changing feedrate.
SECTION 67 — WHY DOES G83 NOT WORK?
Potential causes:
- Incorrect peck value
- Incorrect R plane
- Incorrect depth
- Unsupported syntax
- Wrong active mode
- Controller-specific cycle requirements
Verify the cycle definition for the exact control.
SECTION 68 — WHY DOES M98 NOT WORK?
Potential causes:
- Subprogram missing
- Incorrect program number
- Incorrect call format
- Unsupported storage configuration
- Excessive nesting
- Program access restrictions
Check the caller and target program together.
SECTION 69 — WHY DOES M99 LOOP FOREVER?
Possible causes:
- M99 used in the wrong context
- Incorrect subprogram structure
- Wrong return behavior
- Main program executed as a loop
- Incorrect call chain
Use a subprogram call graph to trace execution.
SECTION 70 — WHY DOES THE TOOL MOVE TO THE WRONG POSITION?
Check:
- G90/G91
- G54–G59
- Tool length compensation
- Machine coordinates
- Macro calculations
- Work offset values
- Program restart state
The problematic move may be caused by a command several blocks earlier.
SECTION 71 — WHY DOES G84 TAP INCORRECTLY?
Check:
- Thread pitch
- Spindle speed
- Feedrate
- Feed mode
- Rigid-tapping requirements
- Spindle synchronization
- Tool and holder
- Controller-specific cycle syntax
Never assume the same tapping program works on all machines.
SECTION 72 — WHY DOES THE CNC PROGRAM RUN SLOWLY?
Possible reasons:
- Excessive air cutting
- Long retracts
- Unnecessary tool changes
- Dense CAM output
- Controller look-ahead limitations
- Acceleration limits
- Excessive corner deceleration
- Conservative cutting parameters
Optimize only after confirming that the current program is safe and dimensionally correct.
PART IX — CNC PROGRAMMING CALCULATORS
SECTION 73 — SPINDLE SPEED CALCULATOR
Metric formula:
RPM = (1000 × Vc) / (π × D)
Where:
Vc = Cutting speed in m/min
D = Tool or workpiece diameter in mm
Example:
Vc = 120 m/min
D = 10 mm
RPM ≈ 3820
This is a mathematical calculation, not a recommendation for a particular tool or material.
SECTION 74 — MILLING FEEDRATE CALCULATOR
For a milling cutter:
Feedrate = RPM × Number of Teeth × Feed per Tooth
Example:
RPM = 3000
Teeth = 4
Feed per tooth = 0.05 mm
Feedrate:
3000 × 4 × 0.05 = 600 mm/min
Actual cutting parameters must be selected using tool and material data.
SECTION 75 — DRILLING FEEDRATE CALCULATOR
For feed per revolution:
Feedrate = RPM × Feed per Revolution
Example:
RPM = 1200
Feed per revolution = 0.10 mm
Feedrate:
120 mm/min
SECTION 76 — BOLT CIRCLE CALCULATOR
Inputs:
- Center X
- Center Y
- Radius
- Hole count
- Start angle
Outputs:
- Hole angles
- X coordinates
- Y coordinates
This is a strong candidate for an interactive CNC website tool.
SECTION 77 — ARC CENTER CALCULATOR
Inputs:
- Start point
- End point
- Radius
- Direction
- Plane
Outputs:
- Valid possible arc centers
- I/J/K values
- Arc sweep
Arc-center solutions must be evaluated for the requested direction and sweep.
SECTION 78 — CYCLE TIME ESTIMATOR
A basic estimator can consider:
- Cutting distance
- Programmed feed
- Rapid distance
- Tool changes
- Spindle acceleration
- Drilling cycles
- Dwell times
A basic feed-move estimate is:
Time = Distance / Feedrate
Actual CNC cycle time also depends on machine dynamics, control processing, acceleration, and operation-specific behavior.
PART X — CONTROLLER-SPECIFIC PROGRAMMING
SECTION 79 — FANUC PROGRAMMING
Common areas:
G-code and M-code programming
Macro B
M98/M99
Canned cycles
Tool compensation
Work offsets
High-speed machining options
Advanced transformations
The exact FANUC series and machine-builder configuration must be identified.
SECTION 80 — HAAS PROGRAMMING
Common areas:
Haas G-code
Haas M-code
M97 local subprograms
M98/M99
G12/G13 circular pocketing
G187 accuracy control
Haas macros
Probing integration
Do not assume every Haas feature exists on every control generation.
SECTION 81 — SIEMENS SINUMERIK PROGRAMMING
SINUMERIK programming can include:
DIN/ISO-style commands
Controller-specific cycles
High-level programming constructs
Coordinate transformations
CYCLE832
TRAORI
Advanced tool management
Digital-twin workflows
Siemens programming conventions are not interchangeable with FANUC Macro B syntax.
SECTION 82 — CNC LATHE VS CNC MILL
The same code number can have different meanings depending on the machine type and controller.
Important differences include:
- Feed modes
- Canned cycles
- Tool selection
- Work coordinate conventions
- Diameter programming
- Spindle modes
- Compensation
Always identify whether a reference applies to milling or turning.
PART XI — BUILDING A CNC CODE EXAMPLES DATABASE
SECTION 83 — THE IDEAL CNC EXAMPLE PAGE
Every example should include:
Program title
What the program demonstrates.
Controller
Exact control family and version.
Machine type
Mill, lathe, or other machine.
Required options
Macro, probing, advanced cycle, or other requirements.
Setup assumptions
Tool, stock, work offset, and coordinate system.
Code
Readable, formatted NC program.
Line-by-line explanation
Meaning of each important command.
Expected toolpath
Diagram or simulation where available.
Common errors
Typical mistakes and diagnostic checks.
Verification
Required simulation and setup checks.
Official references
Applicable manufacturer documentation.
SECTION 84 — SEARCH BY G-CODE
Examples:
G00 example
G01 example
G02 example
G03 example
G43 example
G54 example
G81 example
G82 example
G83 example
G84 example
G90 example
G91 example
G96 example
G97 example
SECTION 85 — SEARCH BY M-CODE
Examples:
M03 example
M04 example
M05 example
M06 example
M08 example
M09 example
M30 example
M97 example
M98 example
M99 example
M198 example
SECTION 86 — SEARCH BY MACHINING OPERATION
Examples:
CNC face milling program
CNC pocket milling program
CNC slot milling program
CNC drilling program
CNC peck drilling program
CNC tapping program
CNC bolt circle program
CNC thread milling concept
CNC rough turning cycle
CNC finishing cycle
CNC threading cycle
CNC macro programming
SECTION 87 — SEARCH BY DIFFICULTY
Beginner
Basic positioning
Straight-line machining
Work offsets
Simple drilling
Intermediate
Arcs
Canned cycles
Tool compensation
Subprograms
Advanced
Macro variables
Loops
Parametric geometry
Controller-specific cycles
Expert
5-axis transformations
Probing
Adaptive machining
Digital-twin verification
Machine automation
PART XII — CNC PROGRAMMING FAQ
SECTION 88 — WHAT IS THE MOST COMMON CNC PROGRAM?
There is no single most common program for every shop.
However, basic positioning, drilling, contouring, and tool-change operations are fundamental to many CNC machining workflows.
SECTION 89 — CAN I COPY A CNC PROGRAM FROM THE INTERNET?
You can study an example, but you should not execute it on a physical machine without verifying controller compatibility, machine configuration, tooling, offsets, workholding, motion, and safety.
SECTION 90 — IS FANUC G-CODE THE SAME AS HAAS G-CODE?
They share many programming concepts, but they are not identical.
Subprograms, macros, canned cycles, M-codes, and advanced features may differ.
SECTION 91 — WHAT IS THE DIFFERENCE BETWEEN M98 AND M99?
On many FANUC-style controls:
M98 calls a subprogram.
M99 returns from a subprogram or performs controller-specific return/repetition behavior.
Exact behavior depends on the control.
SECTION 92 — WHAT IS THE DIFFERENCE BETWEEN G81 AND G83?
G81 commonly performs a basic drilling cycle.
G83 commonly performs peck drilling with repeated cutting and retract behavior.
The exact motion depends on the controller.
SECTION 93 — WHAT IS THE DIFFERENCE BETWEEN G90 AND G91?
G90 commonly selects absolute positioning.
G91 commonly selects incremental positioning.
Incorrect use can cause unexpected machine movement.
SECTION 94 — WHAT IS FANUC MACRO B?
Macro B is a controller programming capability that supports variables, calculations, conditions, and loops on appropriately equipped FANUC controls.
It can be used for parametric machining and automation.
SECTION 95 — WHAT IS THE BEST WAY TO LEARN CNC G-CODE?
A practical learning sequence is:
Understand coordinates.
Learn basic motion.
Understand work offsets.
Understand tool compensation.
Practice drilling cycles.
Learn arcs.
Study subprograms.
Learn macro programming.
Use simulation.
Verify controller-specific behavior.
SECTION 96 — CAN AI WRITE CNC PROGRAMS?
AI can assist with explanations, program structure, calculations, and draft code.
However, AI-generated NC code may contain unsupported commands, incorrect assumptions, or unsafe movements.
Controller-specific verification remains essential.
PART XIII — THE FUTURE OF CNC PROGRAMMING IN 2026
SECTION 97 — AI-ASSISTED CNC PROGRAMMING
Modern manufacturing software increasingly incorporates AI-assisted capabilities.
Potential applications include:
- Feature recognition
- Tool selection assistance
- Machining strategy recommendations
- Program explanation
- Process documentation
- Manufacturing knowledge retrieval
AI should support engineering judgment rather than replace program verification.
SECTION 98 — CNC DIGITAL TWINS
A CNC digital twin can represent the machine environment.
Potential elements include:
- Machine axes
- Kinematics
- Tool assemblies
- Stock
- Fixtures
- NC program
- Controller behavior
Machine-specific simulation can help detect problems that simple G-code visualization may miss.
SECTION 99 — CLOSED-LOOP CNC PROGRAMMING
Closed-loop machining integrates measurement and correction.
Concept:
MACHINE
↓
MEASURE
↓
COMPARE
↓
VALIDATE
↓
CORRECT
↓
VERIFY
Automatic correction must use validated measurements and controlled correction limits.
SECTION 100 — THE PROFESSIONAL CNC PROGRAMMING WORKFLOW
A reliable workflow is:
UNDERSTAND THE DRAWING
↓
PLAN THE OPERATION
↓
SELECT THE TOOL
↓
VERIFY THE SETUP
↓
WRITE OR GENERATE THE PROGRAM
↓
CHECK THE CONTROLLER SYNTAX
↓
VERIFY MODAL STATES
↓
CHECK OFFSETS
↓
SIMULATE THE TOOLPATH
↓
VERIFY MACHINE AND FIXTURE CLEARANCE
↓
PERFORM CONTROLLED PROVE-OUT
↓
INSPECT THE PART
↓
DOCUMENT THE RESULT
The most valuable CNC program is not necessarily the shortest program.
It is the program that is understandable, maintainable, efficient, and correctly verified for its intended machine.
FINAL REFERENCE — CNC PROGRAMMING QUICK LOOKUP
| Code | Common function | Important note |
| G00 | Rapid positioning | Verify motion path |
| G01 | Linear interpolation | Feed mode matters |
| G02 | Clockwise arc | Plane and center matter |
| G03 | Counterclockwise arc | Plane and center matter |
| G17 | XY plane | Common milling convention |
| G18 | XZ plane | Common turning/arc convention |
| G19 | YZ plane | Controller-specific applications |
| G20 | Inch units | Verify controller behavior |
| G21 | Metric units | Verify controller behavior |
| G40 | Cancel cutter compensation | Check lead-out requirements |
| G41 | Cutter compensation left | Geometry and lead-in matter |
| G42 | Cutter compensation right | Geometry and lead-in matter |
| G43 | Tool length compensation | Verify H offset |
| G49 | Cancel tool length compensation | Verify subsequent motion |
| G54 | Work offset | Verify setup |
| G55 | Work offset | Verify setup |
| G80 | Cancel canned cycle | Avoid unintended repetition |
| G81 | Drilling cycle | Verify Z and R |
| G82 | Dwell drilling | Verify dwell units |
| G83 | Peck drilling | Verify Q and retract behavior |
| G84 | Tapping | Verify synchronization |
| G90 | Absolute positioning | Track modal state |
| G91 | Incremental positioning | Track modal state |
| G96 | Constant surface speed | Common turning function |
| G97 | Constant RPM | Common turning function |
| M03 | Spindle clockwise | Verify machine convention |
| M04 | Spindle counterclockwise | Verify machine convention |
| M05 | Spindle stop | Common convention |
| M06 | Tool change | Machine-specific |
| M08 | Coolant on | Machine-specific |
| M09 | Coolant off | Machine-specific |
| M30 | Program end | Common convention |
| M97 | Local subprogram | Haas-specific usage |
| M98 | Subprogram call | Syntax varies |
| M99 | Subprogram return | Context matters |
| M198 | External program call | Option/controller dependent |
CONCLUSION — FROM CNC CODE EXAMPLES TO PROFESSIONAL PROGRAMMING
Learning CNC programming requires more than memorizing G-codes.
A professional CNC programmer understands:
MOTION
How the machine moves.
COORDINATES
How positions are defined.
OFFSETS
How tools and workpieces are located.
CYCLES
How repeated operations are programmed.
SUBPROGRAMS
How code can be reused.
MACROS
How logic and mathematics generate motion.
CONTROLLERS
Why programming conventions differ.
VERIFICATION
How programs are checked before machining.
AUTOMATION
How CNC programming connects to measurement, monitoring, and production systems.
The most effective approach is to combine real programming examples with clear explanations, controller-specific documentation, and reliable verification methods.
LEARN THE CODE. UNDERSTAND THE LOGIC. VERIFY THE MACHINE. PROGRAM WITH CONFIDENCE.
2026 Edition — CNC G-Code Examples, FANUC and Haas Programming, Milling, Turning, Drilling, Tapping, M98/M99 Subprograms, Macro B and Professional CNC Programming Reference.
Leave a comment