Spindle Control with a MOSFET

If you are not using a VFD, and your spindle motor is a DC motor of low enough power, you can control it directly using the MOSFETs on the Smoothieboard.

The power must be low enough in both voltage and current, including any current extremes when stalled.

Overview

MOSFETs are present on-board, but have limited current capacity (up to 12A).

They need to be protected by a ‘flyback’ diode when controlling a motor, electromechanical relay or other inductive load.

For more information, see:

Motors have the property of inductance, which is the electrical equivalent of inertia.

The motor current will continue to flow after the MOSFET is switched off, because the energy stored in the inductor by the current does not instantly disappear.

The current will decrease as the stored energy is dissipated.

This current flow results in the voltage across the motor reversing and increasing (or “spiking”) until the current finds a path to flow.

An appropriately connected ‘flyback’ diode with sufficient voltage and current ratings will provide a safe, intended path for the motor current.

Without a diode, when the MOSFET turns off the motor voltage will spike up until the current finds an unintended path.

Typically this degrades or immediately destroys the MOSFET and perhaps other circuitry such as that driving the MOSFET gate.

Connect the diode in reverse across the motor (when the MOSFET is on, it must NOT cause power to flow through the diode).

Connect the anode (‘arrow’) to the MOSFET Drain terminal and the cathode (‘bar’, often marked by a line on the package) to the motor power supply.

The diode rating should be rated with Voltage exceeding the maximum expected supply voltage by at least 20% (usually low cost to get a higher voltage rating).

And (conservatively) Current at least equal to half the motor current (the current through the diode will not be continuous).

An example part number is 6A4-T, rated 6A and 400V, under $1 USD.

The same considerations apply to other inductive loads such as solenoid valves and relay coils.

Solid state relays are controlled via a GPIO pin, and can control higher loads.

But are on/off only (no control of the exact amount of power sent via PWM).

For more information about SSRs, see this appendix.

Critical Warning: It is critical that you add a diode across your motor if you are going to control it with a Smoothieboard. If you do not do this, you are absolutely guaranteed to destroy the MOSFET, and possibly the Smoothieboard.

Important Note

A MOSFET cannot provide power if it is not provided power via an input.

Make sure you properly connect both the input power and output connections.

MOSFETs

MOSFETs

Warning Safety Warning: If it will burn your skin, don't touch it. Simple.

Smoothie has up to 6 MOSFET controls (6 on 5X, 4 on 4X, and 2 on 3X).

The MOSFETs act as switches to ground: loads must be connected between the power source and the MOSFET switched terminal.

When the MOSFET is switched on, power will be applied to the load.

When the MOSFET is switched off, power will be removed, because one load terminal will be essentially disconnected and current cannot flow.

The exception being inductive load ‘flyback’ switching transients, discussed above.

Connect your PSU to the power input connector for those FETs (providing power to the load), and connect your power-consuming element (be it heating element, spindle, etc.) between the power output terminal and the MOSFET terminal.

Smoothie connects/disconnects the element’s ground as needed to maintain temperature or as requested by G-codes.

There are three main pairs of MOSFETs on the board:

  • Big MOSFET pair: Their outputs are labeled P2_7 and P2_5 on the schematic, the input connector for them is found between them. They are found on the 4X and 5X boards. To power those MOSFETs, you need to provide them with power by wiring their power input to the power supply.

  • Small MOSFET pair: Their outputs are labeled P2_6 and P2_4 on the schematic, the input connector for them is found by their side, between P2_6 and P1_23. They are found on all of the boards. To power those MOSFETs, you need to provide them with power by wiring their power input to the power supply.

  • Mixed MOSFET pair: Their outputs are labeled P1_22 and P1_23 on the schematic. The pair is called “mixed” because it consists of one big MOSFET and one small MOSFET. They do not have a specific input, they take power directly from VBB (the Stepper motors power input described in the Stepper Motors chapter). To power those MOSFETs, you need to provide them with power by wiring their power input (which is the same as the one for the stepper motors) to the power supply.

Multiple Power Inputs: Contrary to other boards, Smoothieboard does not have a single power input, but multiple power inputs.

This allows you to use different voltages for different things if you want, and makes it easier to use more current as the current is shared between more connectors. It does mean wiring one or two more connectors though.

If you are trying to control MOSFETs and they are not turning on, make sure you provided power to their power input.

MOSFETs list:

MOSFET group MOSFET name Controlling pin Output connector Input method Voltage Current
Big MOSFETs First big MOSFET 2.7 X15 Big MOSFETs power input X13 12-24V 12.5A max
Big MOSFETs Second big MOSFET 2.5 X10 Big MOSFETs power input X13 12-24V 12.5A max
Small MOSFETs First small MOSFET 2.4 X7 Small MOSFETs power input X6 12-24V 3A max
Small MOSFETs Second small MOSFET 2.6 X8 Small MOSFETs power input X6 12-24V 3A max
Mixed MOSFETs Third big MOSFET 1.23 X16 VBB (motor) input 12-24V 12.5A max
Mixed MOSFETs Third small MOSFET 1.22 X9 VBB (motor) input 12-24V 3A max

MOSFETs diagram

MOSFET Input Output
Polarity Important: MOSFET power inputs have a polarity, make sure you connect + on that connector to + on your PSU, and - to - on the PSU.

Heater elements, however, do not have a polarity, so you do not have to worry about polarity on the outputs.

If you are using another output element like a Peltier or a Spindle, you need to be careful to respect the polarity for the outputs too.

Never use the big MOSFETS for more than 12.5A (and monitor connector and MOSFET temperatures at that current use, too much heating can be a sign of a bad wire connection), and the small MOSFETS should never be used for more than 3A.

Trying to power a 40W (or more) hotend cartridge heater at 12V with the small FETs will destroy them, usually locking (melting) them to the “ON” state (shorted) and possibly destroying the circuitry driving the MOSFET gate.

If you need to control more than 12 Amps, you cannot do it with one of the MOSFETS on board, however, you can use a Solid State Relay.

For information see the Solid State Relay Appendix on this page.

Alternative Power Method: In the case of both the Big MOSFETS pair and the Small MOSFETS pair, you take power from the PSU (Power Supply Unit) to them via their respective power input connectors.

There is an alternative, however (for currents up to 2 Amps or 4 Amps). For each pair, you can use jumpers (one jumper for the small MOSFETS pair (JP28), two parallel jumpers for the two big MOSFETS pair (JP11 and JP27)).

If you solder the pins for those OR connect a jumper to those pins, closing the circuit to VBB (the stepper motors power input), allowing you to take the power from those MOSFETS from the same place as the stepper motors do.

In the case of the big MOSFETS, you have to solder and put in place two jumpers, in parallel, in order to handle more current.
Jumper Current Limitation: However, WARNING, each jumper is rated for only 2A of current.

This means you cannot use this way of powering your MOSFETS if you are going to use more than 2A (for the small MOSFETS) or 4A (for the big MOSFETS, with both jumpers used, for 2 x 2A).

Do not use the jumpers to power a heated bed, for example, as it uses much more than 4A.

In order to configure your Smoothieboard to use a specific MOSFET, you need to know which pin corresponds to which MOSFET.

Here is a recapitulating table to help you find that out:

MOSFETs Table

This page documents the MOSFET outputs available on Smoothieboard and their specifications.

MOSFETs are used to control high-power devices like heated beds, hotends, fans, and other accessories.

MOSFET Specifications
MOSFET Pair Big MOSFETS Small MOSFETS Mixed MOSFETS      
Label on diagram P2_7 P2_5 P2_4 P2_6 P1_23 P1_22
Digital output pin 2.7 2.5 2.4 2.6 1.23 1.22
Power Input Between P2_7 and P2_5 Between P2_6 and P1_23 Taken from VBB      
Size Big Big Small Small Big Small
Maximum current 12A 12A 3A 3A 12A 3A
Used by default for Heated bed   Hotend 0 Fan Hotend 1  
Understanding MOSFET Pairs

Smoothieboard has three MOSFET pairs:

Big MOSFETs Pair (P2_7 and P2_5)
  • Current capacity: 12A each
  • Power input: Shared between the two outputs (between P2_7 and P2_5 terminals)
  • Typical use: Heated bed (high current devices)
  • Note: Both outputs share the same power input
Small MOSFETs Pair (P2_4 and P2_6)
  • Current capacity: 3A each
  • Power input: Shared between the two outputs (between P2_6 and P1_23 terminals)
  • Typical use: Hotend 0 and fan (moderate current devices)
  • Note: Both outputs share the same power input
Mixed MOSFETs Pair (P1_23 and P1_22)
  • Current capacity: P1_23: 12A, P1_22: 3A
  • Power input: Taken directly from VBB (main power supply)
  • Typical use: P1_23 for Hotend 1, P1_22 for accessories
  • Note: These outputs use the main VBB power supply
Important Safety Notes
Current Limits: Do not exceed the maximum current ratings. Big MOSFETs can handle up to 12A, small MOSFETs up to 3A. Exceeding these limits can damage the board. Shared Power: MOSFETs in the same pair share their power input. Make sure your power supply can handle the combined load of both outputs in a pair.
Configuration

To configure a MOSFET output in your config file, use the appropriate pin number from the “Digital output pin” row.

Example for configuring a heated bed on P2_7:

temperature_control.bed.heater_pin     2.7

Example for configuring a hotend on P2_4:

temperature_control.hotend.heater_pin  2.4

Wiring

TODO: diagram

Configuration

Now that you have located which MOSFET you are going to use for Spindle control, and what GPIO pin it corresponds to, you need to add a spindle control section to your configuration file.

We will be doing this using the Switch module.

Add this to your configuration file:

# Spindle control Switch module
switch.spindle.enable                            true             #
switch.spindle.input_on_command                  M3               #
switch.spindle.input_off_command                 M5               #
switch.spindle.output_pin                        2.7              # Here we are using the first big MOSFET
switch.spindle.output_type                       pwm              # pwm output settable with S parameter in the input_on_command
switch.spindle.max_pwm                           255              # set max pwm for the pin default is 255

Controlling the Spindle

Now that everything is configured, you can use G-codes to control the Spindle.

You can do this either manually by sending G-codes directly to the board via serial or your host software, or add those G-codes to the beginning/end of your files.

You can choose the exact G-codes in the configuration.

Here we used the standard M3 to turn the Spindle ON, and M5 to turn the Spindle OFF.

If you need to choose the power (speed) of your Spindle, you can do so because the output is configured as PWM.

Simply do:

M3 S128

To set the spindle to half power/speed.

PWM values go from 0 to 255.

0 means no power to the spindle, and 255 means full power to the spindle.

Diode

Because the spindle can feed power back into the MOSFET and damage the MOSFET, you also need to wire a diode of sufficient size across the output.

See notes in the first section above.

PID Loopback Spindle Control

To get more accurate RPM control, you can use a feedback sensor.

This can be optical or a hall effect sensor, which senses the rotation of the spindle.

Smoothie then measures the real RPM of the spindle and adjusts the PWM value accordingly.

Hardware Requirements

To use this module, you need to connect your spindle to pins with proper hardware support:

  • PWM output pin: any of P1.18, P1.20, P1.21, P1.23, P1.24, P1.26, P2.0 - P2.5, P2.26, P3.25
  • Feedback sensor pin: must be in port 0 or port 2; P2.6 and P2.7 are available on smoothieboard

PID Configuration Options

| Option | Example value | Explanation | | —— | ————- | ———– |

Spindle Options (Deprecated)

Deprecated Documentation

This part of the documentation is deprecated. Please refer to the spindle module page instead for current information.

Configuration Options

The following table lists the deprecated configuration options for the old spindle module:

Parameter Value Description
spindle_enable true If set to true, enables the Spindle module, which uses an encoder to PID-control a PWM-modulated spindle motor
spindle_pwm_pin 2.4 Output PWM pin (uses hardware PWM). Note: hardware PWM is available only on pins 2.0 to 2.5, 1.18, 1.20, 1.21, 1.23, 1.24, 1.26, 3.25 and 3.26
spindle_pwm_period 100 PWM period to use in microseconds
spindle_feedback_pin 2.6 Feedback input pin (must be Port 0 or 2, meaning the pin number must be 2.x or 0.x)
spindle_pulses_per_rev 3 Number of feedback pulses per revolution on the feedback input pin
spindle_default_rpm 5000 RPM to use if none given in M3 command, in rotations/minute
spindle_control_P 0.0002 PID P factor (unit is 1 / RPM)
spindle_control_I 0.0001 PID I factor (unit is 1 / ( RPM x seconds ))
spindle_control_D 0.000001 PID D factor (unit is 1 / (R PM / seconds ))

G-code Commands

Available G-code commands for PID spindle control:

  • M3 - Start the spindle. M3 S5000 will start the spindle and set speed to 5000 RPM.
  • M5 - Stop the spindle. Last set RPM is remembered and used for next M3 command if S argument is not given.
  • M957 - Report the current spindle speed and PWM value.
  • M958 - Report the current PID parameters. M958 Px.xxx Ix.xxx Dx.xxx will set them (to save the new values, you need to edit config file manually).

Tuning the PID parameters

There is no PID autotuning for spindle parameters currently.

You can use the following process to manually tune the PID:

  1. Set dummy values with M958 P0.0001 I0 D0
  2. Stop the spindle with M5 and make sure spindle is ready to run on next M3 command (i.e. power supply is on).
  3. Run M3 S100000, which should start the spindle at maximum speed.
  4. Measure the time T it takes for the spindle to reach full speed (using your ears and a stopwatch).
  5. Check the maximum RPM R the spindle reaches by running M957

Now set the parameters to:

  • P = 1/R (e.g. for 10,000 RPM, set P = 0.0001)
  • I = 2/(R*T) (e.g. if it took 1 second to speed up to 10,000 RPM, set I = 0.0002)
  • D = T / R / 10 (e.g. D = 0.00001)

Some manual tuning may be needed after this.

Test speed changes using the M3 command and try loading the spindle to see if it reacts fast enough to load changes.