Posted in

What is the G-code for thread milling?

If you’ve ever watched a CNC machine carve out a threaded hole on a workpiece—clean, precise, and tailored to a specific thread pitch and diameter—you’ve likely witnessed the result of thread milling. As a CNC thread milling supplier, I get asked this question at least a dozen times a week: “What is the G-code for thread milling, and how does it actually work?” It’s a fair question, because thread milling G-code isn’t just a standard line of code; it’s a specialized sequence of commands that turns a basic milling machine into a tool capable of creating internal, external, and even custom-shaped threads that taps and dies can’t handle. Let’s break this down, from the core purpose of thread milling G-code to a practical example, and why it matters for anyone working with tight-tolerance parts. CNC Thread Milling

First, let’s start with the basics: what thread milling is, and why G-code is non-negotiable here. Unlike tapping, which uses a single tool that cuts a thread as it feeds straight into a pre-drilled hole, thread milling uses a rotating end mill (our company specializes in solid carbide thread mills designed for this exact process) that moves in a helical path through the workpiece. This helical motion is dictated entirely by G-code: the programming language that tells CNC machines where to move, how fast, and when to change tools. Without the right G-code sequence, the machine would just spin the tool in a circle, not cut a functional thread.

Thread milling G-code isn’t one-size-fits-all, either. It varies based on whether you’re making an internal thread (inside a hole), an external thread (on a bolt or rod), or a tapered thread (like NPT pipes that need to seal tightly). It also depends on the thread’s specifications: pitch, major diameter, minor diameter, and the number of starts (for multi-threaded fasteners). For example, a 10-32 internal thread (common in aerospace and automotive parts) requires a very different G-code sequence than a 1-inch NPT tapered pipe thread used in plumbing or hydraulic systems.

Let’s get into the key G codes that make thread milling work, because these are the commands that form the backbone of any thread milling program. Most thread milling uses three core G codes, plus a few supporting ones:

  1. G01: Linear interpolation, which moves the tool in a straight line. This is used to position the tool before the helical thread starts.
  2. G02/G03: Circular interpolation (clockwise/counterclockwise), which creates the circular profile of the thread.
  3. G02/G03 with a helical axis offset (usually Z-axis), which combines circular movement with linear movement along the Z-axis to create the thread’s helical path.
  4. G20/G21: Unit selection (inches or millimeters), critical for ensuring the thread dimensions match your part’s requirements.
  5. G90/G91: Absolute vs. incremental positioning, which determines whether coordinates are measured from the machine’s origin or the previous tool position.

Now, let’s walk through a practical example of an internal thread milling G-code program, since this is the most common application our customers use. Let’s say we’re milling a 10-32 UNF internal thread in a 0.5-inch diameter pre-drilled hole in aluminum. 10-32 means the thread is 0.190 inches in diameter, with 32 threads per inch. The thread mill we’d recommend for this is a 3-flute solid carbide thread mill with a 0.125-inch diameter shank, designed for aluminum to avoid chip buildup.

Here’s a simplified version of the G-code for this job:
O0012 (Program for 10-32 UNF internal thread, aluminum)
G20 G90 G54 (Set units to inches, absolute positioning, use work offset G54)
S1500 M03 (Spindle speed 1500 RPM, clockwise rotation)
G00 X0 Y0 Z0.1 (Rapid move to position above the hole, at Z 0.1 inches)
G00 X0 Y0.095 (Move tool to the center of the hole; 0.095 is half of the pre-drilled hole’s 0.19-inch diameter)
G01 Z0 F10 (Feed down to Z 0, feed rate 10 inches per minute)
#1=0.19 (Thread major diameter, 10-32 UNF)
#2=32 (Threads per inch)
#3=0.015 (Step depth per pass, to avoid tool breakage in aluminum)
#4=0 (Current Z position, start at Z 0)
(Loop to cut each thread pass)
WHILE [#4 LT -0.32] DO1 (Cutting 0.32 inches deep, standard for 10-32 thread engagement)
#4=#4 – #3 (Decrement Z by step depth)
G02 X[#1/2] Y0 Z#4 I[#1/2] J0 F15 (Helical clockwise move: X to half the major diameter, Y stays 0, Z moves to current depth, I is the X offset from the center)
END1
G00 Z0.1 (Retract tool back to safe Z height)
M05 (Spindle stop)
M30 (Program end)

Wait a second—why the loop? Thread milling uses multiple light cuts instead of one deep cut, which reduces tool wear and produces a smoother thread. That’s a key advantage over tapping, which can work-harden the material if done wrong. As a supplier, we always recommend that our customers use step depths between 0.01 and 0.03 inches per pass for aluminum, and 0.005 to 0.015 inches for steel or titanium, because those harder materials can’t handle as much cut force without breaking the tool.

Now, let’s address a common misconception: G-code for thread milling isn’t just a “copy-paste” job. Even if you use a standard thread formula, you have to adjust it based on your machine’s rigidity, the tool you’re using, and the material. For example, a customer once came to us with a thread milling G-code that kept breaking their 0.125-inch tool every time they tried to machine a 1-inch internal thread in 4140 steel. When we looked at their G-code, we saw they were using a single 0.05-inch deep cut, which was way too aggressive for a small tool in hard steel. We adjusted their G-code to use 0.01-inch step depths over 3 passes, and paired it with a more robust solid carbide thread mill, and their breakage rate dropped to zero. That’s the value of working with a supplier who understands both the G-code and the hardware to make it work.

Another thing to note is external thread milling G-code, which follows a similar logic but reverses the tool path. For an external 3/8-16 UNC thread, you’d program a circular path around the outside of a workpiece, with the Z-axis moving opposite to the internal thread path. This is useful for long parts where tapping would be impossible, or for custom threads that don’t fit standard tap/die sizes. Tapered threads, like NPT, add an extra angle to the Z-axis movement in G-code, so the thread gets wider or narrower as it goes deeper, creating the seal needed for pipes.

What about custom threads? A lot of our customers work with OEMs that need non-standard thread sizes, like metric fine threads or special pitch threads for medical devices or aerospace components. G-code for these is just a matter of inputting the right diameter, pitch, and depth values into the helical path. Unlike taps and dies, which only come in standard sizes, thread mills can be programmed for any custom thread, which is why so many manufacturers are switching to thread milling over traditional tapping.

Now, let’s talk about common mistakes people make with thread milling G-code. The first is incorrect pitch calculation. The helical step for the Z-axis should be exactly equal to the thread’s pitch. For example, a 32-thread-per-inch thread needs a Z movement of 1/32 inches per revolution of the tool. If that Z step is even slightly off, the thread will be misaligned and won’t mate with a fastener. The second mistake is wrong tool positioning: if the tool isn’t centered in the hole for internal threads, you’ll get a thread that’s off-center, leading to loose fasteners. The third is feed rate and spindle speed that’s too high. This causes tool deflection, which makes the thread diameter inconsistent. That’s why our team works with customers to not only provide the right thread mills, but also to help refine their G-code to match their machine’s capabilities.

At the end of the day, thread milling G-code is a bridge between the tool and the part. It’s what turns a programmed design into a functional thread, whether it’s for a small medical device bracket or a large hydraulic valve component. If you’re looking to integrate thread milling into your production, or you’re having trouble with your current G-code leading to broken tools or bad threads, our team is here to help refine your processes and supply the tools you need to succeed. Reach out to discuss your specific thread milling needs, from custom G-code programming support to high-quality solid carbide thread mills for any material.

Circular Mill References

  1. Smid, P. (2007). CNC Programming Handbook. Industrial Press Inc.
  2. DeGarmo, E. P., Black, J. T., & Kohser, R. A. (2011). Materials and Processes in Manufacturing. John Wiley & Sons.
  3. Haas Automation. (2022). CNC Programming Manual. Haas Automation, Inc.
  4. OSG Tap & Die. (2021). Thread Milling Best Practices Guide. OSG Tap & Die, Inc.
  5. Machiningdata.com. (2023). Thread Milling G-Code Fundamentals. Machiningdata.com.

Shangqiu JDA Machinery Technology Co., Ltd.
As one of the most professional cnc thread milling manufacturers and suppliers in China, our products have good reputation in the market. Please rest assured to buy cheap cnc thread milling for sale here from our factory. Quality products and reasonable price are available.
Address: No.55 Pingyuan Middle Road, Liangyuan District, Shangqiu, Henan, China
E-mail: sales@jdamachines.com
WebSite: https://www.jda-machines.com/