Knowledge / Reference

G-Codes & M-Codes

Common CNC preparatory (G) and miscellaneous (M) codes for machining centers and lathes. The codes follow the widely used Fanuc-style convention; your machine's programming manual is the final word.

CNC Programming Reference

96 codes

G-Codes: Machining Centers

Preparatory codes that set the type of move, plane, units, offsets and canned cycles. Most are modal: they stay active until another code from the same group replaces them.

Milling G-codes and their functions
G-CodeFunction
G00Rapid positioning (non-cutting move at maximum traverse rate)
G01Linear interpolation (straight-line move at the programmed feed rate)
G02Circular interpolation, clockwise
G03Circular interpolation, counterclockwise
G04Dwell (pause for a programmed time)
G09Exact stop check, current block only
G10Programmable data input (set work and tool offsets from the program)
G15Polar coordinate command cancel
G16Polar coordinate command
G17XY plane selection
G18ZX plane selection
G19YZ plane selection
G20Inch input
G21Metric (millimeter) input
G28Return to machine reference point (home), through an intermediate point
G29Return from reference point
G30Return to 2nd, 3rd or 4th reference point
G31Skip function (used for probing)
G40Cutter radius compensation cancel
G41Cutter radius compensation, left of the path
G42Cutter radius compensation, right of the path
G43Tool length compensation, positive direction
G44Tool length compensation, negative direction
G49Tool length compensation cancel
G50Scaling cancel
G51Scaling
G50.1Programmable mirror image cancel
G51.1Programmable mirror image
G52Local coordinate system setting
G53Machine coordinate system selection (current block only)
G54Work coordinate system 1
G55Work coordinate system 2
G56Work coordinate system 3
G57Work coordinate system 4
G58Work coordinate system 5
G59Work coordinate system 6
G54.1Additional work coordinate systems (selected with P1, P2, ...)
G61Exact stop mode
G64Cutting mode (continuous path, no exact stop between blocks)
G65Macro call, current block only
G66Modal macro call
G67Modal macro call cancel
G68Coordinate system rotation
G69Coordinate system rotation cancel
G73High-speed peck drilling cycle (chip breaking)
G74Left-hand (counter) tapping cycle
G76Fine boring cycle (spindle orients and shifts off the wall before retract)
G80Canned cycle cancel
G81Drilling cycle (spot drilling)
G82Drilling cycle with dwell at the bottom (counterboring, spot facing)
G83Peck drilling cycle (full retract, for deep holes)
G84Right-hand tapping cycle
G85Boring cycle (feed in, feed out)
G86Boring cycle (spindle stops at the bottom, rapid out)
G87Back boring cycle
G88Boring cycle (dwell and spindle stop at the bottom, manual retract)
G89Boring cycle with dwell at the bottom, feed out
G90Absolute programming
G91Incremental programming
G92Coordinate system setting (workpiece shift)
G94Feed per minute
G95Feed per revolution
G98Canned cycle return to the initial point
G99Canned cycle return to the R point

G-Codes: Lathes

Fanuc lathes (G-code system A) share most of the milling codes for moves, units and work offsets, but use the codes below differently. Lathes program X as a diameter.

Lathe G-codes that differ from the milling list
G-CodeFunction
G32Thread cutting (single pass, constant lead)
G50Maximum spindle speed clamp; also coordinate system setting on older controls
G70Finishing cycle (runs the profile defined for G71, G72 or G73)
G71Stock removal cycle, turning (roughing along Z)
G72Stock removal cycle, facing (roughing along X)
G73Pattern repeating cycle (for castings and forgings)
G74Peck drilling along Z, or face grooving
G75Grooving cycle along X (OD or ID grooving, parting)
G76Multiple-pass threading cycle
G90Outer and inner diameter cutting cycle (single pass)
G92Thread cutting cycle (single pass)
G94End face cutting cycle (single pass)
G96Constant surface speed (spindle speed changes with diameter)
G97Constant spindle speed (constant surface speed cancel)
G98Feed per minute
G99Feed per revolution

M-Codes

Miscellaneous codes that switch machine functions such as the spindle, coolant and tool changer, and control program flow. Above M30, many codes are defined by the machine builder.

M-codes and their functions
M-CodeFunction
M00Program stop (operator presses Cycle Start to continue)
M01Optional stop (stops only when Optional Stop is switched on)
M02End of program
M03Spindle on, clockwise
M04Spindle on, counterclockwise
M05Spindle stop
M06Tool change
M07Mist coolant on
M08Flood coolant on
M09Coolant off
M19Spindle orientation
M29Rigid tapping mode (Fanuc)
M30End of program, rewind and reset
M97Local subprogram call (Haas)
M98Subprogram call
M99Subprogram end and return; at the end of a main program, repeats it

G- and M-codes vary between controls (Fanuc, Haas, Siemens, Heidenhain, Mazak and others) and between machine builders. Check the programming manual for your machine before running a program.