Quick answer: In CNC programming, G-codes (preparatory codes) tell the machine how to move – rapid move G00, straight cut G01, arcs G02/G03, units G21, absolute mode G90 – and M-codes (miscellaneous codes) switch machine functions – spindle on M03, coolant on M08, tool change M06, end of program M30. Below is the complete list of commonly used codes for milling and turning, with two full example programs.
Structure of a CNC program
A CNC program is a list of blocks (lines). Each block contains words made of a letter and a number. The codes below follow the ISO (RS-274) standard used by most CNC controllers; canned cycles can differ slightly between machines, so always check the machine manual.
| Letter | Meaning |
|---|---|
| N | Block (line) number |
| G | Preparatory function – type of motion or mode |
| M | Miscellaneous function – machine on/off actions |
| X, Y, Z | Coordinates of the end point (on a lathe X is usually a diameter) |
| I, J, K | Arc centre offsets from the start point (or R for radius) |
| F | Feed rate (mm/min or mm/rev) |
| S | Spindle speed (rpm or m/min) |
| T | Tool number (and offset on a lathe, e.g. T0101) |
| D / H | Cutter radius offset number / tool length offset number |
Example block: N60 G01 X50.0 Y30.0 F200 – block 60, straight cut to X50 Y30 at 200 mm/min.
G-code list
| Code | Function | Used on |
|---|---|---|
| G00 | Rapid positioning (non-cutting move) | Mill / Lathe |
| G01 | Linear interpolation (straight cut at feed F) | Mill / Lathe |
| G02 | Circular interpolation, clockwise | Mill / Lathe |
| G03 | Circular interpolation, counter-clockwise | Mill / Lathe |
| G04 | Dwell (pause for a set time) | Mill / Lathe |
| G17 / G18 / G19 | Select XY / XZ / YZ plane | Mill (G18 default on lathe) |
| G20 / G21 | Inch / millimetre units | Mill / Lathe |
| G28 | Return to machine reference (home) position | Mill / Lathe |
| G40 | Cancel cutter radius compensation | Mill / Lathe |
| G41 / G42 | Cutter compensation left / right of the path | Mill / Lathe |
| G43 / G49 | Tool length offset on / cancel | Mill |
| G54 – G59 | Work coordinate systems (work offsets) | Mill / Lathe |
| G70 | Finishing cycle | Lathe |
| G71 | Stock removal (rough turning) cycle | Lathe |
| G76 | Multi-pass threading cycle | Lathe |
| G80 | Cancel canned cycle | Mill |
| G81 | Simple drilling cycle | Mill |
| G82 | Drilling with dwell at bottom (spot face, counterbore) | Mill |
| G83 | Peck drilling cycle (deep holes) | Mill |
| G84 | Tapping cycle | Mill |
| G90 / G91 | Absolute / incremental programming | Mill |
| G94 / G95 | Feed per minute / feed per revolution | Mill |
| G96 / G97 | Constant surface speed / constant rpm | Lathe |
| G98 / G99 | Feed per minute / feed per revolution | Lathe |
M-code list
| Code | Function |
|---|---|
| M00 | Program stop (operator must restart) |
| M01 | Optional stop (only if the optional-stop switch is on) |
| M02 | End of program |
| M03 | Spindle on, clockwise |
| M04 | Spindle on, counter-clockwise |
| M05 | Spindle stop |
| M06 | Tool change |
| M08 | Coolant on |
| M09 | Coolant off |
| M30 | End of program and rewind to start |
| M98 | Call a subprogram |
| M99 | End of subprogram / return to main program |
Example 1: Milling program – rectangular profile
This program cuts the outside of a 50 × 30 mm rectangle, 2 mm deep, with a 10 mm end mill. The work zero (G54) is at the lower-left corner of the part.
O1001 (PROFILE 50 X 30 RECTANGLE, 10 MM END MILL)
N10 G21 G17 G90 G40 G80 (mm, XY plane, absolute, cancel comp and cycles)
N20 T01 M06 (tool 1)
N30 G54 G00 X-10.0 Y-10.0 S1200 M03
N40 G43 H01 Z5.0 M08 (tool length offset, coolant on)
N50 G01 Z-2.0 F100 (plunge 2 mm deep)
N60 G41 D01 X0 Y0 F200 (start cutter compensation, left)
N70 Y30.0
N80 X50.0
N90 Y0
N100 X-5.0
N110 G40 X-10.0 Y-10.0 (cancel compensation)
N120 G00 Z50.0 M09
N130 M05
N140 M30Example 2: Lathe program – step turning
This program turns a 40 mm bar down to 30 mm diameter over a length of 25 mm in two passes. On a lathe, X values are diameters and Z = 0 is the front face of the part.
O2001 (TURN 40 MM BAR TO 30 MM FOR 25 MM LENGTH)
N10 G21 G40 G99 (mm, cancel comp, feed per rev)
N20 T0101 (tool 1, offset 1)
N30 G97 S800 M03
N40 G00 X42.0 Z2.0 M08
N50 X35.0 (first pass diameter)
N60 G01 Z-25.0 F0.2
N70 X42.0
N80 G00 Z2.0
N90 X30.0 (final diameter)
N100 G01 Z-25.0
N110 X42.0
N120 G00 X100.0 Z100.0 M09
N130 M05
N140 M30Tips for writing CNC programs
- Start every program with a safety block (G21 G90 G40 G80 or the lathe equivalent) to cancel old modes.
- Use G00 only in clear air; approach the part at least 2 mm away before switching to G01.
- Turn on coolant (M08) before cutting and switch it off (M09) before the tool retracts home.
- Check the program with a dry run or simulation before cutting metal.