Arduino - MOSFET Module

by DIYables in Circuits > Arduino

29 Views, 0 Favorites, 0 Comments

Arduino - MOSFET Module

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-mosfet-module

An Arduino pin only gives 5V and a tiny current, not enough to run a 12V LED strip, fan or motor. A MOSFET module (AOD4184, LR7843 or FR120N) sits between Arduino and the load, letting a small PWM signal switch and dim the full 12V power. This guide wires the module to an Arduino Uno and drives a 12V LED strip, with code to turn it on and off and fade its brightness.

Supplies

Wiring

The module has a 2-pin control header and three screw terminals on the power side. Connect Arduino pin 9 to the PWM pin and Arduino GND to the module's GND pin. Connect the 12V adapter, through the DC power jack, to the + and − screw terminals. Connect the LED strip's positive wire to the + terminal (shared with the adapter +) and its negative wire to the LOAD terminal. Never connect the 12V line to any Arduino pin.

How It Works

The PWM signal feeds a resistor and the LED inside the module's PC817 optocoupler. Light crosses to a phototransistor that drives the MOSFET gate, so the Arduino side and load side stay isolated; their GNDs are not connected. The MOSFET is an N-channel, low-side switch between LOAD and −, so it switches the load's negative wire. A gate resistor holds the gate at 0V when the optocoupler is off. With no signal, the LED stays dark, the phototransistor stays off, and the MOSFET is OFF, acting like an open switch between LOAD and −.

Code

No library is needed. Pin 9 is set as a PWM output and starts at 0 (load off). The sketch turns the load fully on for 2 seconds, then off for 2 seconds, then ramps power up from 10% to 100% in 10% steps every half second, then back down from 90% to 0%, waiting a second before repeating. A setLoad() function converts a percent value to a 0-255 duty cycle with map() and sends it with analogWrite(), printing the percentage to the Serial Monitor at 9600 baud.

arduino-mosfet-module.ino - the sketch from the tutorial, unchanged.

/*
* 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);
}

Things That Go Wrong

Match the adapter voltage to the device, a 12V device needs a 12V adapter, and don't go below 6V. The FR120N only handles small current (about 2A without a heatsink); for bigger loads use the AOD4184 or LR7843. The 50A/161A chip ratings don't apply to the small board, the real limit is heat, so touch the MOSFET after a few minutes and add a heatsink or lower the current if it's too hot. Keep the 12V line away from any Arduino pin.

Full Tutorial

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