G and M codes: Practical CNC Reference

CNC G and M codes reference showing motion and machine-control commands

G and M codes divide CNC instructions into two practical groups: G codes prepare motion, positioning, and cutting behavior, while M codes control machine actions such as the spindle, coolant, tool changes, and program flow. Use this G-code chart to identify motion commands, then use the M-code list for machine-control commands.

The entries below reflect common milling conventions. Extended G and M code assignments can differ by controller, machine builder, and machine type, so confirm specialized commands in the relevant programming manual.

How do G and M codes differ in a CNC program?

A G code tells the control how to interpret movement or preparation. For example, G01 X40.0 F120 commands a straight feed move to X40.0 at a feed rate of 120. An M code triggers a machine function: M03 S2500 starts the spindle clockwise at 2,500 rpm.

Many G codes are modal. A modal command remains active until another command in the same group cancels or replaces it. After G01, later coordinates continue using linear feed moves until G00, G02, or G03 changes the motion mode. Unit selection, work offsets, and absolute positioning are also typically modal.

Non-modal, or one-shot, commands apply only to the block where they appear. G04 dwell is a common example. Most M codes are block-specific machine actions rather than continuously active modes, although exact behavior depends on the control.

G90 selects absolute positioning: X and Y values refer to the active work coordinate zero. G91 selects incremental positioning: each value specifies a distance from the current location. A program should establish the intended mode explicitly.

What belongs in a G-code chart for motion, coordinates, and units?

  • G00 X__ Y__ Z__ — rapid positioning without a cutting feed; modal.
  • G01 X__ Y__ Z__ F__ — straight-line interpolation at feed rate F; modal.
  • G02/G03 X__ Y__ I__ J__ F__ — clockwise or counterclockwise arc movement; modal. Arc syntax varies by plane and control.
  • G17/G18/G19 — select the XY, XZ, or YZ arc plane; typically modal.
  • G20/G21 — select inch or metric units; typically modal.
  • G54–G59 — select a stored work coordinate offset; modal.
  • G90/G91 — select absolute or incremental positioning; modal.
  • G04 P__ — dwell for a specified time or control-specific value; non-modal.
  • G40 — cancel cutter compensation; typically modal cancellation.
  • G43 H__ Z__ — apply tool-length compensation using offset H; modal until canceled.

For example, G21 G90 G54 establishes metric units, absolute coordinates, and work offset 54. A later G01 X25.0 normally means “feed in a straight line to absolute X25.0,” provided G01 remains active.

What should an M-code list show for spindle, coolant, and programs?

  • M03 S__ — start the spindle clockwise at the programmed speed.
  • M04 S__ — start the spindle counterclockwise.
  • M05 — stop the spindle.
  • M06 T__ — perform a tool change to the specified tool; syntax and sequencing vary.
  • M08 — turn coolant on.
  • M09 — turn coolant off.
  • M00 — mandatory program stop.
  • M01 — optional stop when the control’s optional-stop switch is enabled.
  • M30 — end and usually reset or rewind the program.
  • M98 P__ — call a subprogram; M99 commonly returns from it.

Unlike G-code groups, M codes are often executed as discrete actions. A machine may restrict which M codes can share a block, so follow the controller’s documented sequencing rules.

How do you read a short CNC program line by line?

  • % — program delimiter on controls that use it.
  • O1001 — program number.
  • G21 G17 G90 G54 — select metric units, the XY plane, absolute positioning, and work offset 54. These settings are modal.
  • T01 M06 — select tool 1 and execute the tool change.
  • S2500 M03 — set spindle speed to 2,500 rpm and start clockwise rotation.
  • G00 X0 Y0 Z5 — rapidly move to absolute X0, Y0, Z5, usually a clearance position.
  • G01 Z-2.0 F120 — feed down to absolute Z-2.0 at 120 units per minute.
  • G01 X40.0 — continue the modal linear move to absolute X40.0.
  • G00 Z5 — retract rapidly to absolute Z5.
  • M05 — stop the spindle.
  • M30 — end and reset the program.