ESP8266 - MOSFET Module
This tutorial instructs you how to use the ESP8266 NodeMCU to control a 12V DC load, such as an LED strip or a fan, with a MOSFET module. A NodeMCU pin gives out only 3.3V, so it can never feed a 12V device by itself. Here, the pin just tells the module what to do, and the module moves the real power. In detail, we will learn:
- What is on the AOD4184, LR7843 and FR120N boards, and why a 3.3V pin is enough to drive them
- How to wire the NodeMCU pin D5 (GPIO14), the module, a 12V adapter and a 12V LED strip together
- How to write ESP8266 code that switches the load fully ON and fully OFF
- How to change the load power step by step with the ESP8266 analogWrite() function
We suggest:
- Utilizing a relay if the device you want to switch is powered by AC mains. The MOSFET module is for DC only.
- Utilizing a PC817 optocoupler module if your goal is the reverse: letting the ESP8266 detect a 12V or 24V DC signal.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of MOSFET Module
Think of the MOSFET module as a tap for DC electricity. The ESP8266 turns the tap, and the 12V adapter supplies the flow. The tap can be fully open, fully closed, or opened and closed very quickly with PWM. That last mode lets the NodeMCU set how bright an LED strip glows or how fast a fan spins. Other loads you can drive this way include a DC motor, a water pump, a solenoid valve and a small heater.
The same page works for three products: AOD4184 (sometimes printed as D4184), LR7843 and FR120N. Their boards are identical in size, pin layout and wiring, and they run the same sketch. The one thing that changes is the MOSFET chip soldered in the middle.
| AOD4184 | LR7843 | FR120N | |
|---|---|---|---|
| MOSFET | AOD4184A (N-channel) | IRLR7843 (N-channel) | IRFR120N (N-channel) |
| Max load voltage (MOSFET rating) | 40V DC | 30V DC | 100V DC |
| Max current (MOSFET rating) | 50A | 161A | 9.4A |
| On-resistance RDS(on) | about 7 mΩ | about 3.3 mΩ | about 210 mΩ |
| Practical current without heatsink | about 10–15A | about 15A | about 2A |
| Control signal | 3.3V / 5V logic, active HIGH | 3.3V / 5V logic, active HIGH | 3.3V / 5V logic, active HIGH |
| Isolation | PC817 optocoupler | PC817 optocoupler | PC817 optocoupler |
| PWM | yes, keep ≤ ~1 kHz | yes, keep ≤ ~1 kHz | yes, keep ≤ ~1 kHz |
| Load type | DC only | DC only | DC only |
Please read the "Max current" row with care. Those figures describe the bare chip on a perfect cooler. A small module board gets hot long before that, so plan your project around the "without heatsink" row.
The MOSFET Module Pinout

There are five connection points in total. Two of them face the NodeMCU, and three of them face the 12V circuit.
Signal header (2 pins, 2.54mm pitch, fits a breadboard):
- PWM pin: must be connected to a NodeMCU GPIO. In this project it is D5, which is GPIO14 inside the chip.
- GND pin: must be connected to a G (GND) pin of the NodeMCU, so the signal has a return path.
Power screw terminals:
- +: must be connected to the plus wire coming from the 12V adapter AND to the plus wire of the LED strip. Both wires go into this one terminal.
- LOAD: must be connected to the minus wire of the LED strip. The MOSFET drain sits here, so this is the wire that actually gets switched.
- −: must be connected to the minus wire coming from the 12V adapter
※ NOTE THAT:
The arrangement of pins on a module may differ from one manufacturer to another, and even two batches from the same seller can print slightly different labels. It is essential to always refer to the labels printed on the module when using it. Take a close look!
Why a 3.3V NodeMCU Pin Can Drive It
Many power MOSFETs need 5V to 10V on the gate, which the ESP8266 cannot give. This module solves that problem with a PC817 optocoupler at its input. The signal must pass this chain: D5 → PWM pin → small resistor → LED inside the PC817 → light → phototransistor inside the PC817 → MOSFET gate. A resistor on the gate pulls it down to 0V whenever the PC817 is off.
Since light carries the signal across the PC817, the 3.3V side and the 12V side have no wire in common. The module keeps the signal GND and the load GND apart.
The MOSFET is an N-channel part placed on the low side, between the LOAD terminal and the − terminal. It cuts the minus wire of the load, and the plus wire stays connected all the time. Below is what happens for each signal the NodeMCU can put on D5.
D5 at LOW (0V): the Load Is OFF
- No current goes into the PC817, so its inner LED stays dark.
- The phototransistor stays off, and the gate resistor keeps the MOSFET gate at 0V.
- The MOSFET is OFF. It behaves like an open switch between LOAD and −.
- The minus wire of the LED strip has no path to the adapter −, so no current flows and the strip gets 0V.
- In code: analogWrite(MOSFET_PIN, 0), which is a duty cycle of 0%.
D5 at HIGH (3.3V): the Load Is Fully ON
- The 3.3V pin pushes a few milliamps through the PC817 LED. It lights up inside the chip, where you cannot see it. A 3.3V GPIO handles this current easily.
- The phototransistor turns on and passes about half of the load supply to the MOSFET gate. With a 12V adapter, the gate gets about 6V, not the 3.3V from the board.
- The MOSFET turns fully ON. It acts like a closed switch with very low resistance: a few mΩ for the AOD4184 and the LR7843, about 0.2Ω for the FR120N.
- Current now flows in a loop: adapter + → + terminal → LED strip → LOAD terminal → MOSFET → − terminal → adapter −.
- The strip gets almost the full 12V and runs at full power.
- In code: analogWrite(MOSFET_PIN, 255), which is a duty cycle of 100%.
D5 Outputs PWM: the Load Gets Part of the Power
- With analogWrite(MOSFET_PIN, value), D5 jumps between LOW and HIGH about 1000 times per second. The value goes from 0 to 255, and the duty cycle is value / 255.
- The MOSFET copies the signal. It is ON in every HIGH part and OFF in every LOW part, so the load receives short 12V pulses.
- The duty cycle is the share of time the signal stays HIGH. The average voltage and the average power are the duty cycle times the full value.
| analogWrite value | Duty cycle | Average voltage (12V supply) | What you see |
|---|---|---|---|
| 51 | 20% | about 2.4V | strip glows faintly |
| 102 | 40% | about 4.8V | strip at a bit less than half |
| 153 | 60% | about 7.2V | strip at a bit more than half |
| 204 | 80% | about 9.6V | strip almost at full |
- The LED strip looks dimmer. It really flickers, but too fast for the eye to notice. A fan, motor or pump turns slower, because its spinning mass smooths out the pulses. A fan may not start at a low duty cycle.
- The PC817 is a slow part. If the PWM frequency goes too high, the MOSFET does not switch fully, the load does not get clean pulses, and the MOSFET heats up. That is why about 500 Hz to 1 kHz is the right range.
- A solenoid lock or valve must get only LOW or HIGH, never PWM.
One more rule comes from this design. Because the gate gets only half of the load voltage, the load supply needs at least about 6V. Go for 9V or higher when the load pulls more than a few amps. A 5V supply is too weak, so the NodeMCU's own 5V or 3.3V pins can not power the load.
Wiring Diagram
The NodeMCU only sends a signal from D5 and shares its GND with the module header, while all the 12V current flows between the adapter, the screw terminals and the LED strip.

This image is created using Fritzing. Click to enlarge image
| ESP8266 NodeMCU / 12V side | MOSFET Module |
|---|---|
| D5 (GPIO14) | PWM |
| G (GND) | GND |
| 12V adapter + (through the DC power jack) | + |
| 12V adapter − (through the DC power jack) | − |
| LED strip + | + (same terminal as the adapter +) |
| LED strip − | LOAD |
WARNING
The ESP8266 is a 3.3V chip, and 12V on any of its pins, even the VIN pin, can destroy it. Only the PWM and GND wires of the module header go to the NodeMCU. The 12V wires stay on the screw terminals.
Picking another NodeMCU pin
The labels printed on the NodeMCU (D0, D1, D2 ...) are not the same as the GPIO numbers of the chip. For example, D5 is GPIO14, D6 is GPIO12 and D7 is GPIO13. In the code, write the D label (such as D5) and the ESP8266 core finds the right GPIO for you.
If D5 is busy, D1, D2, D6 and D7 are also good choices. Try to avoid D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15), because the chip checks these pins at boot, and your load may flash or the board may fail to start. Also skip D0 (GPIO16), because it cannot output PWM.
Connecting Other 12V Devices to the Screw Terminals
The D5 and G wires on the NodeMCU stay exactly as shown above for every device below. Only the screw terminal side changes. Each device follows the same rule:
| Wire | Screw terminal |
|---|---|
| Adapter + | + |
| Adapter − | − |
| Device + wire | + (same terminal as the adapter +) |
| Device − wire | LOAD |
Two points apply to all of them:
- The adapter must give the voltage the device is made for (a 12V device needs a 12V adapter), and never less than 6V, which is the lowest the module accepts.
- Check how much current the device draws. The FR120N is suited only to small loads of about 2A without a heatsink. For anything bigger, choose the AOD4184 or the LR7843.
The ESP8266 sketch does not change from one device to another. The only exception is a solenoid: for it, write only HIGH or LOW (100% or 0%) and remove the fade part.
12V LED Strip (Single Color)

This image is created using Fritzing. Click to enlarge image
- The strip + wire (often printed 12V) goes to +, and the strip − wire goes to LOAD.
- An LED strip is not a coil, so it needs no diode.
- PWM sets the brightness. A longer strip pulls more current, so read its current rating before picking a module.
- This wiring is for a single color strip only. RGB and addressable strips are wired in a different way.
Flyback Diode for Coil Devices
The fan, motor, pump and solenoid examples below are all built around a coil. The module has no flyback diode on the board, so a coil device must get its own. A 1N5819 or 1N4007 is fine. Place it across the two device wires, with the striped end (cathode) on the device + and the other end (anode) on the device −. Below, this is simply called "the diode".
12V Cooling Fan

This image is created using Fritzing. Click to enlarge image
- The red wire is +, and the black wire is −.
- A third wire (yellow) carries the speed signal. Leave it unconnected.
- Fit the diode here too. It costs very little and does no harm.
- PWM sets the speed. Some fans do not start at a very low duty cycle, so begin above about 30%.
12V DC Motor

This image is created using Fritzing. Click to enlarge image
- Connect the motor + to + and the motor − to LOAD, with the diode across the two motor wires.
- PWM sets the speed.
- The motor spins in one direction only. To make it turn both ways, a motor driver is required. See ESP8266 - DC Motor, which uses the L298N.
12V Water Pump

This image is created using Fritzing. Click to enlarge image
- The pump is a coil device, so the diode is required.
- PWM sets the flow rate.
- Never let the pump run dry.
12V Solenoid Valve (Normally Closed)

This image is created using Fritzing. Click to enlarge image
- Add the diode across the valve wires.
- Full ON opens the valve, and OFF closes it.
- Drive it with ON and OFF only. PWM is not used.
12V Solenoid Lock

This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
- The lock is a coil, so the diode must be there.
- Use only fully ON (100%) or fully OFF (0%). Do not dim it with PWM.
- It heats up if it stays ON for a long time. Give it a short ON pulse to unlock, and then turn it OFF.
How To Program For MOSFET Module
There is nothing to install. The ESP8266 core for Arduino IDE already has every function the sketch needs.
- The first step is to give a name to the NodeMCU pin that is wired to the PWM pin of the module. Using the D label keeps the code in line with what is printed on the board:
- Next, the pin is made an output, and a duty of 0 is written so that the load starts in the OFF state:
- A percentage is easier to read than a raw number, so the code turns 0–100% into a duty value of 0–255 with map(), and then passes it to analogWrite():
- If you only need ON and OFF, digitalWrite(MOSFET_PIN, HIGH) and digitalWrite(MOSFET_PIN, LOW) work as well.
On the ESP8266, analogWrite() is a software PWM that runs at 1 kHz by default. That value fits the slow PC817 well, so there is no need to call analogWriteFreq(). The ESP8266 core 3.x uses a range of 0–255 by default. Very old cores (2.x) used 0–1023, so update your ESP8266 core if the load never reaches full power.
ESP8266 NodeMCU Code for MOSFET Module
The sketch plays one pattern over and over. First, the load is fully ON for 2 seconds and fully OFF for 2 seconds. Next, the power climbs from 10% to 100%, adding 10% every half second. Then it falls from 90% down to 0% at the same pace. Finally, the board waits 1 second before it starts again.
Detailed Instructions
- If this is the first time you use ESP8266, see how to setup environment for ESP8266 on Arduino IDE.
- Wire the components as shown in the diagram, but keep the 12V adapter out of the wall socket for now.
- Connect the ESP8266 NodeMCU board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Pick the board NodeMCU 1.0 (ESP-12E Module) and the COM port of your NodeMCU.
- Copy the code above and paste it into a new sketch in Arduino IDE.
- Press the Upload button and wait until the upload is done.
- Now plug the 12V adapter into the wall socket.
- Open the Serial Monitor and choose 9600 baud.
- Look at the LED strip. It glows at full power for 2 seconds, goes dark for 2 seconds, fades up slowly, and then fades down slowly.
- The Serial Monitor prints lines like these:
With a 12V fan on the terminals, you hear the same story: top speed, silence, then a slow speed-up and a slow slow-down. Do not forget the flyback diode for the fan.
Additional Knowledge
AOD4184 vs LR7843 vs FR120N: Choosing by Your Load
Every module wires to the NodeMCU in exactly the same way, so start from the voltage and the current of your load:
| Your load | Best module | Reason |
|---|---|---|
| 12V or 24V, high current (long LED strip, big motor) | LR7843 | Lowest resistance (about 3.3 mΩ), stays coolest, but max 30V |
| 12V, 24V or 36V, medium to high current | AOD4184 | All-rounder, up to 40V, about 10–15A without heatsink |
| 24V or 48V, small current | FR120N | Only one rated up to 100V, but about 210 mΩ, so about 2A without heatsink |
It is evident that the LR7843 wins when current is high and voltage is low, the FR120N is the only option above 40V, and the AOD4184 covers most jobs in between.
Heat is the real limit on all three boards. Let the load run for a few minutes and then touch the MOSFET with care. If you cannot keep your finger on it, lower the load or fit a heatsink.
MOSFET Module or Relay?
A relay closes a metal contact with a magnet. A MOSFET module switches with a chip. This leads to clear differences:
| Question | MOSFET Module | Relay |
|---|---|---|
| Can it switch AC mains? | No, DC only | Yes |
| Can it dim or change speed (PWM)? | Yes | No |
| Does it make a sound? | No, it is silent | Yes, it clicks |
| How fast does it switch? | Very fast | Slow |
| How long does it last? | Long, nothing wears out | Limited by contact wear |
It is evident that the MOSFET module is the right tool for DC loads on an ESP8266 project, most of all when you want fading or speed control. For a lamp or any device on AC mains, go with the relay instead.