The Ultimate CNC Programming Cheat Sheet: Mastering G-Code and Machine Control

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

ConceptDescription
G-CodeThe primary programming language for CNC machines that controls motion, speed, and other machine functions
M-CodeMiscellaneous machine commands that control auxiliary functions like coolant, tool changes, and program stops
Coordinate SystemDefines the workpiece location and tool movement in 3D space (usually XYZ for mills, XZ for lathes)
Work OffsetReference point on the workpiece from which programmed coordinates are measured
Tool OffsetCompensates for tool dimensions, wear, and geometry
Feed RateSpeed at which the cutting tool moves through the material (inches/min or mm/min)
Spindle SpeedRotational speed of the cutting tool or workpiece (RPM)

Common G-Codes (Motion & Cutting Commands)

CodeNameDescriptionExample
G00Rapid PositioningFastest non-cutting movement between positionsG00 X100 Y50
G01Linear InterpolationControlled-feed linear movementG01 X50 Y25 F300
G02CW Circular InterpolationClockwise arc movementG02 X50 Y50 I0 J25 F200
G03CCW Circular InterpolationCounter-clockwise arc movementG03 X50 Y50 I25 J0 F200
G04DwellPause for specified timeG04 P2.5 (pause 2.5 sec)
G17XY Plane SelectionSets active plane to XYG17
G18XZ Plane SelectionSets active plane to XZG18
G19YZ Plane SelectionSets active plane to YZG19
G20Inch UnitsSet programming units to inchesG20
G21Metric UnitsSet programming units to millimetersG21
G28Return to Home PositionMove to machine home through reference pointG28 X0 Y0 Z0
G40Tool Compensation CancelCancels cutter radius compensationG40
G41Tool Compensation LeftApplies cutter comp left of programmed pathG41 D1 (use offset #1)
G42Tool Compensation RightApplies cutter comp right of programmed pathG42 D2 (use offset #2)
G43Tool Length Compensation +Apply positive tool length offsetG43 H1 (use offset #1)
G49Tool Length Comp CancelCancels tool length compensationG49
G54-G59Work Coordinate SystemsSelect coordinate system 1-6G54 (use WCS #1)
G80Cancel Canned CycleCancels active canned cycleG80
G81Drilling CycleBasic drilling cycleG81 X100 Y100 Z-10 R5 F200
G82Drilling with DwellDrilling with pause at bottomG82 X100 Y100 Z-10 R5 P1 F200
G83Peck Drilling CycleDeep hole drilling with chip breakingG83 X100 Y100 Z-30 R5 Q5 F200
G90Absolute ProgrammingCoordinates measured from work zeroG90 G01 X100 Y100
G91Incremental ProgrammingCoordinates measured from current positionG91 G01 X10 Y5
G94Feed Per MinuteFeed rate in units per minuteG94 F300
G95Feed Per RevolutionFeed rate in units per revolutionG95 F0.005

Common M-Codes (Miscellaneous Functions)

CodeDescriptionExample
M00Program StopM00 (Stops program until restart)
M01Optional StopM01 (Stops if optional stop switch is on)
M02Program EndM02 (End of program)
M03Spindle On ClockwiseM03 S1200 (Spindle on CW at 1200 RPM)
M04Spindle On Counter-ClockwiseM04 S1000 (Spindle on CCW at 1000 RPM)
M05Spindle StopM05 (Stop spindle)
M06Tool ChangeM06 T2 (Change to tool #2)
M07Mist Coolant OnM07 (Turn on mist coolant)
M08Flood Coolant OnM08 (Turn on flood coolant)
M09Coolant OffM09 (Turn off all coolant)
M30Program End and RewindM30 (End program and reset to beginning)
M98Subprogram CallM98 P1000 (Call subprogram O1000)
M99Subprogram Return/EndM99 (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

  1. Program Number/Name (O1000)
  2. Comments/Documentation (enclosed in parentheses)
  3. Setup Commands (units, work offsets, safety positions)
  4. Tool Selection and Compensation
  5. Machining Commands (cutting operations)
  6. End of Program Commands (return to home, program end)

Machining Operations and Canned Cycles

Common Milling Operations

OperationDescriptionExample G-Code
Face MillingMachining a flat surfaceG00 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 MillingCutting along a contourG41 D1 G01 X10 Y10 F30<br>G01 X50<br>G02 X70 Y30 I0 J20<br>G01 X70 Y50<br>G40 G01 X90 Y70
Pocket MillingRemoving material from a closed areaG00 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
DrillingCreating holesG81 X10 Y10 Z-10 R2 F100<br>X30 Y10<br>X50 Y10<br>G80

Common Turning Operations

OperationDescriptionExample G-Code
FacingMachining the end of the workpieceG00 X50 Z5<br>G01 Z0 F0.2<br>G01 X-1.0<br>G00 Z5<br>G00 X45
TurningReducing the diameterG00 X30 Z2<br>G01 X30 Z-50 F0.2<br>G00 X35<br>G00 Z2
GroovingCutting a groove/recessG00 X30 Z-20<br>G01 X20 F0.1<br>G01 X30<br>G00 Z2
ThreadingCutting threadsG76 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

ChallengeSolution
Tool BreakageUse peck drilling for deep holes, reduce feed rates in hard materials, ensure proper coolant
Poor Surface FinishCheck cutting speeds, feed rates, tool condition, use finishing passes, check rigidity of setup
Inaccurate DimensionsVerify tool offsets, check for tool wear, ensure proper workpiece setup, verify machine calibration
Program ErrorsUse simulation before running on machine, use single block mode for verification, check syntax
Complex ContoursUse CAM software for generating toolpaths, verify with simulation
Deep Pocket MachiningUse rough and finish operations, implement step-down approach, ensure chip evacuation
Thin Wall MachiningReduce cutting forces, use climb milling, consider workpiece support

Programming Best Practices

  1. Start with safety commands at the beginning of each program

    G20 G40 G49 G54 G80 G90
    
  2. Use comments liberally to document your code

    (TOOL: 1/2" END MILL)
    (OPERATION: FACE MILLING)
    
  3. Structure programs logically with operations grouped together

  4. Use consistent programming style for readability and maintenance

  5. Include setup information in program header

    (ZERO: LOWER LEFT CORNER, TOP OF PART)
    (STOCK SIZE: 6.0 X 4.0 X 1.0)
    
  6. Plan tool paths to minimize tool changes and maximize efficiency

  7. Use canned cycles for repetitive operations

  8. Verify programs through simulation before running on machine

  9. Keep backup copies of all programs

  10. Document revisions and changes to programs

G-Code Dialects and Controller-Specific Commands

ControllerSpecial FeaturesExample
FanucCustom macros, additional work offsetsG65 P9001 A20 B30 (Call custom macro)
HaasVPS wizards, intuitive programmingG47 (Engraving cycle)
SiemensShopMill/ShopTurn conversationalCYCLE832 (High speed machining cycle)
MazakMazatrol conversationalG05 (AI contour control mode)
OkumaUser task programs, advanced macrosG161 (Polar coordinate conversion)

CNC Machine Types and Programming Considerations

Machine TypeKey Programming Considerations
3-Axis MillTool length compensation, 2D/2.5D toolpaths, work offsets
Multi-Axis MillTool center point control, rotary axis limits, collision avoidance
CNC LatheTool nose radius compensation, constant surface speed, tailstock programming
Swiss-Type LatheGuide bushing, synchronous operations, multiple tool positions
Mill-TurnSpindle synchronization, live tooling, coordinate system transformations
RouterLarge work areas, vacuum fixtures, high feed rates
Wire EDMWire 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.

Scroll to Top