This is a single-page CNC code archive designed as a “bookmark forever” reference. It combines G-code + M-code essentials, modal logic, safe templates, macro safety patterns, and the most common crash-causing misunderstandings. Controls differ (Fanuc, Haas, Siemens, etc.), so always verify your controller manual for machine-specific options and parameter behavior—but the core logic below is universal across most industrial CNC systems.
══════════════════════════════════════════════════════════════════════════════
1) CRASH-PROOF START / END / RESTART TEMPLATES (COPY-PASTE LIBRARY)
══════════════════════════════════════════════════════════════════════════════
1.1 SAFE START BLOCK (MILLING / GENERAL)
Use this at the top of programs and before restarts to rebuild the machine state:
G90 G17 G40 G49 G80
G94
(Select your work offset)
G54
(Then: tool call, spindle, coolant, tool length compensation at safe Z)
Txx M06
S#### M03
M08
G43 Hxx Z100.
Notes:
- G90 ensures absolute positioning (prevents surprise incremental moves).
- G17 ensures XY plane (prevents arc-plane mismatches).
- G40 cancels cutter comp (prevents lateral “jump”).
- G49 cancels tool length comp before re-applying safely.
- G80 cancels canned cycles (prevents “phantom drilling”).
- G94 forces feed per minute (prevents G95 milling disasters).
1.2 SAFE END BLOCK (MILLING / GENERAL)
Make the machine predictable for the next program:
G80
G40 G49
M09
G53 Z0
G53 X0 Y0
M05
M30
1.3 SAFE RESTART MINI-BLOCK (DROP-IN)
Paste above the restart line (and restart ABOVE clearance):
G90 G17 G40 G49 G80
G94
G54
G00 Z100.
1.4 “DON’T CRASH” RAPID RULE
Never assume G00 retracts Z first. Many machines move diagonally.
Professional pattern:
G00 Z100.
G00 X… Y…
══════════════════════════════════════════════════════════════════════════════
2) MODAL LOGIC MAP (THE CNC MEMORY THAT CAUSES MOST CRASHES)
══════════════════════════════════════════════════════════════════════════════
CNC controllers are state machines: many G-codes stay active until changed.
The most crash-prone modal groups to always reset:
- Motion: G00 / G01 / G02 / G03 (only one active)
- Plane: G17 / G18 / G19
- Positioning: G90 / G91
- Feed mode: G94 / G95
- Cutter comp: G40 / G41 / G42
- Tool length: G43 / G49 (and machine-specific variants)
- Canned cycles: G80 cancels G81/G83/etc.
- Work offsets: G54–G59 (and extended systems)
Common “modal crash combos”:
- G91 left active + next program assumes G90 → unexpected travel.
- G95 left active + milling feed → tool overload / instant breakage.
- Active G41/G42 + restart without lead-in → sideways tool jump.
- Canned cycle not canceled (missing G80) + repositioning → unexpected drilling.
══════════════════════════════════════════════════════════════════════════════
3) G-CODE CORE REFERENCE (MOST USED / MOST SEARCHED)
══════════════════════════════════════════════════════════════════════════════
3.1 MOTION / INTERPOLATION
G00 Rapid positioning (highest collision risk)
G01 Linear interpolation (feed)
G02 CW arc interpolation
G03 CCW arc interpolation
G04 Dwell (pause)
Arc reminders:
- Arcs depend on active plane (G17/G18/G19).
- Center format can be I/J/K or R depending on control and settings.
3.2 PLANE SELECTION
G17 XY plane (typical milling)
G18 ZX plane
G19 YZ plane
3.3 PROGRAMMING UNITS
G20 Inch mode
G21 Metric mode
3.4 ABSOLUTE / INCREMENTAL
G90 Absolute programming
G91 Incremental programming
3.5 MACHINE / HOME / REFERENCE MOVES (USE WITH RESPECT)
G28 Reference return (home) — safest common pattern: G91 G28 Z0
G30 2nd reference return (machine-dependent)
G53 Move in machine coordinates (excellent for safe retracts)
Safe retract example:
G53 Z0
3.6 WORK OFFSETS (PART ZERO)
G54–G59 Work coordinate systems
(Extended offsets exist on many controls; naming and access vary.)
3.7 CUTTER RADIUS COMPENSATION
G40 Cancel cutter comp
G41 Cutter comp left
G42 Cutter comp right
Rule:
Always enter G41/G42 with a lead-in move; never restart inside comp.
3.8 TOOL LENGTH COMPENSATION (COMMON CRASH SOURCE)
G43 Tool length comp + (typically) positive direction
G49 Cancel tool length comp
Safe activation:
G43 Hxx Z100.
3.9 CANNED CYCLES (DRILLING / BORING FAMILY)
G80 Cancel canned cycle (always cancel before repositioning)
G81 Drilling (simple)
G82 Drilling with dwell
G83 Peck drilling (chip breaking)
G84 Tapping (machine/control dependent)
G85–G89 Boring / reaming cycles (varies by control; confirm your manual)
Cycle safety pattern:
G98/G99 (retract plane behavior) + conservative R plane + G80 cancel.
3.10 FEEDRATE MODES
G94 Feed per minute
G95 Feed per revolution
3.11 RETURN / REFERENCE OF CANNED CYCLES
G98 Return to initial plane
G99 Return to R-plane
Fixture safety note:
Wrong G98/G99 choice can drive the tool into clamps on retract.
3.12 OPTIONAL STOP / PROGRAM FLOW
M00 Program stop
M01 Optional stop (requires optional stop switch)
M30 Program end and rewind
M99 Return from subprogram (or loop end)
Common structural helpers:
O#### program numbers; subprogram call syntax is control-specific (often M98/M99 style).
══════════════════════════════════════════════════════════════════════════════
4) M-CODE CORE REFERENCE (MOST COMMON MACHINE FUNCTIONS)
══════════════════════════════════════════════════════════════════════════════
Spindle / coolant / program control:
M03 Spindle CW
M04 Spindle CCW
M05 Spindle stop
M08 Coolant on
M09 Coolant off
M00 Stop
M01 Optional stop
M30 End program
Tool change:
M06 Tool change (ATC)
Workholding (often machine-specific):
M10/M11 Clamp / unclamp (common pattern; verify your machine)
Mxx codes for pallet changers, doors, probes, air blast, chip conveyor vary widely.
Overrides:
Some controls use M-codes to enable/disable overrides (machine-dependent).
══════════════════════════════════════════════════════════════════════════════
5) MACRO SAFETY PATTERNS (SELF-CHECKING PROGRAMS)
══════════════════════════════════════════════════════════════════════════════
Macro syntax and available system variables vary by controller/options.
Use the patterns conceptually; verify variable numbers for your control.
5.1 WRONG TOOL PREVENTION (CONCEPT)
Goal: stop before cutting if wrong tool is loaded.
Pattern:
IF[ACTIVE_TOOL ≠ REQUIRED_TOOL] THEN ALARM(“LOAD TOOL ##”)
Fanuc-style example concept (variable numbers may differ):
IF[#4120 NE 12] THEN #3000=1 (LOAD TOOL 12)
5.2 POSITION LIMIT GUARD (CONCEPT)
Goal: stop if Z is below safe threshold (protect fixture).
Pattern:
IF[Z_POSITION < SAFE_Z_LIMIT] THEN ALARM(“Z LIMIT”)
5.3 SIMPLE PART COUNTER (PERSISTENT)
Goal: count cycles/parts for tool life tracking.
Pattern:
PART_COUNT = PART_COUNT + 1
IF[PART_COUNT > LIMIT] THEN ALARM(“CHANGE TOOL”)
5.4 “STATE CHECK” IDEA (ADVANCED DISCIPLINE)
Goal: enforce safe modal state before motion (conceptual).
Examples to enforce:
- absolute mode
- feed per minute
- canned cycles off
- compensation off before positioning
══════════════════════════════════════════════════════════════════════════════
6) THE MOST COMMON REAL-WORLD CNC MISTAKES (AND THE FIX)
══════════════════════════════════════════════════════════════════════════════
Mistake: Rapid XY before Z clearance
Fix: Always retract Z first (separate moves).
Mistake: Restarting below G43 activation
Fix: Restart above clearance plane and re-apply safe start block.
Mistake: G80 forgotten after drilling
Fix: Always cancel canned cycles immediately after the pattern.
Mistake: G91 inherited from manual jog / previous program
Fix: Force G90 in safe start.
Mistake: Wrong work offset (G54/G55…) silently scrapping parts
Fix: Call WCS explicitly every operation; verify offset before cycle start.
Mistake: Restart inside G41/G42 without lead-in
Fix: Restart before cutter comp entry; use proper lead-in/out.
Mistake: G95 inherited from turning ops then milling starts
Fix: Force G94 at the top (and before milling toolpaths).
══════════════════════════════════════════════════════════════════════════════
7) REAL “SHOP-STYLE” MINI EXAMPLES (SHORT, PRACTICAL, SAFE)
══════════════════════════════════════════════════════════════════════════════
7.1 SAFE POSITIONING EXAMPLE
G90 G17 G40 G49 G80
G94
G54
T01 M06
S8000 M03
M08
G43 H01 Z100.
G00 X0 Y0
G00 Z10.
G01 Z-5. F300
G00 Z100.
M09
G53 Z0
M05
M30
7.2 DRILLING PATTERN (REMEMBER G80)
G90 G17 G40 G49 G80
G94
G54
T05 M06
S2500 M03
M08
G43 H05 Z100.
G00 X20 Y20
G98 G81 Z-15. R3. F200
X40 Y20
X60 Y20
G80
G00 Z100.
M09
G53 Z0
M05
M30
══════════════════════════════════════════════════════════════════════════════
8) FINAL TAKEAWAY
══════════════════════════════════════════════════════════════════════════════
Most CNC failures are not “mystery bugs.” They’re predictable results of:
- modal memory inheritance,
- offset stacking,
- compensation misuse,
- unsafe restart behavior,
- and diagonal rapid assumptions.
If you only memorize codes, you will still crash machines.
If you control state, you control machining.
Leave a comment