Introduction
Computer Numerical Control (CNC) programming is the process of creating coded instructions that automate the movement and operation of machine tools. CNC programming enables precise, repeatable manufacturing of complex parts across machining centers, lathes, mills, routers, and other equipment. This cheat sheet provides a comprehensive reference for G-code programming fundamentals, commands, techniques, and best practices for CNC operators and programmers.
Core CNC Programming Concepts
Concept | Description |
---|---|
G-Code | The primary programming language for CNC machines that controls motion, speed, and other machine functions |
M-Code | Miscellaneous machine commands that control auxiliary functions like coolant, tool changes, and program stops |
Coordinate System | Defines the workpiece location and tool movement in 3D space (usually XYZ for mills, XZ for lathes) |
Work Offset | Reference point on the workpiece from which programmed coordinates are measured |
Tool Offset | Compensates for tool dimensions, wear, and geometry |
Feed Rate | Speed at which the cutting tool moves through the material (inches/min or mm/min) |
Spindle Speed | Rotational speed of the cutting tool or workpiece (RPM) |
Common G-Codes (Motion & Cutting Commands)
Code | Name | Description | Example |
---|---|---|---|
G00 | Rapid Positioning | Fastest non-cutting movement between positions | G00 X100 Y50 |
G01 | Linear Interpolation | Controlled-feed linear movement | G01 X50 Y25 F300 |
G02 | CW Circular Interpolation | Clockwise arc movement | G02 X50 Y50 I0 J25 F200 |
G03 | CCW Circular Interpolation | Counter-clockwise arc movement | G03 X50 Y50 I25 J0 F200 |
G04 | Dwell | Pause for specified time | G04 P2.5 (pause 2.5 sec) |
G17 | XY Plane Selection | Sets active plane to XY | G17 |
G18 | XZ Plane Selection | Sets active plane to XZ | G18 |
G19 | YZ Plane Selection | Sets active plane to YZ | G19 |
G20 | Inch Units | Set programming units to inches | G20 |
G21 | Metric Units | Set programming units to millimeters | G21 |
G28 | Return to Home Position | Move to machine home through reference point | G28 X0 Y0 Z0 |
G40 | Tool Compensation Cancel | Cancels cutter radius compensation | G40 |
G41 | Tool Compensation Left | Applies cutter comp left of programmed path | G41 D1 (use offset #1) |
G42 | Tool Compensation Right | Applies cutter comp right of programmed path | G42 D2 (use offset #2) |
G43 | Tool Length Compensation + | Apply positive tool length offset | G43 H1 (use offset #1) |
G49 | Tool Length Comp Cancel | Cancels tool length compensation | G49 |
G54-G59 | Work Coordinate Systems | Select coordinate system 1-6 | G54 (use WCS #1) |
G80 | Cancel Canned Cycle | Cancels active canned cycle | G80 |
G81 | Drilling Cycle | Basic drilling cycle | G81 X100 Y100 Z-10 R5 F200 |
G82 | Drilling with Dwell | Drilling with pause at bottom | G82 X100 Y100 Z-10 R5 P1 F200 |
G83 | Peck Drilling Cycle | Deep hole drilling with chip breaking | G83 X100 Y100 Z-30 R5 Q5 F200 |
G90 | Absolute Programming | Coordinates measured from work zero | G90 G01 X100 Y100 |
G91 | Incremental Programming | Coordinates measured from current position | G91 G01 X10 Y5 |
G94 | Feed Per Minute | Feed rate in units per minute | G94 F300 |
G95 | Feed Per Revolution | Feed rate in units per revolution | G95 F0.005 |
Common M-Codes (Miscellaneous Functions)
Code | Description | Example |
---|---|---|
M00 | Program Stop | M00 (Stops program until restart) |
M01 | Optional Stop | M01 (Stops if optional stop switch is on) |
M02 | Program End | M02 (End of program) |
M03 | Spindle On Clockwise | M03 S1200 (Spindle on CW at 1200 RPM) |
M04 | Spindle On Counter-Clockwise | M04 S1000 (Spindle on CCW at 1000 RPM) |
M05 | Spindle Stop | M05 (Stop spindle) |
M06 | Tool Change | M06 T2 (Change to tool #2) |
M07 | Mist Coolant On | M07 (Turn on mist coolant) |
M08 | Flood Coolant On | M08 (Turn on flood coolant) |
M09 | Coolant Off | M09 (Turn off all coolant) |
M30 | Program End and Rewind | M30 (End program and reset to beginning) |
M98 | Subprogram Call | M98 P1000 (Call subprogram O1000) |
M99 | Subprogram Return/End | M99 (Return to main program) |
CNC Program Structure
Basic Program Template
O1000 (PROGRAM NAME/NUMBER)
(DESCRIPTION: PART DESCRIPTION)
(DATE: MM/DD/YYYY)
(PROGRAMMER: NAME)
G20 G40 G49 G54 G80 G90 (SAFETY LINE)
T1 M06 (TOOL CHANGE - 1/2" END MILL)
G43 H1 (APPLY TOOL LENGTH OFFSET)
M03 S1200 (SPINDLE ON CW AT 1200 RPM)
G00 X0 Y0 (RAPID TO START POSITION)
G00 Z0.5 (RAPID TO CLEARANCE PLANE)
(MACHINING OPERATIONS)
G01 Z-0.25 F10 (PLUNGE TO CUTTING DEPTH)
G01 X1 Y1 F30 (LINEAR MOVE)
...
G00 Z1.0 (RAPID TO SAFE HEIGHT)
M05 (SPINDLE OFF)
G28 G91 Z0 (RETURN TO Z HOME)
G28 X0 Y0 (RETURN TO XY HOME)
M30 (END PROGRAM)
Program Components
- Program Number/Name (
O1000
) - Comments/Documentation (enclosed in parentheses)
- Setup Commands (units, work offsets, safety positions)
- Tool Selection and Compensation
- Machining Commands (cutting operations)
- End of Program Commands (return to home, program end)
Machining Operations and Canned Cycles
Common Milling Operations
Operation | Description | Example G-Code |
---|---|---|
Face Milling | Machining a flat surface | G00 X-10 Y-10 <br>G01 Z-0.1 F10 <br>G01 X110 F30 <br>G01 Y10 <br>G01 X-10 <br>G01 Y30 <br>G01 X110 |
Profile Milling | Cutting along a contour | G41 D1 G01 X10 Y10 F30 <br>G01 X50 <br>G02 X70 Y30 I0 J20 <br>G01 X70 Y50 <br>G40 G01 X90 Y70 |
Pocket Milling | Removing material from a closed area | G00 X10 Y10 <br>G01 Z-0.25 F10 <br>G01 X40 F30 <br>G01 Y40 <br>G01 X10 <br>G01 Y10 <br>G00 Z0.5 |
Drilling | Creating holes | G81 X10 Y10 Z-10 R2 F100 <br>X30 Y10 <br>X50 Y10 <br>G80 |
Common Turning Operations
Operation | Description | Example G-Code |
---|---|---|
Facing | Machining the end of the workpiece | G00 X50 Z5 <br>G01 Z0 F0.2 <br>G01 X-1.0 <br>G00 Z5 <br>G00 X45 |
Turning | Reducing the diameter | G00 X30 Z2 <br>G01 X30 Z-50 F0.2 <br>G00 X35 <br>G00 Z2 |
Grooving | Cutting a groove/recess | G00 X30 Z-20 <br>G01 X20 F0.1 <br>G01 X30 <br>G00 Z2 |
Threading | Cutting threads | G76 X28.25 Z-30 K1.5 D3 F1.5 <br>G00 X50 Z50 |
Tool Compensation and Offsets
Tool Radius Compensation
- G40: Cancel tool radius compensation
- G41: Tool radius compensation left (tool on left side of programmed path)
- G42: Tool radius compensation right (tool on right side of programmed path)
Tool Length Compensation
- G43: Apply positive tool length compensation
- G44: Apply negative tool length compensation
- G49: Cancel tool length compensation
Work Coordinate Systems
- G54-G59: Standard work coordinate systems
- G54.1 P1-P48: Additional work coordinate systems (on some controls)
Advanced CNC Programming Techniques
Parametric Programming
- Using variables instead of fixed values
- Example:
#1=50.0 (DIAMETER)
<br>G01 X#1 F300
Subprograms and Macros
- Creating reusable code blocks
- Example main program:
O1000G90 G54M98 P1001 L5 (CALL SUBPROGRAM 5 TIMES)M30
- Example subprogram:
O1001G01 X10 Y10 F300G01 X20 Y20M99 (RETURN TO MAIN PROGRAM)
Conditional Statements and Loops
- Control program flow based on conditions
- Example:
#1=1 (COUNTER)WHILE [#1 LE 5] DO G01 X#1 Y#1 F300 #1=#1+1END
Common Challenges and Solutions
Challenge | Solution |
---|---|
Tool Breakage | Use peck drilling for deep holes, reduce feed rates in hard materials, ensure proper coolant |
Poor Surface Finish | Check cutting speeds, feed rates, tool condition, use finishing passes, check rigidity of setup |
Inaccurate Dimensions | Verify tool offsets, check for tool wear, ensure proper workpiece setup, verify machine calibration |
Program Errors | Use simulation before running on machine, use single block mode for verification, check syntax |
Complex Contours | Use CAM software for generating toolpaths, verify with simulation |
Deep Pocket Machining | Use rough and finish operations, implement step-down approach, ensure chip evacuation |
Thin Wall Machining | Reduce cutting forces, use climb milling, consider workpiece support |
Programming Best Practices
Start with safety commands at the beginning of each program
G20 G40 G49 G54 G80 G90
Use comments liberally to document your code
(TOOL: 1/2" END MILL) (OPERATION: FACE MILLING)
Structure programs logically with operations grouped together
Use consistent programming style for readability and maintenance
Include setup information in program header
(ZERO: LOWER LEFT CORNER, TOP OF PART) (STOCK SIZE: 6.0 X 4.0 X 1.0)
Plan tool paths to minimize tool changes and maximize efficiency
Use canned cycles for repetitive operations
Verify programs through simulation before running on machine
Keep backup copies of all programs
Document revisions and changes to programs
G-Code Dialects and Controller-Specific Commands
Controller | Special Features | Example |
---|---|---|
Fanuc | Custom macros, additional work offsets | G65 P9001 A20 B30 (Call custom macro) |
Haas | VPS wizards, intuitive programming | G47 (Engraving cycle) |
Siemens | ShopMill/ShopTurn conversational | CYCLE832 (High speed machining cycle) |
Mazak | Mazatrol conversational | G05 (AI contour control mode) |
Okuma | User task programs, advanced macros | G161 (Polar coordinate conversion) |
CNC Machine Types and Programming Considerations
Machine Type | Key Programming Considerations |
---|---|
3-Axis Mill | Tool length compensation, 2D/2.5D toolpaths, work offsets |
Multi-Axis Mill | Tool center point control, rotary axis limits, collision avoidance |
CNC Lathe | Tool nose radius compensation, constant surface speed, tailstock programming |
Swiss-Type Lathe | Guide bushing, synchronous operations, multiple tool positions |
Mill-Turn | Spindle synchronization, live tooling, coordinate system transformations |
Router | Large work areas, vacuum fixtures, high feed rates |
Wire EDM | Wire offset, taper cutting, thread threading |
Resources for Further Learning
Books and Publications
- “CNC Programming Handbook” by Peter Smid
- “Machining For Dummies” by Kip Hanson
- “CNC Trade Secrets” by James Harvey
- “CNC Programming: Principles and Applications” by Mike Mattson
- “Machinery’s Handbook” (tool speeds and feeds reference)
Online Resources
- CNC Zone Forums (www.cnczone.com)
- Haas Tip of the Day (www.haascnc.com)
- NYC CNC YouTube Channel
- MIT OpenCourseWare – Manufacturing Processes
- Autodesk CAM Community
Software Tools
- Fusion 360 (CAD/CAM)
- Mastercam
- SolidCAM
- CNC Simulator Pro
- G-Wizard (speeds and feeds calculator)
Training Resources
- Coursera CNC Programming classes
- Local community college courses
- Machine tool manufacturer training programs
- Industry certification programs (NIMS, etc.)
By mastering G-code programming and understanding the principles outlined in this cheat sheet, you can effectively create efficient, reliable CNC programs for a wide range of manufacturing applications. Remember that practice and continuous learning are key to becoming a proficient CNC programmer.