Introduction to CNC Code
With Computer Numerical Control (CNC) a machine controller manages the operation of the machine. This includes table motions, spindle speeds, tool changes, and other machining functions. A program written in NC code is how these functions are accomplished. The coded instructions use letters, numbers and symbols (+,-,/…. and so on) to create a program.
A Character
An NC code is the language that is read by the CNC machine’s controller. To insure accuracy there is specific structure to this language, and consists of characters, words, blocks and programs.
Here are the nine character types used in CNC programming
1. letters A thru Z
2. a number or combination of numbers 0 thru 9
3. + plus sign
4. – minus sign
5. . decimal or period
6. : colon
7. ; semi-colon
8. / slash
9. % percent
A Word
Here are some examples of NC words and what they mean, these are used to provide specific commands to a machine.
NC Word Description
F as in F12.0 Feed rate at 12.0 inches per minute
G as in G1 Tool movement in linear interpolation while under feed control
M as in M6 Miscellaneous function in this instant a tool change
N as in N100 Block sequence number
R as in R.25 Arc radius 0.25 radius
S as in S1250 Spindle speed at 1250 RPM
T as in T3 Selection of tool number 3
X as in X2.5 X-axis coordinate, 2.5 inches in the X direction
Y as in Y4.75 Y-axis coordinates 4.75 inches in the Y direction
A Block
A series of words defining a single instruction is called a block, and a block is made up of a word or a combination of words. An end of block character terminates a block, the EOB can be either visible or invisible. Here are some examples of both.
N89 M30 (two word block)
N11 T01 M06 (three word block)
N05 G80 G90 G17 (four word block)
N9 G01 X3.0 Y1.7 F4.0 (Five word block)
With an EOB
N400 G0 X0.2 (three word block with “;” used as an EOB)
N5005 G90 G1 F20 X5 Y2 Z4$ (seven word block with “$” used as an EOB)
A Program
A program is a complete set of coded instructions made up of a series of block needed to completely machine a part.
A program and all of its parts can be equated to the English language as follows:
A character is the same in both CNC Code language and the English language
A word is the combination of characters in both the CNC language and the English language.
A block in CNC Code Language is equivalent to a sentence in the English language, conveying specific thoughts, commands and has punctuation that ends them.
A program in CNC Language is equivalent to complete memo of letter in the English Language. They both are a combination of sentences/blocks that convey complete ideas (English) or completed parts (NC Code).
Address Words
Address codes are a single letter character (A-Z) at the beginning of each word that defines what the computer should do with the numerical data that follows. There are address code for Milling and turning, two address codes are considered major codes they are G-codes and M-codes.
Address codes for Milling and Drilling
A Angular dimension around the X axis
B Angular dimension around the Y axis
C Angular dimension around the Z axis
D Cutter compensation register number
E Angular dimension for special axis
F Feed rate
G Preparatory function (G-codes)
I X axis arc center location
J Y axis arc center location
K Z axis arc center location
L Loop count repeat for canned cycles
N Block number
O Program number or subroutine number
P Dwell time
Q Canned cycle repeat dimension
R Arc radius Or z-axis retract distance
S Spindle speed
T Tool selection number
X primary X motion dimension
Y primary Y motion dimension
Z primary Z motion dimension
Address codes for Turning
A Fourth axis rotary motion
B Linear B axis motion
C Fifth axis rotary motion
D First pass cut depth for threading
E Feed rate
F Feed rate
G Preparatory function (G-codes)
I X axis arc center location
J Canned cycle data
K Z axis arc center location
L Loop count repeat for canned cycles
N Block number
O Program number of subroutine number
P Dwell time
Q Canned cycle repeat dimension
R Arc radius or z-axis retract distance
S Spindle speed
T Tool selection number
U Incremental Z axis motion
V Marco parameter
W Incremental Z axis motion
X primary X motion dimension
Y primary Y motion dimension
Z primary Z motion dimension
G Words
G words or G codes as they are commonly called are the major address codes for preparatory functions involving tool movement and material removal. This includes lineal and circular feed moves, rapid moves, canned cycles and dwell. Here are some of the more commonly used G-codes
G-codes for Milling
Rapid positioning (G00)
Interpolation (G01, G02, G03)
Dwell (G04)
Plane selection (G17, G18, G19)
Automatic Reference Returns (G28, G29)
Cutter compensation (G40, G41, G42)
Tool offset (G43-G49)
Work Coordinate Offset (G52-G59)
Unit Input (G20, G21; or G70, G71)
Fixed canned cycles (G80-G89)
Positioning Input (G90, G91)
Set Work Coordinates (G92)
Return Points (G98, G99)
G-codes for lathes
Rapid positioning (00)
Interpolation (G01, G02, G03)
Dwell (G04)
ZX plane selection (G18)
Unit Input (G20, G21)
Automatic Reference Return (G28, G29)
Tool Nose Radius Compensation (G40, G41, G42)
Work coordinate offset (G52-G59)
Fixed cycles:
Finishing Cycle (G70)
Turning Cycle (G71)
Facing Cycle (G72)
Peck-drilling (G74)
Grooving Cycle (G75)
Threading Cycles (76, G92)
O.D./I.D. turning cycle (G90)
Constant Surface Speed (G96, G97)
Free Rate unit (G98, G99)
M Words
Miscellaneous functions that perform instructions that do not involve tool movement are called M words or more commonly M-codes. Included in this is spindle on and off, tool changes, coolant on and off, along with similar other functions. Here are the more commonly used M-codes
for Milling and Lathes.
M-codes for Milling
Program Stops (M00, M01)
End of Program (M02, M30)
Spindle control (M03, M04, M05, M19)
Tool Change (M06)
Coolant Control (M08, M09)
Clamp Control (M10, M11)
Sub-program Control (M98, M99)
M-codes for Lathes
Program Stops (M00, M01)
End of Program (M02, M03)
Spindle control (M03, M04, M05)
Coolant Control (M08, M09)
Clamp Control (M10, M11)
Turret Rotation (M17, M18)
Tail Stock Control (M21, M22)
Sub-program Control (M98, M99)
