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:

We suggest:

Arduino Mega and AOD4184, LR7843 or FR120N MOSFET module

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×MOSFET Module (AOD4184, LR7843 or FR120N)
1×12V LED Strip
1×Alternatively, 12V DC Cooling Fan
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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

AOD4184, LR7843 and FR120N 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.

The wiring diagram between Arduino Mega  with AOD4184, LR7843 or FR120N MOSFET 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)

The wiring diagram between MOSFET module 12V LED strip

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

The wiring diagram between MOSFET module  with a 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

12V cooling fan connected to a MOSFET module

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

The wiring diagram between MOSFET module 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)

Wiring diagram of a 12V solenoid valve with a MOSFET module

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

MOSFET module and 12V solenoid lock wiring

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:
const int MOSFET_PIN = 2;
  • The pin must be set as an output in the setup() function:
pinMode(MOSFET_PIN, OUTPUT);
  • To turn the load fully ON or fully OFF, the pin can be written HIGH or LOW:
digitalWrite(MOSFET_PIN, HIGH); // load ON digitalWrite(MOSFET_PIN, LOW); // load OFF
  • 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():
int duty = map(percent, 0, 100, 0, 255); analogWrite(MOSFET_PIN, duty);

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
/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-mosfet-module */ const int MOSFET_PIN = 2; // Arduino Mega pin connected to the PWM pin of the MOSFET module void setup() { Serial.begin(9600); pinMode(MOSFET_PIN, OUTPUT); analogWrite(MOSFET_PIN, 0); // start with the load OFF } void setLoad(int percent) { int duty = map(percent, 0, 100, 0, 255); // convert 0-100% to 0-255 analogWrite(MOSFET_PIN, duty); Serial.print("Load power: "); Serial.print(percent); Serial.println("%"); } void loop() { // turn the load fully ON, then fully OFF Serial.println("Load: ON"); setLoad(100); delay(2000); Serial.println("Load: OFF"); setLoad(0); delay(2000); // increase the power step by step (PWM) for (int percent = 10; percent <= 100; percent += 10) { setLoad(percent); delay(500); } // decrease the power step by step (PWM) for (int percent = 90; percent >= 0; percent -= 10) { setLoad(percent); delay(500); } delay(1000); }

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:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Load: ON Load power: 100% Load: OFF Load power: 0% Load power: 10% Load power: 20% Load power: 30% Load power: 40% Load power: 50% Load power: 60% Load power: 70% Load power: 80% Load power: 90% Load power: 100% Load power: 90% Load power: 80% Load power: 70% Load power: 60% Load power: 50% Load power: 40% Load power: 30% Load power: 20% Load power: 10% Load power: 0%
Ln 11, Col 1
Arduino Mega Or... on COM15
2

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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!