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
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | Bench Power Supply | Set 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. |
| 1 | 100 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | 10 kΩ resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | IRLZ44N N-channel MOSFET | Voltage-controlled: raise G above S by more than the threshold and D–S conducts. The IRLZ44N switches amps from a 5 V pin. |
| 1 | DC Motor | A 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. |
| 1 | 1N4007 diode | Conducts from A (anode) to K (cathode, the striped end) once about 0.7 V is across it; blocks the other way. |
| 1 | Potentiometer | VCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023. |
| 1 | Oscilloscope | Two 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
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 9 | 100 Ω resistor leg 1 |
| 2 | 100 Ω resistor leg 2 | IRLZ44N N-channel MOSFET G |
| 3 | IRLZ44N N-channel MOSFET G | 10 kΩ resistor leg 1 |
| 4 | Arduino Uno GND | 10 kΩ resistor leg 2 |
| 5 | Arduino Uno GND | IRLZ44N N-channel MOSFET S |
| 6 | Bench Power Supply + | DC Motor + |
| 7 | DC Motor - | IRLZ44N N-channel MOSFET D |
| 8 | 1N4007 diode K | DC Motor + |
| 9 | 1N4007 diode A | DC Motor - |
| 10 | Arduino Uno GND | Bench Power Supply - |
| 11 | Arduino Uno 5V | Potentiometer VCC |
| 12 | Arduino Uno GND | Potentiometer GND |
| 13 | Arduino Uno A0 | Potentiometer SIG |
| 14 | IRLZ44N N-channel MOSFET G | Oscilloscope CH1 |
| 15 | IRLZ44N N-channel MOSFET D | Oscilloscope CH2 |
| 16 | Arduino Uno GND | Oscilloscope GND |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- 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.
- Wire 1. Connect Arduino Uno pin 9 to 100 Ω resistor leg 1.
- Wire 2. Connect 100 Ω resistor leg 2 to IRLZ44N N-channel MOSFET G.
- Wire 3. Connect IRLZ44N N-channel MOSFET G to 10 kΩ resistor leg 1.
- Wire 4. Connect Arduino Uno GND to 10 kΩ resistor leg 2.
- Wire 5. Connect Arduino Uno GND to IRLZ44N N-channel MOSFET S.
- Wire 6. Connect Bench Power Supply + to DC Motor +.
- Wire 7. Connect DC Motor - to IRLZ44N N-channel MOSFET D.
- Wire 8. Connect 1N4007 diode K to DC Motor +.
- Wire 9. Connect 1N4007 diode A to DC Motor -.
- Wire 10. Connect Arduino Uno GND to Bench Power Supply -.
- Wire 11. Connect Arduino Uno 5V to Potentiometer VCC.
- Wire 12. Connect Arduino Uno GND to Potentiometer GND.
- Wire 13. Connect Arduino Uno A0 to Potentiometer SIG.
- Wire 14. Connect IRLZ44N N-channel MOSFET G to Oscilloscope CH1.
- Wire 15. Connect IRLZ44N N-channel MOSFET D to Oscilloscope CH2.
- Wire 16. Connect Arduino Uno GND to Oscilloscope GND.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- 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
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino Push Button LED: Wiring, Code and Simulator