DEV Community

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

Posted on Originally published at arduinogetstarted.com

Arduino - MOSFET Module

An Arduino pin can only push out 5V at a tiny current, so it can't drive a 12V LED strip, fan or motor directly. A MOSFET module sits between Arduino and that load: Arduino sends a small PWM signal, and the module switches the real power. This lets you turn a 12V device on and off, or dim/speed-control it, straight from code with analogWrite().

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

What you need

Wiring

Arduino - MOSFET Module wiring diagram

The module has a control side and a power side. On the control side, wire an Arduino output pin, for example pin 9, to the module's PWM pin, and wire Arduino GND to the module's GND pin. On the power side, connect the 12V adapter's positive wire and the load's positive wire to the + terminal, the load's negative wire to the LOAD terminal, and the adapter's negative wire to the − terminal. Never connect the 12V supply to any Arduino pin.

The LED strip can be swapped for a fan, pump, motor or solenoid without changing the Arduino-side wiring; only match the adapter voltage to the device and pick a module (FR120N, AOD4184 or LR7843) rated for the current it draws.

How it works

The PWM pin feeds a small resistor and an LED inside a PC817 optocoupler. That LED shines on a phototransistor, which drives the MOSFET gate. Because the signal crosses as light, the Arduino-side GND and the load-side GND are never electrically connected, which protects your board.

The MOSFET itself is an N-channel, low-side switch sitting between the LOAD terminal and the − terminal, so it switches the load's negative wire. A gate resistor pulls the gate to 0V whenever the optocoupler is dark, keeping the MOSFET off by default. The module is active HIGH: no signal means off, a HIGH signal turns it on.

Code

No library is needed, just pinMode(), digitalWrite() and analogWrite(). The sketch defines MOSFET_PIN as pin 9, which supports PWM, and sets it as OUTPUT. A setLoad() helper takes a percent from 0 to 100, maps it to a PWM value from 0 to 255 with map(), and calls analogWrite(). The main loop turns the load fully on for 2 seconds, fully off for 2 seconds, ramps brightness up from 10% to 100% in steps, then back down to 0%, printing each power level to the Serial Monitor along the way.

arduino-mosfet-module.ino

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-mosfet-module
 */

const int MOSFET_PIN = 9;  // Arduino 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 happens, double check the PWM and GND wiring on the control side and make sure the power adapter is actually plugged into the DC power jack. If the load behaves oddly or stays on, remember the module is active HIGH and that signal GND and load GND are isolated on purpose, so they shouldn't be tied together.

Heat is the real limit of these boards, not the chip's rated current. After running for a few minutes, touch the MOSFET; if it's too hot, add a heatsink or reduce the current, and pick the FR120N only for small loads around 2A without a heatsink.

Full tutorial

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

Top comments (0)