~steelexodus
Useful FANUC G & M Codes
Posted on 2026-08-23
FANUC G-Codes and M-Codes I use from time to time
Introduction
Below you will find a list of M-Codes and G-Codes I use from time to time inside my programs on the CNC machines.
M-Codes
M99 - Restart Program
This will reset the program and start running from the top again. Useful for something that needs to keep executing, such as a machine warm-up program.
For example:
G28 U0. W0;
G97 S1000 M3 T0101;
M8;
G1 X10.;
M99;
This will home the turrent, start the spindle, turn on the coolant pump, cut to diameter 10, and restart from the top.
I know, stupid example, but you get the idea :)
NOTE: Make sure your panel supports this, as this is usually reserved for sub-program return.
M19 - Lock Chuck
Milling machines uses this to align the spindle for tool changes, but it can also be used on turning machines to lock the chuck in place. I use this in the case where I need to tighten the jaws with the long side of an alankey.
M01 - Optional Stop
After each tool block, I put a M01. This helps when the machine is being set-up.
I simply switch on the Optional Stop switch on the panel, and the tool will cut up until the point it reaches a M01, then it will stop
and wait for me to press cycle start again.
This is useful because if you know a tool is correct, you can let it run till completion and wait for you to be there again in the case you are busy with more than one thing.
G-Codes
G04 - Dwell
The one machine I work on requires that the tool does not start cutting 500ms after indexing, due to interlocks. I use this after each tool change to ensure that the program does not continue before that.
The code accepts the time to wait via the X value. For example, to wait 500ms, use:
G04 X0.5;
To make the machine wait for 1.25 seconds, use:
G04 X1.25;
You get the idea.
G28 - Zero Axis
Before the turrent is indexed on the turning machine, I like to return to the home position so there is ample space to rotate the turret. I use it like this:
N0303; <- Block number to get to tool quickly
G28 U0. W0.; <- Zero both axis at the same time
...
M5 M9; <- Stop the chuck and coolant pump
G28 U0. W0.; <- Ditto
M01;
Using U0. W0. in the same line causes the machine to return home on both axis. For the case where you need to return a single axis
only, use only one of the values, for example U0. returns the X to home, while W0. returns the Z to home.
Want to chat? Send me a mail to hello@steelexodus.com!