# G-Code Programming for CNC Milling and VMC

Learn how G-code drives a CNC mill and VMC: setup blocks, tool length offsets, canned cycles, coordinates, and safe first programs for beginners.

[G-code programming](/what-is-g-code-in-cnc-programming) for CNC milling and VMC work is the craft of writing the line-by-line instructions that tell the machine where to move, how fast to cut, and when to change tools. The program combines G-codes for motion and machine functions with M-codes for spindle and coolant control, plus axis coordinates and feed rates. A machinist builds that program around the part, the stock, the fixture, and the tool list. 

- A milling program is a sequence of blocks, and each block holds a motion or machine command with its coordinates.
- G43 applies the tool length offset so the spindle knows where the tool tip sits in Z.
- Canned cycles such as G81 shorten repetitive drilling and boring work into one call plus coordinates.
- Work offsets place the part in the machine, while the program works from part coordinates.
- Verify a new program above the part with the feed and rapid overrides turned down before cutting stock.
 

## What a milling G-code program contains 

A milling G-code program is a text file of numbered blocks that the control reads in order. Each block can carry a G-code for motion or machine mode, an M-code for spindle and coolant, axis words such as X, Y and Z, and values for feed rate or spindle speed. The control executes the block and moves to the next one. 

The program starts with a safe header that cancels old modes and sets the work plane and units. G17 selects the XY plane, G21 sets millimeters, and G90 selects absolute positioning. A tool call with M06 and a spindle start with M03 follow, along with a spindle speed and a coolant command. 

The body of the program moves the tool along the part contour, drills holes, and clears pockets. The end of the program stops the spindle, retracts the tool, and returns the machine to a safe position. Comments in parentheses or after a semicolon explain each step for the next person who reads the file. 

## Words and blocks in G-code 

G-code is built from words, and each word is a letter address followed by a number. G words select the mode, X, Y and Z give positions, F sets the feed rate, and S sets the spindle speed. T selects a tool pocket and M handles machine functions such as spindle direction and coolant. 

Modal commands stay active until another command changes them. Once G01 linear feed is active, later blocks only need new coordinates to keep feeding. Non-modal commands such as G04 dwell act once and then stop affecting the machine. 

A block can hold several words, and the control reads them together as one instruction. Keep each block short and readable, and put one clear action in it where you can. Consistent block structure makes a program easier to check at the machine. 

## Setting the work offset and tool length 

Work offsets tell the control where the part sits on the table. G54 through G59 store the distance from machine home to the part datum, so the program can use part coordinates. The operator touches off the part edges and the top surface to fill in that offset. 

G43 tool length offset is the command that tells the control how long the current tool is. Each tool has a length stored in the offset table, and G43 H with the tool number applies it. Without a correct G43 call, the tool may plunge to the wrong depth or crash into the part. 

Tool length is measured with a presetter or on the machine with a touch-off. The value belongs in the correct offset register, and the program must call the matching H number. A mismatch between the tool in the spindle and the H number is a setup error that no program can correct. 

## Canned cycles for holes and pockets 

Canned cycles are built-in routines that repeat a motion pattern at several positions. G81 drilling cycle feeds to depth, retracts, and waits for the next XY position. The machinist writes the cycle call once, then lists hole coordinates, and the control repeats the drill at each point. 

Other canned cycles cover peck drilling, boring, and tapping. G83 peck drilling breaks a deep hole into steps so chips clear and the drill does not bind. Each cycle has parameters for depth, retract height, and the first peck, and those values depend on the material and the tool. 

Cancel a canned cycle with G80 before any other motion that is not a hole. A cycle left active can turn a simple move into an unexpected plunge at the next coordinate. Group all hole work together and cancel the cycle before contour milling starts. 

## Planning a first program for beginners 

Beginner milling programming works from a drawing, a stock size, and a tool list. The programmer decides the order of operations, the workholding, and which features need which tool. A written plan before any code keeps the program organized and reduces rework. 

Start with the largest tool for roughing and work toward smaller tools for detail. Set the zero point at a corner or the center of the part and keep it consistent through the program. Write the header, the tool changes, and the safe retract lines first, then fill in the cutting moves. 

VMC programming adds the tool changer to the plan, because each tool change costs time and needs clearance. Keep the Z axis at a safe height before every tool change and confirm the pocket numbers. A beginner should simulate the program and run it above the part before the first cut. 

## Checking a program before the first cut 

Program verification starts on the control or in a simulation, where the toolpath is checked for gouges and collisions. Look for moves that go below the part zero without a tool call and for rapid moves through material. The simulation shows the cutting path, but it does not prove the setup is correct. 

At the machine, run the program with the feed and rapid overrides reduced and the tool clear of the stock. Watch the distance-to-go display and the tool position at each step. Stop the program if the motion does not match the drawing or the planned path. 

Check the active work offset, the active tool number, and the H value before the tool touches the part. Confirm that the coolant is directed at the cut and that the part is clamped. A trained machinist should approve the setup before anyone runs a new program in production. 

## Where beginners make costly mistakes 

A missing or wrong G43 call is a frequent cause of a bad first cut. The tool goes to the programmed Z depth measured from the wrong reference, and the mistake shows up as a plunge or a cut in air. Always check the H number against the tool in the spindle. 

Another error is a wrong work offset, where the part is cut in the wrong place on the table. The program may be correct, but the operator touched off the wrong corner or skipped an axis. Recheck each offset value before the cycle starts. 

Leaving a canned cycle active or forgetting to cancel cutter compensation can spoil a contour. Read the program in order and trace each modal change. Keep a printed setup sheet with the tool list, offsets, and zero points next to the machine. 

## Questions about CNC G-code milling 

### Can a beginner learn CNC milling machine programming using G-code? 

Yes, a beginner can learn CNC milling machine programming using G-code by starting with simple motion, coordinates, and tool calls. Practice on a simulator and a single feature before writing a full part program. A trained machinist should check the first programs before they run on a machine. 

### What does G43 do in a milling program? 

G43 applies the tool length offset for the tool in the spindle. The H word names the offset register that holds that tool length. A correct G43 call keeps the programmed Z depth measured from the tool tip. 

### How is VMC programming different from milling on a manual machine? 

VMC programming writes the full sequence of moves, tool changes, and spindle commands in advance. The program repeats the same path on every part and uses stored offsets. A manual machinist turns handwheels, while the VMC follows the G-code blocks. 

### What is a canned cycle such as G81 used for? 

A canned cycle such as G81 repeats a drilling motion at a series of XY positions. The machinist writes the cycle once with the depth and retract values, then lists the hole coordinates. The cycle is canceled with G80 before other motion begins. 

### Which mistakes should a beginner watch for in a first program? 

A beginner should watch for a wrong or missing G43 call and an incorrect work offset. Both errors send the tool to the wrong place in Z or in XY. Checking the tool number, the H value, and the offset before the first cut prevents most of them.
