Thread milling on a CNC machine is a great way to cut threads that are accurate. You can cut either internal or external threads provided you have the correct thread milling tool. This is an example of such a tool. There are other types available. Two things are important, first the diameter of the cutting head must be smaller than the inside diameter of the hole you want to thread if cutting inside threads. The second factor is the reduced diameter of the tool must be long enough to reach the bottom of the thread you want to cut, whether inside or outside. This is a typical mill threading cutter.
I made a thread milling tool to accept carbide inserts. I chose a 16IRG60 insert since it was fairly small and could cut a reasonable range of thread pitches (8-14 TPI). The radius of this tool is about 0.48 inch, so it will not work for internal threads below about 1" NPT, but can be used on a variety of external pipe threads.
The concept is quite simple, the tool is installed in your mill spindle and spun at a speed appropriate to achieve the SFM that is correct for the material you are threading and your tool diameter. Threads may be cut from top to bottom, or from bottom to top. I prefer moving the tool to the bottom of the thread and cutting upward, since it prevents surprises like hitting your vice, or the bottom of a blind hole. Either right hand or left hand threads can be cut by reversing the direction of spiral. If you want to use climb cuts for threading, then the tool may have to be designed for that purpose and may need to be turned in reverse to achieve the desired cut.
The spinning tool is then moved toward the material to be threaded the amount required for proper thread depth, and then moved upward in a spiral until the top of the part is reached. This is fairly easy to implement for straight threads since they have a constant radius for the spiral path, but for tapered pipe threads, the radius is constantly changing to create the taper. Some CAM programs have this feature, but not all. For example I use BobCadCAM V27, and it only supports straight threads, not tapered. Newer versions may support tapered threads.
I created an Excel spreadsheet (having problems attaching file??, email me for spreadsheet) to solve this problem. The way it works is that the circular path is divided into a series of tiny straight lines, and the tool path is then composed of several hundred line segments that approximate a spiral of the desired taper. The spreadsheet is attached for those wanting to test the idea. There are multiple sheets in the file for different size pipe threads as well as an instruction sheet.
A blank part is mounted vertically in the milling machine, and the spindle is centered with the X-Y origin at the center of the part (hole or rod, depending on male or female thread).
For best results, the part should be tapered first to the correct angle, either internal or external. This can be done with a ball end mill, a corner rounded straight mill, or even with a square corner end mill if you can accept slight imperfections in the surface. Keep in mind that the end mill must have a smaller diameter than the starting hole for female threads. This video shows the taper being cut for a 1/2 NPT external pipe. The raw material was cut to the correct OD before insertion in the collet.
To use the spreadsheet, select a pipe size, then fill in the green cells with y or n to the questions of internal (n for external thread) and left hand (n for right hand thread). Enter the radius of the tool used for cutting the threads. This radius can be tweaked from actual size to cut deeper or less deep (decrease value for a deeper cut) if needed to fine tune the number of threads engaging an existing part. Enter the diameter of the end mill being used to cut the taper. All other cells on the spreadsheet are locked and the values are derived from thread charts.
The G-code is created in the yellow cells of the spreadsheet for both the taper and the thread when you enter the data into the green cells. Select all the cells, copy and paste into a text editor, then name the files with an extension your machine recognizes (e.g. half inch male external taper.nc and half inch male external thread.nc for Mach3). I create two separate programs since I do not have a tool changer and want to avoid confusion. Note that the files may have a thousand or more lines of code, and there is code in hidden cells, so it is best to select the top cell in yellow, then hold shift and click the bottom cell to select all the code.
This video show a 1/2-14 NPT external thread being milled in stainless steel.