CNC G-Code Programming Basics: Write a Milling Profile Program

A CNC (computer numerical control) machine moves its tool along paths described by a program of short commands called G-code. CAM software generates most production programs today, but every machinist and manufacturing engineer still needs to read and edit G-code by hand: to fix a crash, change a feed, or write a simple facing or drilling routine at the machine. This article covers the most-used codes, the calculation of speed and feed, and a complete commented program to mill the outside profile of a plate on a vertical machining centre.

Axes and Work Coordinates

On a vertical milling machine the X axis runs left to right, Y runs front to back, and Z is the spindle axis, with positive Z moving the tool up and away from the work. The operator sets a work zero, usually a corner or centre of the part, and stores its machine position in a work offset such as G54. All program coordinates are then measured from that point.

Programs in this article use Fanuc-style syntax, which is followed closely by many controls. Details differ between controllers, so always check the machine's programming manual.

Essential G and M Codes

CodeMeaningNotes
G00Rapid positioningFastest move; never use for cutting
G01Linear interpolation (cutting move)Uses feed rate F
G02 / G03Circular interpolation clockwise / counter-clockwiseArc given by R or by I, J centre offsets
G17XY plane selectionDefault for milling arcs
G20 / G21Inch / millimetre unitsSet once at the start
G28Return to machine reference pointUsually with G91 Z0 to lift Z first
G40 / G41 / G42Cutter compensation cancel / left / rightOffsets tool path by tool radius
G43 H_Tool length compensationH number holds the tool length
G54Work coordinate system 1G55 to G59 for more fixtures
G90 / G91Absolute / incremental positioningMost programs use G90
M03 / M05Spindle on clockwise / stopSpeed set by S word
M06Tool changeWith T number
M08 / M09Coolant on / off
M30Program end and rewind

Calculating Spindle Speed and Feed

Spindle speed   S = 1000 * Vc / (pi * D)      (rpm)
Feed rate       F = fz * z * S                (mm/min)

Vc = cutting speed (m/min), D = cutter diameter (mm)
fz = feed per tooth (mm), z = number of flutes

For a 6 mm two-flute carbide end mill in aluminium, using a conservative cutting speed of 60 m/min and a feed per tooth of 0.05 mm:

S = 1000 * 60 / (pi * 6) = 3183 rpm  -> use 3000 rpm
F = 0.05 * 2 * 3000      = 300 mm/min

Carbide tools in aluminium can often run much faster than this on a rigid machine; the conservative figure suits small or older machines and beginners. Plunge feed is kept lower, here 100 mm/min, because the end of the cutter cuts less efficiently than its sides.

Complete Program: Rectangular Profile with Corner Radius

The part is a 50 mm by 30 mm rectangle with a 5 mm radius on the top-right corner, cut 3 mm deep around the outside of a larger plate. Work zero is the bottom-left corner of the finished profile on the top face. The tool travels clockwise around the part with G41, which keeps it on the outside and gives climb milling.

Fanuc-style milling program for a 50 x 30 mm profile, 3 mm deep, using cutter radius compensation
%
O1001 (RECTANGLE PROFILE 50 X 30 WITH R5 CORNER)
(T1 = 6 MM 2-FLUTE CARBIDE END MILL, ALUMINIUM)
(WORK ZERO G54 = BOTTOM-LEFT CORNER, TOP FACE Z0)

G21 G17 G40 G49 G80 G90   (MM, XY PLANE, CANCEL COMP AND CYCLES, ABSOLUTE)
T1 M06                    (LOAD TOOL 1)
G54                       (SELECT WORK OFFSET)
S3000 M03                 (SPINDLE 3000 RPM CLOCKWISE)
G00 X-10. Y-10.           (RAPID TO START POINT OUTSIDE PART)
G43 Z25. H01              (TOOL LENGTH COMP, SAFE HEIGHT)
M08                       (COOLANT ON)
Z2.                       (RAPID DOWN TO 2 MM ABOVE TOP FACE)
G01 Z-3. F100.            (PLUNGE TO DEPTH OUTSIDE THE PART)
G41 D01 X0. Y-5. F300.    (LEAD-IN WITH CUTTER COMP LEFT)
Y30.                      (LEFT EDGE, MOVING UP)
X45.                      (TOP EDGE TO START OF ARC)
G02 X50. Y25. R5.         (CLOCKWISE R5 CORNER)
G01 Y0.                   (RIGHT EDGE, MOVING DOWN)
X-5.                      (BOTTOM EDGE, PAST START CORNER)
G40 G01 X-10. Y-10.       (LEAD-OUT, CANCEL COMP)
G00 Z25.                  (RETRACT)
M09                       (COOLANT OFF)
M05                       (SPINDLE STOP)
G91 G28 Z0.               (RETURN Z TO HOME)
G90                       (BACK TO ABSOLUTE)
M30                       (END AND REWIND)
%

How the Program Works

  • The safety line G21 G17 G40 G49 G80 G90 puts the control in a known state, whatever the previous program left active.
  • G43 with H01 applies the measured length of tool 1 so that Z0 means the top face of the part, not the spindle nose.
  • The plunge happens at X-10 Y-10, well clear of the part, so the cutter never plunges into finished material.
  • G41 D01 is activated on a linear lead-in move. The control offsets the path by the radius stored in D01 (3 mm for a 6 mm tool), so the program can use part dimensions directly.
  • G02 X50. Y25. R5. cuts the corner arc from (45, 30) to (50, 25) with its centre at (45, 25), moving clockwise.
  • The bottom edge continues to X-5 so the tool fully clears the starting corner before compensation is cancelled on the lead-out.

Common Mistakes

  • Missing decimal points on controls where X10 means 10 microns instead of 10 mm; writing X10. avoids this.
  • Activating G41 or G42 on an arc or with no move; most controls raise an alarm.
  • Forgetting G43, so the tool rapids to the wrong height.
  • Using G00 near the part after changing a coordinate; rapid moves are not straight lines on some machines.
  • Leaving the control in G91 after G28, so later absolute coordinates are read as incremental.

Summary

G-code is a sequence of motion (G00, G01, G02, G03), setup (G17, G21, G54, G90) and machine function (M03, M06, M08, M30) commands. Spindle speed comes from cutting speed and tool diameter; feed comes from feed per tooth, flutes and speed. With a safety line, length and radius compensation, and careful lead-in and lead-out moves, you can write and verify simple profiles directly at the machine.