What does the P CNC code do?
The P code is used for multiple uses in CNC programming.
Those uses depend on which other codes the P code is combined with.
The four uses of the P code are selecting a subprogram, setting a dwell time, selecting an offset number, and setting a scale factor.
The most common uses of the P code are to select a subprogram and to set dwell times.
Subprogram number when choosing a subprogram with M97 or M98
Subprograms are a separate CNC program selected to run from within another program.
They are used to perform repetitive machining operations or sequences such as drilling, counterboring and countersinking a hole.
CNC machines run the lines of code in a program in order.
Using subprograms allows the programmer to jump around to different sections of the current program or to run a different program and come back to the current program.
When used with either the M97 code or the M98 code, the P code tells the machine where to go run the subprogram.
This will either be a specific line in the current program (M97) or an entirely different program (M98).
Ready to master CNC programming?
Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!
The P code when using M97 [subprogram call by line]
M97 P1234 L5
This line of code will tell the machine to move to line N1234 of the current program.
The L code tells the CNC machine to run the subprogram five times. The L code can be left out if the subprogram will only be run once.
Line 1234 will be after the program end command (M30). Once the machine reaches the M99 command it will return to the line after the example shown above.
The P code when using M98 [subprogram call by program number]
M98 P5678 L2
This line of code will tell the machine to go run program number 5678 two times.
The L code tells the CNC machine to run the subprogram twice. The L code can be left out if the subprogram will only be run once.
Program 5678 will perform any necessary machining functions and then end with an M99 code instead of an M30 code.
The M99 command will make the machine return to the line after the example shown above.
On the other hand, both commands (M98 and M97) use parameters K (or L) as the number of repetitions of a given subprogram.
The use of the letter K or L depends on the CNC model. Always check the machine’s manual for further details.
Dwell time with G04
Another use of the P code is to set a dwell time.
A G04 code makes the cutting tool stop moving for specified amount of time. Following that amount of time, the machine will proceed to the next line of code.
There are some variations between machines when using G04.
Not all machines will use the P code to set the dwell time.
Some CNCs use either F, U or X instead of P.
Also, some machines will read the value after the P code as seconds and some will read it as milliseconds.
1 second = 1,000 milliseconds.
So P300 could mean 300 seconds (5 minutes) or 0.3 seconds depending on how your CNC reads the code. Makes sure to verify how you machine will read the P code when using the G04 command.
Dwell time with G82 and G89
G82 [drill cycle with dwell] and G89 [boring cycle with dwell] are both canned cycles with dwells.
Similar to the G04 command, G82 and G89 both use the P code to specify how long to stop and wait at the bottom of the hole.
Usually this time is in milliseconds. Machines vary though so make sure your machine isn’t reading the P code as full seconds.
Fixture offset number with G10
Using the P code with G10 tells the machine which fixture offset to change.
Below is a list of the values used with the P code and which fixture offset they correspond to:
Code | Fixture Offset |
G10 P1 | G54 |
G10 P2 | G55 |
G10 P3 | G56 |
G10 P4 | G57 |
G10 P5 | G58 |
G10 P6 | G59 |
Scale factor with G51
CNC programs can be scaled up or down using the G51 command.
The P code is used to set the scaling factor when used with G51.
This means that a P2 code will tell the machine to make everything in the program 2x larger.
On the opposite end, a P0.5 code will tell the machine to make everything half the normal size.
Conclusion
The P code can be used in many different ways when CNC programming.
Using the P code to select a subprogram is pretty much universal across the different CNC machine manufacturers.
The other uses of the P code are less standard.
At times the CNC will read the code different such as seconds vs. milliseconds or it will require a different code altogether such as in the case of dwell times with G04.
Make sure you check your individual machine to understand the requirements of your individual CNC.
Ready to master CNC programming?
Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!