Programming G-code might sound intimidating at first, but we’re here to help
with the basics. Programming is a fundamental skill for all types of CNC
machinery, not just CNC mills. While programs used to design parts for CNC
milling, such as Autodesk Fusion 360 and SolidWorks CAM, can create G-
code without you ever touching the keyboard, understanding how to use G-
code will not only let you write your own custom programs but also
troubleshoot when a machine fails.
Every machine uses a slightly different type of G-code, so a Fanuc G-code
won’t work on a Haas controller; however, the basics are the same. So let’s
dive into the details of programming CNC milling G-code!
More Articles On
CNC
G-CODE TUTORIAL FOR CNC PROGRAMMING
Coordinate Systems
The G55 and G54 WCS offsets
(Source: From CadCam To CNC Machine via Facebook)
Additive machines, such as 3D printers, build parts from the ground up, so
they always start from the build plate no matter the part geometry. CNC
mills, on the other hand, can start basically anywhere in the machine build
space, so letting your machine know the location of the stock material is
crucial, otherwise, your machine will just be lost.
CNC machines each have their own coordinate system with a fixed origin.
“Homing” your CNC mill will hit the limit switches on the X-, Y-, and Z-axes,
but this point is usually tedious to work with.
In contrast, the work coordinate system (WCS) defines an origin point
anywhere the programmer wants. This is usually set by the G54 command,
which offsets the machine coordinate system.
ADVERTISEMENT
ADVERTISEMENT
G-CODE TUTORIAL FOR CNC PROGRAMMING
Plane Designations
The planes for the three axes
(Source: Siemens)
In CNC milling machines, you have three planes defined by the main three
axes of the machine: X/Y, X/Z, and Y/Z. Yet, as the number of axes on a
milling machine increases, things start to get more complex –for example,
the additional rotation movements as seen on something like a 6-axis
CNC machine. Defining the plane in which the machine will be working is
crucial for it to know where to mill and how to calculate offsets correctly.
Once the machine knows in which plane to work and the operator has dialed
in the new WCS (via probing, an edge finder, or using dial indicators), the
machining process can start. The action of dialing the WCS into the machine
is commonly known as “zeroing” the machine.
ADVERTISEMENT
G-CODE TUTORIAL FOR CNC PROGRAMMING
Command Structure
Some G-code samples
(Source: Marti Deans via Autodesk)
G-code is a variation of an alphanumeric pattern. A standard G-code
command conforms to the following logic:
N## G## X## Y## Z## F## S## T## M##
N is the line number the program is in and it ensures that the program
follows a logical order.
G indicates a motion using the Cartesian coordinates that follow. There
are many G commands (hence the name G-code) and they vary from
machine to machine, so make sure to check your controller manual.
Nevertheless, there are three basic types of G-codes:
o G00 is used for rapid, non-cutting movements.
o G01is used for linear movements at a programmed feed speed,
usually used to cut material.
o G02 is used for circular movements at a feed speed.
X Y Z commands are used to define X, Y, and Z coordinates. The Z-axis
is the “scariest” to program and the one we recommend being extra
careful with, as crashing a machine spindle is always an expensive fix.
This is where simulating comes in handy. (More on that later.)
Fstands for feed rate, which is basically how fast the machine should
move from point to point. It’s usually expressed in inches/minute or
mm/minute.
S is spindle speed, which determines how fast the spindle should rotate
in rotations per minute (rpm).
T is used for tool selection; most CNC mills now have 10 or more tools
ready to use on the machine.
M are miscellaneous functions to turn things on and off, flood coolant,
air blast, close machine doors, and more. Every machine has different
M commands, so make sure to read your machine’s manual carefully.
I and J are used for arcs. The X and Y coordinates of where the arc
ends are needed as well as the X and Y distances from the arc start
point to the arc center point.
Take a look at the following G-code example:
N2 G01 X10 Y20 Z5 F300 S3000 T1
Breaking it down, we can determine that
the program is in line 2;
the spindle is going to perform a feed move (G01) from (X0, Y0, Z0),
the starting position, to (X10, Y20, Z5);
the selected tool is tool 1 (T1);
the feed speed is 300 mm/min (F300); and the spindle rpm is 3000
(S3000).
Easy, right? A great G-code guide can go a long way when you get lost in
what each G-code segment means.
Something you may be wondering at this point is whether CNC milling G-
code uses absolute or relative positioning. In fact, you can use either, and
this is something we’ll have to tell the machine, but for now, let’s assume
absolute positioning. This will be expanded on in the following section.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
G-CODE TUTORIAL FOR CNC PROGRAMMING
Absolute vs. Relative
Avoid miscommunications with
the machine (Source: Najumsfot Technologies via YouTube)
Let’s assume all coordinates are given in millimeters. When giving
coordinates, for example “X10”, there are two ways the machine could
interpret this instruction. The machine could move to coordinate 10 mm in
the X-axis from the origin, or it could move 10 mm in the X-axis from its
current position. The first option is called “absolute positioning”, meaning
that all coordinates are fixed according to an origin point. The second option
is “relative positioning”, meaning the coordinates are parting from the
current position.
To explain this further, absolute positioning indicates a position to move to,
and relative indicates a distance. For example, if we’re at X2 and we indicate
“X4” in absolute positioning, the final coordinate will be X4. If we’re at X2
and we indicate “X4” in relative positioning, the final coordinate will be X6 –
you’ll have moved 4 mm from the previous position.
You can change between absolute and relative positioning using G-code. G90
sets it to absolute and G91 to relative. This is especially important when
zeroing, meaning finding the zero point or home of your machine, as you
don’t know the origin point at that stage, therefore you move in increments.
More On Absolutes & Relatives
G91 / G90 G-code: Absolute & Incremental Positioning
ADVERTISEMENT
ADVERTISEMENT
G-CODE TUTORIAL FOR CNC PROGRAMMING
Program Logic & Cycles
An example of a drilling cycle
(Source: Renato Calvinisti via All3DP)
To understand G-code, you need to know how a program is formatted. G-
code programs keep to the following structure:
Program header: This starting G-code basically tells the machine how
to prepare itself for the upcoming job. Common aspects include stock
dimensions, offsets, and planes.
Program blocks: Also called the body of the program, this is where
the main part of the job is described, including axis moves, speeds,
feeds, and tool changes. Codes following the logic described in the
previous section go here. (Most errors also appear here.)
End of program: This part tells the machine that the work has ended
so it can retract to a safe position and turn everything off.
The above image presents an example of these program sections, but it also
shows something else. As you can see, the lines have very little variation,
only the Z coordinates are changing, while everything else remains the
same. This is called a cycle.
Basically, a cycle is a group of simple operations that together form complex
movements, like 3D contours, drilling, ramping, and slotting. In this
particular example, the code incorporates a center drilling cycle followed by
a peck drilling cycle.
ADVERTISEMENT
ADVERTISEMENT
G-CODE TUTORIAL FOR CNC PROGRAMMING
Your First CNC Mill G-code
A part drawing done in Inventor
Professional 2017 (Source: Renato Calvinisti via All3DP)
Now that you’ve got the basics, writing a simple program should be easy.
Let’s say we want to contour mill the part in the above drawing with a ½-inch
mill. All measurements are expressed in inches (no conversion needed), so
let’s get to it!
ADVERTISEMENT
ADVERTISEMENT
1
G-CODE TUTORIAL FOR CNC PROGRAMMING
Setting the Work Coordinate
System
The Work Coordinate System
location (Source: Renato Calvinisti via All3DP)
This first step consists of defining where we want the “Zero” or “Origin” point
of the process to be. If you’re doing it in CAM software, how you do so varies
depending on the program. However, we’re doing this example manually to
explain how G-code works. This step isn’t actually written anywhere on the
G-code, but it’s something you need to decide about your design given that
all coordinates part from here.
When deciding where to locate the Work Coordinate System (WCS), take into
account that this point will also have to be located on the physical stock of
material to be machined. It can be wherever you want it, but generally you
should think of a place that’s easy to locate physically and that will simplify
your G-code, such as a corner or the center of a drawing or circumference.
For the example we’re solving, the WCS will be at the exact center of the
part.
ADVERTISEMENT
ADVERTISEMENT
2
G-CODE TUTORIAL FOR CNC PROGRAMMING
Ordering Your Path
The elected milling toolpath
(Source: Renato Calvinisti via All3DP)
As with any other programming language, solving a problem with CNC mill G-
code can be done in many different ways. That said, optimal machining
paths, which make a program as efficient as possible, should always be the
programmer’s goal. Generally speaking, efficiency equates to the fewest
possible lines of code.
The way you order your path will directly affect what the program will look
like. Using the drawing as a guide, we’ll program for the following path,
where each line is an instruction that we’ll later translate to G-code.
Remember we’re using absolute positioning and starting from the center of
the figure, so anything below the center and to the left of the center will be
in absolute coordinates. This is also why you need to indicate both
coordinates, X and Y, even if your translation will only be in one direction.
1.This is where the program will start. As the position (X0, Y0, Z0) is
implied, no G-code will be necessary.
2.Move rapidly and diagonally to the starting point, located at (X-0.75,
Y-1.5).
3.Plunge rapidly to Z-0.5, the desired depth of the contour.
4.Move linearly up at feed speed to (X-0.75, Y0.75).
5.Move at an arc to (X0.75, Y0.75). For this edge, we also need the X
and Y distances from the beginning of the arc to the center of the arc.
For X, this is 0.75 (I0.75), and for Y, this is 0 (J0).
6.Move linearly down at feed speed to (X0.75, Y-1.5).
7.Move linearly left at feed speed to the starting point (X-0.75, Y-1.5).
8.Retract rapidly to a safe point Z0.
9.Move rapidly to (X0, Y0).
ADVERTISEMENT
ADVERTISEMENT
3
G-CODE TUTORIAL FOR CNC PROGRAMMING
Translating to G-code
A simulation of the written G-
code (Source: Renato Calvinisti via All3DP)
Essentially, the body of the program should have a total of eight lines for the
desired contour. Once our path has been chosen, translation from
instructions to G-code language will look as follows:
1. G00 X-0.75 Y-1.5 This line moves the spindle rapidly (G00) from (X0, Y0)
to (X-0.75, Y-1.5).
2. G01 Z-0.5 F3 This line plunges the spindle (G01) from Z0 to Z-0.5 at a
feed speed of 3 in/min (F3).
3. G01 X-0.75 Y0.75 F30 This line moves the spindle (G01) from (X-0.75, Y-
1.5) to (X-0.75, Y0.75) at a feed speed of 30 in/min (F30).
4. G02 X0.75 Y0.75 I0.75 J0 F30 This line moves the spindle in an arc
movement (G02) from (X-0.75, Y0.75) to (X0.75, Y0.75) with a radius
of 0.75 inches (I0.75) at a feed speed of 30 in/min (F30).
5. G01 X0.75 Y-1.5 F30 This line moves the spindle (G01) from (X0.75,
Y0.75) to (X0.75, Y-1.5) at a feed speed of 30 in/min.
6. G01 X-0.75 Y-1.5 F30 This line moves the spindle (G01) from (X0.75, Y-
1.5) to (X-0.75, Y-1.5) at a feed speed of 30 in/min (F30).
7. G00 Z0 This line moves the spindle rapidly (G00) from Z-0.5 to Z0.
8. G00 X0 Y0 This line moves the spindle rapidly from (X-0.75, Y-1.5) to
(X0, Y0).
Now it’s time to simulate! Numerical control (NC) simulation is super
important, as you need to test what you’ve done in order to know that the
movements are correct (and that you won’t run into dangerous or expensive
problems). If you’re not familiar with NC simulators, we recommend you take
a look at NC Viewer, which is a great free online NC simulator.
ADVERTISEMENT
ADVERTISEMENT
4
G-CODE TUTORIAL FOR CNC PROGRAMMING
Setting the Tool Offset
The basic program (left), the
program with the correct offset (right) (Source: Lauren Fuentes & Renato
Calvinisti via All3DP)
In the image above, notice that the mill passes the contour right on the
center, meaning that the final measurements won’t be the actual desired
measurements, but smaller. To solve this we must apply an offset.
In the example, the mill used has a diameter of 1/2 in. We should apply an
offset to take out the half of the tool that’s working inside the contour. To fix
this, an offset of 0.25 inches (half the tool diameter) to the corresponding
sides of all coordinates is necessary. Contours on the left need to be offset
0.25 inches to the left, contours on the right need to be offset 0.25 inches to
the right, and contours up and down need to be offset 0.25 inches up and
down, respectively. Applying this, the resulting program should look as
follows:
1. G00 X-1 Y-1.5 (Rapid diagonal movement)
2. G01 Z-0.5 F3 (Plunge movement at 3 in/min)
3. G01 X-1 Y1 F30 (Linear movement up at 30 in/min)
4. G02 X1 Y0.75 I1 J0 F30 (Arc movement at 30 in/min)
5. G01 X1 Y-1.75 (Linear movement down at 30 in/min)
6. G01 X-1 Y-1.75 (Linear movement left at 30 in/min)
7. G00 Z0 (Rapid retraction)
8. G00 X0 Y0 (Rapid diagonal movement back to the WCS)
ADVERTISEMENT
ADVERTISEMENT
5
G-CODE TUTORIAL FOR CNC PROGRAMMING
Writing Cycles
A simuation of the contour cycle
(Source: Renato Calvinisti via All3DP)
Let’s say that, instead of a contour depth of 0.5 inches, a 0.7-inch contour is
desired, 0.3 inches deeper than the original program. All that’s needed is to
repeat the program and add “-0.3” to the Z coordinate in line 2, resulting
in G01 Z-0.8 F3. Everything else remains the same!
Why not just do this from the start? Because milling at high depths at once
will cause extreme forces that may very likely break your tool. The best
practice is instead, when you need great depths, to go little by little, adding
passes of increased height.
1. G00 X-0.1 Y-1.5 (Rapid diagonal movement)
2. G01 Z-0.5 F3 (Plunge movement at 3 in/min)
3. G01 X-1 Y0.75 F30 (Linear movement up at 30 in/min)
4. G02 X1 Y0.75 I1 J0 F30 (Arc movement at 30 in/min)
5. G01 X1 Y-1.75 (Linear movement down at 30 in/min)
6. G01 X-1 Y-1.75 (Linear movement left at 30 in/min)
7. G00 Z0 (Rapid retraction)
8. G00 X0 Y0 (Rapid diagonal movement back to the WCS)
o (Next cycle, 0.3 inches deeper)
9. G00 X-1 Y-1.5 (Rapid diagonal movement)
10. G01 Z-0.8 F3 (Plunge movement at 3 in/min)
11. G01 X-1 Y0.75 F30 (Linear movement up at 30 in/min)
12. G02 X1 Y0.75 I1 J0 F30 (Arc movement at 30 in/min)
13. G01 X1 Y-1.75 (Linear movement down at 30 in/min)
14. G01 X-1 Y-1.75 (Linear movement left at 30 in/min)
15. G00 Z0 (Rapid retraction)
16. G00 X0 Y0 (Rapid diagonal movement back to the WCS)
ADVERTISEMENT
ADVERTISEMENT
6
G-CODE TUTORIAL FOR CNC PROGRAMMING
Adding the Final Touches
The simulation of the final G-
code (Source: Renato Calvinisti via All3DP)
Now that the body of the G-code is done, the header and end of program G-
code can be added. Some trimming also doesn’t hurt if it makes the G-code
more efficient.
One thing to note here: A line of G-code will stay “on” unless another G-code
turns it off. For example, turning the spindle coolant on is usually done with
“M8”. If this code is used, the coolant will stay on until another line of G-code
shows up turning it off, with “M9”. This means that repeating the same G-
code every single time is unnecessary. For this example, we’re going to be
adding the G-code header and ending for a Mach4 controller as well as doing
some final trimming.
HEADER
1. G90 G94 G91.1 G40 G49 G17 (Machine-specific starting G-code)
2. G20 (Use inches as units)
3. G28 Z0 (Go to predetermined “Home” Z0 position)
4. G90 (Use absolute positioning)
5. T1 M6 (Perform tool change to tool number 1)
6. S1528 M3 (Start spindle clockwise at 1528 rpm)
7. G54 (Use WCS offsets saved in G54)
8. M8 (Turn cooling on)
BODY
(First cycle for 0.5-inch deep contour)
1. G00 X-1 Y-1.5
2. G01 Z-0.5 F3
3. Y0.75 F30
4. G02 X1 Y0.75 I1 J0
5. G01 Y-1.75
6. X-1
(Next cycle for 0.3-inch deeper contour)
1. Y-1.5
2. Z-0.8 F3
3. G01 Y0.75 F30
4. G02 X1 Y0.75 I1 J0
5. G01 Y-1.75
6. X-1
7. Y-1.5
ENDING
1. M9 (Turn cooling off)
2. G28 Z0 (Go to the “Home” position in Z)
3. G28 X0 Y0 (Go to the “Home” position in X and Y)
4. M30 (End of program, rewind and reset modes)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
G-CODE TUTORIAL FOR CNC PROGRAMMING
Wrapping Up
Happy milling! (Source: Renato
Calvinisti via All3DP)
G-code knowledge is useful either to program or to troubleshoot an existing
program. Nowadays, awesome post-processors and CAD/CAM software take
a lot of the load off of programmers, however, understanding the basics
helps us understand why CNC machines work the way they do, in some
cases allowing us to overcome issues or make work processes more efficient.
There are literally thousands of G-codes, and we only scratched the surface
of this language. Experimenting in an NC simulator is a good way to learn
your way around and advance your G-code programming skills.
CNC Programming Examples
As a CNC operator and programmer, you choose CAM systems for NC programming.
Most likely, you may feel afraid or have not been trained properly in following some
specific rules in programming through the control of CNC machines.
These rules concern the correct structure and writing of a CNC program; they provide
information that renders the utilization and intertemporal operation making the process
easy and effective. The modern controllers (MCUs) of CNC machine tools - besides the
possibilities that provisions regarding the programming - also give you the ability to
make the process more effective, in terms of writing of numerical control programs and
their operation, facilitating the flow of working in every company. If you need more
information you can read our article NC G-code programming and management: 6 key
rules.
Below we recommend some basic aspects that you can follow when it comes to the CNC
milling and turning programming for typical prismatic components. In general, there are
many approaches to follow when establishing a process plan and consequently a CNC
program for a given part. However, success is rather achieved when adopting an
organized philosophy when programming.
Based on our extensive experience and industrial know-how, we give you some tips to
follow, in the form of typical CNC programming examples for some discrete operations
that almost any part comprises.
Simple CNC milling programming examples for a
typical prismatic part
Despite the simplicity of the part presented, it comprises most of the basic features
usually found in actual industrial applications, such as contour milling, drilling, tapping,
and circular pocket milling. For this part, we present the recommended G-code for
program start, face milling/profile contouring, and contour finishing.
CNC Milling Example 1: Program start / stopper
programming
Especially when we are to produce small or bigger batches, we need a quick reference
point for our stock of material so that operator needs less time to clamp the part to the
vice or the clamping fixture. In this milling example, the use of a simple X-axis
positioning strategy is applied to orient the part at a fixed X-axis position. The CNC
program starts by setting the work offset (G54) and determining the absolute
coordinate system (G90).
The following table gives the NC program with comments, from block #1 (program
name) to block #9 (retract the stopper at tool change position). In this case, we use a
Ø8 mm tool.
Block CNC program
1 O10001 (CNC-Training Part-3) Program name
2 G90 G54 Work offset G54, Absolute coordinate system
3 T10 M06 (STOP 8mm) Stopper selection to manually clamp the left side of the part at X0 l
4 G00 X-7.5 Y12.5 Stopper motion in X, Y and downward shit towards safe Z height with
5 G00 G43 H10 Z20.
6 G00 Z-8.
7 M00 Optional stop to clamp the part in X0 position that the stopper determin
8 G00 Z20. Upward shift of the stopper in safe Z height.
9 G00 Z100. Retract at tool change position
CNC Milling Example 2: Face milling and profile
contouring
In this milling example, the top face of the part is machined in a single X-Y pass using a
face mill, 50mm in diameter, with five cutting inserts. In addition, a profile contouring
operation is executed in four passes to machine the “25mm x 65mm” profile.
The following table gives the NC program corresponding to the face milling operation.
The code starts from block #10 (Ø50 face-mill selection) to block #54 (retract at tool
change position). In first, a pass is executed to prepare the “20mm x 60mm” contour
with 2mm height on the top surface of the part.
10 T1 M06 (FACEMILL 50) Ø50 face-mill selection.
11 S5000 M03 Spindle clockwise, 5000 rpm
12 G00 X-40. Y12.5 Initial X,Y positioning, while staying in safe Z height (Ζ100)
13 G00 G43 H01 Z20. M08 Rapid downward shift in safe Z height with active tool length compensation. Coo
Positioning at Z0. (Note that Z0 is not necessarily the top surface of the part. It c
14 G00 Z0.
and define Z0 onto a clean surface)
15 G01 X95. F1000. Face milling from Χ-40 to Χ95
16 G00 Z20. Tool retract at Ζ20
17 G00 X-30. Y-30. Rapid shift towards a safe position Χ-30, Υ-30 at the left side of the part.
18 G00 Z-3. Programming of the first depth at Ζ = -3mm for contour milling.
G01 G41 D01 X0. Y-25.
19 The face mill shifts at X-30, Y-30 to allow for activating left cutter radius compe
F1000.
20 G01 Y25. ,C0.5 Machining of the left side of the part with simultaneous chamfer, using “C” para
21 G01 X65. ,C0.5 Machining of the top surface of the part with simultaneous chamfer, using “C” p
22 G01 Y0. ,C0.5 Machining of the right side of the part with simultaneous chamfer, using “C” par
23 G01 X0. ,C0.5 Machining of the bottom side of the part with simultaneous chamfer, using “C” p
24 G01 Y50. Shift towards the left side of the part once again for cancelling tool radius compe
25 G01 G40 X-30. Y55. Tool radius compensation cancellation with linear / diagonal shift in Χ,Υ axes.
26 G00 X-30. Y-30. Return to safe position X-30, Υ-30.
27 G00 Z-6. Programming of the second depth at Ζ = -6mm for contour milling.
G01 G41 D01 X0. Y-25.
28
F1000.
29 G01 Y25. ,C0.5
30 G01 X65. ,C0.5
31 G01 Y0. ,C0.5 Same sequence of commands, as for Ζ-3 depth.
32 G01 X0. ,C0.5
33 G01 Y50.
34 G01 G40 X-30. Y55.
35 G00 X-30. Y-30.
36 G00 Z-9. Programming of the third depth at Ζ = -9mm for contour milling.
G01 G41 D01 X0. Y-25.
37
F1000.
38 G01 Y25. ,C0.5
39 G01 X65. ,C0.5
40 G01 Y0. ,C0.5 Same sequence of commands, as for Ζ-3 and Z-6 depths.
41 G01 X0. ,C0.5
42 G01 Y50.
43 G01 G40 X-30. Y55.
44 G00 X-30. Y-30.
Programming of the fourth and last depth at Ζ = -12.5mm for contour milling. (In
45 G00 Z-12.5
side of the part).
G01 G41 D01 X0. Y-25. Same sequence of commands, as for Ζ-3, Z-6 and Z-9 depths.
46
F1000.
47 G01 Y25. ,C0.5
48 G01 X65. ,C0.5
49 G01 Y0. ,C0.5
50 G01 X0. ,C0.5
51 G01 Y50.
52 G01 G40 X-30. Y55.
53 G00 Z20. M09 Tool retract at Ζ20. Coolant off.
54 G00 Z100. Retract to tool change position.
CNC Milling Example 3: Contour finishing
In this milling example, the part’s “20mm x 60mm” peripheral contour is finished in a
single X-Y pass using a 2-flute, Ø6 end-mill. This tool finishes the 3mm radius, as well.
The code for this operation continues from block #55 (Ø6 end-mill selection) to block
#69 (retract at tool change position).
55 T2 M06 (ENDMILL 6) Ø6 end-mill selection.
56 S5000 M03 Spindle clockwise, 5000 rpm
57 G00 X-6. Y-6. Initial X,Y positioning
58 G00 G43 Z20. H02 M08 Rapid downward shift in safe Z height with active tool length compensatio
59 G00 Z-2. Positioning at depth Ζ-2
60 G01 G41 D02 X2.5 Y-3. F700. Linear shift to facilitate the activation of G41 for finishing the fillet that the
61 G01 Y22.5 ,R3.
62 G01 X59.5
63 G03 X62.5 Y19.5 R3.
64 G01 Y2.5 ,C3. Sequence of commands for contour finishing.
65 G01 X2.5 ,C0.5
66 G01 Y28.
67 G01 G40 X-6. Y31.
68 G00 Z20. M09 Tool retract at Ζ20. Coolant off.
69 G00 Z100. Retract to tool change position.
Simple CNC turning programming examples for a
typical part
Turning parts are very popular and 2-axis CNC turning programming can be easy for
CNC operators and programmers, as the basic operations for completing a part, such as
roughing, finishing, drilling, tapping, and threading are less than in CNC milling
programming.
For this simple part, we present the recommended G-code for Program start/bar feeder
adjustment, roughing operation to remove material from the outer contour, and center
drilling in the part’s face.
CNC turning example 1: Program start/bar feeder
adjustment
In this example, the CNC program starts by setting the bar feeder to a proper position to
orient the stock length needed for machining the part. In addition, the first tool which is
a roughing tool is selected for the first machining operation, so we are ready for the
second operation without changing tool and gain some time.
Tip: Consider your approaching and retracting positioning to be both safe and quick.
The NC blocks are shown and commented from block #1 (program name) to block #7
(program pause to adjust the bar feeder).
Block number NC Program
1 O12346 (TRAINING 102 Lathe) Program name
2 G54 Work offset definition
3 G50 S1800 Constant spindle speed limit to 1800 rpm.
4 T101 Tool change Τ1 (rough machining insert-external t
5 G96 S220 M3 Clockwise spindle rotation with constant cutting speed
6 G00 X30. Z.5 Rapid shift above the part, 0.5mm in front to the part’
7 M00 Program pause to adjust the bar feeder to proper stock
CNC turning example 2: Roughing operation to remove
material from the outer contour
In this example, the part’s contour is programmed with the commands starting with Ν1
(appeared in block #15) up to Ν3 (block #24). The canned cycle responsible for rough
machining in turning, is activated by G71 accompanied by its corresponding
parameters. The NC blocks are shown and commented from block #8 (Tool change T1)
to block #25 (Tool retract).
8 T101 Tool change Τ1 (rough machining insert-external tool holder) with tool offsets regis
9 G00 X62. Z2. Μ08 Rapid shift above the part’s diameter (2mm), and 2mm in front to the face – coolant on.
10 G96 S220 M3 Clockwise spindle rotation with constant cutting speed (Vc) 220m/min.
11 G00 Z0 Rapid shift towards the part’s face
12 G01 X-2. F.2 Vertical cut until Χ-2 owing to the 0.8mm rounded tool tip with feed rate equal to 0.2mm
13 G00 X60. Z2. Rapid shift to the exact part diameter (X60) and 2mm in front of the part’s face. Preparat
G71 P1 Q3 D2. U.4 G71 roughing canned cycle. The part’s contour is programmed with the commands starti
14
W.05 F.2 finishing equal to 0.4mm in diameter (X-axis) and 0.05mm in Z-axis. Feed rate equal to 0
15 N1 G00 X31. Z2. N1 to N3 nested group of commands for contour roughing using the G71 canned cycle.
16 G01 Z0
17 G01 Χ35. Ζ-2.
18 G01 Z-23.
19 G01 X33.5 Z-23.75
20 G01 Z-29.
21 G01 X39.1
22 G01 Z-35.
23 G01 X60. Z-55.
24 N3 G01 Z-90.
G00 X100. Z180.
25 Tool retract at Χ100 and return to safe Z distance away from the part’s face.
M9
CNC turning example 3: Center drilling in the part’s face
In this example, a center drilling is performed in the part’s face for preparing the drilling
operation that follows next. Simple drilling operation is activated by G81 canned cycle
accompanied by its corresponding parameters. The NC blocks are shown and
commented from block #26 (Tool change T7) to block #33 (Tool retract).
26 T707 Tool change Τ7 (center drill) with tool offsets registered to 07.
27 G97 S800 M3 Clockwise spindle rotation with constant spindle speed (n) 800rpm
28 G00 Z20. Preparation for center drilling (centering) – tool positioning 20mm away from t
29 G00 X0 Z2. M8 Rapid shift 2mm away from the face at the center of the part (X0)– coolant on.
30 G81 Z-2. R2. F.08 Simple drilling operation with G81 canned cycle, final depth equal to 2mm, too
31 G80 G81 cycle cancellation.
32 G00 Z2. M9 Tool retract at 2mm away from the face – coolant off.
33 G00 Z130. Tool retract at a safe Z distance to facilitate tool change.