Programming · 6 min read

The most common CNC G-codes and M-codes (with examples)

A practical list of the G-codes and M-codes you will see on almost every CNC mill program, what each one does and a short example program.

A CNC program is a list of blocks. G-codes tell the machine how to move; M-codes switch machine functions on and off. You do not need to memorize hundreds of them: about twenty cover most of what you will read on the shop floor.

Motion and modes

  • G00: rapid move, at full speed, never cutting.
  • G01: straight line at the programmed feed (F).
  • G02 / G03: arc clockwise / counterclockwise.
  • G17: XY plane (the usual one on a mill).
  • G20 / G21: inch / metric units (Fanuc).
  • G90 / G91: absolute / incremental coordinates.
  • G94 / G95: feed per minute / feed per revolution.

Offsets and compensation

  • G54 to G59: work offsets, where the part zero is.
  • G43 H__: turn on tool length compensation. G49 cancels it.
  • G41 / G42: cutter radius compensation left / right. G40 cancels it.
  • G28: return to machine reference.

Canned cycles

  • G81: simple drilling.
  • G83: peck drilling for deep holes.
  • G84: tapping.
  • G80: cancels any canned cycle.

M-codes you will see in every program

  • M03 / M04 / M05: spindle clockwise / counterclockwise / stop.
  • M06: tool change.
  • M08 / M09: coolant on / off.
  • M00 / M01: program stop / optional stop.
  • M30: end of program and rewind.

A short example

G21 G17 G40 G49 G80 G90
T1 M06
G54 G00 X0 Y0
S3820 M03
G43 H01 Z50. M08
G01 Z-2. F300
G01 X40. F764
G00 Z50. M09
M05
G28 G91 Z0
M30

Read it line by line: safe start line, tool 1, go to part zero, start the spindle, apply the tool length, feed down, cut 40 mm in X, retract, stop and end.

!

Siemens controls share most G-codes but write cycles as CYCLE81, CYCLE83 and CYCLE84, and use G70 / G71 for inch / metric. Always check the manual of your control.