Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator

To build this Arduino DC Motor PWM Speed Control with MOSFET project you need an Arduino Uno and a bench power supply, a 100 Ω resistor, a 10 kΩ resistor, an IRLZ44N N-channel MOSFET, a DC motor, a 1N4007 diode, a potentiometer and an oscilloscope. The pot sets a PWM duty on pin 9; a logic-level IRLZ44N switches a 12 V motor from the bench supply, with a flyback diode across it. Speed follows the average voltage. Make the 16 connections in the wiring table, upload the Arduino C++ code, and turn the pot and watch the rpm on the motor follow, the ammeter needle swing, and the gate PWM on CH1. Remove the diode (in real life, do not) and look at the drain on CH2.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1Bench Power SupplySet the voltage and a current limit. It holds the voltage (CV) until the load wants more than the limit, then holds the current (CC). + and − float; GND is earth.
1100 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
110 kΩ resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1IRLZ44N N-channel MOSFETVoltage-controlled: raise G above S by more than the threshold and D–S conducts. The IRLZ44N switches amps from a 5 V pin.
1DC MotorA small brushed motor (~8 Ω). Its back-EMF grows with speed, so the current is high at start and falls as it spins up. Drive it through a transistor or MOSFET, never straight from a pin, with a flyback diode across it.
11N4007 diodeConducts from A (anode) to K (cathode, the striped end) once about 0.7 V is across it; blocks the other way.
1PotentiometerVCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023.
1OscilloscopeTwo channels against earth (GND). Turn TIME/DIV and VOLTS/DIV to fit the wave, set the trigger level to steady it, and read Vpp, RMS and frequency. X-Y mode draws Lissajous figures. Choose a digital (DSO) or phosphor (CRO) screen.

Wiring

#FromTo
1Arduino Uno pin 9100 Ω resistor leg 1
2100 Ω resistor leg 2IRLZ44N N-channel MOSFET G
3IRLZ44N N-channel MOSFET G10 kΩ resistor leg 1
4Arduino Uno GND10 kΩ resistor leg 2
5Arduino Uno GNDIRLZ44N N-channel MOSFET S
6Bench Power Supply +DC Motor +
7DC Motor -IRLZ44N N-channel MOSFET D
81N4007 diode KDC Motor +
91N4007 diode ADC Motor -
10Arduino Uno GNDBench Power Supply -
11Arduino Uno 5VPotentiometer VCC
12Arduino Uno GNDPotentiometer GND
13Arduino Uno A0Potentiometer SIG
14IRLZ44N N-channel MOSFET GOscilloscope CH1
15IRLZ44N N-channel MOSFET DOscilloscope CH2
16Arduino Uno GNDOscilloscope GND

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a bench power supply, a 100 Ω resistor, a 10 kΩ resistor, an IRLZ44N N-channel MOSFET, a DC motor, a 1N4007 diode, a potentiometer and an oscilloscope from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 9 to 100 Ω resistor leg 1.
  4. Wire 2. Connect 100 Ω resistor leg 2 to IRLZ44N N-channel MOSFET G.
  5. Wire 3. Connect IRLZ44N N-channel MOSFET G to 10 kΩ resistor leg 1.
  6. Wire 4. Connect Arduino Uno GND to 10 kΩ resistor leg 2.
  7. Wire 5. Connect Arduino Uno GND to IRLZ44N N-channel MOSFET S.
  8. Wire 6. Connect Bench Power Supply + to DC Motor +.
  9. Wire 7. Connect DC Motor - to IRLZ44N N-channel MOSFET D.
  10. Wire 8. Connect 1N4007 diode K to DC Motor +.
  11. Wire 9. Connect 1N4007 diode A to DC Motor -.
  12. Wire 10. Connect Arduino Uno GND to Bench Power Supply -.
  13. Wire 11. Connect Arduino Uno 5V to Potentiometer VCC.
  14. Wire 12. Connect Arduino Uno GND to Potentiometer GND.
  15. Wire 13. Connect Arduino Uno A0 to Potentiometer SIG.
  16. Wire 14. Connect IRLZ44N N-channel MOSFET G to Oscilloscope CH1.
  17. Wire 15. Connect IRLZ44N N-channel MOSFET D to Oscilloscope CH2.
  18. Wire 16. Connect Arduino Uno GND to Oscilloscope GND.
  19. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  20. Run it. Press Run. Turn the pot and watch the rpm on the motor follow, the ammeter needle swing, and the gate PWM on CH1. Remove the diode (in real life, do not) and look at the drain on CH2.

Arduino C++ code

// Motor speed control: PWM on pin 9 drives a logic-level MOSFET, which
// switches a 12 V DC motor. A flyback diode across the motor catches the
// kick each time the MOSFET turns off. The pot on A0 sets the speed.
//
//   pin 9 -- 100 Ohm -- G   (IRLZ44N)   D -- motor -- +12 V
//                  10 kOhm pull-down    S -- GND
//
// Watch the scope: CH1 is the gate (the PWM), CH2 the motor's low side.

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  int duty = analogRead(A0) / 4;     // 0..255
  analogWrite(9, duty);
  Serial.print("speed ");
  Serial.print(duty * 100 / 255);
  Serial.println("%");
  delay(300);
}

What to try

Turn the pot and watch the rpm on the motor follow, the ammeter needle swing, and the gate PWM on CH1. Remove the diode (in real life, do not) and look at the drain on CH2.

Frequently asked questions

How do I wire the DC Motor PWM Speed Control with MOSFET project on an Arduino Uno?

Make 16 connections: Arduino Uno pin 9 to 100 Ω resistor leg 1; 100 Ω resistor leg 2 to IRLZ44N N-channel MOSFET G; IRLZ44N N-channel MOSFET G to 10 kΩ resistor leg 1; Arduino Uno GND to 10 kΩ resistor leg 2; Arduino Uno GND to IRLZ44N N-channel MOSFET S; Bench Power Supply + to DC Motor +; DC Motor - to IRLZ44N N-channel MOSFET D; 1N4007 diode K to DC Motor +; 1N4007 diode A to DC Motor -; Arduino Uno GND to Bench Power Supply -; Arduino Uno 5V to Potentiometer VCC; Arduino Uno GND to Potentiometer GND; Arduino Uno A0 to Potentiometer SIG; IRLZ44N N-channel MOSFET G to Oscilloscope CH1; IRLZ44N N-channel MOSFET D to Oscilloscope CH2; Arduino Uno GND to Oscilloscope GND.

What parts do I need for this Arduino project?

An Arduino Uno and a bench power supply, a 100 Ω resistor, a 10 kΩ resistor, an IRLZ44N N-channel MOSFET, a DC motor, a 1N4007 diode, a potentiometer and an oscilloscope.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

The pot sets a PWM duty on pin 9; a logic-level IRLZ44N switches a 12 V motor from the bench supply, with a flyback diode across it. Speed follows the average voltage.

More Arduino Uno projects