Motor speed control using PWM and a MOSFET

In my post on using relays, I connected a motor to an Arduino Nano using a relay module. The relay is good for switching anything that runs on a higher voltage and current than the microcontroller can directly drive. The disadvantage of the relay is that it can only switch on and off, with no speed control.

In this post, I have wired up a P36NF06 N-Channel MOSFET to switch a motor on and off and control its speed.

The MOSFET has 3 legs – Gate, Drain and Source.  The gate voltage (VGS) controls the resistance between drain and source. When the gate is at 0V, there is a high resistance between drain and source and no current flows. When the gate voltage is positive with reference to Source, the resistance between drain and source decreases. The P36NF06 that I’m using starts conducting at around 4V, and is fully switched on at 8V. It has 40 milliohms resistance between drain and source when switched on. Because it is an N-Channel MOSFET, the drain voltage must be positive with reference to the source (VDS). Other MOSFETs will have different switch-on voltages and resistances. P-Channel MOSFETS have the opposite voltage requirements (VGS and VDS must be negative).

P36NF06 Pinout. Note I've soldered it to a .1" header to fit better in a breadboard.

P36NF06 Pinout. Note I’ve soldered it to a .1″ header to fit better in a breadboard.

Test circuit
Before connecting the MOSFET to a microcontroller for PWM, I first built up a simple circuit using a button connected to the gate and an LED and resistor between the drain and 5v. There is a 47ohm resistor between G and the switch, and then a 1Mohm resistor to ground. The 1Mohm resistor is to stop the gate floating. The transistor is so sensitive that it will change state just by touching it without this. The other side of the switch is connected to +5v.

Connecting to microcontroller

I’m using the Arduino analogWrite example sketch, with 7 for the analog in pin, and D3 for the analog out pin. The sketch reads the voltage at A7,  and which results in a value between 0 and 1023, corresponding to voltages between 0 and 5V. (See AnalogRead reference). This value is divided by 4 to scale it to the 0-255 range of AnalogWrite. The Arduino keeps outputting this value in the form of pulse width modulation (PWM) – the output pin continually switches between 0 and 5V, with the on-time proportional to the value.

Download my sketch

Arduino Digital output connected to MOSFET gate. Analog input connected to variable resistor between gnd and 5V

Arduino Digital output connected to MOSFET gate. Analog input connected to variable resistor between GND and 5V

Breadboard layout for PWM and button test

Breadboard layout for PWM and button test

 

Once this is wired up and the sketch loaded, you should be able to vary the brightness of the LED by adjusting the variable resistor. Once this is working you can replace the LED with a motor or other higher powered device. In the picture below I have connected a 12V power supply to the motor, via the transistor:

Driving a motor using Arduino PWM:

Motor connected to MOSFET

Motor connected to MOSFET

Motor Connected to MOSFET Diagram

Diagram – Motor Connected to MOSFET

If you watch the video above you may have noticed the ‘whine’ noise that the motor makes as it starts up. It reminds me of the sound of an electric train. What you’re hearing is the frequency of the pulse width modulation. If you add a capacitor to smooth out the voltage, the sound mostly goes away, but you then have less control of the speed.

After getting the circuit to work I re-arranged it to make it neater: The motor is attached to the board using cable ties. Nice and neat.

neat circuit sv

Author: Paul

Share This Post On

1 Comment

  1. Hi Paul,

    Thanks for another great article.
    Nice and neat!

    Graeme

    Post a Reply

Leave a comment