CNC Programming Concepts and Commonly Used G-Codes in
CNC Programming Concepts and Commonly Used G-Codes in
There are two major types of CNC codes, or letter address, in any program. The
major CNC codes are called G-codes and M-codes.
• G-codes are preparatory functions, which involve actual tool moves (for example,
control of the machine). These includes rapid moves, feed moves, radial feed
moves, dwells, and roughing and profiling cycles.
• M-codes are miscellaneous functions, which includes actions necessary for
machining, but not those that are an actual tool movement (for example, auxiliary
functions). These include spindle on and off, tool change, coolant on and off,
program stops, and other similar related functions.
Letter Function
Other letter addresses or variables address
N Block number
are used in the G- and M- codes to G Preparatory function
make words. Most G-codes contain a X X axis coordinate
Y Y axis coordinate
variable, defined by the programmer, Z Z axis coordinate
for each specific function. Each I X axis location of arc center
J Y axis location of arc center
designation used in CNC K Z axis location of arc center
programming is called a letter S Sets the spindle speed
F Assigns a feed rate
address. The letters used for T Specifies tool to be used
programming are as follows: M Miscellaneous function
A-Address
Components of a CNC block/line commandscode
B-Data
C-Word
N001 G00 G28 U0. W0. ; D-Block
C, F C C C C E-End of block
E F-block number
A B A B A B A B A B
Block format
Block format is often more important than
D program format. It is vital that each block of CNC
code be entered into the CPU correctly. Each
block comprises different components, which can
produce tool moves on the machine.
Programming terminologies
Address
An address is a part of a program word that is expressed using letters of the alphabet.
Data
Data are all numbers after an address including the sign and decimal point.
Word
A word is consisted by an address and a data put together to specify a machine code function/s.
Programming terminologies
Block
A block is consisting of 1 or more words to specify a cycle that can be done together at the same time
or within a series of machine movements.
Program number
CNC machine programs are arranged in numerical order by the presence of a program number, it is set
with an alphabet “O” followed by four or less digit numbers.
Sequence number
An alphabet “N” followed by five or less digit numbers is used to express a sequence number. It is
used as position identification of each program line/ block that is being executed in the program.
Three major parts of a CNC Program
N140 M30
d
G- Codes
• G-codes are preparatory functions, which involve actual tool moves. These include rapid
moves, feed moves, radial moves, dwells, and roughing and profiling cycles. Most G-codes
described here are Modal, meaning that they remain active until cancelled by another G-code.
G-codes, also called preparatory codes, are any word in a CNC program that begins with the
letter “G”. Generally, it is a code telling the machine tool what type of action to perform.
Modal G-Codes
• codes that remains active until cancelled by another G-code.
• Is a G-code that remains effective in CNC program until another G-code in the same group is
encountered (programmed). Every G-code has a group associated with it, only one G-code of
associated group can be active at a time in CNC program
Commonly used CNC Lathe G-codes
G- codes Functions
G00 Rapid positioning
G01 Linear interpolation
G02 Circular interpolation CW
G03 Circular interpolation CCW
G04 Dwell
G20 Set units in inch
G21 Set units in metric
G28 Automatic zero return
G29 Return from zero reference position
G40 Tool nose radius compensation cancel
G41 Tool nose radius compensation left
G42 Tool nose radius compensation right
G70 Finishing cycle
G71 Turning cycle
G72 Facing cycle
G74 Peck drilling cycle
G75 Grooving cycle
G76 Threading cycle
G90 Absolute programming
G91 Incremental programming
G98 Linear feed rate per time
G99 Feed rate per revolution
M-code Function M-code Function
Format: N G00 X Z ;
N- Block number
X- final tool destination in X-axis
Z- final tool destination in Z-axis
• The G00 command is used primarily to move the tool to and from a cutting position. It is used most often
before and after G01, G02, and G03 command. It can also be used to position the tool for a tool change.
• G00 or also known as rapid or traverse move. G00 command is usually used for moves when machine
travels from position to position above the material and when NO CUTTING is applied. When G00
command is stated we also need to specify in which direction move will occur.
Start position of the tool upon execution of G00
Format: N G01 X Z F ;
N- Block number
X- final tool destination in X-axis
Z- final tool destination in Z-axis
F- feed rate
• We use G01 with moves when machine is moving tool through material and therefore
applying various types of cutting. Normally speed of G01 move is set with F-word
(distance/ time).
Excess materials
Start position of the tool upon execution of G01
Format: N G02 X Z R ;
N G03 X Z R ;
X- End position of arc on x-axis
Z- End position of arc on z-axis
R- Radius of Arc
• Once either the G02 or G03 mode is established, arcs are defined in G-code by identifying
their 2 endpoints and the center which must be equidistant from each endpoint or an alarm
will occur.
P4 G02
G01
G03
P3 P2
P1
Pause/ dwell for precise
CNC timing (G04)
G 04 Pause/ dwell for precise CNC Timing
Format: G04 P ;
• Minimum dwell is the time required to complete one revolution of the spindle.
For example, calculating dwell for 600 spindle revolution, divide 60 by r/min. ,
60/600 = 0.1 secs
Cnc-programming-tips.blogspot.com/2014/11/g04-dwell-command.html?m=1
Calculating Dwell Sec.:
Example:
N170 G90 G0 Z5.
N171 G82 X12. Y6. R2 Z-4. P300 F100 (dwell command 0.3
seconds)
Unit setting and
reference positioning
commands
G 20 Units set in English
G21 units set in metric
• The role of G20 and G21 G-codes is to tell your controller what
units the numbers in the program are in. if the program starts
with a G20, it will assume the program uses inches for
coordinates. If it starts with a G21 G-code, it will assume
millimeters.
G 28 Return to reference
Format/s: N G28 U0. W0. (tool movement going to machine zero point)
N G28 X0. Z0. (tool movement approaching to work zero point)
3 7
Turning
Boring
6
8
1
4 5
Back facing
G 40 Cancel tool nose radius compensation
G 41 Tool nose radius compensation to left
G 42 Tool nose radius compensation to right
• The G40 command cancels any compensation that was applied to the tool during a
program and acts as a safeguard to cancel any cutter compensation applied previously.
• The G41 command applies tool nose radius compensation left to allow the programmer
with turning and boring tools using actual coordinates, not following for the radius of the
tool. The offset register value will compensate for the difference caused by the small radius
on the tool.28
• The G42 command applies tool nose radius compensation right to allow the programmer to
program with turning and boring tools using actual coordinates, not allowing for the radius
of the tool. The offset register value will compensate for the difference caused by the small
radius on the tool.
Commonly used M-
codes
M-codes
M 00 Program Stop
Format: N M02
N M02 (inserted in the end part of the program)
• The M02 command indicates an end of the main program cycle operation.
Upon encountering the M02 command, the MCU switches off all machine
operations (for example, spindle, coolant, all axes, and any auxiliaries), and the
program is terminated.
• This command is found by itself on the last line of the program.
M 02 Program End
M30 program stop and rewind
Format: N M02
N M30 (inserted in the end part of the program)
• M02- end the program. Pressing cycle start (“R” in the axis GUI) will
restart the program at the beginning of the file.
• M30- exchange pallet shuttles and end the program. Pressing the cycle
start button will start the program at the beginning of the file.
M03 Spindle on clockwise
M04 spindle on counter clockwise
M05 spindle off
Format: N S M03
N S M04
N M05 (inserted mostly in the end part of the program or before tool changing operation)
S- spindle speed/ RPM
• M03 is the M-code used in the CNC program to activate the spindle along the clockwise direction with
given spindle speed specified with the alphabet “S”.
•
• M04 is the M-code used in the CNC program to activate the spindle along the counter clockwise direction
with given spindle speed specified with the alphabet “S”.
M06 tool change
• Format: N T M06;
• M06 is the M-code used in the CNC program to activate the automatic tool
change along which T number called on the program. As soon as the controller
reads the T number with M06 it will search the tool magazine for the T number
specified and rotates and brings the tool into the spindle.
M07 shower coolant or flood coolant
M08 coolant on
M09 coolant off