G28 G-Code: Machine Zero Return Explained for Beginners

G28 tells your CNC machine to move to a safe intermediate point, then rapid all the way to the machine’s home position. It’s one of the most common safety commands in CNC programming — and one of the easiest to crash a machine with if you use it wrong.

Key Takeaways

  • G28 moves the machine to an intermediate point first, then returns to the machine zero position
  • Always pair it with G91 (incremental mode) to avoid crashing into your workpiece
  • Use it before tool changes, pallet changes, or any time you need the machine out of the way
  • It is NOT a modal command — it only runs once on the line it appears
  • G28 Z only (Z-axis first) is the safest approach in most programs
G25 – At A Glance
FunctionMachine Zero Return (Reference Point Return)
FormatG28 [axis][value]
TypeNon-modal (one-shot)
Typical UseBefore tool changes, end of program, safe retract
Related CodesG53, G28.1, G29, G30

What Does G28 Do?

G28 moves the machine in two steps. First, it rapids to an intermediate point you specify. Then it rapids from that point to the machine’s zero position (also called the home position or reference point).

That intermediate stop is the key. Without it, a direct rapid to machine zero could slam the tool straight through a clamp, a vise jaw, or the part itself.

illustration of a cnc machine that shows how the machine moves when using a g28 code
The tool moves to intermediate point, then to machine zero

Think of it like backing a car out of a tight parking spot. You don’t just floor it in reverse — you pull out at an angle first to clear the car next to you, then straighten out.

When to use a G28 code?

G28 shows up near the end of a tool path section, right before the machine needs to do something else — like a tool change or a pallet change.

Common situations where you’ll use it:

  • Before an M06 tool change
  • At the end of a program (returning the machine to home)
  • Before a pallet change on a horizontal machining center
  • Any time you need to guarantee the machine is clear of the part and fixtures
a cnc mill with multiple fixtures and coolant lines
G28 helps you avoid fixtures like these

G28 and the G91 Pairing — Why It Matters

This is where most beginners get tripped up.

G28 uses whatever positioning mode is currently active — absolute (G90) or incremental (G91). If your program is in absolute mode (which most programs are), the intermediate point values in G28 are interpreted as work coordinates. That means the machine might try to move to X0 Y0 Z0 of your workpiece before going to machine zero — and that’s a crash.

COMMON MISTAKE – Running G28 X0 Y0 Z0 while still in absolute mode (G90)

Why it matters: In absolute mode, X0 Y0 Z0 refers to your work offset zero — which is likely right on top of your part. The machine will try to move there before going to machine zero. This is one of the most common crash scenarios in CNC programming.

The fix is simple: add G91 on the same line as G28.

G91 G28 Z0       (Switch to incremental, move 0 units in Z, then return Z to machine zero)
G90              (Switch back to absolute mode)

With G91 active, the intermediate point values are treated as distances from the current position. A value of Z0 means “move zero units in Z first, then go to machine zero” — so the tool goes straight home without an intermediate detour.

COMMON MISTAKE – Forgetting to restore G90 after using G91 with G28

Why it matters: If absolute mode isn’t restored before the next tool’s program begins, every position in your program will be interpreted as an incremental move. This can cause rapid crashes on the very first move of the next operation.

Always put G90 on the line immediately after your G28 block.

Z First — The Safest Approach

In most programs, you’ll see G28 used on the Z-axis alone before touching X or Y.

G91 G28 Z0       (Retract Z to machine zero first)
G90
G91 G28 X0 Y0    (Then home X and Y — optional, often not needed)
G90

Moving Z first clears the tool out of the part before moving the table. If you home X and Y with the spindle still down in the cut, you’re dragging the tool across the part — or worse.

G28 Code Examples

G28 G91 Z0 — Most Common Usage

G91 G28 Z0       (Incremental mode, Z moves 0 units, then rapids to machine zero)
G90              (Restore absolute mode)

This is the line you’ll see in the vast majority of CNC programs before a tool change. The Z-axis returns to its home position. X and Y are unaffected.

G28 X0 Y0 Z0 — All Axes Home

G91 G28 X0 Y0 Z0    (All three axes move to machine zero simultaneously)
G90

Use this at the end of a program when you want the machine fully homed. Be aware that all three axes move at the same time — so make sure your tool is already clear of the part before running this.

G28 G91 Z35.0 — Clearance Move First

G91 G28 Z35.0    (Move Z up 35mm from current position, then home Z)
G90

When you need guaranteed clearance before homing — for example, if you’re in a deep pocket — specify the clearance distance instead of zero. The tool moves up 35mm first, then rapids to machine zero.

G28 U0 W0 — CNC Lathe Syntax

G28 U0 W0        (Return to machine zero on a CNC lathe)

On many CNC lathes, U and W are the incremental axis designators for X and Z respectively. Using U0 W0 with G28 returns both axes to machine zero with no intermediate move — a common and safe pattern on lathes.

cnc lathe cutting threads on part

G28 vs G53 — What’s the Difference?

G53 is G28’s close cousin. Both can move the machine to a safe position using machine coordinates.

The key difference: G53 lets you specify exactly where you want to go in machine coordinates, on a single line. G28 always goes all the way to machine zero.

G53 is often more flexible — you can use it to position the spindle at a tool changer or a pallet station without sending everything to machine zero. But not every CNC control supports G53. G28 works on virtually every machine.

Pick one and stick with it consistently. Mixing G28 and G53 in the same shop makes programs harder to read and troubleshoot.

G28 vs G28.1

G28.1 lets you define where G28 goes. When you run G28.1 at a specific position, that position becomes the new reference point for future G28 commands — instead of the default machine zero.

Most shops never need G28.1. It’s useful in specific setups where machine zero isn’t a practical home position. If you’re just getting started, ignore it for now and use G28 the standard way.

Is G28 a modal command?

No. G28 is a one-shot (non-modal) command. It executes once on the line where it appears, and that’s it.

Compare that to something like G90 or G91, which stay active until you change them. G28 doesn’t “stay on” — it runs its two-step move and then the machine does whatever comes next.

FAQs

What is G28 used for in CNC programming?

G28 returns the machine to its home (zero) position. It’s most often used before tool changes and at the end of programs to move the machine clear of the workpiece and any fixtures.

Why do you use G91 with G28?

G91 switches the machine to incremental positioning mode. This makes the intermediate point in G28 a relative move from the current position, not an absolute coordinate. Without G91, if your program is in absolute mode, G28 could try to move to work coordinate zero — which might be right on top of your part.

What’s the difference between G28 and G53?

Both codes can move the machine using machine coordinates. G28 always moves to machine zero (in two steps). G53 lets you specify any machine coordinate position on a single line, making it more flexible. G53 is not supported on all controls; G28 is universal.

Does G28 move all axes at once?

Only the axes you specify. G28 Z0 only moves the Z-axis. G28 X0 Y0 Z0 moves all three. When multiple axes are specified, they all move simultaneously in each step.

G04 Dwell Code: What It Does and How to Use It

G04 tells your CNC machine to pause in place for a set amount of time. The tool stops moving, but the spindle and coolant keep running. Once the time is up, the machine reads the next line of code and picks up where it left off.

Key Takeaways

  • G04 is the dwell command — it pauses machine motion for a specified amount of time
  • The spindle and coolant stay on during a dwell; only the axis motion stops
  • Dwell time is set with a letter (P, F, U, or X) that varies by machine and controller
  • G04 resumes automatically — unlike M00 or M01, no button press is required
  • Always check your machine manual: some controllers use seconds, others use milliseconds
G04 – At A Glance
FunctionDwell (timed pause)
FormatG04 P___ or G04 F___ (letter varies by controller)
TypeNon-modal (one-shot — active only for the block it’s programmed on)
Spindle during dwellStays on
Coolant during dwellStays on
ResumesAutomatically after dwell time expires

What Does G04 Do?

G04 is the dwell command. It tells the machine to sit still for a specified amount of time before moving on to the next line in the program.

Think of it as a pause button — but one the machine releases on its own. You set the time, the machine waits, then it moves on without any input from you.

When Should You Use G04?

There are three main reasons machinists reach for G04.

Breaking chips on a lathe

When you’re turning long, stringy materials like stainless steel or aluminum, the chip can grow into one continuous ribbon that wraps around the part or the tool.

Those chips are sharp!

A short dwell breaks the chip so it falls free. It’s a simple fix that saves a lot of cleanup — and prevents those razor-sharp stringers from becoming a safety hazard.

Improving surface finish

Dwelling at the bottom of a hole or at the end of a facing pass gives the tool time to clean up the surface before retracting. On milling operations, it can smooth out the bottom of a pocket. On turning, it can improve finish on a shoulder or face.

Giving the machine time to catch up

CNC machines are fast, but not every function happens instantly. Coolant takes a moment to flow. A pallet changer needs time to complete. If you’re programming a move right after one of these events, a G04 can give the machine the margin it needs before the next operation begins.

G04 Syntax and Format

Here’s where G04 gets a little inconsistent. The dwell time format varies by machine brand and controller. Three things can change depending on what you’re running:

1. The letter used to call out the time

Common options are P, F, U, and X. Fanuc controls typically use P. Check your machine manual to confirm which letter your controller expects.

2. Seconds vs. milliseconds

Some controllers treat the number as seconds. Others treat it as milliseconds. This matters a lot — a dwell of P3 could mean 3 seconds or 3 milliseconds depending on the machine.

1 second = 1000 milliseconds

3. Decimal or no decimal

Some controllers require a decimal point. Some don’t allow one. And some accept either but interpret the number differently based on whether you include the decimal.

Here’s an example of how the same 3-second dwell might look on three different controllers:

G04 P3       (3 seconds on some controls — e.g., Fanuc with integer = seconds)
G04 P3000    (3 seconds on controls that use milliseconds with no decimal)
G04 U3.0     (3 seconds on some lathe controls — decimal = seconds)

COMMON MISTAKE – Assuming your controller uses seconds when it actually uses milliseconds (or vice versa)

Why it matters: G04 P3 on a milliseconds-based controller gives you a 3-millisecond dwell — essentially nothing. G04 P3000 on a seconds-based controller pauses the machine for 50 minutes. Always confirm the format in your machine manual before running a new dwell in a program.

The bottom line: Always verify how your specific machine handles G04 before using it in a real program. Consult the machine manual or contact the manufacturer if you’re not sure.

G04 Program Example

Here’s how G04 looks in a real drilling program. In this case, we’re dwelling at the bottom of each hole to improve the finish on the hole floor.

G90 G54 G00 X1.0 Y1.0       (Rapid to first hole position)
G43 H01 Z0.1 M03 S1500       (Tool length comp on, spindle on)
G01 Z-0.5 F5.0               (Feed to bottom of hole)
G04 P1.0                     (Dwell 1 second at bottom — improves finish)
G00 Z0.1                     (Retract)
X2.5 Y1.0                    (Move to second hole)
G01 Z-0.5 F5.0               (Feed to bottom)
G04 P1.0                     (Dwell again)
G00 Z0.1                     (Retract)
M05                          (Spindle off)
M30                          (Program end)

The G04 P1.0 line tells the machine to wait 1 second at the bottom of the hole before retracting. The spindle keeps spinning the whole time — only the Z-axis stops moving.

G04 vs G4: Is the Zero Required?

No. G04 and G4 are identical. The CNC controller reads them the same way.

You’ll typically see G04 written with the leading zero in textbooks and reference materials. In practice, many programmers drop the zero and just write G4. Either format is fine.

If you work in a shop with a style standard, follow whatever format the team uses. If you’re on your own, pick one and be consistent.

How G04 Differs from M00 and M01

G04, M00, and M01 all pause the machine — but they work very differently.

With G04:

  • The machine pauses for the programmed time, then resumes on its own
  • The spindle stays on
  • Coolant stays on
  • No operator input required

With M00 (program stop):

  • The machine stops completely — spindle off, coolant off
  • The program won’t resume until the operator hits cycle start
  • Useful when you need to measure a part mid-cycle or swap a clamp
fanuc cnc control panel with cycle start button highlighted
Cycle start on a Fanuc control

With M01 (optional stop):

  • Same as M00, but only activates if the optional stop switch is turned on at the control
  • Useful for stopping during setup and skipping the stop in production

Use G04 when you just need a timed pause and want the machine to keep running. Use M00 or M01 when you need a human to intervene before continuing.

Canned Cycles with Built-In Dwell

If you’re drilling or boring, you may not need a standalone G04 at all. Several canned cycles have dwell built right into the code:

These are worth knowing about. If you’re already using a canned cycle, the built-in dwell is often the cleaner solution.

FAQs

What is G04 used for in CNC programming?

G04 is the dwell command. It pauses machine motion for a set amount of time while keeping the spindle and coolant running. Common uses include breaking chips on a lathe, improving surface finish at the bottom of a hole, and giving machine functions like coolant or pallet changers time to complete.

Does G04 turn off the spindle?

No. During a G04 dwell, the spindle keeps running. Only the axis motion stops. If you need to stop the spindle, use M05.

What is the difference between G04 and M00?

G04 pauses for a timed amount of time and resumes automatically. M00 is a full program stop — it turns off the spindle and coolant and waits for the operator to press cycle start before continuing.

Why does my G04 not seem to be dwelling long enough?

Your controller may be interpreting the dwell value as milliseconds instead of seconds. G04 P1 on a milliseconds-based controller dwells for only 1 millisecond — effectively nothing. Check your machine manual to confirm whether your controller uses seconds or milliseconds, and adjust your value accordingly.

F Code in CNC Programming: How Feedrate Works (With Examples)

The F code sets the feedrate — how fast the cutting tool moves through the material. It’s one of the most important values in any CNC program. Set it too slow and you waste time. Set it too high and you break tools.

Key Takeaways

  • F sets the cutting speed (feedrate) for moves made with G01, G02, G03, and most canned cycles
  • G00 (rapid) ignores the F value entirely — the machine always rapids at max speed
  • Two feedrate modes exist: per minute (in/min or mm/min) and per revolution (in/rev or mm/rev)
  • Mills typically run in per-minute mode (G94); lathes more often use per-revolution mode (G99)
  • Feedrate stays active until you change it — it’s a modal command
F Code – At A Glance
FunctionSets the cutting feedrate
FormatF[value] — example: F15.0 or F0.008
TypeModal (stays active until changed)
Used withG01, G02, G03, canned cycles
Ignored byG00 (rapid travel)
Unit modes (mill)G94 = per minute, G95 = per revolution
Unit modes (lathe)G98 = per minute, G99 = per revolution

What Does the F Code Do?

The F code tells the machine how fast to move the cutting tool when making a cut. It works with any motion code that cuts material — linear moves (G01), arcs (G02/G03), and drilling cycles.

One thing to know right away: G00 ignores the feedrate. Rapid moves always run at the machine’s maximum speed, no matter what F value was last programmed.

F Code Format

The basic format is just the letter F followed by a number:

F15.0

Decimal points are allowed. The number of decimal places depends on the unit mode your machine is running.

  • Inch, per minute: one decimal place — example: F12.5
  • Metric, per minute: one decimal place — example: F200.0
  • Inch, per revolution: four decimal places — example: F0.0846
  • Metric, per revolution: three decimal places — example: F0.329

Feedrate Modes: Per Minute vs. Per Revolution

Per Minute (mm/min or in/min)

This mode sets how far the tool travels in one minute. It does not depend on spindle speed — if the spindle slows down, the feed rate stays the same.

This mode is standard on most mills. It’s activated with G94 on mills and G98 on lathes.

Per Revolution (mm/rev or in/rev)

This mode sets how far the tool moves per spindle rotation. As the spindle speeds up or slows down, the feed automatically adjusts to keep chip load consistent.

This mode is common on lathes. It’s activated with G95 on mills and G99 on lathes.

Most lathes switch between these modes regularly depending on the operation. Most mills stay in per-minute mode.

COMMON MISTAKE – Forgetting that G98 and G99 mean something different on lathes vs. mills

On a mill, G98 is a canned cycle return mode (initial point). On a lathe, G98 sets feedrate per minute. Copying code between machine types without checking this will cause problems.

F Code in a Real Program

Here’s what F looks like in context on a mill program:

G90 G54 G00 X1.0 Y0.5      (Rapid to start position)
G43 H01 Z0.1               (Tool length offset, move to clearance)
G01 Z-0.25 F5.0            (Feed down to depth at 5 in/min)
G01 X3.0 F15.0             (Feed across at 15 in/min)
G00 Z1.0                   (Rapid back to clearance — F ignored)

What Factors Affect the Right Feedrate?

There’s no single correct feedrate — it depends on your specific setup. Here are the main factors:

Cutting Tool

  • Sharpness: A dull tool requires slower feeds to avoid breaking
  • Size: Larger tools can generally handle higher feeds than small diameter tools
  • Material: Carbide tools run faster than high-speed steel (HSS). Ceramic tools can run faster still

Workpiece Material

Softer materials like aluminum, brass, and plastic allow much higher feedrates than tough materials like stainless steel or titanium. Start conservative with any new material.

Spindle Speed

Feedrate and spindle speed work together. “Speeds and feeds” are paired for a reason — they both affect chip load, heat, and tool life.

Depth and Width of Cut

illustration showing the depth of cut on a cnc machine

Deeper or wider cuts put more force on the tool. When you increase depth or width, you typically need to slow the feed down to compensate.

Coolant

cnc machine table with coolant lines and spindle shown
Flood coolant in use

Flood coolant reduces heat and can allow higher feedrates. Running dry often means cutting slower.

Machine Condition

a white and black cnc machine
Not everyone works with a machine this nice

Worn or loose machines may need slower feeds to hold tolerance and avoid vibration. A newer, rigid machine handles higher feeds better.

How to Calculate Feedrate

Milling Feedrate (per minute)

The standard formula for milling feed rate is:

Feed Rate (in/min) = RPM × Number of Flutes × Chip Load
  • RPM = spindle speed in revolutions per minute
  • Number of flutes = number of cutting edges on the tool
  • Chip load = how thick each chip should be (get this from your tool manufacturer’s chart)

Example: 4-flute end mill, RPM = 2000, chip load = 0.004 in/tooth

Feed Rate = 2000 × 4 × 0.004 = 32.0 in/min

Program that as: F32.0

Converting Between Per Minute and Per Revolution

Feed per revolution = Feed per minute ÷ RPM
Feed per minute = Feed per revolution × RPM

Example: F200.0 mm/min at 1000 RPM = 200 ÷ 1000 = 0.200 mm/rev

COMMON MISTAKE – Too aggressive chip load for a long tool

A long tool deflects more than a short one. The same chip load that works fine on a short end mill can cause chatter, poor finish, or breakage on a long reach tool.

When sticking out more than 3× the tool diameter, reduce your feed rate.

How to Choose a Starting Feedrate

Most machinists start with manufacturer specs — the tool data sheet or catalog will give you recommended chip loads and speeds for different materials. Use those as your starting point, then adjust from there.

Good starting resources:

  • Tool manufacturer charts (Kennametal, Sandvik, OSG, etc.)
  • Machinery’s Handbook
  • CAM software (most calculate feeds and speeds automatically)
  • Online feeds and speeds calculators (FSWizard, G-Wizard, etc.)

General guidelines:

  • Carbide tools can run faster than HSS — in some cases 3–5× faster
  • Harder workpiece materials need slower feeds
  • Slower feeds = longer tool life, less heat, better finish
  • Most manufacturers give a range — start at the low end and work up

Feedrate Override

feedrate override knob on a Lillian VMC 1100 CNC mill
Feedrate override knob on a Lillian VMC 1100 CNC mill

The feedrate override knob on the machine control panel lets the operator adjust the programmed feedrate on the fly — anywhere from 0% to 200% of the programmed value.

This is useful when running a new program for the first time. Set the override to 50% and gradually increase it as you gain confidence in the program.

COMMON MISTAKE – Forgetting about your feedrate override

Why it matters: The next operator (or you, later) assumes the machine is running at programmed speed.

Cycle times will be off, and the parts may not be cut correctly if feeds are intentionally matched to spindle speeds.

FAQs

What does the F code control in CNC?

The F code controls the feedrate — the speed at which the cutting tool moves through the material during a cut. It applies to G01 linear moves, G02/G03 arc moves, and most canned cycle operations.

Does G00 use the F code?

No. G00 (rapid positioning) always moves at the machine’s maximum speed regardless of the F value. The feedrate is only active during cutting moves.

What’s the difference between feed per minute and feed per revolution?

Feed per minute sets how far the tool moves in 60 seconds, independent of spindle speed. Feed per revolution sets how far the tool moves per spindle rotation, so it automatically adjusts if the spindle speed changes. Lathes commonly use per-revolution mode (G99); mills typically use per-minute mode (G94).

How do I know what feedrate to use?

Start with the tool manufacturer’s recommended chip load for your material. Multiply chip load × number of flutes × RPM to get your starting feed per minute. Adjust based on machine rigidity, tool stickout, and cut depth.

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.

G-Code Comments: How to Add Them (By Machine Brand)

G-code comments are notes inside your CNC program that the controller ignores. They help operators, setup people, and your future self understand what the program is doing — without changing how the machine runs.

Key Takeaways

  • Comments are ignored by the CNC controller — they’re notes for humans, not the machine
  • The two most common comment characters are parentheses () and the semicolon ;
  • Fanuc, Haas, Hurco, Mitsubishi, and Yasnac use parentheses; Heidenhain and Siemens use a semicolon
  • Place comments at the start of program sections and after key moves — not on every line
  • Avoid placing comments in the middle of a line if you want maximum machine compatibility

How G-Code Comments Work

A comment is a section of your program that the controller reads but skips over. It doesn’t move any axes, change the spindle speed, or trigger any machine action. Its only job is to communicate information to whoever is reading the code.

Different machine brands use different characters to mark a comment. Knowing which one your machine uses is important — use the wrong character and some controllers will throw an alarm.

Comment Characters by Manufacturer

Machine Control ManufacturerG-Code Comment Character
FanucParentheses ()
HaasParentheses ()
HeidenhainSemicolon ;
HurcoParentheses ()
MitsubishiParentheses ()
Sinumerik (Siemens)Semicolon ;
YasnacParentheses ()

When in doubt, check your machine’s programming manual. There are dozens of control manufacturers out there, and some older or less common controls use different conventions.

How Parentheses Work

When you use parentheses, the controller ignores everything between the opening and closing parenthesis:

G00 X1.0 Y1.0 (RAPID TO FIRST HOLE POSITION)
G43 H01 Z1.0  (TOOL LENGTH COMP - 1/2 INCH DRILL)

Only the text inside the () is treated as a comment. The rest of the line runs normally.

COMMON MISTAKE – Putting a semicolon inside parentheses on a Fanuc/Haas machine.

Why it matters: On these controls, the semicolon inside the parens is just part of the comment text — it won’t comment out the rest of the line. Only what’s inside the parentheses is ignored.

How the Semicolon Works

On Heidenhain and Siemens controls, a semicolon tells the controller to ignore everything after it on that same line:

G00 X1.0 Y1.0 ; RAPID TO FIRST HOLE POSITION
G43 H01 Z1.0  ; TOOL LENGTH COMP - 1/2 INCH DRILL

Everything to the right of the ; on that line is ignored. A semicolon on a Fanuc machine behaves differently — it marks the end of a block, not a comment.

COMMON MISTAKE – Using a semicolon as a comment on a Fanuc control.

On Fanuc, the semicolon is an end-of-block character (like a carriage return). Using it as a comment delimiter can confuse the controller or cause errors depending on the version.

What to Include in a G-Code Comment

Keep comments short and useful. A comment that fills three lines of text is harder to read than no comment at all.

Good things to include:

  • Tool number and description (T01 = 1/2″ DRILL)
  • Operation being performed (ROUGH FACE, FINISH BORE, DRILL HOLES)
  • Offset numbers being used (H01, D01)
  • Instructions for the operator (INSPECT PART BEFORE CONTINUING, FLIP PART HERE)
  • Program revision or date at the top of the file

Here’s a realistic example of a well-commented program header:

%
O1001 (PART: 12345-A HOUSING - REV 2)
(DATE: 2024-09-01  PROGRAMMER: B. FOWLER)
(MATERIAL: 6061 ALUMINUM)
(MACHINE: HAAS VF-2)
(-----TOOL LIST-----)
(T01 - 3/4 INCH FLAT ENDMILL)
(T02 - 1/2 INCH DRILL)
(T03 - 1/2-13 TAP)

This kind of header is worth the extra lines. Anyone who opens the program — including you, six months from now — can understand it at a glance.

Where to Put Comments in Your CNC Program

There are a few spots where comments consistently add value.

At the top of the program — Include a header block with the part name, revision, programmer name, date, machine, and tool list. CAM software often generates this automatically.

At the start of each section — When you switch from one operation to another (roughing to finishing, drilling to tapping), a comment line helps the operator track where they are:

(-----BEGIN DRILLING CYCLE-----)
G99 G83 Z-1.25 Q0.25 R0.1 F12.0

At the end of key lines — A brief note after a critical move or setting is enough:

G92 S3000 (SPINDLE SPEED LIMIT)
G50 S3000 (MAX RPM - LATHE)

Avoid Putting Comments in the Middle of a Line

Some Fanuc-based controls allow inline parentheses comments mid-line, but not all do. Stick to end-of-line comments for the best cross-machine compatibility.

When to Use Comments (And When to Skip Them)

Use comments where they genuinely help someone understand the program. You don’t need to comment every single line.

A good rule: comment at transitions and at the top of each tool or operation. That’s usually enough. If someone reads your header and your section comments, they should be able to follow the program without needing a note on every G00.

On older machines or machines with limited memory, excessive comments can increase program size. It’s rarely a problem today, but worth keeping in mind if you’re running a very old control.

FAQs

Do g-code comments slow down the machine?

No. The controller reads and ignores them — they don’t affect machine motion, feed rate, or cycle time in any meaningful way.

Can I put a comment anywhere in the program?

Generally, yes — but the safest approach is at the end of a line or on its own line. Putting comments in the middle of active code can cause errors on some controllers.

What happens if I use the wrong comment character for my machine?

On many controls, using the wrong character will just be ignored or cause a syntax error. On some machines, an unrecognized character in the wrong place can stop the program or trigger an alarm. Always verify the correct character for your specific control.

Do all CNC machines support comments?

Most modern CNC controls do. Very old or simplified controls may not support them, or may only support one format. When in doubt, check the programming manual for your specific machine.

G85 G-Code: The Ream/Bore Canned Cycle Explained

G85 is a canned cycle that feeds a reamer or boring tool into a hole — and feeds it back out at the same rate. That feed-in, feed-out motion is what makes it different from most other boring cycles.

Key Takeaways

  • G85 feeds the tool into the hole and back out at the programmed feedrate
  • Use it for reaming or finish boring — not for drilling from scratch
  • The hole must be pre-drilled before running G85
  • Retract at feedrate (not rapid) reduces tool marks and can improve surface finish
  • Cancel with G80 when you’re done or the machine will bore at every new XY position
G85 – At A Glance
FunctionReam / Bore Canned Cycle
FormatG85 X_ Y_ Z_ R_ F_
TypeModal (Canned cycle)
Cancelled byG80
Used withG98 or G99, G43 (tool length comp)

What G85 Does

When the machine executes G85, it follows this sequence every time it hits a new hole position:

  1. Rapid down to the R plane (your clearance height above the part)
  2. Feed from the R plane down to the Z depth at your programmed feedrate
  3. Feed back out to the R plane at that same feedrate — no rapid retract
  4. If G98 is active, rapid back up to the initial point. If G99, stay at R plane.

That feed-out on step 3 is the key. Other cycles like G81 rapid back out. G85 feeds back out — which gives the tool a second light pass on the way up. For a reamer, that can clean up the finish. For boring, it helps avoid gouging the wall on retract.

G85 Cycle Visualizer
Rapid
Feed
Crash
0 / 6
Ready
Hit Next Step to begin.
Retract:

G85 Code Format

G85 X2.0 Y3.0 R0.1 Z-1.0 F12.0

In practice, most programs split the XY position from the cycle call:

X2.0 Y3.0
G98 G85 R0.1 Z-1.0 F12.0

Here’s what each address does in the G85 context:

  • K — Number of times to repeat the cycle at the same location (rarely used for reaming)ll clear any and all obstacles including clamps, fixtures and the part itself.
  • X / Y — Position of the hole center
  • Z — Bottom of the bore/ream stroke (not the surface of the part — the depth you want to reach)
  • R — The R plane: where the tool stops rapid and starts feeding. Set this above the part surface with enough clearance to clear clamps and fixtures.
  • F — Feedrate for both the plunge and the retract

COMMON MISTAKE – Setting Z to the part surface instead of the bottom of the hole.

Why it matters: Z is the depth of the bore stroke, not where the hole starts. If your part surface is at Z0.0 and you want to ream 1.5 inches deep, Z should be Z-1.5 — not Z0.0. Getting this backwards means either not reaming deep enough or crashing through the part.

COMMON MISTAKE – Using the same feedrate for reaming that you used for drilling.

Why it matters: Reamers run at lower feedrates than drills. Running a reamer too fast removes too much material per flute and leaves a rough finish — or breaks the tool. Check your tooling manufacturer’s speeds and feeds for the material you’re cutting.

When to Use G85

Use G85 when you need one of these results:

Finishing a hole to a tight tolerance

If a drawing calls for a reamed hole — like an H7 or similar fit — G85 with a chucking reamer is how you get there. The hole needs to be pre-drilled close to final size first (typically 0.010″–0.015″ undersize for reaming, though check your tool manufacturer’s recommendation).

Smooth bore retract to protect surface finish

Some boring bar operations benefit from feeding back out rather than rapid retracting. A rapid retract can leave a small mark or scratch where the tool lifts off the bore wall. G85 avoids that by keeping the tool moving on the way out.

COMMON MISTAKE – Running G85 on a hole that hasn’t been drilled yet

Why it matters: Reamers and boring bars aren’t designed to create holes from solid material. Using G85 on solid stock will damage the tool, the part, and potentially the machine. Always drill first.

Hole Size and Setup Considerations

Pre-drill close to final size

Leave as little stock as possible for the reamer to remove — usually around 0.010″–0.015″ for smaller holes, slightly more for larger diameters. Too much stock and the reamer deflects and leaves a bad finish. Too little and it skips over the surface.

Speeds and feeds matter

Reaming is slower than drilling — typically half the RPM and a higher feed per revolution. Your tooling supplier’s spec sheet for the material you’re cutting is the right starting point. Don’t guess.

Set your R plane with room to spare

The R value is your clearance height between holes. It needs to be above any clamps, fixture plates, or part features the tool could hit while moving from hole to hole. When in doubt, go higher and use G98 to return to the initial point between holes.

G98 vs. G99 with G85

G98 and G99 control where the tool goes after it retracts from the hole.

  • G98 — After the G85 retract, the tool rapids back to the initial point (wherever Z was before the cycle started). Use this if you have clamps, tall fixtures, or part features between holes.
  • G99 — After the retract, the tool stays at the R plane and moves directly to the next hole. Faster cycle time, but only safe if your R plane clears everything.

For most reaming operations, G98 is the safer default.

visualization of how a cnc machine moves using g98 and g99 codes shows motion of travel for the machine

Canceling G85

Use G80 to cancel the G85 cycle:

G80

Once G85 is active, the machine will attempt to ream at every new XY position it encounters — even ones you didn’t intend. G80 turns that off. Put it on its own line when you’re done reaming.

G85 vs. Similar Canned Cycles

G85 vs. G81

G81 is the basic drilling cycle. It rapids the tool back out after reaching depth. G85 feeds back out. If you ran G81 with a reamer, the rapid retract could scratch the bore wall. G85’s feed-out retract is the reason it exists.

G85 vs. G86

G86 also bores, but it stops the spindle before retracting. That spindle stop protects the bore wall from the cutting edge dragging across it on the way out. The tradeoff: you get a small mark where the spindle stopped, and cycle times are longer. G86 is used more for roughing. G85 is used where you need a clean finish without stopping the spindle.

All three cycles — G81, G85, and G86 — are canceled with G80.

Example Program

Here’s a simple G85 program reaming two holes:

G90 G54 G00 X1.5 Y2.0          (Rapid to first hole)
G43 H01 Z1.0                   (Tool length comp, initial point at Z1.0)
G98 G85 X1.5 Y2.0 R0.1 Z-1.25 F8.0   (Ream first hole, return to Z1.0)
X4.0 Y2.0                      (Move to second hole, ream it)
G80                             (Cancel canned cycle)
G00 Z5.0                       (Retract to safe height)
M05                             (Spindle off)

The R0.1 puts the R plane just above the part surface. The Z-1.25 is the bottom of the ream stroke. G98 sends the tool back to Z1.0 between holes, which is safe if there are fixtures in the area.

FAQs

What’s the difference between G85 and G81?

Both are canned cycles used for hole operations, but G81 rapids the tool back out after reaching depth. G85 feeds the tool back out at the programmed feedrate. For reaming, that feed-out retract is important — it keeps the reamer from scratching the bore wall on the way up.

Does G85 work for drilling?

No. G85 is designed for reaming and finish boring on holes that have already been drilled. Using it to drill from solid material will damage the reamer and likely the workpiece. Use G81 or G83 for drilling.

What feedrate should I use with G85?

That depends on the tool and material. Reamers generally run at roughly half the RPM you’d use for drilling the same size hole, with a higher feed per revolution. Check your tooling manufacturer’s chart for the specific reamer diameter and material combination.

How do I cancel G85?

Use G80 on its own line after your last hole. G85 is modal, which means it stays active and will try to ream at every new XY position until you cancel it.

G74 G-Code: Left-Hand Tapping Cycle Explained (With Example)

If you’ve ever seen “LH” on a print next to a threaded hole, you’ve found yourself a left-hand thread. They tighten counterclockwise instead of the usual way, and your standard G84 tapping cycle won’t cut them correctly.

That’s what G74 is for.

G74 is the left-hand tapping canned cycle. It runs the spindle in reverse, feeds down to depth, then reverses out — same basic idea as G84, just spinning the other direction. You’ll also use it for peck tapping when you need to clear chips out of a deep hole.

This page covers how G74 works, how to format it, and what to watch out for before you run it.

Key Takeaways

  • G74 is the left-hand tapping canned cycle — it cuts threads that screw in the opposite direction of standard threads
  • The spindle must be set to counterclockwise rotation with M04 before calling G74
  • The feedrate (F) must equal the pitch of the thread — not a cutting speed
  • Add the Q parameter to switch from rigid tapping to peck tapping
  • Cancel with G80 when done, or the machine will attempt to tap every new position in the program
G74 – At A Glance
FunctionLeft-Hand Tapping Canned Cycle
FormatG98 G74 X__ Y__ R__ Z__ F__
TypeModal (Group 9)
Spindle directionCounterclockwise (M04)
Cancelled byG80
Peck tappingYes — add Q parameter

What does G74 do?

G74 activates the left-hand (reverse) tapping canned cycle on a CNC machining center.

three different taps used for creating internal threads with a CNC machine

Tapping creates internal threads — the kind that a bolt or screw gets threaded into. G74 specifically cuts left-hand threads, which are the opposite of standard threads. They tighten counterclockwise and loosen clockwise. Think lefty-tighty, righty-loosey.

Thread Direction Explainer

Right-Hand Thread — G84
TOP VIEW — M03 CLOCKWISE ↻ SIDE VIEW — RH THREAD ╱
G-codeG84
SpindleM03 (CW)
Tightens↻ Clockwise
Loosens↺ Counter-CW
Left-Hand Thread — G74
TOP VIEW — M04 COUNTER-CW ↺ SIDE VIEW — LH THREAD ╲
G-codeG74
SpindleM04 (CCW)
Tightens↺ Counter-CW
Loosens↻ Clockwise
Right-Hand (G84) Righty-tighty. Standard threads on bolts, screws, and most tapped holes.
Left-Hand (G74) Lefty-tighty. Used where rotation would loosen a standard thread — left pedals, grinder arbors, turnbuckles.

When G74 runs, the spindle rotates counterclockwise (M04), feeds down to the programmed depth, reverses direction, and feeds back out. That’s the whole cycle.

Use G74 when the print calls for a left-hand thread. For standard right-hand threads, use G84 instead.

Parameters Used with G74

These are the words (addresses) that control how G74 runs:

  • X, Y — Position of the hole to be tapped
  • R — The R-plane (retract plane) — the Z height the tool clears to when moving between holes
  • Z — Final tapping depth (bottom of the thread, not top)
  • F — Feedrate — must equal the thread pitch
  • Q — Peck depth (optional — only include if you want peck tapping)
  • K or L — Number of times to repeat the cycle (machine-dependent)
  • G98 or G99 — Controls where the tool retracts between holes (initial point vs. R-plane)

The R-plane should clear all clamps, fixtures, and part features. When tapping, set it a little higher than you normally would. The machine needs a moment to sync spindle speed and feedrate before the tool enters the hole.

visualization of how a cnc machine moves using g98 and g99 codes shows motion of travel for the machine

Programming Format

The standard format looks like this:

G98 G74 X__ Y__ R__ Z__ F__

In most programs, you’ll see the hole position on a separate line:

X2.0 Y3.0
G98 G74 R0.25 Z-1.5 F0.0625

The program moves the tool to the hole location first, then calls the tapping cycle.

The Z value is the bottom of the thread — not the surface of the part. Don’t confuse it with the start of the hole.

Setting the Feedrate

The feedrate for tapping is not a cutting speed. It must equal the pitch of the thread.

For inch threads, pitch = 1 ÷ threads per inch.

  • 1/4-20 thread → F = 1 ÷ 20 = 0.050 IPR (or IPM at the corresponding RPM)
  • 5/16-18 thread → F = 1 ÷ 18 = 0.0556

For metric threads, the pitch is listed directly on the print.

  • M10 x 1.5 → F = 1.5 mm/rev

Speeds and feed overrides are disabled during tapping for safety. The machine locks them out so you can’t accidentally change the ratio between spindle speed and feed — which would strip or break the tap.

Hole Prep Before Tapping

G74 requires a pre-drilled hole. Do not use it to drill and tap in one step.

The tap drill diameter = thread major diameter − pitch.

  • M10 x 1.5 → tap drill = 10 − 1.5 = 8.5 mm

Drill the hole first with a standard drilling cycle (G81 or G83), then call G74.

COMMON MISTAKE – Wrong Spindle Direction

G74 requires M04 (counterclockwise) — not M03.

If you run G74 with the spindle turning clockwise, you’ll strip threads or break the tap immediately. Always confirm M04 is active before the G74 block runs. This is the single most common G74 setup error.

G74 Rigid Tapping

Rigid tapping means the spindle and the Z-axis feed are electronically synchronized — the machine treats the tap like a leadscrew. There’s no floating tap holder needed.

Not all machines support rigid tapping. On Fanuc-based controls, it’s typically enabled with M29 before the G74 call:

M29 S500
G98 G74 X2.0 Y3.0 R0.25 Z-1.5 F0.0625

Check your machine’s parameter settings and manual to confirm rigid tapping is enabled. On older machines, you may need a tension-compression tap holder instead.

Rigid tapping is fast and accurate, but it can run into trouble with chip buildup in deep holes or tough materials. That’s where peck tapping helps.

G74 Peck Tapping

Adding the Q parameter to G74 switches it to peck tapping mode. The tap feeds in to the Q depth, retracts partially to break chips, then feeds deeper — one peck at a time until it reaches the Z depth.

illustration that shows the difference between peck drilling and standard drilling in a CNC machine
The graphic is for drilling but peck tapping works in the same way
G98 G74 X2.0 Y3.0 R0.25 Z-1.5 Q0.25 F0.0625

This example pecks in 0.25″ increments.

Peck tapping is slower than rigid tapping, but it keeps chips from packing into the hole. Use it for deep threads, small taps, or sticky materials like aluminum.

G84 vs. G74

G84 is the standard right-hand tapping cycle. G74 is the left-hand version.

The only real difference: G84 uses M03 (clockwise spindle), G74 uses M04 (counterclockwise). The formats are otherwise identical.

G84G74
Thread typeRight-hand (standard)Left-hand (reverse)
SpindleM03 — clockwiseM04 — counterclockwise
TightensClockwiseCounterclockwise
Peck capableYes (Q)Yes (Q)

Both are modal — they stay active until you cancel with G80.

Canceling G74

Cancel the cycle with G80:

G80

If you forget G80, the machine will attempt to tap a hole at every new X/Y position in the program. That’s a bad day. Always cancel your canned cycles before moving on.

G74 code example

G00 X50.0 Y50.0          (Rapid to first hole location)
M04 S500                  (Spindle on counterclockwise at 500 RPM)
G98 G74 R5.0 Z-40.0 F1.5 (Left-hand tap, 40mm deep, 1.5mm pitch)
X25.0                     (Tap second hole at X25, Y50)
Y25.0                     (Tap third hole at X25, Y25)
G80                       (Cancel canned cycle)
Z10.0                     (Retract Z)
M30                       (End program)

The tool taps three holes in sequence. G98 returns the Z to the initial point between moves. G80 ends the cycle after the last hole.

FAQs

What is G74 used for?

G74 is used to cut left-hand internal threads in a pre-drilled hole. It’s the reverse tapping canned cycle — the spindle runs counterclockwise to cut threads that tighten counterclockwise.

What’s the difference between G74 and G84?

G84 cuts standard right-hand threads with a clockwise spindle (M03). G74 cuts left-hand threads with a counterclockwise spindle (M04). The programming format is otherwise the same.

How do I set the feedrate for G74?

The feedrate must equal the pitch of the thread. For metric threads, use the pitch number directly (M10x1.5 → F1.5). For inch threads, divide 1 by the threads per inch (1/4-20 → F0.050).

Do I need to drill a hole before using G74?

Yes. G74 only cuts threads — it does not drill. Always run a drilling cycle (G81 or G83) with the correct tap drill size before calling G74.

M05 CNC Code: Spindle Off Command Explained

The M05 code stops the spindle. That’s it. One code, one job.

When the CNC controller reads M05, it cuts power to the spindle motor and the spindle coasts to a stop. Any active spindle rotation — whether started with M03 or M04 — is cancelled.

Key Takeaways

  • M05 stops the CNC spindle
  • It cancels both M03 (clockwise) and M04 (counterclockwise) rotation
  • M5 and M05 are the same — the leading zero is optional
  • Several other codes also stop the spindle, but M05 is the dedicated command
  • Always stop the spindle before changing tools on machines that don’t handle this automatically
M05 – At A Glance
FunctionStop spindle rotation
TypeNon-modal
CancelsM03, M04

What Does M05 Do?

M05 cancels any active spindle rotation. It works whether the spindle was turned on with M03 (clockwise) or M04 (counterclockwise).

Spindle direction

You’ll see M05 used most often in two places: between operations that require the spindle to be stopped, and at the end of a program alongside M30.

Here’s a simple example showing M05 in context:

T01 M06               (Tool change — tool 1)
G54 G90 G00 X0. Y0.   (Move to start position)
S1200 M03             (Spindle on, 1200 RPM, clockwise)
G01 Z-0.5 F10.        (Feed down to depth)
G00 Z1.0              (Retract)
M05                   (Spindle off)
M00                   (Program stop — operator inspects part)
S1200 M03             (Restart spindle for next operation)

In that example, M05 stops the spindle before the program pause so the operator isn’t working near a spinning tool.

Try It: Spindle Control Simulator

The best way to understand M05 is to see it in action. Use the simulator below to toggle the spindle on and off using the same M-codes you’d program into a real CNC machine.

Click M03 or M04 to start the spindle, then hit M05 to stop it. Drag the speed slider to change the RPM and watch how the spindle behaves at different speeds.

Try It CNC Spindle Control Simulator
Status OFF
Dir.
1200 RPM
Program Output
— click a button above —

Notice that M03 and M04 both require M05 to stop — the spindle won’t stop on its own just because your tool is done cutting. That’s why M05 shows up in almost every CNC program.

M5 vs M05 — Is There a Difference?

No. They’re the same command. The CNC controller reads both identically.

The leading zero is just formatting. You’ll see M05 in textbooks and reference materials because it looks consistent alongside other two-digit M-codes. On the shop floor, plenty of programmers write M5 because it’s one less character.

If you’re programming on your own, use whichever style you prefer. If you’re working in an established shop, match the style already used in existing programs.

Do You Need M05 Before a Tool Change?

It depends on the machine and the postprocessor.

Most modern CNC machining centers automatically stop the spindle when they see a tool change command (M06). If that’s your machine, you don’t need to manually program M05 before M06 — the control handles it.

On older machines or certain lathes, the spindle doesn’t stop automatically. If you’re not sure about your machine, check the manufacturer’s documentation or look at how existing programs are written.

COMMON MISTAKE – Assuming the spindle will stop automatically before a tool change

On some machines it does — on others it doesn’t. If your machine doesn’t auto-stop and you skip M05 before M06, you risk a tool change with a spinning spindle.

Always verify what your specific machine does.

What other codes stop the spindle?

M05 is the dedicated spindle-off command, but several other codes also stop the spindle as part of a larger action.

M00 — Program Stop Stops the spindle, coolant, and all axis motion. The program pauses until the operator presses Cycle Start again. Use it when you need to inspect the part mid-program.

M01 — Optional Stop Does the same thing as M00, but only if the Optional Stop switch on the machine is turned on. If the switch is off, the machine skips M01 entirely. Useful for production runs where you want to skip checks after a part is dialed in.

M02 — Program End (Legacy) A legacy code from the days of paper tape. It ends the program. On most modern machines, M02 is treated the same as M30. You won’t see it much in new programs.

M30 — Program End with Rewind The standard program-end code. Stops the spindle, stops coolant, ends the program, and resets the cursor back to the top of the program. Use M30 to end every program.

The key difference between M05 and these codes: M05 only stops the spindle. The other codes stop everything. If you just need the spindle off — and you want the program to keep running — M05 is the right choice.

FAQS

Does M05 stop the coolant too?

No. M05 only stops the spindle. To stop coolant, you need M09. Most programs use both M05 and M09 together at the end of an operation.

Can I use M05 in the middle of a program?

Yes. M05 can appear anywhere in a program. You’d use it any time you need the spindle to stop without pausing or ending the program — like before an inspection step or between spindle-direction changes.

Does M05 brake the spindle or let it coast?

It depends on the machine. Some controllers apply an orientation or brake after M05; others let the spindle coast to a stop naturally. High-speed machining centers often have active braking. Check your machine’s documentation if timing matters.

What’s the difference between M05 and M00?

M05 stops only the spindle. M00 stops the spindle, the coolant, all axis motion, and pauses the program until the operator restarts it. Use M05 when you want the spindle off but the program to continue. Use M00 when you need to pause everything.

G20 G-Code: How to Set Inch Mode on Your CNC Machine

G20 sets your CNC machine to inch mode. Once active, every coordinate, feed rate, and offset value in the program is read as inches.

If you’re working from an inch drawing — or running a program written in inches — G20 needs to be active.

Key Takeaways

  • G20 switches the CNC to inch mode — all values are interpreted as inches
  • It’s a modal command, so it stays active until you switch to G21 (metric mode)
  • There’s no cancel command — either G20 or G21 must always be active
  • Place G20 in your safety line at the start of every program that uses inch values
  • Never switch between G20 and G21 mid-program
G21 – At A Glance
FunctionSet inch mode
TypeModal (Group 6)
Cancelled byG21 (switches to metric mode)
Used withAny program using inch dimensions

What Does G20 Do?

A G20 code switches the CNC into inch mode.

G20 tells the CNC to read all values in inches. That includes X, Y, and Z coordinates, feed rates, offsets, and any other numerical input in the program.

This is called a modal command. Modal means it stays active on its own — you don’t have to repeat it on every line. It stays in effect until something cancels or changes it.

One thing that trips up beginners: G20 stays active even if you restart the program or the machine loses power and restarts. The last active unit mode is remembered. That’s why safety lines exist.

G20 vs G21: Inch Mode vs Metric Mode

G21 is the other unit mode. When G21 is active, the machine reads all values as millimeters. When G20 is active, it reads inches.

These two codes are a pair. You can’t cancel either one — you can only switch between them. One of them must always be active.

Do not switch between G20 and G21 in the middle of a program. If a program starts in inches, keep it in inches throughout. Mixing units causes calculation errors and can crash the machine.

Where to Put G20 in Your Program

Most machinists place G20 (or G21) in the safety line at the very start of the program, along with other setup codes like G90 and G54.

Here’s what a typical safety line looks like in an inch program:

G20 G90 G40 G49 G80    (Safety line: inch mode, absolute, cancel comp/cycles)
G54                     (Select work coordinate system)
G00 X0. Y0.            (Rapid to starting position)

Placing G20 here makes sure the machine is always in the right mode — even if someone ran a metric program before yours.

You can also place G20 at the start of a specific section of code if that section uses inch values. But as a rule, avoid mixing units in the same program.

⇄ Inches / Millimeters Converter

Common values

1 in= 25.4 mm
0.5 in= 12.7 mm
0.25 in= 6.35 mm
0.125 in= 3.175 mm
0.001 in= 0.0254 mm

What to think about when using a G20 code?

When you use a G20 code you simply need to be aware of what type of units you are working in.

Most machine shops will work in only one unit mode, either inches or mm.

There are definitely some shops out there that will use mixed units, but in most cases CNC programs will be written in one or the other for every program they make.

Know whether your parts are in inches or mms.

What to Watch Out For

Know what units your part drawing uses. Most shops work in one system — either inches or millimeters — and stick to it for every program they run.

If you’re not sure whether G20 or G21 is currently active, check the active modal codes on the control. Most controls show the current unit mode on the main status screen.

COMMON MISTAKE – Running an Inch Program in Metric Mode

Running an inch program on a machine that’s still set to G21 (metric mode). A 1.0″ move becomes a 1mm move — the machine will run the program, but your parts will come out completely wrong.

Always verify your unit mode before hitting cycle start.

FAQS

Does G20 reset when I turn off the machine?

It depends on the control. Most modern CNC controls remember the last active unit mode after a power cycle. That’s exactly why you should always include G20 (or G21) in your program’s safety line — so you’re never depending on whatever mode was left over from the last operator.

Can I switch from G21 to G20 in the middle of a program?

Technically yes, but you should avoid it. Switching unit modes mid-program is a serious source of errors and is not recommended on most controls. Keep your programs consistent — all inches or all metric.

What happens to feed rates when G20 is active?

Feed rates are also interpreted in the active unit mode. In G20, a feed rate of F10 means 10 inches/min. In G21, F250 means 250 mm/min. This is another reason not to mix modes in a single program.

Is G20 the same on all CNC controls?

G20 for inch mode is standard across most Fanuc-based and Fanuc-compatible controls. Some older or less common controls may handle unit modes differently — always check your control’s programming manual if you’re unsure.

M06 CNC Code: Tool Change Command Explained

The M06 code tells your CNC machine to perform a tool change. When the machine reads M06, it swaps the tool waiting in the ready position of the automatic tool changer (ATC) into the spindle.

That’s the short version. Here’s everything else you need to know to use it correctly.

Key Takeaways

  • M06 triggers a tool change on CNC machines with an automatic tool changer (ATC)
  • Always pair M06 with a T code — the T code selects the tool, M06 executes the swap
  • The machine must be in a safe position (usually home or tool change position) before M06 runs
  • M06 and M6 are identical — the leading zero is optional
  • Without an ATC, use M00 to pause the program and change tools manually
M06 – At A Glance
FunctionAutomatic tool change
FormatT## M06 (or T## M6)
TypeNon-modal
Machine typeMachining centers with ATC
Common pairingT code, G43 (tool length compensation)

How M06 Works

When the controller reads M06, several things happen in sequence. The spindle stops, the machine moves to the tool change position (often the same as the home position), and the ATC rotates the carousel to place the called tool into the spindle. The previous tool gets returned to its pocket.

an automatic tool changer on a dmg mori cnc machine
Automatic tool changer

All of that is automatic. Your job is to set it up correctly in the program.

Pairing M06 with the T Code

M06 doesn’t know which tool to grab on its own. You have to tell it using a T code.

The T code selects the tool number. M06 executes the swap. They’re almost always written on the same line:

T02 M06

This calls Tool #2 from the carousel and puts it in the spindle. On some machines, you can put the T code on the line before M06 — the T code stages the tool in the ready position, and M06 swaps it in. Check your machine’s control manual for the exact behavior.

After a tool change, you’ll typically follow up with G43 to apply tool length compensation:

T02 M06
G43 H02 Z1.0

H02 calls offset #2, which stores the length of Tool #2. Without this, the machine has no idea how long the new tool is.

Try It: See How the Tool Change Works

The best way to understand M06 is to watch it happen. Use the simulator below to see exactly how the T code and M06 work together.

Here’s what to do:

  • Click any pocket on the ATC wheel to call the T code — the carousel rotates to bring that tool to the ready position
  • Press Execute M06 to pull the staged tool into the spindle
  • Then try the red button — run M06 without a T code and see what happens

That last step is the one worth remembering. The T code tells the machine which tool to load. M06 just says do it. Skip the T code and the controller either loads whatever was staged from the last program or throws an alarm. Either way, it’s not the tool you wanted.

M06 Tool Change — ATC Simulator

See how T## and M06 work together — and what happens when you skip the T code

Staged (T called)
In Spindle
Available
Empty
Automatic Tool Changer — click a pocket
ATC
HUB
READY
Spindle
SPINDLE
T01 — IN SPINDLE
½″ Flat End Mill
General-purpose milling. Slots, pockets, profiling.
2-Flute HSS
G-Code Line Builder BUILDING…
_
Click a pocket on the ATC wheel above to call the T code and stage a tool.
Step 1 — T Code
Click any available pocket on the wheel to call the T code and rotate the carousel.
Step 2 — M06
After staging a tool, press M06 to pull it into the spindle.
Click any pocket on the ATC wheel to begin.

Key rule: The T code rotates the carousel and stages the tool in the ready position. M06 executes the swap into the spindle. Skip the T code and you get the wrong tool — or nothing at all.

Preparing for a Safe Tool Change

Before M06 runs, the machine needs to be in a safe position. On most machines, this means moving to the tool change position — usually Z home or a designated safe height.

If the spindle is still near the part when M06 fires, you risk a collision. Most post-processors handle this automatically, but if you’re writing code by hand, make sure you’ve retracted first.

Also think about your tool’s physical size before loading it:

  • Diameter — Large-diameter tools (face mills, large boring heads) may need to be placed near empty pockets in the carousel so they don’t hit adjacent tools during rotation.
  • Length — Usually less of an issue, but extra-long tools can cause clearance problems with the spindle head or tool changer arm on some machines.
  • Weight — Every machine has a maximum tool weight rating. That number includes the tool, holder, and any extensions — not just the cutter. Check the spec sheet.

COMMON MISTAKE – Calling M06 Without Retracting First

Running M06 while the tool is close to the part or a fixture is one of the fastest ways to cause a crash. The machine moves to the tool change position automatically — but it takes the most direct path to get there.

If you haven’t cleared the work area first, something’s getting hit.

Performing a Tool Change Without an ATC

Not every machine has an automatic tool changer. Knee mills, older VMCs, hobby CNCs, and some small production machines require manual tool changes.

In that case, M06 isn’t needed — and on many controls, calling it will just trigger an alarm.

Instead, use M00 (Program Stop) to pause the program at the right moment. The machine stops, the spindle shuts off, and the operator changes the tool manually. When ready, they press Cycle Start to resume.

Write it like this:

M00 (CHANGE TO T02 — 1/2" END MILL)

The comment in parentheses tells the operator exactly what to do. Don’t skip the comment — whoever’s running the machine shouldn’t have to guess.

Performing a tool change without an automatic tool changer

If your machine doesn’t have an automatic tool changer, then the operator (probably you) will need to change the tools manually.

The best way to do this is to put a program stop in your program using the M00 code. You should include comments in your program at the program stop to tell the operator what they are expected to do.

M6 vs M06 — Is the Zero Required?

No. M6 and M06 are the same command. The CNC controller reads them identically.

The leading zero is just a formatting convention. Textbooks and reference materials tend to use M06. In practice, plenty of programmers write M6 to save a keystroke.

If you’re working solo, use whichever format you prefer. If you’re in a shop environment, match whatever format is already in use — consistency makes programs easier to read and troubleshoot.

FAQS

Do I need to use M06 if I don’t have an automatic tool changer?

No. Without an ATC, M06 either does nothing or triggers a control alarm depending on your machine. Use M00 to stop the program and change tools manually instead.

Can I use M06 to switch to an empty spindle?

Yes. Call up a tool pocket that has no tool loaded in it, then run M06. The machine will swap in the empty holder, leaving the spindle bare. This is sometimes useful when you need the spindle out of the way.

What happens if I forget the T code before M06?

The machine will either pull whatever tool was previously staged in the ready position, or it won’t execute the change at all — behavior varies by control. Always include the T code on the same line or immediately before M06.

Does M06 stop the spindle automatically?

Yes. The spindle must be stopped for a tool change. Most controls will stop it automatically when M06 runs. In hand-coded programs, some programmers add an explicit M05 before M06 just to be safe.