Arduino Mega - MOSFET Module
This tutorial instructs you how to use the Arduino Mega 2560 to switch and dim a 12V DC load through a MOSFET module. The Mega pin only gives 5V and a few milliamps, but the module lets that small signal control a strong load such as an LED strip, a fan or a pump. In detail, we will learn:
- How the AOD4184, LR7843 and FR120N MOSFET modules work, and how they differ
- How to connect the Arduino Mega 2560, the MOSFET module, a 12V power adapter and a 12V load
- How to program the Arduino Mega 2560 to turn the load ON and OFF
- How to program the Arduino Mega 2560 to set the load power from 0% to 100% with PWM
We suggest:
- Utilizing a relay if you need to switch an AC mains device. A MOSFET module works with DC only.
- Utilizing a PC817 optocoupler if you want the opposite job: letting the Arduino Mega read 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
A MOSFET module is an electronic switch for DC power. The Arduino Mega sends a logic signal to it, and the module opens or closes the power path of the load. It has no moving parts, so it makes no click, it does not wear out, and it can switch very fast. Because it is fast, it accepts a PWM signal. This means the Arduino Mega can not only turn a 12V LED strip on and off, but also set its brightness, or set the speed of a fan or motor.
This tutorial covers three modules: AOD4184 (also sold as D4184), LR7843 and FR120N. They share the same board design, the same pins, the same wiring and the same code. The only difference is the MOSFET chip on the board.
| 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 |
The big current numbers (50A and 161A) are the limits of the chip itself. The small board cannot carry that much. In practice, the limit is heat, so use the "without heatsink" row as your guide.
The MOSFET Module Pinout

The module has two sides. The control side is a 2-pin header (2.54mm pitch) for the Arduino Mega. The power side is a group of three screw terminals for the load and its power supply.
Control side:
- PWM pin: must be connected to an Arduino Mega output pin. This pin receives the ON/OFF or PWM signal.
- GND pin: must be connected to GND of the Arduino Mega. This is the signal ground.
Power side:
- + pin: must be connected to the positive wire of the load power supply (for example, 12V) and to the positive wire of the load. Both wires share this one terminal.
- LOAD pin: must be connected to the negative wire of the load. This is the side that the MOSFET switches.
- − pin: must be connected to the negative wire (ground) of the load power supply
※ NOTE THAT:
The arrangement of pins on a module may differ from one manufacturer to another, and the labels can change a little between batches. It is essential to always refer to the labels printed on the module when using it. Take a close look!
How The MOSFET Module Works
The module is built from two main parts:
- A PC817 optocoupler on the input side. The PWM pin feeds a small resistor and a tiny LED inside it. The light of this LED turns on a phototransistor, also inside the PC817, and the phototransistor drives the MOSFET gate. Because the signal crosses as light, the Arduino Mega side and the load side are electrically isolated. The signal GND and the load GND are NOT connected on the module.
- An N-channel MOSFET used as a low-side switch. It is placed between the LOAD terminal and the − terminal, so it switches the negative wire of the load. A resistor on its gate pulls the gate to 0V whenever the optocoupler is off.
The gate voltage comes from the load supply through a voltage divider (about half of the supply). Because of this, the load supply must be at least about 6V. If the load draws more than a few amps, use 9V or more. The module will NOT work well with a 5V load supply.
The module is active HIGH. The three cases below show what happens for each signal that the Arduino Mega can send to the PWM pin.
Case 1: The PWM Pin Is HIGH (5V)
- The 5V signal pushes a few mA through the LED of the optocoupler, and the LED lights up inside the chip. Any Arduino Mega pin can supply this small current directly.
- The phototransistor conducts, and the MOSFET gate rises to about half of the load supply (about 6V with a 12V adapter).
- The MOSFET is fully ON. It acts as a closed switch with a very small resistance: a few mΩ for the AOD4184 and LR7843, about 0.2Ω for the FR120N.
- Current now flows in a loop: adapter + → + terminal → load → LOAD terminal → MOSFET → − terminal → adapter −.
- Almost the whole supply voltage (about 12V) reaches the load, so it runs at full power.
- In code: digitalWrite(MOSFET_PIN, HIGH) or analogWrite(MOSFET_PIN, 255).
Case 2: The PWM Pin Is LOW (0V)
- The pin gives 0V, so no current passes through the optocoupler LED, and it stays dark.
- The phototransistor is off. The gate resistor holds the MOSFET gate at 0V.
- The MOSFET is OFF and acts as an open switch between LOAD and −.
- The − wire of the load has no path to ground, so no current flows. The load sees 0V and is OFF.
- In code: digitalWrite(MOSFET_PIN, LOW) or analogWrite(MOSFET_PIN, 0).
Case 3: The PWM Pin Gets a PWM Signal
- AnalogWrite() makes the pin jump between LOW and HIGH about 500 to 1000 times per second.
- During each HIGH part, the module is in Case 1 (load ON). During each LOW part, it is in Case 2 (load OFF). The load therefore receives 12V pulses.
- The duty cycle is the share of each period that the pin stays HIGH. It is set by the analogWrite() value: duty = value / 255. The average voltage and average power of the load are equal to the duty cycle multiplied by the full value.
| Duty cycle | analogWrite() value | Average voltage on a 12V supply | Result |
|---|---|---|---|
| 20% | 51 | about 2.4V | very dim LED strip, very slow motor |
| 40% | 102 | about 4.8V | less than half power |
| 60% | 153 | about 7.2V | more than half power |
| 80% | 204 | about 9.6V | almost full power |
- An LED strip looks dimmer because it flickers faster than the eye can follow. A motor, fan or pump turns slower because its spinning mass smooths out the pulses.
- The PWM frequency must stay low (about 500 Hz to 1 kHz). At a higher frequency, the slow PC817 cannot switch the MOSFET fully, the pulses lose their shape, and the MOSFET heats up. See the frequency note in the programming part below.
- The example code in this tutorial walks through the duty cycle from 10% to 100%.
Wiring Diagram
The Arduino Mega sends its signal from pin 2 to the PWM pin of the module, while the 12V LED strip gets its power from the 12V adapter through the screw terminals of the module.

This image is created using Fritzing. Click to enlarge image
| From | To |
|---|---|
| Arduino Mega pin 2 | Module PWM pin |
| Arduino Mega 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 |
WARNING
Never connect the 12V supply to any pin of the Arduino Mega. The 12V goes only to the screw terminals of the MOSFET module.
The Arduino Mega has 15 PWM pins (pins 2 to 13 and 44 to 46), so you can pick another PWM pin if pin 2 is already in use. Just change the pin number in the code. A PWM pin is needed for the brightness part; for simple ON/OFF, any digital pin works.
Wiring the MOSFET Module to Other 12V Devices
The Arduino Mega side stays the same for every device below: pin 2 to the PWM pin, GND to the GND pin. Only the power side changes. In all cases, the adapter + goes to +, the adapter − goes to −, the device + wire also goes to +, and the device − wire goes to LOAD.
Before you choose a device, check two things:
- The adapter voltage must match the device. A 12V device needs a 12V adapter. It must also be 6V or more, or the module will not switch well.
- The module must suit the device current. The FR120N is for small devices only (about 2A without a heatsink). For larger currents, use the AOD4184 or the LR7843.
About the flyback diode: a motor, a fan, a pump and a solenoid all have a coil inside. When the MOSFET turns off, the coil makes a voltage spike that can harm the MOSFET. The module has no diode on board, so you must add one (1N5819 or 1N4007) across the device wires. The cathode (the side with the stripe) goes to the device + wire, and the anode goes to the device − wire. The tables below just say "flyback diode: required" when you need it.
The code in this tutorial works for every device here without any change. The only exception is the solenoids: for them, use only HIGH or LOW (100% or 0%), and do not use the fade part.
Example 1: 12V LED Strip (Single Color)

This image is created using Fritzing. Click to enlarge image
| Item | Detail |
|---|---|
| Strip + wire (often marked 12V) | Module + terminal |
| Strip − wire | Module LOAD terminal |
| Flyback diode | not needed (no coil) |
| PWM effect | brightness |
A longer strip draws more current, so read the current of your strip before you pick a module. This wiring is for a single-color strip only. RGB and addressable strips are wired in a different way.
Example 2: 12V DC Motor

This image is created using Fritzing. Click to enlarge image
| Item | Detail |
|---|---|
| Motor + wire | Module + terminal |
| Motor − wire | Module LOAD terminal |
| Flyback diode | required |
| PWM effect | speed |
The motor turns in one direction only. If you need to change the direction, use a motor driver such as the L298N instead. See the Arduino Mega - DC Motor tutorial.
Example 3: 12V Cooling Fan

This image is created using Fritzing. Click to enlarge image
| Item | Detail |
|---|---|
| Red wire (+) | Module + terminal |
| Black wire (−) | Module LOAD terminal |
| Yellow wire (speed signal, if any) | not connected |
| Flyback diode | required (cheap and harmless) |
| PWM effect | speed |
Some fans do not start at a very low duty cycle. Start above about 30%.
Example 4: 12V Water Pump

This image is created using Fritzing. Click to enlarge image
| Item | Detail |
|---|---|
| Pump + wire | Module + terminal |
| Pump − wire | Module LOAD terminal |
| Flyback diode | required |
| PWM effect | flow rate |
Never let the pump run dry. Keep its inlet under water.
Example 5: 12V Solenoid Valve (Normally Closed)

This image is created using Fritzing. Click to enlarge image
| Item | Detail |
|---|---|
| Valve + wire | Module + terminal |
| Valve − wire | Module LOAD terminal |
| Flyback diode | required |
| Control | ON = valve open, OFF = valve closed |
A valve has only two states, so ON/OFF control is enough. Do not use PWM.
Example 6: 12V Solenoid Lock

This image is created using Fritzing. Click to enlarge image
| Item | Detail |
|---|---|
| Lock + wire | Module + terminal |
| Lock − wire | Module LOAD terminal |
| Flyback diode | required |
| Control | full ON or full OFF only, no PWM |
The lock coil gets hot if it stays ON for a long time. Turn it ON only for a short pulse to unlock the door, then turn it OFF. For a full lock project, see the Arduino Mega - Solenoid Lock tutorial.
How To Program For MOSFET Module
No library is needed. The code only uses the built-in Arduino functions.
- The first step is to specify the Arduino Mega pin that is connected to the PWM pin of the module:
- The pin must be set as an output in the setup() function:
- To turn the load fully ON or fully OFF, the pin can be written HIGH or LOW:
- To set the load power, a PWM value from 0 to 255 is written with analogWrite(). The code converts a percentage to this range with map():
The default PWM frequency of analogWrite() on the Arduino Mega is about 490 Hz (about 980 Hz on pins 4 and 13). Both values are inside the safe range of the module, so keep the default and do not raise the PWM frequency.
Arduino Mega 2560 Code for MOSFET Module
The code below does these steps again and again:
- Turns the load fully ON for 2 seconds, then fully OFF for 2 seconds
- Raises the power from 10% to 100% in steps of 10%, one step every 0.5 seconds
- Lowers the power from 90% back to 0%, then waits 1 second
Detailed Instructions
- If this is the first time you use Arduino Mega 2560, see how to setup environment for Arduino Mega on Arduino IDE.
- Wire the components as shown in the diagram. Leave the 12V adapter unplugged for now.
- Connect the Arduino Mega board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Select the Arduino Mega or Mega 2560 board and the correct COM port.
- Copy the above code and paste it into the Arduino IDE.
- Click the Upload button to upload the code to the Arduino Mega.
- Plug in the 12V power adapter.
- Open the Serial Monitor and set the baud rate to 9600.
- Watch the LED strip: it is full bright for 2 seconds, off for 2 seconds, then it slowly gets brighter, and then slowly gets dimmer.
- See the result on the Serial Monitor:
If you use a 12V fan instead of the LED strip, the fan spins at full speed, stops, then speeds up and slows down in the same pattern.
Additional Knowledge
AOD4184 vs LR7843 vs FR120N: Which One to Choose
| AOD4184 | LR7843 | FR120N | |
|---|---|---|---|
| Max load voltage | 40V | 30V | 100V |
| On-resistance | about 7 mΩ | about 3.3 mΩ | about 210 mΩ |
| Practical current (no heatsink) | about 10–15A | about 15A | about 2A |
| Good for | 12V / 24V / 36V loads | 12V / 24V loads with big current | 24V / 48V small loads |
- LR7843: it has the lowest resistance, so it stays the coolest at high current. It is the best pick for strong 12V or 24V loads, like a long high-power LED strip or a big motor. Its limit is 30V.
- AOD4184: a good all-rounder. It handles up to 40V and a strong current.
- FR120N: it handles the highest voltage (up to 100V), but its resistance is the highest, so it is only for small currents of about 2A without a heatsink.
It is evident that the LR7843 is the best choice for high current, the FR120N is the best choice for high voltage, and the AOD4184 sits in the middle. Whichever one you use, touch the MOSFET after a few minutes of running. If it is too hot to touch, add a heatsink or reduce the load.
MOSFET Module vs Relay
| MOSFET Module | Relay | |
|---|---|---|
| Load type | DC only | AC or DC |
| Noise | silent | clicks |
| Moving parts | none | yes |
| Switching speed | very fast | slow |
| PWM (dimming, speed control) | yes | no |
| Life | long | limited by contact wear |
Choose the MOSFET module for DC loads, especially when you want to control brightness or speed. Choose the relay when the device runs on AC mains power.