G-code is the universal programming language used in CNC machining. Most modern CNC machines including Fanuc, Haas, Siemens, Mitsubishi, and other industrial controllers follow the ISO G-code standard.
Although certain controllers may include additional proprietary commands, the majority of motion commands, drilling cycles, offsets, and machine control codes remain consistent across most CNC milling and turning machines.
This mega library collects the most widely used G-codes and M-codes used in modern CNC machining operations.
════════════════════════════════════════════════════════════
SECTION 1 — STANDARD CNC PROGRAM START BLOCK
════════════════════════════════════════════════════════════
Professional CNC programs begin with a safe start block that resets machine states.
Example
%
O1000
G90 G17 G40 G49 G80
G54
T1 M06
S2500 M03
G00 G43 Z100 H01
This ensures safe machine initialization before machining operations begin.
════════════════════════════════════════════════════════════
SECTION 2 — MOTION CONTROL G-CODES
════════════════════════════════════════════════════════════
G00 Rapid Positioning
Moves the tool rapidly between locations.
Example
G00 X0 Y0 Z100
G01 Linear Interpolation
Used for straight cutting moves at controlled feedrate.
Example
G01 X50 Y20 F200
G02 Circular Interpolation CW
Clockwise circular cutting path.
Example
G02 X40 Y40 I10 J0 F150
G03 Circular Interpolation CCW
Counter-clockwise circular motion.
Example
G03 X20 Y20 I-10 J0 F150
════════════════════════════════════════════════════════════
SECTION 3 — PLANE SELECTION CODES
════════════════════════════════════════════════════════════
G17 XY Plane Selection
Standard milling plane.
G18 ZX Plane Selection
Used primarily for turning operations.
G19 YZ Plane Selection
Used for specialized toolpaths.
Example
G17
════════════════════════════════════════════════════════════
SECTION 4 — COORDINATE SYSTEM COMMANDS
════════════════════════════════════════════════════════════
Work coordinate systems define the part zero location.
G54 Work Offset 1
G55 Work Offset 2
G56 Work Offset 3
G57 Work Offset 4
G58 Work Offset 5
G59 Work Offset 6
Example
G54
G00 X0 Y0
════════════════════════════════════════════════════════════
SECTION 5 — TOOL COMPENSATION
════════════════════════════════════════════════════════════
Tool radius compensation allows precise contour machining.
G40 Cancel Cutter Compensation
G41 Cutter Compensation Left
G42 Cutter Compensation Right
Example
G41 D01
Tool length compensation
G43 Tool Length Compensation Positive
G49 Cancel Tool Length Compensation
Example
G43 H01 Z100
════════════════════════════════════════════════════════════
SECTION 6 — DRILLING CANNED CYCLES
════════════════════════════════════════════════════════════
Canned cycles automate drilling operations.
G81 Drilling Cycle
Example
G81 X20 Y20 Z-15 R2 F150
G82 Spot Drilling Cycle
Example
G82 X30 Y10 Z-10 R2 P1 F120
G83 Peck Drilling Cycle
Example
G83 X40 Y40 Z-30 R3 Q5 F120
G84 Tapping Cycle
Example
G84 X10 Y10 Z-12 R2 F1.5
G85 Boring Cycle
Example
G85 X50 Y20 Z-20 R3 F120
G80 Cancel Canned Cycle
════════════════════════════════════════════════════════════
SECTION 7 — MACHINE CONTROL M-CODES
════════════════════════════════════════════════════════════
M-codes control machine operations.
M00 Program Stop
M01 Optional Stop
M02 Program End
M03 Spindle Clockwise
M04 Spindle Counter-Clockwise
M05 Spindle Stop
M06 Tool Change
M07 Mist Coolant ON
M08 Flood Coolant ON
M09 Coolant OFF
M30 Program End and Reset
Example
S3000 M03
M08
════════════════════════════════════════════════════════════
SECTION 8 — ADVANCED CNC COMMANDS
════════════════════════════════════════════════════════════
G04 Dwell
Pauses machine motion.
Example
G04 P2
G28 Return to Machine Home
Example
G28 Z0
G53 Machine Coordinate Move
Example
G53 Z0
G10 Programmable Offset Setting
Example
G10 L2 P1 X0 Y0 Z0
════════════════════════════════════════════════════════════
SECTION 9 — CNC MACRO PROGRAMMING EXAMPLE
════════════════════════════════════════════════════════════
Advanced CNC controllers support macro variables.
Example
100 = 50
WHILE[#100 GT 0] DO1
G01 X#100
100 = #100 – 5
END1
Macros allow automation of repetitive machining operations.
════════════════════════════════════════════════════════════
SECTION 10 — COMPLETE MILLING PROGRAM EXAMPLE
════════════════════════════════════════════════════════════
%
O2001
G90 G17 G40 G49 G80
G54
T1 M06
S3000 M03
G00 G43 Z100 H01
G00 X0 Y0
G01 Z-5 F200
G01 X50
G01 Y50
G01 X0
G01 Y0
G00 Z100
M30
════════════════════════════════════════════════════════════
FINAL PRINCIPLE
CNC machining relies on standardized G-codes for motion control and M-codes for machine operations. By understanding the structure and application of these commands, programmers and machine operators can create safe, efficient, and reliable machining programs for modern CNC machines across multiple controller platforms.
Leave a comment