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.

G49 G-Code: How to Cancel Tool Length Compensation

G49 cancels tool length compensation on your CNC machine. Once you call it, the machine stops accounting for any H offset. From that point on, it moves the spindle nose — not the tip of the cutting tool — to each programmed location.

You’ll almost always be running a tool. So G49 isn’t something most programmers use all the time. But it’s worth understanding so you know what it does and how to use it safely if you need it.

Key Takeaways

  • G49 cancels tool length compensation
  • After G49, the machine moves the spindle nose, not the tool tip
  • It’s rarely required — most programs never call it at all
  • Some machines immediately shift position when G49 is called, which can cause a crash
  • Tool length comp is also cancelled by M30, a control reset, or by calling H0
G49 – At A Glance
FunctionCancel tool length compensation
TypeModal (Group 8)
Cancelled byG43, G44
Related codesG43, G44, H code

What does G49 do?

G49 turns off tool length compensation. That means the machine no longer adjusts for the physical length of the cutting tool.

When you use a G43 or G44 code, you’re telling the machine: “This tool is a certain length — account for it when you move in Z.” G49 cancels that. The machine goes back to moving the raw spindle position with no offset applied.

an illustration of a cnc machine that shows how a g43 code offsets the program

What is tool length compensation?

Tool length compensation is how your CNC accounts for the fact that every cutting tool is a different length. You measure each tool and enter that value into the offset library as an H offset.

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

When you program G43 H02, you’re saying: “Turn on tool length compensation using offset number 2.” The machine reads that value and adjusts all Z moves accordingly.

mach 3 tool offset table
A tool offset library

This is important because your program is written to a specific reference point — usually the top of the part. Without tool length compensation, the machine has no idea how long the tool is and can’t hit the right depth.

Where do you actually see G49?

Most programs don’t call G49 at all.

You’ll almost always have a tool loaded, and you’ll almost always want the machine to account for that tool’s length. Canceling that compensation mid-program is rarely useful.

Some programmers include G49 in their safety block — a group of codes at the start of a program that resets the machine to a known state. Others skip it and just make sure the correct H offset is called whenever they change tools.

As long as you’re calling the right offset for the currently loaded tool, G49 is optional.

Code examplE

Here’s a typical tool change section where G49 would appear:

G49                      (Cancel tool length compensation)
G91 G28 Z0               (Return to machine home in Z)
M06 T02                  (Tool change to tool 2)
G90 G54 G00 X0 Y0        (Move to part zero)
G43 H02 Z1.0             (Apply tool 2 length offset, move to clearance)

G49 is called before the Z home move. That clears the old offset before the machine moves. Then G43 is called again with the new tool’s H value.

COMMON MISTAKE – Calling G49 while the tool is still near the part

On some controls, canceling tool length compensation causes an immediate Z-axis shift — because the machine moves to where the spindle nose would have been, not where the tool tip is.

If you’re anywhere near the workpiece, this can cause a crash. Always retract Z to a safe clearance height before calling G49.

Try it yourself

Tool Length Compensation — G43 & G49 Explainer

Tool Length Compensation

What changes when G43 and G49 are active

Spindle Nose ➤ Tool Tip (ignored) Z = 0 (part top) Tool Length
Spindle nose is the Z reference

Without TLC active, the machine moves the bottom of the spindle housing to every programmed Z position. It has no idea a tool is even installed.

Tool tip is ignored

No H offset is loaded, so the machine cannot account for the physical length of the cutting tool hanging below the spindle.

Z0 = top of the workpiece

Your program’s Z origin is the part surface — but the machine won’t move the right point there without compensation.

The problem: Program Z−0.500 to drill a half-inch hole and the machine moves the spindle nose to Z−0.500 — not the drill tip. You’ll drill the wrong depth every time, or crash on the first move.
H offset Spindle Nose Tool Tip (active) ➤ Z = 0 (part top)
Tool tip becomes the Z reference

G43 shifts the reference point down from the spindle nose to the tip of the cutting tool. Every Z move now targets the correct point.

H offset = tool length

You measure the tool, enter that value as the H offset, and the control adds it to every Z calculation automatically. One measurement, done.

Spindle nose is now secondary

The machine still tracks the spindle position internally, but it’s no longer what gets moved to your programmed Z values.

How to call it: G43 H02 Z1.0 — activates TLC using height offset #2 and rapids the tool tip to Z1.0 clearance. Stays active (modal) until cancelled by G49 or a tool change.
G43 H02 Z1.0 (TLC on — tool 2 offset, clear to Z1.0)
G01 Z-0.500 F10.0 (tool TIP drills to exactly Z-0.500)
Z = 0 (part top) Safe clearance H offset CANCELLED CRASH! Spindle Nose Tool Tip
G49 cancels the H offset

The machine drops the tool length value entirely. Spindle nose becomes the Z reference again, instantly.

Z shifts immediately on some machines

Some controls physically move the Z-axis the moment G49 executes — compensating for the now-missing offset value.

Always retract Z first

If the tool is anywhere near the part when G49 cancels and triggers that shift — the tool drives straight into the workpiece.

See it live: “Safe Sequence” shows the correct way. “Crash Demo” shows what happens when G49 is called without retracting first.
Ready
— Safe way —
G91 G28 Z0 (retract Z to machine home FIRST)
G49 (now safe to cancel TLC)
M06 T02 (tool change)
G43 H02 Z1.0 (new tool offset active)

Other CNC cancel commands

Code

Description

G40

Cancel cutter compensation

G49

Cancel tool length compensation

G50

Cancel scaling

G67

Cancel custom macro call

G69

Cancel rotation

G80

Cancel canned cycles

Codes that do the same thing

You don’t have to use G49 to cancel tool length compensation. These will also cancel it:

  • M30 — End of program / rewind also resets TLC
  • H0 — Calling a height offset of zero effectively cancels compensation
  • Control reset — Pressing reset on the control clears active offsets on most machines

If you’re switching tools, calling G43 H[new offset number] is the cleanest approach — you don’t need to cancel first.

FAQS

What does G49 do in CNC?

G49 cancels tool length compensation. After G49 is called, the machine moves the spindle nose to programmed positions instead of the cutting tool tip.

Do I need G49 in every program?

No. Most programs don’t use G49 at all. As long as you’re calling the correct H offset for each tool, there’s no need to cancel tool length compensation between tool changes.

What happens if I call G49 while the tool is near the part?

On some machines, calling G49 mid-program causes the Z-axis to shift immediately. This can result in a crash if you haven’t retracted to a safe height first. Always move Z to a clearance position before calling G49.

What’s the difference between G43 and G49?

G43 turns tool length compensation ON using a specified H offset. G49 turns it OFF. You’ll use G43 at the start of a tool path; G49 is used when you want to cancel that offset before a tool change or end of program.

G43 G-Code: Tool Length Compensation Explained

The G43 code turns on tool length compensation (TLC) on a CNC machining center. When G43 is active, the machine adjusts all Z-axis movements to account for the length of the cutting tool loaded in the spindle.

an illustration of a cnc machine that shows how a g43 code offsets the program

Without G43, the machine has no idea how long your tool is. It just moves the spindle nose to wherever the program tells it to go — which means your tool tip ends up somewhere you didn’t intend.

Key Takeaways

  • G43 activates positive tool length compensation
  • It works with an H offset value that stores the tool’s length in the machine’s memory
  • Best practice: match your tool number to your H offset number (Tool 1 → H1)
  • G43 is a modal command — it stays active until you cancel it with G49 or change tools
  • Always activate G43 while the tool is still a safe distance above the part
G43 – At A Glance
FunctionTool Length Compensation (Positive)
FormatG43 H__ Z__
TypeModal (Group 8)
Cancelled byG49, G49 H0

What Are H Offsets?

The G43 code turns compensation on, but the H code tells the machine how much to compensate.

Each tool gets measured and its length gets stored in the machine’s offset table — a list of values saved in the controller’s memory. The H number in your program points to that stored value.

mach 3 tool offset table
Example offset table/library

For example, G43 H02 tells the machine: “turn on tool length compensation using the length stored in offset #2.”

The standard practice is to match your tool number to your H number. Tool 1 uses H1, Tool 2 uses H2, and so on. It keeps things simple and reduces the chance of calling the wrong offset.

How G43 Works in Your Program

There are three parts to a G43 command: the G43 code itself, the H offset number, and a Z-axis move.

A typical G43 line looks like this:

G43 H02 Z1.0

This line does three things at once. It activates tool length compensation, loads the length value stored in H2, and moves the tool to Z1.0. The machine applies the offset on that first Z move.

After that line runs, every Z move in the program accounts for the tool length automatically — until you cancel it or change the offset.

When to Use G43 in Your Program

G43 should appear at the start of every tool’s section in your program.

Put it on the same line as your first Z move for that tool, while you’re still well above the part. A safe retract position or R-plane height is the right time to call it out.

Don’t wait until you’re close to the work. If G43 isn’t active yet and you’re already near the part, the offset gets applied mid-approach — that’s how crashes happen.

G43 is a modal command, meaning it stays on until you cancel it or change it. Even so, don’t assume it’s already active when you need it. Call it out explicitly in every tool section. This is part of good safety line practice.

COMMON MISTAKE – Turning on G43 Too Close to the Part

Activating G43 after the tool has already moved close to the part is a an accident waiting to happen. The offset kicks in on the next Z move — if you’re already near the work surface, that sudden position shift can crash the tool into the part.

Always call G43 while you’re still at a safe height.

How to Cancel G43

G49 cancels tool length compensation. It clears any active G43 or G44 mode.

You’ll usually see G49 in the safety block at the top of the program, before any tools load. This makes sure compensation from a previous run isn’t accidentally carried over.

Some shops also program G43 H0 instead of G49 — calling G43 with offset zero effectively zeroes out the compensation. Either approach works, but check what your shop or post processor prefers.

G43 vs G44

G43 and G44 are opposites. G43 applies positive length compensation — the offset value gets added to the machine’s position. G44 applies negative compensation — the value gets subtracted.

In practice, almost every shop uses G43. Tool length offsets are measured as positive values and stored that way. G44 exists for situations where a negative offset is intentional, but you’ll rarely encounter it.

If you’re just getting started, stick with G43 and don’t worry about G44 yet.

Related Cutter Compensation Codes

G43 handles the tool’s length — but there are also codes that handle the tool’s diameter.

G41 and G42 are cutter compensation codes. G41 offsets the tool path to the left of the programmed path (used in conventional milling). G42 offsets it to the right (used in climb milling). These adjust for the cutter’s radius, so your programmed path doesn’t need to account for it manually.

Where G43 compensates in Z, G41 and G42 compensate in X and Y. On most programs, you’ll use all three.

Like G43, both G41 and G42 are modal commands. They stay active until you cancel them with G40.

G43 Code Example

N100 T02 M6                  (Select and load Tool 2)
N105 G00 G43 H02 Z1.0        (Rapid to Z1.0, activate tool length comp with H2)
N110 X1.5 Y2.0               (Rapid to XY start position)
N115 G01 Z-0.5 F10.0         (Feed down to depth)
N120 G00 Z1.0                (Retract to safe height)
N125 G49                     (Cancel tool length compensation)
N130 M30                     (End program)

Line N105 is where G43 does its work. The machine loads the length stored in H2 and applies it as the tool moves to Z1.0. Every Z move after that automatically accounts for the tool’s length — until G49 cancels it on line N125.

FAQS

What happens if I forget to program G43?

The machine will move the spindle nose to the Z locations in the program — not the tool tip. This will almost always mean a crash.

Do I need a new G43 line for every tool change?

Yes. Each tool has its own H offset, so you need a new G43 command with the correct H number after every tool change. Don’t assume the previous tool’s offset carries over correctly.

What’s the difference between G43 and G44?

G43 applies positive tool length compensation — the offset value is added to the Z position. G44 applies negative compensation — the value is subtracted. Nearly all shops use G43 with positive offset values stored in the tool table.

Can I use G43 H0 instead of G49 to cancel compensation?

Yes. Programming G43 H0 loads a zero offset, which effectively cancels any active compensation. Many post processors use this method. Either G49 or G43 H0 will work — check what your machine and shop standard prefer.

G82 G-Code: Spot Drilling with Dwell at the Bottom of the Hole

G82 is a canned cycle that drills a hole in one straight pass and pauses at the bottom before retracting. That pause is called a dwell, and it’s the one thing that makes G82 different from a standard G81 drill cycle.

Key Takeaways

  • G82 drills a hole in a single pass and dwells (pauses) at the bottom before retracting
  • The dwell cleans up the bottom of the hole and improves surface finish
  • Use P to set the dwell time — units are usually milliseconds, but check your control
  • G82 is modal — it stays active until you cancel it with G80 or another canned cycle
  • For deep holes, use G83 instead — G82 has no peck capability
G82 – At A Glance
FunctionDrilling with dwell at bottom
FormatG82 X_ Y_ Z_ R_ P_ F_
TypeModal (Canned Cycle)
Cancelled byG80, or any other canned cycle
Used withG98, G99, G90/G91

What does G82 do?

G82 rapid-moves to the XY position, feeds down to the Z depth you specify, pauses for the dwell time you set, then retracts back to the R plane or initial point depending on whether G99 or G98 is active.

G82 Code Illustration

The dwell lets the tool spin in place at the bottom of the hole for a moment. This clears any remaining material and leaves a cleaner, more accurate hole bottom. It’s a small thing that makes a real difference on close-tolerance blind holes.

When should you use G82?

Use G82 when you need a cleaner bottom finish than G81 gives you, but the hole isn’t deep enough to require peck drilling.

illustration that shows the difference between peck drilling and standard drilling in a CNC machine

Common applications:

  • Spot drilling to accurately locate holes before drilling to full depth
  • Counterboring (creating a flat-bottomed step in a hole)
  • Shallow blind holes where bottom finish matters

If the hole is deep — generally more than 3–4 times the drill diameter — switch to G83. G82 drills in one shot with no chip clearing, so chip packing becomes a real problem in deeper holes.

G82 code format

G98 G82 X2.0 Y3.0 R0.1 Z-0.75 P500 F10.0

Here’s what each word means:

  • X2.0 Y3.0 — location of the hole in XY
  • R0.1 — the R plane (where the tool slows from rapid to feed rate, just above the part surface)
  • Z-0.75 — the bottom of the hole (negative = down into the part)
  • P500 — dwell time at the bottom (usually milliseconds — so P500 = 0.5 seconds)
  • F10.0 — feed rate in inches per minute
  • G98 — return to initial Z point after each hole

In practice, the XY coordinates often appear on their own line before the canned cycle call:

X2.0 Y3.0
G98 G82 R0.1 Z-0.75 P500 F10.0
X4.0 Y3.0
X6.0 Y3.0
G80

The machine positions at each XY, runs the canned cycle, then moves to the next one. Once G80 is called, the drilling stops.

COMMON MISTAKE – P Value Units

The P word sets dwell time, but the units depend on your machine.

Most Fanuc controls use milliseconds (P500 = 0.5 seconds). Some controls use seconds (P0.5). Always check your control’s parameter documentation before running the program. A P5000 when you meant P500 will just waste time — but getting it backwards on a tight-tolerance hole can affect finish quality.

Milliseconds to seconds converter

Result

G98 and G99: Where does the tool go after drilling?

After completing each hole, the machine needs to move to the next XY location. G98 and G99 control where the Z-axis goes between holes.

  • G98 — returns to the initial Z height (wherever the tool was before the canned cycle started)
  • G99 — returns to the R plane only (the reference plane just above the part)
visualization of how a cnc machine moves using g98 and g99 codes shows motion of travel for the machine

G98 is the safer default. It puts more air between the tool and the part when moving between holes, which matters if you have any clamps, fixture bolts, or features in the way.

G99 makes cycle times shorter because the tool doesn't travel as far. Use it when your setup is clean and open with nothing to crash into.

Positioning mode matters

G82 reads your X, Y, and Z values based on whatever positioning mode is active.

Most programs use G90. If G91 is active by mistake, your Z depth and XY positions will be completely wrong — and that's a crash waiting to happen. Double-check your positioning mode before running any canned cycle.

How to cancel G82

Use G80 to cancel G82 and all other canned cycles.

G80

If you don't cancel it, the machine will try to drill a hole at every XY move that follows. That's usually not what you want after your hole pattern is complete. Put G80 on its own line right after the last hole.

G82 vs Similar Canned cycles

CodeNameUse When
G81Standard drillingSimple through-holes, no finish requirement at bottom
G82Drilling with dwellBlind holes, counterbores, spot drilling with better finish
G83Peck drilling (full retract)Deep holes that need chip clearing
G73Peck drilling (chip break)Moderate-depth holes where cycle time matters

FAQS

What is the dwell in G82 and why does it matter?

The dwell is a short pause at the bottom of the hole while the spindle continues to spin. It allows the tool to clean up any remaining material before retracting, which improves the surface finish and accuracy of the hole bottom. It's especially useful on blind holes and counterbores.

What's the difference between G81 and G82?

G81 drills to depth and immediately retracts — no pause at the bottom. G82 is identical except it adds a dwell (P) at the bottom of the hole before retracting. If bottom finish matters, use G82. If you just need to drill through, G81 is faster.

Is G82 good for deep holes?

No. G82 has no peck capability — it goes straight to full depth in one pass. On deep holes, this causes chips to pack around the drill, which can break the tool or damage the hole. Use G83 (full retract peck drilling) or G73 (chip-break peck) for holes deeper than 3–4 times the drill diameter.

What does P500 mean in a G82 block?

On most Fanuc-style controls, P500 means 500 milliseconds, or half a second. The P word sets the dwell time at the bottom of the hole. Units vary by machine control — some use seconds, some use milliseconds. Check your control documentation to be sure.

G41 G-Code: Cutter Compensation Left Explained

G41 turns on cutter compensation left. It’s a modal command, which means it stays active until you turn it off.

When G41 is active, the CNC shifts the cutting tool to the left of the programmed path — relative to the direction the tool is moving. Instead of the center of the cutter following your coordinates, the edge of the cutter follows them.

Key Takeaways

  • G41 activates cutter compensation left — the tool shifts left relative to its direction of travel
  • Use G41 for climb milling, which is the standard in most CNC milling operations
  • The amount of shift comes from the D offset stored in the tool offset table
  • Always cancel G41 with G40, and make a move when you do — never cancel on the part
  • G41 lets you run the same program with different cutter sizes without rewriting coordinates
G41 – At A Glance
FunctionCutter Compensation Left
FormatG41 D[offset number]
TypeModal (Group 7)
Cancelled byG40
Related codesG40 (cancel), G42 (comp right), G43 (tool length comp)

What Does G41 Do?

G41 tells the CNC controller to shift the tool path to the left based on the diameter of the cutter. The shift amount is half the cutter diameter — the radius.

a cnc cutting tool with it's diameter, radius, and center point identified

Without cutter compensation, the controller moves the center of the tool to the programmed coordinates. That means the edge of the cutter is cutting half a diameter away from where you told it to go. For rough cuts this might be fine, but for precise contour work it will give you the wrong part size.

G41 fixes that. When it’s active, the edge of the cutter — not the center — follows your programmed path.

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

What is cutter compensation

Cutter compensation is a CNC mode that adjusts the tool path based on the size of the cutting tool.

Two cutters from the same manufacturer, even the same model, won’t be exactly the same diameter. They’re close, but not identical. Cutter compensation lets you store each tool’s actual diameter in the offset table and have the machine account for that difference automatically.

The direction of the shift is always relative to the direction the tool is moving. If you’re looking in the direction the tool travels, G41 shifts left, and G42 shifts right.

D Offsets and Cutter Size

G41 works with D offsets. A D offset stores the diameter of a specific cutting tool in the machine’s controller.

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

When you call G41 D1, you’re telling the machine: turn on cutter comp left, and use the diameter stored in offset number 1.

Even two identical endmills from the same box will have slight variations in diameter. Measuring each one and entering the actual diameter into the offset table is how you get consistent part sizes across tools.

The Tool Offset Table

The tool offset table (sometimes called the offset library or tool table) is where you store these values. Depending on your machine, it may store diameter offsets separately from length offsets, or combine them under the same offset number.

mach 3 tool offset table

Check your machine’s documentation to know which format yours uses. Some machines use D offsets for diameter only. Others store both diameter (D) and length (H) under a single offset number per tool.

The bottom line: the table tells the machine the physical size of each cutter. G41 reads from that table to know how far to shift the tool path.

G41 Programming Format

There are multiple ways of formatting aThe most common format is:

G41 D1

G41 turns on cutter compensation left. D1 tells the machine to use the diameter stored in offset number 1. On most Fanuc-style controls and industrial machining centers, this is the format you’ll use.

Two other formats exist, mostly for hobby or entry-level machines:

  • G41 P[radius] — directly specifies the tool radius as a number (e.g., G41 P0.25 for a 0.5″ diameter endmill)
  • G41 X[value] — same function as the P format on some controls

If you’re working on industrial equipment, learn the D offset format. That’s what you’ll use in a real shop.

G41 Example Program

Here’s a simple contour milling example using G41:

G90 G54 G00 X-1.0 Y0        (Rapid to approach position, outside the part)
G43 H01 Z0.1                 (Tool length comp, move to clearance height)
S1200 M03                    (Start spindle at 1200 RPM)
G01 Z-0.25 F10.0             (Plunge to depth)
G41 D01 X0 Y0 F15.0          (Turn on comp left, lead-in move to part)
X4.0                         (Mill along the part edge)
Y3.0
X0
Y0
G40 X-1.0                    (Cancel comp, lead-out move — off the part)
G00 Z1.0                     (Rapid to safe Z)
M05                          (Stop spindle)

Notice that G41 is turned on during a move (the lead-in), and G40 is also canceled during a move (the lead-out). Both moves are made away from the part. This is the correct way to use cutter compensation.

G41 vs G42

G41 and G42 are the two cutter compensation directions:

  • G41 — shifts left relative to tool travel direction (climb milling)
  • G42 — shifts right relative to tool travel direction (conventional milling)

Climb milling is standard on CNC machines. The cutter and the workpiece feed in the same direction, chips fall behind the cutter, and tool life is better. G41 is almost always the right choice.

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

Conventional milling (G42) cuts against the feed direction. The tool has more resistance, generates more heat, and wears faster. You’ll rarely program it intentionally, but you might encounter it in older G-code programs.

Canceling G41 with G40

G41 is canceled with G40. When you’re done with your comp operation, you need to turn it off correctly.

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

Two rules that matter:

1. Cancel cutter comp off the part. When G40 executes, the tool shifts back to center. If you cancel while the cutter is against the part, the tool will move into the workpiece. Cancel with at least half the cutter diameter of clearance.

2. Always make a move when canceling. Include a coordinate in the same block as G40 — or in the next block. Some controllers react unexpectedly if no motion is programmed when cutter comp is canceled. The machine has to know where to move..

COMMON MISTAKE – Canceling G41 while the cutter is still on the part

When cutter comp cancels, the tool shifts back to its center position — which means it moves into the part.

To avoid problems when canceling cutter comp, always lead out to a clear position before canceling. Cancel off the part and make a move when you cancel.

When to Use G41

G41 is used during climb milling, which is the standard milling method on CNC machines.

In climb milling, the cutter moves in the same direction as the feed. The chips fall behind the cutter, there’s less resistance against the tool, and you get a better surface finish with less tool wear. G41 positions the tool correctly for this cut.

G42 is used for conventional milling — where the cutter moves opposite to the feed direction. It’s rarely used in CNC work, but you’ll encounter it on some older programs.

Common operations that use G41:

  • Contouring (milling along a profile)
  • Pocketing
  • Facing
  • Engraving

Any operation where the cutter’s edge needs to follow a precise profile should have cutter compensation active.

Other types of compensation

Cutter compensation (G41/G42) handles the diameter of the tool. There’s also tool length compensation, which handles how long the tool is.

  • G43 — tool length compensation positive (most commonly used)
  • G49 — cancels tool length compensation

Almost every program you write will use G43 along with G41 or G42. The two work together: G43 sets the correct depth, G41 or G42 sets the correct diameter offset.

FAQS

What’s the difference between G41 and G42?

G41 shifts the tool to the left of its travel direction — used for climb milling, which is standard on CNC machines. G42 shifts right and is used for conventional milling, which is rare in CNC work.

What happens if I forget to cancel G41?

If cutter compensation stays active into a tool change or the end of the program, most controllers will alarm out. On some older machines, uncanceled comp can cause unexpected moves. Always cancel with G40 before a tool change or end of program.

Do I need to use cutter compensation on every operation?

Not always. Drilling, tapping, and boring cycles don’t need cutter comp — the tool cuts on its tip, not its side. Cutter comp is mainly for milling operations where the side of the cutter is cutting a profile or contour.

What is a D offset in G41?

A D offset stores the diameter of a cutting tool in the machine’s offset table. When you program G41 D1, the machine reads the diameter from offset 1 and uses it to calculate how far to shift the tool path.

G54–G59 Work Offsets: What They Do and How to Use Them

If you’ve ever watched a CNC machine start a program and wondered how it knows where your part is — that’s G54.

Before the machine can cut anything, it needs a reference point. That reference point is called a work offset, and G54 is the code that activates it. It’s one of the first codes you’ll see in almost every CNC program, and understanding it makes the rest of CNC programming click.

This page covers what G54 does, how it relates to G55 through G59, and why you’ll want it in every section of your program — not just the beginning.

Key Takeaways

  • G54 sets the work coordinate zero — it tells the machine where your part is
  • G54 through G59 are all work offsets; they work the same way
  • G54 is a modal command — it stays active until you change it
  • Most CNC programs include G54 at the start of every operation section, not just once
  • Always include the correct work offset in your safety lines to avoid machine crashes
G54 – At A Glance
FunctionWork coordinate system selection
TypeModal (Group 14)
Default offsetYes — G54 is the most common starting offset
Related codesG55, G56, G57, G58, G59, G53
Cancelled/changed byAnother work offset command (G55–G59) or G53

What Does G54 Do?

G54 tells the CNC machine where your part is located.

More specifically, it activates a stored coordinate location called a work offset — or work coordinate system (WCS). That stored location defines where zero is on your part. Once G54 is active, every X, Y, and Z move in your program is measured from that zero point.

Think of it like GPS coordinates you saved to a preset. When you call G54, the machine knows exactly where to go to find your part.

CNC program zero list showing XYZ coordinate locations
Work offset locations in your CNC

G54 is a modal command

Once you call G54, it stays active for the rest of the program — even if you restart.

That type of command is called a modal command. Modal means it stays on until you turn it off or switch to something else. G54 stays active until you call a different work offset like G55 or G56.

This is important to understand because it can cause problems if you’re not careful. If the wrong offset is active when the machine starts cutting, it will machine in the wrong location.

That’s why most CNC programs include safety lines at the start of each section. Safety lines reset all the key modes — including the work offset — so you always know what state the machine is in before it cuts.

G54 vs. G55–G59

G55, G56, G57, G58, and G59 all work exactly the same way as G54. They’re just additional slots for storing different zero locations.

Each one holds a different set of X, Y, and Z coordinates. You set those coordinates in the machine’s offset table, then call the matching code in your program.

Work offsets work like presets on your radio, except you store a location instead of a radio frequency. You can then call it up quickly and switch between them as needed.

Common reasons to use multiple work offsets:

  • Machining the same part in two different setups (Op 1 and Op 2 on the same fixture)
  • Running multiple parts on one fixture at the same time
  • Referencing different faces of the same part

G54 is used first because it’s the default — most programs only need one work offset, so G54 is the go-to.

The picture below shows how multiple work offsets can be set in a CNC and how they compare to the machine zero location.

visual to show cnc work offsets G54-G59 with the zero locations shown
Interactive

Work Offset Visualizer — G54 through G57

Click a part to see which offset it uses and why.

CNC MACHINE TABLE MACHINE ZERO G54 Single part · Op 1 G55 Same part · Op 2 G56 2nd part same fixture G57 3rd part same fixture
Click a part above to see its offset details

Offset values are examples. Your actual values depend on how the part is fixtured.

A G54 Code Example

Here’s what G54 looks like in a real program. This example drills holes, then counterbores them.

O1000                          (Program number)
T01 M06                        (Tool change - drill)
G90 G54 G00 X1.0 Y1.0         (Absolute mode, G54 offset, rapid to first hole)
G43 H01 Z1.0 M03 S800          (Tool length comp, spindle on)
G99 G81 Z-0.75 R0.1 F8.0      (Drill cycle)
X2.0 Y1.0                      (Next hole)
X3.0 Y1.0                      (Next hole)
G80 M05                        (Cancel canned cycle, spindle off)

T02 M06                        (Tool change - counterbore)
G90 G54 G00 X1.0 Y1.0         (G54 called again at start of new section)
G43 H02 Z1.0 M03 S600
G99 G82 Z-0.25 R0.1 P500 F5.0 (Counterbore cycle)
X2.0 Y1.0
X3.0 Y1.0
G80 M05
M30

Notice that G54 is called at the start of both the drilling section and the counterboring section — even though they use the same offset. That’s intentional. If you ever ran just the counterbore section by itself, the machine would still have the correct offset active.

COMMON MISTAKE – Only calling G54 at the start of the program.

If you ever run a single section of the code out of sequence — which happens more than you’d think — the machine picks up whatever offset was last active. That could be G55, G56, or something leftover from the previous job. Always include the work offset in the safety lines of each operation.

When to Use G54

G54 should appear at the start of every section of your program where a work offset matters — not just the beginning.

The most common place: right before your first positioning move after a tool change. That’s when the machine needs to know where it’s working.

A good rule of thumb: if you have a new tool change block, put a fresh safety line with G54 (or whichever offset applies) right after it.

Which Work Offset Is Used Most?

G54 is the most commonly used work offset. If your job only needs one setup and one zero location, G54 is what you’ll use.

G55 through G59 come into play when you need more than one zero point — multiple parts on a pallet, multi-op fixtures, or setups that reference different part features.

Most machines have at least G54–G59 as standard. Some controls offer extended work offsets beyond G59, but those six cover the vast majority of setups.

Related Codes Worth Knowing

G53 — Machine Coordinate System

G53 sends the machine to a location based on machine zero — the absolute home position of the machine itself, not your part.

It’s not modal. G53 only affects the single line it’s on, then the previous work offset takes back over.

You’ll typically see G53 used to send the spindle to a safe tool change position. Not all machines support it — and older controls may interpret it differently — so check your machine’s manual before relying on it.

G28 — Return to Machine Zero

G28 sends the machine through an intermediate point and then to machine zero in one or more axes.

It’s commonly used in the program header or at tool changes to get the spindle out of the way before the next operation.

How G10 Affects Work Offsets

G10 lets you change an offset value from inside the program, without going into the offset table manually.

The format looks like this:

G10 L2 P1 X1.5 Y2.3 Z3.0
  • L2 tells the control you’re setting a work offset
  • P1 specifies which offset (P1 = G54, P2 = G55, and so on)
  • X, Y, Z are the new coordinate values

G10 is an advanced feature. The format isn’t the same on every control, so always check your machine’s programming manual before using it. It’s not something beginners need to worry about right away.

FAQS

What is G54 in CNC programming?

G54 is a work offset command. It activates a stored coordinate location that tells the machine where your part’s zero point is. All moves in the program are measured from that location while G54 is active.

What is the difference between G54, G55, G56, G57, G58, and G59?

They all do the same thing — they each activate a different stored zero location. G54 is just the first one and the most commonly used. You’d use multiple offsets when you have more than one part or setup on the machine at the same time.

Does G54 need to be in every tool change block?

It’s good practice to include it at the start of every operation section, not just the first one. That way, if you ever run one section of the program on its own, the correct offset is always set before the machine starts cutting.

Is G54 the same on all CNC machines?

Yes — G54 through G59 are standard across Fanuc, Haas, Mazak, and most other CNC controls. The way you set the offset values in the machine’s offset table may vary slightly by control, but the G54–G59 commands work the same way.

G21 G-Code: How to Set Metric Mode on Your CNC Machine

G21 sets your CNC machine to metric mode. Once active, every coordinate, feed rate, and offset value in the program is read as millimeters.

If you’re working from a metric drawing — or running a program written in mm — G21 needs to be active.

Key Takeaways

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

What Does G21 Do?

G21 tells the CNC to read all values in millimeters. 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 every line. It stays in effect until something cancels or changes it.

One thing that trips up beginners: G21 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.

G21 vs G20: Metric vs Inch Mode

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

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 metric, keep it metric throughout. Mixing units causes calculation errors and can crash the machine.

⇄ 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

Where to Put G21 in Your Program

Most machinists place G21 (or G20) 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 a metric program:

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

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

You can also place G21 at the start of a specific section of code if that section uses metric values and the rest of the program doesn’t. But again — avoid mixing units in the same program if you can help it.

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.

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

COMMON MISTAKE – Running a Metric Program in Inch Mode

Running a metric program on a machine that’s still set to G20 (inch mode). A 25mm move becomes a 25-inch move.

The machine will crash or alarm out almost immediately — but always double-check your unit mode before hitting cycle start.

FAQS

Does G21 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 G21 (or G20) 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 G20 to G21 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 metric or all inch.

What happens to feed rates when I switch to G21?

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

Is G21 the same on all CNC controls?

G21 for metric 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.

P Code in CNC: Every Use Explained Simply

The P code doesn’t do just one thing in CNC. It does four different things depending on what code it’s paired with.

That’s what makes it confusing for beginners — and why it’s worth taking a few minutes to understand each use.

Key Takeaways

  • P is most often used to call a subprogram with M97 or M98
  • P also sets dwell time when used with G04, G82, or G89 With G10,
  • P selects which fixture offset to change
  • With G51, P sets a scale factor for the entire program
  • Always check your machine manual — P reads differently on different controls (seconds vs. milliseconds)
P Code – At A Glance
Primary useSubprogram selection (M97/M98)
Also used withG04, G82, G89, G10, G51
TypeNon-modal
Control variationsYes — check your machine manual

What Does the P Code Do?

The P code is an address word — it passes a value to another code that needs it. On its own, P does nothing. It only has meaning when combined with a G or M code.

The four main uses are:

  • Calling a subprogram (with M97 or M98)
  • Setting a dwell time (with G04, G82, or G89)
  • Selecting a fixture offset (with G10)
  • Setting a scale factor (with G51)

The most common use by far is subprograms.

P Code with M97 and M98 (Subprogram Calls)

Subprograms are separate blocks of CNC code that you can call from within a main program. They’re used for repeating operations — like drilling a hole pattern, or running the same chamfer at multiple locations.

The P code tells the machine where to find the subprogram.

P Code with M97 (Call by Line Number)

M97 calls a subprogram inside the current program. The P value is the line number (N number) where the subprogram starts.

M97 P1000 L3

This tells the machine: jump to line N1000 in the current program and run it 3 times. The L code sets the number of repetitions. If you leave L out, the subprogram runs once.

illustration that shows the flow of a cnc program when using the m97 command to call a subprogram

The subprogram sits below the main program end command (M30) and finishes with M99, which sends the machine back to the next line after the M97 call.

P Code with M98 (Call by Program Number)

M98 calls a completely separate program stored on the control. The P value is the program number.

M98 P5678 L2

This tells the machine: run program O5678 two times. Like M97, leaving out L means it runs once.

M98 Code Flow

Program O5678 ends with M99 instead of M30. M99 returns control to the main program.

A quick note: some controls use K instead of L for the number of repetitions. Check your machine’s manual if L isn’t working.

P Code with G04 (Dwell Time)

G04 pauses machine movement for a set amount of time. The P code sets how long.

G04 P500

This looks straightforward, but there’s a real catch.

COMMON MISTAKE – Seconds vs Milliseconds

Some CNC controls read the P value after G04 as seconds. Others read it as milliseconds. P500 could mean 500 seconds (over 8 minutes) on one machine, or 0.5 seconds on another. Always check your control’s manual before using G04 with P for the first time. Some machines also use F, X, or U instead of P for dwell time.

Not all machines use P for dwell. If G04 P isn’t working, try G04 F or G04 X — common alternatives on older or Fanuc-based controls.

P Code with G82 and G89 (Canned Cycle Dwell)

G82 is the spot drill cycle with dwell. G89 is the boring cycle with dwell. Both use P to set how long the tool pauses at the bottom of the hole.

G82 X1.0 Y1.0 Z-0.5 R0.1 P300 F10.0

The pause at the bottom lets the tool dwell briefly, which cleans up the hole bottom and improves surface finish.

As with G04, the P value is usually in milliseconds on most modern controls — but confirm this for your specific machine. P300 is typically 0.3 seconds, not 5 minutes.

P Code with G10 (Fixture Offset Selection)

G10 is used to set work offsets programmatically — directly in the G-code, without going to the offset screen. The P code tells the machine which fixture offset you’re changing.

Here’s how the P numbers map to work coordinate systems:

Code

Fixture Offset

G10 P1

G54

G10 P2

G55

G10 P3

G56

G10 P4

G57

G10 P5

G58

G10 P6

G59

Example:

G10 L2 P1 X5.0 Y3.0 Z0.0

This sets the G54 offset X to 5.0, Y to 3.0, and Z to 0. The L2 tells the machine you’re setting a work coordinate offset (as opposed to a tool offset).

G10 is handy in automated setups where offsets change between parts or pallets.

P Code with G51 (Scaling)

G51 scales a CNC program up or down. The P code sets the scale factor.

G51 X0. Y0. P2.0

A P value of 2.0 doubles the size of everything in the program. A P value of 0.5 cuts everything in half.

This is used mainly in special applications — mirroring, family-of-parts programming, or teaching scenarios. It’s not commonly used in everyday production machining.

Cancel scaling with G50 when you’re done.

Conclusion

The P code is a multi-use address word. Its meaning depends entirely on what it’s paired with.

Subprogram calls with M97 and M98 are the most common use, and those are fairly consistent across controls. The other uses — especially dwell time with G04 — can behave differently from machine to machine.

When in doubt, check the manual for your specific control before running a new program with P.

FAQS

What does P mean in CNC programming?

P is an address word used to pass a value to another code. Its meaning changes depending on what G or M code it’s combined with. The most common use is selecting a subprogram number with M97 or M98.

Does G04 P read in seconds or milliseconds?

It depends on your CNC control. Some read P as milliseconds (P1000 = 1 second), others as full seconds (P1 = 1 second). Always check your machine manual before using G04 P for the first time.

What’s the difference between M97 P and M98 P?

M97 P jumps to a line number (N number) within the current program. M98 P calls a completely separate program stored on the control by program number. Both return to the main program when M99 is reached.

Can I use P without any other code?

No. P doesn’t do anything on its own. It only has meaning when combined with another code like M97, M98, G04, G10, or G51.