Skip to content

Reference

G-code Reference

Our CNC simulators run real, Fanuc-style G-code — not a simplified toy interpreter. Below are all 60 supported commands, with their parameters and a working example for each. Type them into the MDI line or run them as a full program, and watch the material get removed exactly as your code cuts it.

Motion & Interpolation

Positioning, feed moves, arcs and thread cutting.

G0

Rapid positioning move

Parameters X Y Z
G90 G0 X50 Y25   (rapid to XY)
G0 Z2            (rapid down to clearance)

G1

Linear interpolation move

Parameters X Y Z F
G1 X80 Y25 F150   (feed move at 150 mm/min)

G2

Circular interpolation, clockwise

Parameters X Y Z I J K F X Y Z R F
G17 G2 X40 Y20 I10 J0 F120   (CW arc, centre = current + I,J)
G2 X40 Y20 R10 F120         (same arc using radius R)

G3

Circular interpolation, counterclockwise

Parameters X Y Z I J K F X Y Z R F
G17 G3 X20 Y20 I0 J10 F120   (CCW arc, centre = current + I,J)

G32

Thread cutting (spindle-synchronized feed move)

Parameters X Z F
M3 S800
G32 Z-30 F1.5   (single-point thread pass, F = pitch)

G4

Dwell (P = milliseconds, X = seconds)

Parameters P X
G4 P500   (pause 500 ms)
G4 X2     (pause 2 seconds)

G28

Return to machine zero position

Parameters X Y Z
G28 Z0       (return Z to machine zero)
G28 X0 Y0    (then return X and Y)

Planes, Units & Positioning

Modal state that determines how the following coordinates are read.

G17

Select XY plane for circular interpolation

G17   (XY plane — default for milling arcs)

G18

Select XZ plane for circular interpolation

G18   (XZ plane — default for lathe arcs)

G19

Select YZ plane for circular interpolation

G19   (YZ plane)

G20

Set units to inches

G20   (interpret coordinates as inches)

G21

Set units to millimeters

G21   (interpret coordinates as millimeters)

G90

Absolute positioning mode

G90        (coordinates are absolute positions)
G0 X10 Y10

G91

Incremental positioning mode

G91        (coordinates are distances from current point)
G1 Z-2     (move 2 mm below current Z)

Feed & Spindle Modes

How feed rate and spindle speed are interpreted — including constant surface speed for turning.

G94

Feed rate per minute

Parameters F
G94 F150   (feed expressed in mm per minute)

G95

Feed rate per spindle revolution

Parameters F
G95 F0.2   (feed expressed in mm per spindle revolution)

G96

Constant Surface Speed (CSS)

Parameters S
G96 S180   (hold 180 m/min surface speed; rpm varies with diameter)

G97

Constant spindle speed (RPM mode)

Parameters S
G97 S1200   (hold a fixed 1200 rpm)

G50

Clamp maximum spindle RPM

Parameters S
G96 S180     (constant surface speed)
G50 S3000    (clamp spindle to max 3000 rpm)

Work Offsets & Coordinate Systems

Six work coordinate systems plus programmable offset setting, as on a Fanuc control.

G53

Machine coordinate system (MCS)

G53 G0 Z0   (rapid in machine coordinates, ignoring work offset)

G54

Work coordinate system 1

G54   (select work offset 1)

G55

Work coordinate system 2

G55   (select work offset 2)

G56

Work coordinate system 3

G56   (select work offset 3)

G57

Work coordinate system 4

G57   (select work offset 4)

G58

Work coordinate system 5

G58   (select work offset 5)

G59

Work coordinate system 6

G59   (select work offset 6)

G10

Programmable offset setting

Parameters L P X Y Z
G10 L2 P1 X-300 Y-250 Z-150   (set G54 origin; P1..P6 = G54..G59)
G10 L20 P1 X0 Y0 Z0          (set G54 so current position = 0)

Tool Compensation

Cutter radius and tool length compensation.

G40

Cancel tool radius compensation

G40   (cancel cutter radius compensation)

G41

Tool radius compensation left

G41              (cutter comp left — uses the mounted tool radius)
G1 X20 Y10 F150
G40              (cancel before retract)

G42

Tool radius compensation right

G42              (cutter comp right — uses the mounted tool radius)
G1 X20 Y10 F150
G40              (cancel before retract)

G43

Apply tool length compensation

T1 M6
G43 Z50   (apply tool length compensation)

G49

Cancel tool length compensation

G49   (cancel tool length compensation)

Drilling & Boring Cycles

Canned cycles with retract-mode control, the way a real controller repeats them hole after hole.

G81

Simple drilling cycle: feed in, rapid retract

Parameters X Y Z R F
G98 G81 X20 Y20 Z-12 R2 F100   (drill to Z-12, R = retract plane)
X40                            (repeat at next hole)

G82

Drilling with dwell at bottom, rapid retract

Parameters X Y Z R P F
G98 G82 X20 Y20 Z-12 R2 P300 F100   (drill + dwell 300 ms at bottom)

G83

Peck drilling: incremental steps with chip clearing

Parameters X Y Z R Q F
G98 G83 X20 Y20 Z-25 R2 Q4 F80   (peck in 4 mm steps, clear chips)

G84

Tapping with synchronized spindle reversal

Parameters X Y Z R F
G95
M3 S500
G84 X20 Y20 Z-15 R3 F1.25   (tap; F = pitch, spindle reverses to back out)

G85

Boring: feed in, retract at feed rate for a smooth finish

Parameters X Y Z R F
G85 X20 Y20 Z-15 R2 F60   (bore in and out at feed for clean wall)

G86

Boring with spindle stop at bottom, rapid retract

Parameters X Y Z R F
G86 X20 Y20 Z-15 R2 F60   (bore, stop spindle at bottom, rapid out)

G87

Back boring cycle

Parameters X Y Z R F
G87 X20 Y20 Z-15 R2 F60   (boring cycle: feed in, rapid retract)

G88

Back boring with manual retract

Parameters X Y Z R P F
G88 X20 Y20 Z-15 R2 P300 F60   (bore + dwell, spindle stops, program pauses for retract)

G89

Back boring with dwell, retract at feed rate

Parameters X Y Z R P F
G89 X20 Y20 Z-15 R2 P300 F60   (bore + dwell 300 ms, retract at feed)

G80

Cancel canned drilling or boring cycle

G80   (cancel active drilling/boring cycle)

G98

Retract to initial Z plane

G98 G81 X10 Y10 Z-10 R2 F100   (retract to initial Z between holes)

G99

Retract to R plane level

G99 G81 X10 Y10 Z-10 R2 F100   (retract only to R plane between holes)

Turning Cycles

Multi-pass roughing, finishing and threading cycles used on the CNC lathe.

G70

Finishing cycle: single pass along the profile defined by P/Q

Parameters P Q
G70 P10 Q20   (finish pass over profile N10..N20)

G71

Longitudinal roughing: Z-parallel passes stepping in X

Parameters U R P Q U W F
G71 U2 R1                    (cut depth 2 mm, retract 1 mm)
G71 P10 Q20 U0.5 W0.1 F150   (rough profile N10..N20, leave 0.5/0.1)
N10 G0 X20
G1 Z-30
N20 X40

G72

Facing roughing: X-parallel passes stepping in Z

Parameters W R P Q U W F
G72 W2 R1                    (cut depth 2 mm, retract 1 mm)
G72 P10 Q20 U0.5 W0.1 F150   (face-rough profile N10..N20)
N10 G0 Z-30
G1 X40
N20 Z0

G76

Threading cycle: multi-pass thread cutting

Parameters P Q R X Z P Q R F
G76 P010060 Q0.05 R0.05         (1 finish pass, 60 deg tool, min cut 0.05)
G76 X18.2 Z-30 P0.9 Q0.3 F1.5   (thread: height 0.9, first cut 0.3, pitch 1.5)

Spindle, Tooling & Coolant (M-codes)

M3

Start spindle clockwise

Parameters S
M3 S1200   (spindle ON clockwise at 1200 rpm)

M4

Start spindle counterclockwise

Parameters S
M4 S1200   (spindle ON counterclockwise at 1200 rpm)

M5

Stop spindle rotation

M5   (stop the spindle)

M6

Tool change

Parameters T
T2 M6   (load tool 2 into the spindle)

M8

Coolant on

M8   (flood coolant ON)

M9

Coolant off

M9   (coolant OFF)

M0

Program pause

M0   (pause program — press start to resume)

M2

Program end without rewind

M2   (end of program)

M30

Program end with rewind

M30   (end of program and rewind to the start)

Subprograms

Call a block repeatedly instead of copying it — local or external.

M97

Local subprogram call

Parameters P L
M97 P1000 L3   (run local block N1000 three times)
...
N1000
G91 G1 Z-2 F100   (step 2 mm deeper)
G90
M99

M98

External subprogram call

Parameters P L
M98 P1000 L3   (run program O1000 three times)
...
O1000
G91 G1 Z-2 F100   (step 2 mm deeper)
G90
M99

M99

Subprogram end and return to caller

M99   (end of subprogram — return to the caller)

Macro Programming

Parametric, Fanuc-style macros: assign variables, compute expressions inline and drive the toolpath with loops and conditions. Trigonometry works in degrees.

Variables & expressions

#100 = 10 + 5
G1 X[#100 / 2] Y[(#100 + 2) * 0.5] F100   (X7.5 Y8.5)

Math functions

  • SIN Sine of an angle in degrees, e.g. SIN[30]
  • COS Cosine of an angle in degrees, e.g. COS[60]
  • TAN Tangent of an angle in degrees
  • ASIN Arc sine, returns degrees
  • ACOS Arc cosine, returns degrees
  • ATAN Arc tangent, returns degrees
  • SQRT Square root, e.g. SQRT[9]
  • ABS Absolute value, e.g. ABS[-7]
  • ROUND Round to the nearest integer
  • FIX Truncate toward zero (drop the fraction)
  • FUP Round away from zero
  • LN Natural logarithm
  • EXP Exponential, e^x

Flow control

  • GOTO n

    Jump to the line labelled Nn

  • IF [cond] GOTO n

    Jump to Nn when the condition is true

  • IF [cond] THEN #var = expr

    Assign a variable when the condition is true

  • WHILE [cond] DOn ... ENDn

    Repeat the block while the condition is true (n = 1-3, nestable)

Write this G-code for real

Every command above runs in our simulators with real-time, GPU-accelerated material removal — the same code you would write on the shop floor.