Email Us
en

M Code / G Code List – The CNC Programming Essentials Explained

Table of Content [Hide]

    Whether you’re a CNC machinist, programmer, or manufacturer looking to optimize production, understanding M codes and G codes is non-negotiable. These two types of commands form the foundation of CNC programming, enabling precise control of machine operations like cutting, tool changes, coolant flow, and motion paths.

    This article provides a comprehensive guide to common G and M codes, how they function, and where they're used—especially within FANUC-controlled CNC systems.


    What Are G Codes and M Codes?

    • G Codes (Geometric Codes) define machine movement and tool paths.

    • M Codes (Miscellaneous Codes) handle machine functions such as turning on coolant, stopping the spindle, or tool changing.

    Think of it this way:

    • G codes = How the machine moves.

    • M codes = What the machine does while moving (or before/after).

    Both are essential for a successful CNC program.


    G Code List – Common Geometric Commands

    G CodeFunctionDescription
    G00Rapid positioningMoves tool quickly from point to point without cutting
    G01Linear interpolationMoves tool in a straight line at a set feed rate
    G02Circular interpolation (clockwise)Cuts arcs or circles in a clockwise direction
    G03Circular interpolation (counter-clockwise)Cuts arcs or circles counter-clockwise
    G04DwellPauses the machine for a set time
    G17Select XY planeDefault plane for 2D machining
    G18Select ZX planeUseful in turning centers
    G19Select YZ planeFor vertical or sidewall machining
    G20Inch unitsSets machine units to inches
    G21Metric unitsSets machine units to millimeters
    G28Return to machine zeroSends tool to home position
    G40Cancel cutter compensationTurns off tool radius offset
    G41Cutter compensation leftOffsets tool to the left of path
    G42Cutter compensation rightOffsets tool to the right of path
    G43Tool length compensation +Compensates for tool height
    G49Cancel tool length compensationResets tool length offset
    G54 – G59Work coordinate systemsAllows multiple part setups
    G80Cancel canned cycleStops any active drilling cycle
    G81Drilling cycleBasic vertical drilling operation
    G82Drilling with dwellPauses at bottom of drill before retract
    G83Peck drilling cycleBreaks chips while drilling deep holes
    G90Absolute programmingCoordinates based on part origin
    G91Incremental programmingCoordinates based on last position
    G94Feed per minuteFeed rate in mm/min or in/min
    G95Feed per revolutionFeed rate based on spindle RPM

    Note: G codes can vary slightly depending on the machine type (mill vs. lathe) and the controller (FANUC, Siemens, Haas, etc.)


    M Code List – Common Machine Function Commands

    M CodeFunctionDescription
    M00Program stopStops machine until manually restarted
    M01Optional stopStops only if optional stop is enabled
    M02Program endEnds the program
    M03Spindle on (CW)Starts spindle clockwise
    M04Spindle on (CCW)Starts spindle counter-clockwise
    M05Spindle stopStops the spindle rotation
    M06Tool changeAutomatically changes to next tool
    M08Coolant onActivates coolant flow
    M09Coolant offStops coolant flow
    M30Program end and rewindEnds program and resets to the top
    M98Call subprogramRuns a separate subroutine
    M99End of subprogram/loopReturns from subprogram or loops program

    Pro Tip: Always check your machine's specific M-code list in the operator’s manual. Not all machines treat M-codes the same way

    .

    M Code vs. G Code: What’s the Difference?

    FeatureG CodeM Code
    PurposeControls movementControls machine functions
    ExamplesG01, G02, G90M03, M06, M30
    AffectsMotion/geometryAuxiliary machine tasks
    Editable by User?YesYes
    SequenceMust be preciseTypically follows G code actions


    Where Are G and M Codes Used?

    • Milling machines

    • Turning centers (lathes)

    • Multi-axis machining centers

    • Drilling machines

    • Robotic systems with CNC integration

    They are especially prevalent in FANUC-based systems, where standardization and efficiency are critical for large-scale automation.


    How to Learn CNC G/M Codes Quickly

    1. Start with simulation software like NC Viewer or FANUC’s CNC Guide.

    2. Practice writing simple programs on a test machine or emulator.

    3. Use cheat sheets like this article as a quick reference.

    4. Understand the logic, not just the syntax—know why each code is used.

    5. Read real CNC programs from your shop or online libraries.


    Sample CNC Code Using G and M Codes

    gcodeCopyEditG21          ; Set to metric
    G90          ; Absolute positioning
    G00 X0 Y0    ; Rapid to start point
    M03 S1200    ; Spindle on clockwise at 1200 RPM
    G01 Z-5 F100 ; Feed to depth
    G02 X50 Y50 R25 ; Cut clockwise arc
    M09          ; Coolant off
    G00 Z100     ; Retract tool
    M30          ; End program


    Conclusion

    Understanding G codes and M codes is like learning the alphabet of CNC programming. Without these foundational instructions, machine tools wouldn't know where to move, when to start, or how to perform key functions like tool changes or coolant control.

    Whether you're maintaining a FANUC-controlled line or writing programs from scratch, mastering these codes improves both productivity and precision.


    FAQs

    Q1: Are G codes and M codes the same across all CNC machines?
    Not always. Many are standardized, but some codes vary by controller brand (FANUC, Haas, Siemens) and machine type.

    Q2: Can I create custom M codes?
    Yes, some controllers allow custom M-code programming, especially for macro-based automation or user-defined tasks.

    Q3: What happens if I forget to include an M30 at the end of a program?
    The machine may not rewind to the top of the program or may not stop correctly. Always include a proper program end code.

    Q4: Is it possible to run a CNC program with only G codes?
    Technically yes, but you would lack control over machine functions like spindle, coolant, or tool changes—limiting its usefulness.

    Q5: Where can I download a complete G/M code manual for FANUC?
    You can get official documentation from FANUC’s support center, your machine tool builder, or authorized dealers.



    References
    Related Funac Parts

    WEITE INSIGHTS & BLOG

    Weite Welcomes Your Inquiries for More Fanuc Parts
    Talk to Us
    Need Fanuc Parts Details?
    Weite Welcomes Your Inquiries for More Fanuc Parts
    East side of the 2nd floor, Building B, No. 333, Heting Street, Qingshan Lake Street, Hangzhou
    Weite Welcomes Your Inquiries for More Fanuc Parts
    Talk to Us
    East side of the 2nd floor, Building B, No. 333, Heting Street, Qingshan Lake Street, Hangzhou
    fanucalex@gmail.com +8613858013242
    Need Fanuc Parts Details?
    Weite Welcomes Your Inquiries for More Fanuc Parts