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).
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.
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:
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.