[ Video ] CNC Programming, G-Code Tutorials

CNC Programming | G-Code Tutorial

1. CNC PROGRAMMING G-Code basics

2. Basic CNC Programming

3. CNC G Code Programming: A CNC Mill Tutorial explaining G Codes


Reference books


CNC programming methods

Based on the degree of automation of programming tasks, one distinguishes two programming methods:

Manual Programming (Manual Programming)

* Best ebooks for CNC programming: 
CNC Programming Basics 
G-Code and M-Code 
CNC programming 
G-Code Reference 
CNC Machine Tutorial 

When programming by hand the programmer bases on the drawing to enter the data according to the commands from the keyboard into the machine's memory. This programming is time-consuming, confusing when the details are complex. Due to the disadvantages above, this method only applies to simple details or to edit existing programs. The means of support are mainly search tables or machine catalogs, personal computers to calculate programming parameters. Manual programming requires that the programmer, in addition to mastering the programming method, must have knowledge of math and technology knowledge.

Machine Programming (Automatic Programming).

When programming (computer assisted), the programmer describes (geometrically) the geometry of the workpiece, the trajectory of the cutting tool and the machine's function in the machine language. understandable.

Machine programming has the advantage that hand calculations do not need to be performed, just access some data but can generate large amounts of data for the necessary calculations while limiting the errors made. submit. When programming by computer, the computer must have the following special computation programs:

  • Pre-treatment program.
  • Processor.
  • Postprocessor program.

A processor is a software program that performs geometric and technological calculations. Cutter Location Data (CLD) data is called the data, which provides a general solution that is independent of any CNC machine tool. The CLD has the cutting tool location data. The CLD contains the most concise commands and the codes that do not match the CNC system.

In the geometric description of details such as points, segments, arcs, etc., the description of the machining process such as drilling, milling, cutting mode, cutting tools, Here create a source program. From this source program, the computer creates an NC program suitable for CNC machines thanks to the post-processing unit.

To use the CLD for a particular CNC system, a special program called a postprocessor is required, which translates the NC program into a CLD so that the CNC can understand. and performs the machining process.


Python Tutorial

CNC Programming Tutorials

CNC Programming Examples - Threading

Fanuc G71 Turning Cycle

G71 turning cycle is used for rough-material removal from a cnc lathe component. G71 turning cycle makes large diameter cutting easy. Cutting can be done in simple straight line or a complex contour can also be machined very easily.

* Best ebooks for CNC programming: 
CNC Programming Basics 
G-Code and M-Code 
CNC programming 
G-Code Reference 
CNC Machine Tutorial 

Through G71 turning cycle parameters cnc machinists can control
  • Depth of cut.
  • Retract height.
  • Finishing allowance in x-axis and z-axis.
  • Cycle cutting-feed, spindle speed.


G71 U... R...
        G71 P... Q... U... W... F... S...


First block
Parameter Description
U Depth of cut.
R Retract height.
Second block
Parameter Description
P Contour start block number.
Q Contour end block number.
U Finishing allowance in x-axis.
W Finishing allowance in z-axis.
F Feedrate during G71 cycle.
S Spindle speed during G71 cycle.

G71 Turning Cycle Overview

  • G71 turning cycle cuts the whole contour repeatedly which is given in P Q blocks.
  • Depth of every cut can be controlled by first-block U value.
  • Second-block U W are the finishing allowances which can be given if you want to make a finish cut with G70 finishing cycle.
  • F is cutting feed and S is spindle speed (given in second-block) which are used during G71 turning cycle.
Note – The F and S given inside P Q block will not be used during G71 turning cycle, they are used with G70 finishing cycle if later called.

G71 Turning Cycle Working

N60 G71 U10 R10
        N70 G71 P80 Q90 U3 W0 F0.25
        N80 G00 X60
        N90 G01 Z-75
When G71 turning cycle is run the whole operation will be done in following sequence,
1 – Tool will move in x-axis U (depth of cut) deep with programmed feed from starting-point.
2 – Tool will travel with feed in z-axis (destination point in z-axis is given in P Q blocks )
3 – Tool rapidly retracts R amount in both x-axis and z-axis (at 45 degrees).
4 – Tool rapidly travel in z-axis to start-point
5 – Tool rapidly moves to last cut depth.
6 – Tool moves with feed in x-axis U deep (first-block U depth of cut).
7 – Tool with feed moves in z-axis (destination point given in P Q blocks).
8 – Tool rapidly retracts in x-axis and z-axis R amount (45 degrees).
9 – Tool rapidly moves to start-point only in z-axis.
This whole sequence of operation keep on going, until the destination point in x-axis is met.
If finishing allowance is given tool will not make the exact diameter and length given in P Q blocks but will leave that much allowance, This finishing allowance can be later machined by calling G70 finishing cycle.

Fanuc G71 Turning Cycle

Fanuc G71 Example

Here is a cnc part-program which shows how G71 turning cycle can be used, this is the program for the drawing given above
N50 G00 X106 Z5 M3 S800
        N60 G71 U10 R10
        N70 G71 P80 Q90 U3 W0 F0.25
        N80 G00 X60
        N90 G01 Z-75
In this program G71 turning cycle will keep repeating the contour given inside P Q blocks shown below
N80 G00 X60
        N90 G01 Z-75
These two cnc program blocks tell us that we want to remove material till X60 deep and in Z-75 in length.
The depth of cut is given in first-block U10 retract amount is also given R10.
Finishing allowance in x-axis is U3 but there is no finishing allowance given in z-axis W0.

G70 Finishing Cycle

If you programmed G71 turning cycle with finishing allowances then that finish allowances can be removed with G70 finishing cycle.
G70 finishing cycle repeats the whole contour the G71 way, but in just one-cut removing the finishing allowances.

Why Use G70 Finishing Cycle

As material can be removed with G71 turning cycle, but if you want a different cutting-feed and spindle speed for the last cut, then it is recommended that you use G70 finishing cycle.
G70 finishing cycle use F and S values which are given inside P Q programmed blocks. (G71 use F S values which are given inside G71 second block.)

Fanuc G70 Example

N50 G00 X106 Z5 M3 S800
        N60 G71 U10 R10
        N70 G71 P80 Q90 U3 W0 F0.25
        N80 G00 X60
        N90 G01 Z-75 F0.15
        N100 G00 X200 Z100
        N110 G92 S1200
        N120 T3 G96 S150 M03
        N130 G00 X106 Z5
        N140 G70 P80 Q90
        N150 G00 X200 Z100
        N160 M30

G70 G71 Example

G71 Rough Turning Cycle Example
        G00 X200 Z10 M3 S800
        G71 U2 R1 F200
        G71 P80 Q120 U0.5 W0.2
        N80 G00 X40 S1200
        G01 Z-30 F100
        X60 W-30
        N120 X100 W-10
        G70 P80 Q120

CNC Programming Examples - Taper Threading

Fanuc G84 Tapping Cycle

* Best ebooks for CNC programming: 
CNC Programming Basics 
G-Code and M-Code 
CNC programming 
G-Code Reference 
CNC Machine Tutorial 

G84 tapping cycle performs tapping operation, tapping is performed by rotating the spindle clockwise, when bottom of the hole has been reached, the spindle is rotated in the reverse direction for retraction. This whole operation creates threads.

G84 X... Y... Z... R... P... F... K...
Parameter Description
X Hole position in x-axis.
Y Hole position in y-axis.
Z Depth, tapping from R-plane to Z-depth.
R Position of the R plane.
P Dwell time.
K Number of cycle repetitions (if required) .
F Feedrate.
Once given in program G84 tapping cycle is repeated at every axis movement until G80 is given in program to end tapping cycle.


Feedrate override is ignored during tapping.
Feed-hold does not stop the machine until the return operation is completed.


N150 M6 T2
        N160 G90 G00 X60 Y28 Z12 S100 M03
        N170 G99 G84 X60 Y28 Z-17 P300 R2 F120
        N180 G98 Y12
        N190 G91 G80 G28 X0 Y0 Z0 M05
        N200 M30


Brief description of how G84 tapping cycle works,

G84 tapping cycle working
0- CW Spindle rotation command (M03) must be given in part-program.
1- Rapid traverse to X, Y position (pre-drill hole position).
2- Rapid traverse to R-plane.
3- Tapping operation is done till Z-depth (with tapping feed given with G84).
4-1- Dwell for time specified with P
4-2- Spindle is rotated CCW
5- Tap is Retracted with the specified feed.
At R-plane spindle is rotated CW.
If G99 is specified then tapping for this hole ends here,
6- if G98 is given in program then tap is moved to Initial-level.

G98 G99 Modes

After completing tapping the return height can be controlled by using G98 or G99.
G98 Tap will return to the Initial level
G99 Tap will return to R-plane.
G98, G99 can be used multiple times during G84 tapping cycle.

Repeat Drilling

G84 tapping cycle can be repeated multiple times if K value is given.
For working example see G81 drilling cycle.

Working Example

G84 tapping cycle example
        N10 T1 M06
        N20 G90 G54 G00 X30 Y25
        N30 S100 M03
        N40 G43 H01 Z5 M08
        N50 G84 Z-20 R2 F1.25
        N60 X80 Y50
        N70 G80 G00 Z100 M09
        N80 M30

CNC Programming Examples - Step Turning CNC Lathe

CNC Mill Subprogram Example Joining Multiple Arcs G02 G03 G41

* Best ebooks for CNC programming: 
CNC Programming Basics 
G-Code and M-Code 
CNC programming 
G-Code Reference 
CNC Machine Tutorial 

CNC milling program to describe how two or more radii can be joint together in a cnc mill program.

CNC Mill Subprogram Example

CNC Part Program

N10 T1 H1 M6 G43 M3
        N20 F150 S250
        N30 G0 X-21 Y50 Z0.5
        N40 G0 Z0
        N50 M98 P040050
        N60 G49
        N70 G0 Z50
        N80 M30


        N10 F160 S400
        N20 G0 Z-2.5 G91
        N30 G1 G90 X5 Y50 G41      (P1)
        N40 G2 X22 Y85.23 I45 J0   (P2)
        N50 G3 X78 Y85.23 R45      (P3)
        N60 G2 X78 Y14.77 R45      (P4) 
        N70 G3 X22 Y14.77 R45      (P5)
        N80 G2 X5 Y50 R45          (P1)
        N90 G0 G40 X-21
        N100 M99

G M S T Codes Explanation

Code Description
G00 Rapid traverse
G01 Linear interpolation
G02 Circular interpolation CW
G03 Circular interpolation CCW
G40 Cutter compensation cancel
G41 Tool nose radius compensation left
G43 Tool length compensation + direction
G49 Tool length compensation cancel
G90 Absolute command
G91 Increment command
M03 Spindle start forward CW
M06 Tool change
M30 End of program (Reset)
M98 Subprogram call
M99 End of subprogram
T Tool
S Speed
F Feed


CNC Pocket Milling Program Example – Peck Milling

CNC milling program example which shows how a cnc program can be made to machine Pockets on a cnc mill.
This program example uses Peck milling to cut material to machine a rectangular and one round pocket.
  • CNC Pocket Milling Program Example
    • Main Program
    • Subprogram
    • Explanation

CNC Pocket Milling Program Example

Main Program

Milling cutter diameter: 10mm
N05 G55
            N10 M6 T2 H3 G43 M3
            N15 S1000 F60
            N20 G0 X9 Y9 Z1
            N25 G1 Z0
            N30 M98 P030035
            N35 G0 Z1 G90
            N40 X42 Y38
            N45 G1 Z-2 F30
            N50 X47 F300
            N55 G3 X47 Y38 I-5 J0
            N60 G0 Z100
            N65 G49
            N70 M30


            N05 G1 Z-2 G91 F30
            N10 X10 F100
            N15 Y36
            N20 X-10
            N25 Y-36
            N30 M99


Although this cnc mill program is self explanatory
M98 P030035
this code mean call Subprogram No. 0035 three times.


Multi Start Threads with Fanuc G76 Threading Cycle

Fanuc cnc controls has no direct threading cycle for cutting multi start threads on cnc. But you can cut multi start threads on a cnc with fanuc control by using Fanuc G76 Threading Cycle.
Related: Fanuc G76 Thread Cycle for Dummies

Multi Start Threads with Fanuc G76 Threading Cycle
There are multiple techniques for cutting multi-start threads on cnc with Fanuc G76 threading cycle. Here is one of them.
This cnc programming example uses cnc subprogram call to cut multi-start threads on Fanuc cnc.

CNC Main program
N50 G00 X48 Z5
                N60 M98 P0034713

CNC Sub Program

N10 G76 P020000 Q100 R0.05
                N20 G76 X42 Z-15 P974 Q200 F4.5
                N30 G00 W1.5
                N40 M99
The above cnc main program calls the 4713 sub-program three times.