DEV Community

Cover image for Arduino Mega - MOSFET Module
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino Mega - MOSFET Module

This tutorial wires an Arduino Mega 2560 to a MOSFET module (AOD4184, LR7843 or FR120N) so a plain 5V logic pin can switch a 12V LED strip, fan, pump or motor on and off, or dim it with PWM. It's a handy pattern anytime your code needs to control a load that's bigger than what a microcontroller pin can handle directly.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Mega - MOSFET Module

What you need

Wiring

Arduino Mega - MOSFET Module wiring diagram

The module has a 2-pin control header and a 3-terminal power side. On the control side, PWM goes to an Arduino Mega output pin (pin 2 in the example, though any of the 15 PWM-capable pins work, or any digital pin for simple on/off) and GND goes to the Mega's GND. On the power side, the + terminal takes both the power adapter's positive wire and the load's positive wire, LOAD takes the load's negative wire, and − takes the adapter's negative wire. For motors, fans, pumps and solenoids, add a flyback diode (1N5819 or 1N4007) across the device wires, cathode to +, anode to the device's negative wire, since the coil inside them creates a voltage spike when the MOSFET switches off.

How it works

Inside the module, a PC817 optocoupler takes the PWM signal and lights a tiny internal LED, which turns on a phototransistor that drives the MOSFET gate. Because the signal crosses as light, the Arduino's GND and the load's GND are never electrically connected. The MOSFET itself sits between LOAD and −, switching the load's negative wire, and its gate voltage comes from the load supply through a divider, about half the supply voltage. That means the load supply needs to be at least roughly 6V; a 5V supply won't switch the MOSFET properly. The module is active HIGH: a HIGH signal turns the load on, LOW turns it off, and PWM values in between set partial power.

Code

No library is needed, just built-in Arduino functions. The sketch defines the Mega pin connected to the module's PWM input, sets it as OUTPUT in setup(), then uses digitalWrite(MOSFET_PIN, HIGH) or LOW for full on/off, or analogWrite(MOSFET_PIN, value) with a value from 0 to 255 to set partial power, like dimming an LED strip or slowing a fan.

arduino-mega-mosfet-module.ino

/*
 * 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);
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If nothing switches, check the load supply voltage: below about 6V the gate divider can't turn the MOSFET on fully. Match the module to your load's current, the FR120N handles only small loads (about 2A without a heatsink), while the AOD4184 and LR7843 handle more. For motors, fans, pumps and solenoids, skipping the flyback diode risks damaging the MOSFET over time. Solenoids should only get HIGH or LOW, never PWM. And a DC motor wired this way only spins one direction, for reversible control you need a proper motor driver instead.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-mega/arduino-mega-mosfet-module

Top comments (0)