ESP32 - MOSFET Module
This tutorial instructs you how to use ESP32 to switch a 12V DC load ON and OFF and set its power level with a MOSFET module, and print the power level to Serial Monitor. The tutorial works the same way for the AOD4184, LR7843 and FR120N modules, and shows how to wire both an LED strip and a DC motor or fan.
This tutorial shows how to program the ESP32 using the Arduino language (C/C++) via the Arduino IDE. If you’d like to learn how to program the ESP32 with MicroPython, visit this ESP32 MicroPython - MOSFET Module tutorial.
Hardware Used In This Tutorial
Or you can buy the following kits:
| 1 | × | DIYables ESP32 Starter Kit (ESP32 included) | |
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Introduction to MOSFET Module
A GPIO pin of the ESP32 outputs only 3.3V and a few milliamps. That is enough for a small LED, but far too little for a 12V LED strip, a DC motor, a fan, a pump, a solenoid valve or a heater. The MOSFET module works as an electronic switch between the two. The ESP32 gives a weak signal, and the module passes the strong current of the load.
Three versions of this module are on the market: AOD4184 (the same chip is also sold as D4184), LR7843 and FR120N. The board is identical for all three. Size, pins, wiring and code do not change. The only difference is the MOSFET chip in the middle, which decides how much voltage and current the board can take.
| AOD4184 | LR7843 | FR120N | |
|---|---|---|---|
| The MOSFET chip | AOD4184A (N-channel) | IRLR7843 (N-channel) | IRFR120N (N-channel) |
| The max load voltage (chip rating) | 40V DC | 30V DC | 100V DC |
| The max current (chip rating) | 50A | 161A | 9.4A |
| The on-resistance RDS(on) | about 7 mΩ | about 3.3 mΩ | about 210 mΩ |
| The practical current without heatsink | about 10–15A | about 15A | about 2A |
| The control signal | 3.3V / 5V logic, active HIGH | 3.3V / 5V logic, active HIGH | 3.3V / 5V logic, active HIGH |
| The isolation | PC817 optocoupler | PC817 optocoupler | PC817 optocoupler |
| The PWM support | yes, keep ≤ ~1 kHz | yes, keep ≤ ~1 kHz | yes, keep ≤ ~1 kHz |
| The load type | DC only | DC only | DC only |
To pick one, think about your load. The LR7843 has the lowest resistance, so it stays the coolest when a lot of current flows. Use it for high-power 12V or 24V LED strips and big motors, but never above 30V. The AOD4184 is the safe middle choice: strong current and up to 40V, which covers 12V, 24V and 36V loads. The FR120N accepts the highest voltage, up to 100V, but its resistance is much higher. That means it gets warm quickly, so keep it for small 24V or 48V loads of about 2A.
Do not trust the 50A or 161A numbers too much. They describe the bare chip, not this small board. On the board, heat is the real limit. After the load has run for a few minutes, touch the MOSFET carefully. If it feels too hot, add a heatsink or use a smaller load.
MOSFET Module Pinout

The pins sit in two places. A small 2-pin header (2.54mm pitch) is for the ESP32, so it plugs into a breadboard easily. A row of three screw terminals is for the load and its power supply.
Control header:
- PWM pin: connect this pin to a GPIO pin of the ESP32 (GPIO18 in this tutorial)
- GND pin: connect this pin to GND of the ESP32
Screw terminals:
- + pin: connect this pin to the positive wire of the load power supply (e.g. 12V) and to the positive wire of the load. Both wires share this one terminal
- LOAD pin: connect this pin to the negative wire of the load. This is the side the MOSFET switches
- − pin: connect this pin to the negative wire (ground) of the load power supply
The printed labels can change a little from one batch to another, so read the text on your own board before wiring.
We highly recommend you using the AOD4184 module if you are not sure which one to buy.
How MOSFET Module Works
The signal from GPIO18 does not reach the MOSFET directly. It enters the PWM pin, passes a small resistor, and lights a tiny LED inside the PC817 optocoupler. That light switches a phototransistor in the same chip, and the phototransistor feeds the gate of the MOSFET. A resistor on the gate pulls it back to 0V whenever the optocoupler is off. The MOSFET itself sits between the LOAD terminal and the − terminal, while the load hangs between + and LOAD. So what the load does depends only on the state of the PWM pin, and there are three possible states.
When GPIO18 outputs 0V (LOW)
With 0V on the PWM pin, no current can flow through the LED of the optocoupler, so it stays dark. The phototransistor therefore stays off, and the gate resistor keeps the MOSFET gate at 0V. The MOSFET is OFF, which means the path from LOAD to − is open, just like an open switch. The negative wire of the load has no way back to the adapter, so no current flows and the load sits at 0V. You get this state with analogWrite(MOSFET_PIN, 0) or digitalWrite(MOSFET_PIN, LOW).
When GPIO18 outputs 3.3V (HIGH)
The ESP32 only gives 3.3V, but that is enough: the LED of the optocoupler needs just a few milliamps, so the GPIO pin can drive it directly (a 5V board would work the same way). The LED lights up inside the chip, where you cannot see it, and the phototransistor turns on. Now the MOSFET gate receives about half of the load supply, roughly 6V when you use a 12V adapter. The MOSFET switches fully ON and behaves like a closed switch with a tiny resistance, only a few mΩ on the AOD4184 and LR7843 boards and about 0.2Ω on the FR120N board. Current runs from the adapter positive into the + terminal, through the load, into the LOAD terminal, through the MOSFET, out of the − terminal and back to the adapter negative. The load receives nearly the full 12V and works at full power. In code, this is analogWrite(MOSFET_PIN, 255) or digitalWrite(MOSFET_PIN, HIGH).
When GPIO18 outputs a PWM signal
A PWM signal is just the two states above, repeated very fast. With analogWrite(), GPIO18 flips between LOW and HIGH about 1000 times per second, and the MOSFET copies every flip: it is ON during each HIGH part and OFF during each LOW part. The load therefore receives short 12V pulses. The share of time the pin stays HIGH is called the duty cycle, and the value you pass to analogWrite() sets it (duty cycle = value / 255). The average voltage and power that the load gets are simply the duty cycle times the full value. With a 12V supply, for example:
- 10% (value 26): ON one tenth of the time, average about 1.2V, a very weak glow or a crawl
- 50% (value 128): ON half of the time, average about 6V, about half power
- 90% (value 230): ON nine tenths of the time, average about 10.8V, close to full power
An LED strip does not visibly blink at this speed, so it simply looks dimmer. A motor, fan or pump turns slower, because the mass of its rotor smooths out the pulses. Keep the frequency low, around 500 Hz to 1 kHz, because the PC817 is a slow part. At a much higher frequency, the MOSFET does not have time to switch fully, the load gets messy pulses instead of clean ones, and the MOSFET heats up. The ESP32 analogWrite() function already uses 1 kHz by default, so no change is needed. The code in this tutorial uses this PWM case to step the power from 10% up to 100%.
A few more facts about the board help you avoid problems:
- The optocoupler keeps the ESP32 side and the load side electrically apart. The signal GND and the load GND are not connected on the board, and you should not join them.
- Since the MOSFET is an N-channel type on the low side, it cuts the negative wire of the load, not the positive one.
- The gate voltage comes from the load supply through a voltage divider, which is why it is only about half of that supply. So the load supply must be about 6V or higher. Choose 9V or more when the load pulls more than a few amps. A 5V load supply will not work well.
- The board has no flyback diode. Motors, pumps, solenoids and relays need one added outside the board.
- It is for DC loads only. Never use it with AC mains power. For AC devices, see the ESP32 relay tutorial.
Wiring Diagram
The 12V power goes only to the screw terminals of the module. Never let it touch any pin of the ESP32.
How to connect ESP32 and MOSFET module with a 12V LED strip

This image is created using Fritzing. Click to enlarge image
| From | To |
|---|---|
| ESP32 GPIO18 | Module PWM pin |
| ESP32 GND | Module GND pin |
| 12V adapter + (via DC power jack) | Module + terminal |
| 12V adapter − (via DC power jack) | Module − terminal |
| LED strip + | Module + terminal (shared with the adapter +) |
| LED strip − | Module LOAD terminal |
How to connect other 12V devices to the MOSFET module
Many devices can take the place of the LED strip. GPIO18 and GND of the ESP32 stay wired exactly as in the table above, so the pictures below show only the power side. The pattern never changes: the adapter positive goes to +, the adapter negative goes to −, the positive wire of the device also goes to +, and its negative wire goes to LOAD.
Pick an adapter with the same voltage as your device, so a 12V device gets a 12V adapter. Whatever the device, the supply should not be lower than 6V. Also think about the current your device draws. The FR120N board can only handle light devices of about 2A without a heatsink, while the AOD4184 and LR7843 boards suit heavier ones.
You can keep the ESP32 code exactly as it is for all of these devices. With a solenoid lock or a solenoid valve, though, send only full power or zero (HIGH or LOW) and leave out the fade.
Using a 12V DC motor

This image is created using Fritzing. Click to enlarge image
Connect the motor positive wire to the + terminal and the motor negative wire to LOAD. Inside a motor there is a coil, and when the MOSFET turns off, that coil kicks back a voltage spike. Over time, this spike can damage the MOSFET. To absorb it, place a flyback diode such as a 1N5819 or a 1N4007 across the motor wires, with its striped end (the cathode) on the motor positive wire and its other end (the anode) on the motor negative wire. The coil devices further down need this same diode, placed the same way.
The PWM duty cycle controls the speed, but only in one direction. If you need the motor to spin both ways, use a driver such as the L298N, as shown in the ESP32 DC motor tutorial.
Using a 12V cooling fan

This image is created using Fritzing. Click to enlarge image
The red fan wire is the positive one and goes to the + terminal, and the black wire goes to LOAD. If your fan has a yellow third wire, it only reports the speed, so leave it unconnected. Add the flyback diode here as well; it is cheap and causes no harm. Many fans refuse to start at a very low duty cycle, so begin at around 30% or higher.
Using a 12V water pump

This image is created using Fritzing. Click to enlarge image
Wire the pump positive to the + terminal and the pump negative to LOAD, and fit the flyback diode across the pump. A higher duty cycle pushes more water. Make sure the pump is always under water, because running it dry can ruin it.
Using a 12V solenoid valve

This image is created using Fritzing. Click to enlarge image
The valve positive goes to the + terminal and the valve negative goes to LOAD, with the flyback diode across it. A normally closed valve opens when the ESP32 turns the module fully ON, and closes again when it turns it OFF. Do not try to dim it with PWM; it is either open or closed.
Using a 12V solenoid lock

This image is created using Fritzing. Click to enlarge image
Connect the lock positive to the + terminal and the lock negative to LOAD, and add the flyback diode. Drive the lock only fully ON or fully OFF. Its coil heats up quickly, so switch it ON for a short moment to open the latch, then switch it OFF again. A complete door lock example is in the ESP32 solenoid lock tutorial.
Using a single-color 12V LED strip

This image is created using Fritzing. Click to enlarge image
This is the same circuit as the main diagram, seen from the power side. The strip positive, which is often printed as 12V, goes to the + terminal, and the strip negative goes to LOAD. An LED strip has no coil, so it does not need a diode. The duty cycle sets the brightness. Keep in mind that a longer strip pulls more current, so check its rating before choosing a module. RGB and addressable strips need a different setup.
If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.
How To Program For MOSFET Module
No library is needed. The code uses functions that are already built into the ESP32 core.
- Give a name to the GPIO pin that drives the module: const int MOSFET_PIN = 18;
- Set the pin as an output: pinMode(MOSFET_PIN, OUTPUT);
- Turn the load fully ON: analogWrite(MOSFET_PIN, 255); (or digitalWrite(MOSFET_PIN, HIGH);)
- Turn the load fully OFF: analogWrite(MOSFET_PIN, 0); (or digitalWrite(MOSFET_PIN, LOW);)
- Set part of the power, for example 50%: analogWrite(MOSFET_PIN, 128);
- Turn a percent into a PWM value from 0 to 255: int duty = map(percent, 0, 100, 0, 255);
ESP32 Code - MOSFET Module
The code below turns the load fully ON for 2 seconds and fully OFF for 2 seconds. Then it raises the power from 10% to 100% in steps of 10%, one step every half second, and lowers it back to 0% the same way. After a 1-second pause, the cycle starts again.
Quick Instructions
If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
- Wire the ESP32, the MOSFET module, the LED strip (or fan) and the 12V power as shown in the diagram above.
- Plug the USB cable into the ESP32 and your PC. Keep the 12V adapter unplugged for now.
- Open Arduino IDE, then select the ESP32 Dev Module board and the right COM port.
- Copy the above code and paste it into Arduino IDE.
- Click to the Upload button on Arduino IDE to compile and upload the code to the ESP32.
- Plug in the 12V power adapter.
- Click to the Serial Monitor icon, then type or select 9600 as the baud rate.
- Watch the LED strip: full brightness for 2 seconds, dark for 2 seconds, then it slowly gets brighter and slowly gets dimmer. A fan will do the same with its speed.
- Look at the messages on Serial Monitor.
If the load does not turn on at all, check the load supply voltage first. Remember that it must be about 6V or more.
Video Tutorial
Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.