CNC Programming Basics

How a G-code program is put together, what each part means, and how to check one before you run it.

CNC programming is writing the instructions a CNC machine follows to make a part. Those instructions are almost always G-code: a plain-text list of moves, feed rates, spindle speeds and machine commands, read and run one line at a time by the machine's controller. A program that cuts a simple part can be twenty lines long; a 3D surface can run to millions.

Three ways to write a CNC program

  • CAM software, such as Fusion 360, Carbide Create, VCarve or Mastercam. You draw or import the part, choose toolpaths and tools, and a post processor writes the G-code for your controller. This is how most programs are made. See what a toolpath is for how that works.
  • By hand. Simple parts, drilling patterns, facing passes and quick fixes are often typed directly, and every machinist edits G-code by hand at some point.
  • Conversational programming at the machine. Many industrial controls let you fill in forms (a pocket this size, holes on this bolt circle), and the control generates the moves.

Whichever way it's made, it helps to be able to read the result.

The words in a line of G-code

Each line, or block, is made of words: a letter followed by a number. G1 X34 F600 means “feed in a straight line to X34 at 600 mm per minute”. The letters you'll see most often:

Word Meaning Example
GPreparatory code: motion and modesG0, G1, G90
MMiscellaneous code: spindle, coolant, tool change, program endM3, M6, M30
X, Y, ZPosition on each axisX34 Y30 Z-1
FFeed rate for cutting movesF600
SSpindle speed (RPM)S10000
TTool numberT1 M6
I, J, KArc center, as an offset from the arc's start pointG3 X40 Y6 I0 J6
RArc radius, or the retract plane in a drilling cycleG81 … R2
NLine number (optional)N100
( ) or ;Comment, ignored by the machine(3 mm end mill)

Modal and non-modal codes

Most G-codes are modal: they stay in effect until another code from the same group replaces it. After G1, a line with only X6 is another straight cutting move; after G90, every position is absolute until a G91. Feed rate and spindle speed are modal too. That's why programs are much shorter than they would be if every line repeated everything, and also why a single wrong mode, such as G91 left on by mistake, can change every move after it.

A few codes are non-modal and only act on their own line, such as G4 (dwell) and G28 (return to home).

The structure of a program

Almost every milling program follows the same outline:

  1. Safe start. A line that puts the machine into known modes, whatever the last program left behind: units (G20 or G21), plane (G17), absolute positioning (G90), cutter compensation off (G40) and canned cycles cancelled (G80).
  2. Work offset. G54 (or G55–G59) selects where the part zero is.
  3. Tool change. T1 M6 loads the tool. Machines with tool length offsets then apply one with G43 H1.
  4. Spindle and coolant. S10000 M3 starts the spindle clockwise; M8 turns on flood coolant if the machine has it. See the M-code list.
  5. Approach. Rapid to a safe height, then over to the start point, then down to just above the part.
  6. Cutting moves. G1 lines and G2/G3 arcs at the programmed feed rate, or canned cycles for holes.
  7. Retract. Rapid back up to a safe height. Repeat steps 3 to 7 for each tool.
  8. End. Spindle and coolant off (M5, M9), often a return to home with G28, and M30 to end the program.

An annotated example

This program cuts a 40 × 30 mm rectangle with rounded corners, 1 mm deep, with a 3 mm end mill. The part zero is the lower-left corner on top of the stock. The comments in brackets show which step each line belongs to.

(CNC programming example)
(Part: 40 x 30 mm rectangle with 6 mm corner radii, cut 1 mm deep)
(Origin at the lower-left corner of the part, top of the stock is Z0)
N10 G21 G17 G40 G80 G90 (safe start: mm, XY plane, no comp, no cycle, absolute)
N20 G54 (work offset)
N30 T1 M6 (3 mm flat end mill)
N40 S10000 M3 (spindle on, clockwise)
N50 G0 Z10 (rapid to safe height)
N60 X6 Y0 (rapid to the start point, G0 is still active)
N70 Z2
N80 G1 Z-1 F200 (feed down into the part)
N90 X34 F600 (cutting moves)
N100 G3 X40 Y6 I0 J6
N110 G1 Y24
N120 G3 X34 Y30 I-6 J0
N130 G1 X6
N140 G3 X0 Y24 I0 J-6
N150 G1 Y6
N160 G3 X6 Y0 I6 J0
N170 G0 Z10 (retract)
N180 M5 (spindle off)
N190 M30 (end of program)
Open this example in CutViewer

Step through it in CutViewer to see which line makes each move.

  • N10 is the safe start: millimeters, XY plane, no cutter compensation, no canned cycle, absolute positions.
  • N50 to N70 are the approach. G0 stays active, so X6 Y0 and Z2 are rapids too.
  • N80 feeds down 1 mm at 200 mm/min. From N90 on, the feed rate is 600 mm/min and stays that way, because F is modal.
  • N100 is a counter-clockwise arc: it ends at X40 Y6, and I0 J6 puts the arc's center 6 mm above its start point.
  • N170 to N190 retract, stop the spindle and end the program.

The program follows the edge of the part exactly, so the 3 mm cutter cuts a groove centered on the outline. A program that cuts a part free would offset the path by the tool radius, as in the toolpath example.

Check the program before you run it

Mistakes in a CNC program cost broken tools, scrapped parts and sometimes a damaged machine, so check every new program before you cut:

  • Look at the toolpath. Open the file in a viewer and look for rapid moves through the material, cuts in the wrong place, or a part that's the wrong size.
  • Check the extents. The lowest Z should be your intended depth, and the X and Y range should fit your stock and your machine.
  • Check the units and modes. A G20 program on a machine expecting millimeters, or a stray G91, changes every move.
  • Run it in the air. Many machinists run a new program with the Z zero set above the part first, or step through it in single-block mode.

CutViewer does the first three in your browser: it draws the program in 3D, lets you step through it line by line, and its Job panel shows the part size, Z depth, estimated run time and tools used, with pre-flight checks for problems such as a missing feed rate, cutting with the spindle off, missing units or a rapid below Z0.

Where to go next

Frequently Asked Questions

What is CNC programming?

CNC programming is writing the instructions a CNC machine follows to make a part. The instructions are usually G-code, a plain-text list of moves, speeds and machine commands, created with CAM software, typed by hand, or entered at the machine's control.

What language do CNC machines use?

Most use G-code, standardized as RS-274 and ISO 6983. Every controller adds its own variations, so the same program may need small changes to run on a different machine. A few machines use their own languages, such as Heidenhain's conversational programming or ShopBot's OpenSBP.

Do I need to learn G-code if I use CAM software?

You don't need to write it, but being able to read it helps. When something goes wrong, the G-code is what the machine actually ran, and knowing the main codes lets you spot a wrong unit, a missing spindle start or a bad retract height.

What is the difference between G-codes and M-codes?

G-codes set motion and modes, such as G1 for a straight cutting move or G90 for absolute positioning. M-codes control machine functions, such as M3 to start the spindle, M6 to change tools or M30 to end the program.

What does modal mean in G-code?

A modal code stays in effect until another code from the same group replaces it. G1 is modal, so a line with only X and Y after a G1 is another straight cutting move. Feed rate F and spindle speed S work the same way.

How do I test a CNC program without the machine?

Open it in a G-code viewer such as CutViewer. It draws the toolpath in 3D, lets you step through it line by line, and flags problems such as a missing feed rate, cutting with the spindle off or a rapid below Z0.