A probe stylus costs more than a fancy dinner, and a single bad feed rate can snap it in half. The G31 CNC code controls how your machine touches a surface and stops — and when you write it carelessly, you damage the probe or miss the measurement entirely. Done right, G31 turns your CNC machine into a measuring instrument that sets datums from the real part, not from a guess about where the fixture holds it.
This guide is for CNC programmers, machinists, and manufacturing engineers who want to use G31 with confidence. You’ll learn exactly what the skip function does, how to write it Fanuc-style, which system variables hold the contact position, real probing examples, when to reach for G31, and the mistakes that wreck styluses and scrap parts. We’ll also cover where Fanuc and Haas part ways, so you can pick the right approach for your machine.
What the G31 CNC Code Actually Does
G31 is the skip function. It commands a feed move that stops the instant a probe signal fires. The machine feeds along a path you define, and the moment the probe touches a surface — or any input trips — motion halts and the control records the exact position where contact happened.
That captured position is the whole point. Most G-code moves run until they reach a target. G31 is different: it may stop anywhere along the path, wherever the surface actually sits. The control then stores those coordinates so you can use them in your program.
This behavior is the foundation of on-machine probing. With G31, you measure part surfaces, find edges, locate rough stock, and set work offsets based on the physical part in front of you. No more assuming the fixture put the material exactly where the drawing says.
G31 Syntax and Format
The format is short and looks a lot like a standard feed move. The difference is what happens when the probe trips. A basic Fanuc-style probing move reads:
G31 Z-50.0 F50.0
Breaking it down:
G31triggers the skip move.Z-50.0Is the target the machine feeds toward?F50.0sets the probing feedrate — keep it slow for accuracy and probe safety.
The key detail: if the probe trips at, say, Z-12.3, the move ends right there. The axis never reaches Z-50.0. That target is simply a limit — a safe endpoint in case the probe never makes contact. Always pick a target that gives the probe room to trip but stops the axis before it overtravels into something solid.
System Variables: Where the Contact Position Lands
When the probe trips, the control captures the machine position at the moment of contact and writes it into system variables. On most Fanuc controls, the skip position lands here:
#5061— X-axis position at contact#5062— Y-axis position at contact#5063— Z-axis position at contact
These hold the coordinates from the last G31 move. To use them, copy the value into a local or common variable right after the probing line, before the next G31 overwrites it:
G31 Z-50.0 F50.0 #100 = #5063
Here, the probe feeds down, trips on the surface, and the Z contact position drops into #5063. We immediately copy it into #100 so we can use it later in macro logic — for example, to write a work offset or compare against a tolerance.
One word of caution: the exact variable numbers depend on your control and how the builder configured the skip and macro system. Some setups also offer variables that report the position in the work coordinate system rather than machine coordinates. Confirm the right numbers for your machine before you trust them.
Practical Program Examples
Probing a Top Surface and Capturing Z Height
A common task is finding the real top of a part. Feed the probe down, capture the height, then retract:
G90 G54 G00 X0 Y0 G43 H20 Z25.0 G31 Z-30.0 F50.0 #101 = #5063 G91 G28 Z0 M30
Walk through what happens:
- The machine positions itself over the probing point and lowers the probe to a safe start height.
- G31 feeds the probe down at a slow 50 mm/min. On contact, motion stops.
- The contact height lands in
#5063, and we copy it into#101. - The Z axis retracts home, clearing the probe off the surface.
Writing the Probed Value into a Work Offset
The real payoff comes when you feed the measured value straight into a datum. Probe the surface, then write that height into G54:
G90 G31 Z-30.0 F50.0 #101 = #5063 G10 L2 P1 Z#101 G91 G28 Z0
The probe finds the real surface, #5063 captures it, and G10 L2 P1 Z#101 writes that exact value into the G54 work offset. No manual entry, no rounding, no operator guesswork. The datum now reflects the physical part.
When to Use G31
G31 earns its keep anywhere you need to measure with the machine rather than assume. The clearest cases:
- Set work offsets relative to the actual part: Probe a face or edge, then write the result to G54 with G10. The datum matches reality.
- In-process inspection: Check a feature mid-cycle and branch your program if it’s out of tolerance — rework, alarm, or skip a pass.
- Finding rough stock surfaces: Locate where casting or forging stock actually sits before you commit a cutting pass.
- Repeatable multi-part setups: Probe each part within a fixture grid and automatically set individual offsets.
The common thread is trust in the real geometry. Whenever a fixed assumption about part position risks a scrapped part or a crash, G31 lets the machine find the truth first.
Common G31 Mistakes and How to Avoid Them
Most G31 problems trace back to a handful of repeat offenders. Learn these, and you’ll protect both your probe and your parts.
Feedrate Too High
This is the expensive one. A fast G31 feed rate overshoots the trip point before the axis can decelerate, damaging the probe stylus and corrupting the reading. Probes need slow, consistent feeds — often in the range of a few tens of millimeters per minute for the final touch. Keep your G31 feed rate slow and steady every time.
No Retract After Contact
G31 stops at the trip point, but it does not pull away. Leave it there, and the probe stays loaded against the surface, which can deflect the stylus or block the next move. Always program a retract — like G91 G28 Z0 or a small clearance move — immediately after you capture the position.
Ignoring Missed-Contact Overtravel
If the probe never trips before reaching the target, the machine keeps feeding right up to that endpoint. Pick a target that stops the axis before it drives the probe into the part or fixture. For critical routines, add macro logic to verify that the contact actually occurred and alarm out if it didn’t.
Reading the Wrong System Variables
The correct skip-position variables differ by control and macro configuration. Grab the wrong variable, and you’ll write a stale or meaningless value into your offset. Verify the right variable numbers for your machine, and copy the result into your own variable right after the G31 line, before anything overwrites it.
Fanuc vs Haas: Key Differences
Both Fanuc and Haas support G31 as the standard skip function, and the core idea — feed until contact, then stop and store the position — works the same way on each. On Fanuc, the contact position lands in #5061–#5063, and you build your own macro logic around those variables.
Haas also accepts G31, but most Haas probing happens through the builder’s own routines. If your Haas runs a Renishaw or Blum probe, you’ll usually rely on intuitive probing cycles and Haas macro variables rather than raw G31. These canned routines handle the feed, retract, and offset writing for you, removing much of the manual macro work.
Which Approach to Choose
- Raw G31 with macros (Fanuc-style): Best when you need full control over the probing logic, custom inspection routines, or non-standard moves.
- Builder probing cycles (Haas-style): Best for common tasks — edge finding, surface measurement, bore centering — where a tested canned cycle is faster and safer than writing your own.
The practical takeaway: know that G31 is the engine underneath probing on both platforms, but reach for the builder’s probing macros when they cover your task. Save raw G31 for the routines that those cycles can’t handle.
Key Takeaways
- G31 is the skip function — it feeds until the probe trips, then stops and records the contact position.
- Contact coordinates land in system variables (
#5061–#5063on Fanuc); copy them out right after the G31 line. - Keep the feed rate slow to protect the stylus and get an accurate reading.
- Always retract after contact, and set a safe target to handle overtravel from missed contact.
- On Haas, lean on builder-probing cycles for common tasks and save raw G31 for custom routines.
Master G31 and your machine stop guessing about part position and start measuring it. Begin with a simple surface-probing routine, confirm the captured value matches a manual touch-off, then build toward automated datum setting with G10.
G31 is one piece of a larger setup workflow. To see how it works alongside programmable offsets and reference return, read the full pillar guide: G10, G28, and G31 CNC Codes: The Complete Guide to Offsets, Reference Return, and Probing →
Need Precision CNC Machining You Can Trust?
Knowing the code is one thing. Turning it into accurate, repeatable parts is another. At Essengold, we combine deep programming expertise with tight process control — including on-machine probing and inspection — to deliver components that meet your specs every time. From rapid prototypes to full production runs, our team handles the probing, datums, and offsets so you don’t have to.
Get a quote on your CNC machining project today → and see how precise, reliable manufacturing keeps your project moving forward.


