G Code To Include in Your CNC Program After It Ends

There are some G-code commands you might want your operators to occasionally execute but not for every cycle. Consider these examples.


Facebook Share Icon LinkedIn Share Icon Twitter Share Icon Share by EMail icon Print Icon
an operator facing a CNC screen

CNCs execute commands sequentially. When the cycle start button is pressed, the control will execute the program from start to finish, block by block. Most controllers allow the operator to determine the point from which a CNC will begin executing the program. If the program cursor is placed on the program’s first command, the CNC will begin executing from the beginning. If the operator places the cursor on the command that begins the third cutting tool, the CNC will start at the third tool.

Once the cycle start button is pressed, the CNC will continue executing program commands until it is told to stop. Commands that will cause the CNC to stop executing include M01 (optional stop), M00 (program stop) and M30 (end of program command). When stopped by M01 or M00, pressing the cycle start button will cause the CNC to continue executing from where it stopped. When the program ending command is executed, the CNC will stop and also return to the program’s beginning. When the cycle start button is eventually pressed again, the CNC will begin executing from the program’s first command.

If the special sequence commands are to be executed, the operator will have to scan to them, just as they would when rerunning a cutting tool.

If your CNCs behave in this manner, there are some helpful things you can include in your CNC programs after the program’s ending word (M30). In normal operation, the CNC will not “see” or execute these commands. If the special sequence commands are to be executed, the operator will have to scan to them, just as they would when rerunning a cutting tool. These commands are ones that you might want your operators to execute occasionally but not for every cycle. 

Consider the examples below. With a little thought, you can probably come up with others that are more appropriate to your specific needs.

Sending a Turning Center’s Axes to a Safe Starting Position

It may be important for the machine’s axes to be in a specific location when the program is run. Maybe obstructions (like the tailstock) could cause interference if the axes are not appropriately pre-positioned. Consider these commands:


O0001 (program number)

N005 T0101 (index to station one)

N010 G96 S500 M03 (start the spindle)

N015 G00 X1.7 Z0.1 M08 (move to first X/Z position, start coolant)

. (begin commands for the rest of the program)

N550 G00 Z0.1 M09 (retract last tool, stop coolant)

N555 X8.0 Z5.0 (move to safe index position)

N560 M30 (end of program command)

N999 (beginning of special program sequence)

G28 U0 (move to X-axis reference position)

G28 W0 (move to Z-axis reference position)

G00 W-12.0 (move Z axis to safe starting position)

M30 (returns execution to the program’s beginning)


Note that a parameter setting controls when the CNC stops loading a program. For this program to be loaded in its entirety, the parameter must be set to the end-of-file delimiter (%). Otherwise, the CNC will stop reading when the first M30 is read, and the N999 sequence will not be loaded.

The commands after the first M30 will not be executed during normal operation. Whenever the operator needs to send the machine to the safe starting position (after setup, beginning of shift, after stopping the program, etc.), they scan to sequence number N999 and run the program from there. When the CNC reaches the second M30, it will stop the program’s execution and return to the program’s first command. The N999 special sequence will not be executed again unless the operator scans to it.

If you elect to use this technique, use an easy-to-remember sequence number (like N999) as the special sequence starting command. Do so for every program in which you incorporate this technique so that operators can always scan to the same sequence number.

Including Program Zero Assignments (G10 Commands)

If you make qualified setups on machining centers, you probably include a series of G10 commands that enter values into related fixture offsets. These commands are probably being executed every time the program is run. If the operator needs to tweak a fixture offset value — and if they do so in the fixture offset itself — the adjustment will be overwritten the next time the program is run. This issue can be overcome by including the G10 commands in the special N999 sequence. The operator will scan to and execute the special sequence once during setup.

Special Setup Commands

The special sequence may be able to provide some help for setup people. Maybe a fixture hole or pin must be indicated to assign the program’s origin for a machining center program. You can include a rough positioning movement to the hole’s or pin’s X/Y center in the special sequence, like so:

N550 M30 (end of machining program)

N999 G91 G28 Z0 (send the Z axis to its reference position)

G28 X0 Y0 (send the X/Y axes to their reference positions)

G00 X-10.0376 Y-7.9037 (move over the hole/pin in XY)

M30 (end of special sequence)

Calculation Commands

Using custom macro techniques, you can allow an operator to perform certain job-specific calculations, like determining offset adjustment amount for sizing a thread after a measurement has been taken over pins.

Rework Commands

If workpiece rework is sometimes required after machining, include the related commands in a special N999 sequence. For instance, maybe the operator must polish or sand a turned diameter. The commands to start and stop the spindle could be included in the special sequence.