Getting started with CNC spindle controls
The most basic codes to control your CNC spindle are:
But there is more to controlling your CNC spindle than simply turning it on and off.
For instance, most CNC machines have multiple ways to turn the spindle off and each way has it’s own use case.
Controlling the spindle speed is another function that has multiple modes. Choosing the wrong one could potentially be disastrous.
Learn the ins and outs of controlling your spindle below.
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!
Codes for turning your spindle on
There are two CNC commands for turning the spindle on, M03 and M04.
The S code is used together with these codes to set the speed of the spindle.
M03 [spindle on, clockwise]
The M03 code turns the spindle on in a clockwise direction at a set speed.
The speed is set with the most recent S code.
Clockwise rotation with M03 is the most common spindle rotation direction when CNC machining.
Clockwise rotation is used with right hand cutting tools.
M04 [spindle on, counterclockwise]
The M04 code turns the spindle on in a counterclockwise direction at a set speed.
Just like M03, the set speed is the value used with the most recent S code.
M04 is used with left hand cutting tools.
Counterclockwise spindle rotation is much less common than clockwise.
This is especially true for machining centers (mills). For lathes, M03 is still more common but M04 gets more use.
Codes for turning your spindle off
The M05 code is the only code that just turns the spindle off.
However, there are other codes listed below that turn off multiple machine functions and the spindle is included in those functions.
M05 [spindle off]
The M05 code is used to stop the spindle.
It is normally used when there is a tool change or other change in the machining process.
When the spindle is turned back on with an M03 or M04 code after being stopped, the spindle will return to the same speed it was previously set at with the last S code.
M00 [program stop]
The M00 code is called a program stop. It stops the current program in the middle of running.
Using the M00 code turns off the following functions:
- Movement of all axes
- Spindle rotation
- Coolant flow
- The machine pauses reading the program
When the M00 code is used, nothing is reset. In other words, any active modes or parameters will stay as they are.
The program is merely paused when it reads the M00 code.
The machine will resume reading the program when the cycle start button is pressed after the program stop has occurred.
One thing to keep in mind is that the machine doesn’t turn on functions when cycle start is pushed.
If the M00 code turns something off such as the spindle, it will need to be turned back on in the program.
M01 [optional stop]
The M01 code is called an optional stop.
It stops the current program in the middle of running, but only if the optional stop switch or button is on.
If the optional stop switch is not on, then the machine will ignore any M01 codes it reads and continue running the program like normal.
When the machine reads an M01 code and the optional stop switch is on, it will turn off the following functions:
- Movement of all axes
- Spindle rotation
- Coolant flow
- The machine pauses reading the program
When the M01 code is used, nothing is reset. Same as with the M00 code.
In other words, any active modes or parameters will stay as they are. The program is merely paused when it reads the M01 code.
The machine will resume reading the program when the cycle start button is pressed after the program stop has occurred.
One thing to keep in mind is that the machine doesn’t turn on functions when cycle start is pushed.
If the M01 code turns something off such as the spindle, it will need to be turned back on in the program.
M30 [program end]
The M30 code ends the program.
This makes it different from M00, M01 or M05.
M00 and M01 shut down various machine functions and pause the program.
M30 shuts down machine functions and ends it.
Pressing cycle start after an M30 code will start the entire program over again.
How to set spindle speeds
CNC spindle speeds are set using the S code.
S code
Using the S code sets the spindle speed that will be used whenever the spindle is turned on. The units are revolutions per minute (RPM).
The same spindle speed value will be maintained until it is changed.
Below is a small section of code with comments in parentheses that show what will happen as the machine reads the code.
M03 S3000 (Spindle On CW, Speed = 3,000 RPM)
M05 (Spindle Off)
M04 (Spindle On CCW, Speed = 3,000 RPM)
Notice how the spindle turns back on at the same set speed. This happens even if the spindle rotation direction is changed from clockwise to counterclockwise or vice versa.
S codes show up frequently in programs at the beginning of a new section of code. This can be switching tools or switching machining operations such as going from cutting the outside profile of the part to drilling holes.
Some other notes about the S code:
- Must be a whole number (no decimals such as 3000.5)
- Most machines have a limit to the range of spindle speeds that can be used. 1-9999 is common but 1-99999 is also used by some machines
Spindle speed modes
In CNC programming there are two codes that control the spindle speed mode, G96 and G97.
G97 is the most common and sets the spindle mode to RPM.
G96 sets the spindle mode to constant surface speed which adjusts the speed of the spindle based on the size of the part being machined.
G97 [speed in RPM]
When the G97 mode is active, the machine will maintain a constant spindle speed based on the RPM value set with the most recent S code.
If G97 is active, spindle speed will only change when the S code is changed.
G96 [constant surface speed]
When the G96 mode is active, the machine will maintain a constant surface speed.
Imagine a monster truck tire. Now picture rolling the monster truck tire one complete revolution in one minute. In that one minute, you might move 20 feet.
Now imagine you have a normal car tire. If you roll the car tire one revolution in one minute, you might only travel 4 feet.
You covered a lot less distance in that one minute, which means you traveled at a slower speed.
When things are spinning based on an RPM speed, the actual speed that the cutting edge of the tool is traveling can change.
Using constant surface speed with the G96 code tells the machine to change the spindle speed over time as the size of the part changes so that the speed where the cutting action is happening remains the same.
This is a simplified explanation of how G96 works. For more information about G96 see our full guide to the G96 command.
How to change the tool in your spindle
Most machining centers have automatic tool changers that can switch tools into and out of the CNC spindle.
The tool to be placed into the spindle is called up with the T code. This places the new tool in location so that it is ready to be switched.
The M06 command is then used to perform the tool change.
Here is an example section of code:
T01 (Tool 1 is readied for change)
M06 (Tool 1 is placed in the spindle)
T02 (Tool 2 is readied for change)
T03 (Tool 3 is readied for change)
M06 (Tool 3 is placed in the spindle)
Notice that even though tool 2 was readied for change, it never actually was put in the spindle. Instead, tool 3 was readied before the tool change occurred and it was placed in the spindle instead.
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!