CNC G & M Codes: Complete Guide with Examples

CNC G and M codes are the programming language used to control CNC machines. G codes (preparatory functions) command the machine’s movement and positioning, while M codes (miscellaneous functions) control auxiliary actions like spindle start/stop and coolant. This guide provides a complete, practical reference with real‑world examples—no brand names, just standard industry practice.

01What Are G and M Codes? (Core Definition)

G codes (e.g., G00, G01, G02) define how the tool moves: rapid positioning, linear feed, circular interpolation, etc.

M codes (e.g., M03, M08, M30) manage on/off or program flow actions: spindle rotation, coolant, program end.

Both follow the ISO 6983 standard,ensuring consistency across most CNC controllers (Fanuc, Haas, Siemens, etc.).

02Essential G Code List (Most Frequently Used)

CodeFunctionExample Use Case
G00Rapid positioning (non‑cutting move)Move quickly to a safe start point
G01Linear interpolation (controlled feed)Mill a straight edge or turn a diameter
G02Circular interpolation clockwiseCut a convex arc
G03Circular interpolation counter‑clockwiseCut a concave arc
G17XY plane selectionDefault for most milling
G20Inch unitsSet for inch‑based part programs
G21Metric unitsSet for metric‑based programs
G90Absolute positioningCoordinates from fixed zero point
G91Incremental positioningCoordinates from current tool position
G54‑G59Work coordinate systemsStore part zero offsets (G54 is most common)

03Essential M Code List

CodeFunctionExample Use Case
M03Spindle on (clockwise)Start cutting with right‑hand tool
M04Spindle on (counter‑clockwise)For left‑hand tools or tapping
M05Spindle stopAfter finishing a cut
M08Flood coolant onDuring heavy material removal
M09Coolant offBefore tool change or program end
M30Program end and rewindFinish main program, reset to start
M06Automatic tool changeUsed with T code (e.g., T01 M06)
M00Program stop (optional)Pause for inspection
M01Optional stopOnly stops if operator enables

04Real‑World Example: Machining a Simple Part

Scenario: You need to face the top of a 100mm x 100mm aluminum block, then drill a 10mm hole at the center.

Safe start (home position)

N10 G21 G90 G17 (Metric, absolute, XY plane)

N20 G54 (Select work offset)

N30 M03 S2000 (Spindle CW, 2000 RPM)

N40 G00 X50 Y50 (Rapid to center of block – assuming zero at lower left)

Face milling (remove 0.5mm depth)

N50 G01 Z-0.5 F200 (Feed down to cut depth)

N60 X-50 F300 (Move beyond left edge)

N70 X150 (Move beyond right edge)

N80 G00 Z10 (Retract)

Drilling cycle

N90 G81 R2 Z-12 F100 (Canned cycle: rapid to R plane, drill to Z-12)

N100 X50 Y50 (Drill at center)

N110 G80 (Cancel cycle)

Finish

N120 M05 (Spindle off)

N130 M09 (Coolant off)

N140 G00 Z50 (Move Z up)

N150 G91 G28 X0 Y0 (Return home via reference point)

N160 M30 (Program end)

> What happened? G00, G01, G81 controlled movements; M03, M05, M09 handled spindle and coolant; G90/G91 switched positioning.

05Critical Rules to Avoid Crashes

Always include a safety block at program start: G21/G20, G90, G17, G54.

Never omit G00 or G01 – a missing feed command may cause rapid movement into the part.

M codes are often “modal” – M03 stays active until M05 or M30.

G00 rapid moves should be at least 5mm above the part (depends on machine).

Test new programs using the machine’s graphics simulation or a backplotter (e.g., NCViewer).

06Common Mistakes and Fixes

MistakeConsequenceFix
Forgetting G90/G91Wrong coordinates, crashAlways set at program start
No G17 before circular movesArc in wrong planeSpecify G17 (milling) before G02/G03
Using M06 without T codeNo tool changeWrite “T01 M06”
M30 at end of subprogramResets entire programUse M99 to return from subprogram

07How to Learn and Practice (Actionable Advice)

1. Master the top 10 G codes and top 8 M codes – they cover 90% of daily work.

2. Use a free CNC simulator (e.g., CNC Simulator Pro trial or LinuxCNC) to test snippets.

3. Write one small program per day – start with facing, then add drilling, then contouring.

4. Review your machine’s manual – some M codes vary (e.g., M08 vs M07 for mist coolant).

5. Always prove out new code at 10% feed override with “single block” mode.

Core takeaway: G codes define how the tool moves; M codes define what the machine does. Without both, a CNC program is incomplete. Start with the examples above, simulate every line, and gradually build complexity.

Action plan for today: Open your controller’s editor (or a simulator), type the facing example from section 4, run it in graphics mode, then change feed rates to see the effect. Repeat with a new shape tomorrow.

YPMFG

Factory CNC Machined Parts

Need high-precision parts for your project? Get instant pricing & DFM feedback

Ready for Your Project?

YP-MFG is a leading manufacturer specializing in high-precision metal parts and CNC machining services.

Contact

WhatsApp/Phone

+86 137 9493 0097

Address

Building A6, The Third Industrial Zone, Fenghuang Community, Fuyong Street, Bao’an District, Shenzhen

Copyright YP-MFG © 2025 All Rights Reserved

滚动至顶部

Is your design ready for CNC machining?​

Upload your CAD file and discuss it directly with an experienced engineer.
Get a professional DFM review and full project consultation.