CNC Programming
Parts of a CNC program
• Just like a written letter to a friend or business
contact is comprised of three main parts
• salutation,
• body, and
• closing,
a CNC program is essentially broken into three similar
main parts:
• the salutation “program safe-start,”
• the body “material removal,”
• and the closing “program ending.”
CNC Programming
• There is no standardized format for writing a CNC
program that is compatible with all machine control
models.
• Each MCU manufacturer has developed its own
unique programming format.
• Each one has minor differences, but the principles
contained in the context of a program are the same
among them all.
• The programming examples provided in these slides
will relate the most closely to Fanuc-type controllers;
however, the principles may be applied to any
manufacturer’s programming format (see the
specific machine’s programming manual).
CODES
• Along with telling the machine what position to go to, the
programmer must also provide the machine with instructions
telling it what to do at that position.
CODES
Preparatory codes
G codes
M codes
Miscellaneous codes
CODES
Establish machine Carry out machining
settings operations
G codes
M codes
Control the machine
functions other than
tool movements
CODES
Establish machine Carry out machining
settings operations
G codes
G Codes - Axial Movements
Moves the tool at a very fast rate to a
G00 Rapid traverse
specified point
Linear Machining in a straight line at a feed
G01
interpolation rate
Clockwise Circular Machining in a clockwise circular arc
G02
interpolation at a given feed rate
Counter-clockwise
Machining in a counter-clockwise
G03 Circular
circular arc at a given feed rate
interpolation
Return to machine (Rapid traverse) Moves the tool at a
G28
home position very fast rate to the home position
Coordinate positioning : Radial
We will NOT use Radial
system for this course
+X
+Z
-Z
-X
Coordinate positioning : Diametral
We will use Diametral
system for this course
+X
+Z
-Z
-X
G Codes - Axial Movements
Moves the tool at a very fast rate to a
G00 Rapid traverse
specified point
G00 X30 Z5
Tool
Home Position
+X X30 Z5
40 -Z
+Z
-X
G Codes - Axial Movements
Linear Machining in a straight line at a feed
G01
interpolation rate (one or more axes)
G00 X30 Z5
G01 X30 Z-20 F40 Feed rate
Tool
X30 Z-20
+X X30 Z5
40 -Z
+Z
-X
G Codes - Axial Movements
G00 X30 Z5 (Home 1)
G01 X30 Z00 F40(1 2)
X40 Z-35 (2 3)
3 Home
Position
2 1
+X
Ø40 Ø30 -Z
+Z
-X
35
G Codes - Axial Movements
Clockwise Circular
G02 Machining in a clockwise circular arc
interpolation
G00 X20 Z5
G01 X20 Z-15 F40
3 G02 X40 Z-25 R10
2 1
+X Radius
R10 Clockwise
Ø40 Ø20 -Z +Z
-X
15
25
G Codes - Axial Movements
Counter-clockwise
Machining in a counter-clockwise
G03 Circular
circular arc
interpolation
G00 X20 Z5
G01 X20 Z-15 F40
3
G03 X40 Z-25 R10
2 1
R10 +X Radius
Ø40
Ø40 Ø20
Ø20 -Z +Z Counter-
clockwise
-X
15
25
G Codes – Machine Setup
For Milling ONLY
All coordinates are relative to the
G90 Absolute positioning reference position
(i.e. Workpiece Origin)
All coordinates are relative to the
G91 Incremental positioning current position
For Turning ONLY
No need to write G code for the following in Turning
× Absolute positioning
Machine will run on Absolute Mode by
default
U Incremental mode in X axis
× Incremental positioning W Incremental mode in Z axis
G Codes - Axial Movements
Return to machine (Rapid traverse) Moves the tool at a
G28
home position very fast rate to the home position
G28 U00 W00
Incremental Incremental
mode in mode in
Tool
X axis Z axis
Important point
Home Tool will
+X Position • move 00 in the X direction
and 00 in the Z direction
40 (incremental mode)
+Z
-Z
• and then will return to the
-X Home position
G Codes – Machine Setup
G20 Imperial input Inch programming units
Millimetre programming units
G21 Metric input
G98 Feed per minute inches/millimetres per minute
G99 Feed per revolution inches/millimetres per rev
D X Y
C
A
B
A B
C
0
+Y
D
0
-X +X
-Y
CODES
M codes
Control the machine
functions other than
tool movements
M codes : Group 1
M00 Program Stop utility stop for operator actions
M01 Optional Program Stop operator selected or ignored
needs manual reset to start
M02 Program End
again from the beginning
Program End and
M30 stop and automatic reset
Rewind
M codes : Group 2
M03 Spindle ON (Clockwise)
M04 Spindle ON (Anti-Clockwise)
M05 Spindle OFF
M06 Tool Calling
M08 Coolant ON
M09 Coolant OFF
+X G00 X20 Z5
G01 X20 Z-10 F40
E X40 Z-30
D C X40 Z-45
B A
-Z G02 X50 Z-50 R5
+Z
Ø50 Ø40
Ø20
10
30
45
50
-X
+X
-Z
+Z
-X
+X
Roughing depth of
Cut
-Z
+Z
-X
+X
-Z
+Z
-X
+X
-Z
+Z
-X
+X
-Z
+Z
-X
+X
-Z
+Z
-X
+X
-Z
+Z
-X
+X
-Z
+Z
Material to be left
for finish pass
-X
+X
Material being
removed by
finishing pass
-Z
+Z
-X
+X
Roughing depth of
Cut
-Z
+Z
Material to be left
for finish pass
-X
Canned Cycles
• Many turning operations require tedious and
redundant motions, such as the multiple passes
performed during a rough turning operation or
multiple pecks required for deep hole-drilling
operations.
• Machine control manufacturers have equipped the
controls with features that make these tedious
operations easier and faster to program.
• These machining routines can be packaged or
“canned” into one or two blocks of code and are
called canned cycles.
Stock Removal Turning Cycle : G71
• Function G71 is the stock removal cycle for turning
that removes metal along the direction of the Z axis.
In a case where there is a lot of material to be
removed, this cycle provides an easy method for
programming.
• There are two program blocks required for function
G71.
Block 1 G71 U R
Block 2 G71 P Q U W F
Stock Removal Turning Cycle : G71
depth of
roughing cut amount of retract, along
(no sign) the X axis, for each cut
Block 1 G71 U R
Block 2 G71 P Q U W F
block number where the amount of material for roughing feed rate
code for the contour finishing to be left on
begins all diameters
(X axis)
amount of material for
block number where the finishing to be left on
code for the contour all faces
ends (Z axis)
Stock Removal Turning Cycle : G71
+X
Rough Stock
finish amount to be left diameter
on Z-axis faces W
Depth of Cut U
R12.5
Retract amount R
finish amount to be left
90 50 on the diameter U
-Z +Z
10
25
62.5
75
-X
Stock Removal Turning Cycle : G71
N10 G00 X95 Z5
Block 1 N20 G71 U5 R0.5
Block 2 N30 G71 P40 Q80 U1 W1 F30
N40 G00 X10 Z5
N50 G01 X10 Z-25
Cycle Blocks
N60 X50 Z-62.5
N70 G02 X75 Z-75
N80 G01 X95 Z-75
Finishing Cycle : G70
G70 P Q F S
block number where the
feed rate, effective for
code for the finishing contour
blocks P through Q
begins
spindle speed,
effective for blocks P
through Q
block number where the
code for the finishing contour
ends
Stock Removal Turning Cycle : G71 and Finishing
Cycle G70
N10 G00 X95 Z5
Block 1 N20 G71 U5 R0.5
Block 2 N30 G71 P40 Q80 U1 W1 F30
N40 G00 X10 Z5
Cycle Blocks
N50 G01 X10 Z-25
N60 X50 Z-62.5
N70 G02 X75 Z-75
N80 G01 X95 Z-75
Finishing Cycle G70 P40 Q80 F10 S1500
Examples of Stock Removal Turning Cycle G71
program
• Refer to “CNC Lab Qs” pdf file
Facing Cycle : G94
G21 G98 (Metric unit and feed in mm/min)
G28 U0 W0 (Go to home position)
M06 T0101 (Tool 1 & Offset 1) P
Ø10
M03 S2500 (Clockwise spindle at 2500rpm) X12 Z2
+X
G00 X12 Z2 ( Home
(Go → position)
to home P)
G94 X0 Z-2 F100 (Facing cycle) -Z +Z
Z-4
-X
G28 U0 W0 (Go to home position)
M05 (Spindle stop)
M30 (Program end and rewind)
20
Facing Cycle : G94
G21 G98 (Metric unit and feed in mm/min)
G28 U0 W0 (Go to home position)
M06 T0101 (Tool 1 & Offset 1) P
Ø10
M03 S2500 (Clockwise spindle at 2500rpm) X12 Z2
+X
G00 X12 Z2 ( Home
(Go → position)
to home P)
G94 X0 Z-2 F100 (Facing cycle) -Z +Z
Z-4
-X
G28 U0 W0 (Go to home position)
M05 (Spindle stop)
M30 (Program end and rewind)
18 Z-2
20
Facing Cycle : G94
G21 G98 (Metric unit and feed in mm/min)
G28 U0 W0 (Go to home position)
M06 T0101 (Tool 1 & Offset 1) P
Ø10
M03 S2500 (Clockwise spindle at 2500rpm) X12 Z2
+X
G00 X12 Z2 ( Home
(Go → position)
to home P)
G94 X0 Z-2 F100 (Facing cycle) -Z +Z
Z-4
-X
G28 U0 W0 (Go to home position)
M05 (Spindle stop)
M30 (Program end and rewind)
18 Z-2
20
Facing Cycle : G94
G21 G98 (Metric unit and feed in mm/min)
G28 U0 W0 (Go to home position)
M06 T0101 (Tool 1 & Offset 1) P
M03 S2500 (Clockwise spindle at 2500rpm) X12 Z2
+X
G00 X12 Z2 ( Home
(Go → position)
to home P)
G94 X0 Z-2 F100 (Facing cycle) -Z +Z
Z-4
-X
G28 U0 W0 (Go to home position) 16 Z-4
M05 (Spindle stop)
M30 (Program end and rewind)
18 Z-2
20
Peck Drilling Canned Cycle G74
amount of retract
G74 R
G74 X Z Q F
X-axis hole location Feed rate
(part centerline) peck increment
or
total absolute Z-depth depth of cut in Z axis
Micrometre (µm)
Clearance
G74 R
Z
G74 X Z Q F
Micrometre (µm)
R
Q Micrometre (µm)
Clearance
Clearance
G74 R1
20
G74 X0 Z-20 Q5000 F50
Micrometre (µm)
1
5000 Micrometre (µm)
Clearance
Threading Cycle : G76 angle of Micrometre (µm) in
software
Relief angle (to the thread mm in machine
number of
retract it may be minimum cut
finishing cuts
require some depth for rough
angle) cut
depth of the final
pass
(finishing
Block 1 G76 P000000 Q R allowance)
Block 2 G76 X Z P Q F
minor diameter Height of thread in feed rate (normally
(Major Dia - 2*Thread Height) micron equal
(= 0.613*Pitch) to the thread pitch).
Micrometre (µm) in software
mm in machine
First pass depth of cut
Final end position of the
Micrometre (µm) in software
thread along the Z axis mm in machine
Threading
Threading