D Code in CNC: What It Does and How to Use It (Diameter Offset)

The D code tells your CNC machine how wide the cutting tool is. That measurement gets stored in the controller and used when cutter compensation is active. Without it, the machine has no idea where the edge of the tool actually is.

illustration of a cnc cutting tool that shows what an D offset is

Key Takeaways

  • The D code selects which diameter offset register the machine uses during cutter compensation
  • D offsets are stored in the tool offset table — you type the tool’s diameter (or radius, depending on your control) directly into that register
  • Cutter compensation must be turned on with G41 or G42 for the D offset to have any effect
  • The D number usually matches the tool number — D1 for Tool 1, D2 for Tool 2, and so on
  • Only one D offset can be active at a time
D Code – At A Glance
FunctionSelects the diameter offset register for cutter compensation
Used WithG41 (cutter comp left), G42 (cutter comp right)
Value StoredTool diameter or radius (depends on control)
Cancelled byG40 (cutter compensation cancel)

What does the D code do?

The D code picks which offset register your machine reads when cutter compensation is active. Think of offset registers like numbered slots in a spreadsheet — each one holds a value for a specific tool. D1 points to slot 1, D2 to slot 2, and so on.

When you turn on cutter compensation with G41 or G42, the machine reads the value stored in that D register and shifts the tool path by that amount. Without a D value, the machine doesn’t know how far to shift.

What is cutter compensation?

Cutter compensation is a mode that lets the CNC adjust the tool’s path based on the tool’s size. Instead of programming around the exact centerline of the cutter, you can program the part profile and let the machine do the math.

an animation showing how the cnc machine will move with cutter compensation left on

When compensation is active, the machine shifts the tool path by half the diameter — which is the radius. G41 shifts the tool to the left of the programmed path. G42 shifts it to the right. The direction is based on looking in the direction of travel.

an animation showing how the cnc machine will move with cutter compensation right on

The practical benefit: you can program one set of coordinates and change how the part comes out just by adjusting the D offset value. That flexibility is what makes cutter compensation useful in a real shop.

How to use the D code in a program

The D code appears on the same line as G41 or G42. Here’s what a basic cutter compensation block looks like:

G90 G54 G00 X0 Y0          (Rapid to start position)
G41 D01 G01 X1.0 F15.0     (Turn on cutter comp left, use D1 offset)
Y3.0                        (Move along profile)
X4.0                        (Continue)
Y0                          (Continue)
G40 G00 X0 Y0              (Cancel cutter compensation, return to start)

In this example, D01 tells the machine to read offset register 1. Whatever diameter value is stored there, the machine uses it to shift the tool path to the left of the programmed profile.

COMMON MISTAKE – Leaving cutter compensation active

Leaving cutter compensation active (G41/G42) at the end of a program or before a tool change.

Why it matters: Most controls require G40 to cancel cutter compensation before a tool change or program end. Skipping G40 often triggers an alarm, and on some machines it can cause unexpected moves.

an animation showing how a cnc machine will move when cutter compensation is off

Where are D offset values stored?

D offset values live in the tool offset table — a table built into the machine’s control. You can usually find it by navigating to the offset or tool page on the control panel.

Offset Library
Tool offset table with D offsets listed on the right side

The table lists every offset register available on the machine. You type the tool’s diameter directly into the matching register. For a 0.5″ end mill assigned to Tool 1, you’d enter 0.5 into the D1 register.

Some controls store the radius instead of the diameter — meaning you’d enter 0.25 for that same tool. Check your machine’s manual if you’re not sure. Entering the wrong value (diameter when it expects radius, or vice versa) is one of the most common setup mistakes.

COMMON MISTAKE – Entering the radius instead of the diameter (or vice versa) in the D offset register

Why it matters: The machine will cut at the wrong size. A 0.500″ end mill entered as 0.500 radius will shift the path 0.500″ instead of 0.250″ — doubling the offset. This usually means the part comes out undersized or the cutter crashes.

D offset numbering

The D number doesn’t have to match the tool number, but it’s strongly recommended. Keeping them the same (D1 for T01, D5 for T05) makes programs easier to read and troubleshoot.

Here’s what a tool call with a D offset looks like in context:

T01 M06               (Call Tool 1, execute tool change)
G43 H01 Z1.0 F-      (Apply tool length offset H1)
G41 D01 G01 X0.5 F12.0  (Apply cutter comp left with D1 offset)

You can have multiple D offsets stored in the machine at once — one for each tool. But only one can be active at a time. Calling a new D number while compensation is active cancels the previous one.

mach 3 tool offset table
Tool offset table

D offset vs. H offset

D and H offsets both live in the tool offset table, but they control different things.

D offset — stores the diameter (or radius) of the tool. Used with G41 and G42 to control side-to-side position of the cutter.

H offset — stores the length of the tool. Used with G43 to control how far down the Z-axis the tool reaches.

illustration of a cnc cutting tool that shows what an D offset is
illustration of a cnc cutting tool that shows what an H offset is

Most programs use both. A full tool compensation setup in the header of a program typically looks like this:

T02 M06               (Tool change to Tool 2)
G43 H02 Z1.0          (Tool length comp using H2)
G41 D02 G01 X0 F10.0  (Cutter comp left using D2)

H02 handles the Z-axis. D02 handles the XY plane. Together they give the machine the full picture of the tool’s size.

For more on H offsets and tool length compensation, see the G43 page.

FAQs

What does D mean in CNC code?

What does D mean in CNC code?

Do you need a D offset if you’re not using cutter compensation?

No. The D offset only does something when G41 or G42 is active. If you’re not using cutter compensation, the D code has no effect on the tool path.

Can you have more than one D offset active at a time?

No. Only one D offset can be active at a time. You can store as many as your machine supports, but only the currently called D number is in use.

What happens if I forget to program a D offset with G41?

Most controls will default to D0 if no D offset is specified, which means a stored value of zero — no offset shift. Your tool will cut right on the programmed line. On some controls, omitting the D code can also trigger an alarm or undefined behavior, depending on the machine.

Leave a Comment