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)
| Code | Function | Example Use Case |
|---|---|---|
| G00 | Rapid positioning (non‑cutting move) | Move quickly to a safe start point |
| G01 | Linear interpolation (controlled feed) | Mill a straight edge or turn a diameter |
| G02 | Circular interpolation clockwise | Cut a convex arc |
| G03 | Circular interpolation counter‑clockwise | Cut a concave arc |
| G17 | XY plane selection | Default for most milling |
| G20 | Inch units | Set for inch‑based part programs |
| G21 | Metric units | Set for metric‑based programs |
| G90 | Absolute positioning | Coordinates from fixed zero point |
| G91 | Incremental positioning | Coordinates from current tool position |
| G54‑G59 | Work coordinate systems | Store part zero offsets (G54 is most common) |
03Essential M Code List
| Code | Function | Example Use Case |
|---|---|---|
| M03 | Spindle on (clockwise) | Start cutting with right‑hand tool |
| M04 | Spindle on (counter‑clockwise) | For left‑hand tools or tapping |
| M05 | Spindle stop | After finishing a cut |
| M08 | Flood coolant on | During heavy material removal |
| M09 | Coolant off | Before tool change or program end |
| M30 | Program end and rewind | Finish main program, reset to start |
| M06 | Automatic tool change | Used with T code (e.g., T01 M06) |
| M00 | Program stop (optional) | Pause for inspection |
| M01 | Optional stop | Only 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
| Mistake | Consequence | Fix |
|---|---|---|
| Forgetting G90/G91 | Wrong coordinates, crash | Always set at program start |
| No G17 before circular moves | Arc in wrong plane | Specify G17 (milling) before G02/G03 |
| Using M06 without T code | No tool change | Write “T01 M06” |
| M30 at end of subprogram | Resets entire program | Use 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.

