Raspberry Pi - MOSFET Module

This tutorial instructs you how to use Raspberry Pi to drive a 12V DC load, such as an LED strip or a fan, with a MOSFET module. A Raspberry Pi GPIO pin works at 3.3V and gives only a tiny current. The MOSFET module takes that weak signal and uses it to control a much bigger load from a separate power supply. In detail, we will learn:

We suggest:

Hardware Preparation

1×Raspberry Pi 5
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 Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

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

Think of the MOSFET module as a solid-state switch that sits on the negative wire of your DC load. The Raspberry Pi tells it when to conduct, and the module lets the 12V current flow through the load or blocks it. Nothing moves inside, so there is no click and nothing wears out over time. It also reacts quickly enough to follow a PWM signal. With PWM, the Raspberry Pi can make an LED strip dimmer or brighter, or make a fan or motor run slower or faster.

One page covers three boards here: AOD4184 (you may also see it sold as D4184), LR7843 and FR120N. The PCB layout, the pins, the wiring and the Python script are identical for all three. Only the MOSFET chip soldered on the board is different.

AOD4184 LR7843 FR120N
MOSFET chip 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 it at about 1 kHz or lower yes, keep it at about 1 kHz or lower yes, keep it at about 1 kHz or lower
Load type DC only DC only DC only

How the Module Works Inside

The GPIO signal never reaches the MOSFET directly. It travels through these parts, in this order: the PWM pin of the module, a small resistor, the LED inside a PC817 optocoupler, then (as light) the phototransistor inside the same PC817, and finally the gate of an N-channel MOSFET. The MOSFET is placed between the LOAD terminal and the − terminal, so it switches the negative wire of the load (a low-side switch). The load itself is connected between + and LOAD.

The logic is active HIGH. Below, you can see what happens inside the module for each signal that GPIO16 can send.

When GPIO16 Outputs 3.3V (HIGH)

A Raspberry Pi GPIO pin gives only 3.3V, never 5V. This is enough for the module:

  • The optocoupler LED needs only a few milliamps, so the 3.3V pin can drive it directly. A small current flows, and the LED lights up inside the chip (you cannot see it).
  • The light turns on the phototransistor. The MOSFET gate now receives about half of the load supply, which is about 6V with a 12V adapter.
  • The MOSFET turns fully ON and becomes a closed switch. Its resistance is very low: a few mΩ for the AOD4184 and the LR7843, and about 0.2Ω for the FR120N.
  • The current must go around this loop: adapter + → + terminal → load → LOAD terminal → MOSFET → − terminal → adapter −.
  • The load receives almost the full supply voltage (about 12V) and runs at full power.

In Python, this is GPIO.output(MOSFET_PIN, GPIO.HIGH), or a duty cycle of 100% with pwm.ChangeDutyCycle(100).

When GPIO16 Outputs 0V (LOW)

  • The optocoupler LED gets no current, so it does not light up.
  • Without light, the phototransistor stays off. A resistor on the gate pulls the MOSFET gate down to 0V.
  • The MOSFET turns OFF and becomes an open switch between LOAD and −.
  • The negative wire of the load is now cut off from ground. No current can flow, the load gets 0V, and it is OFF.

In Python, this is GPIO.output(MOSFET_PIN, GPIO.LOW), or pwm.ChangeDutyCycle(0).

When GPIO16 Outputs a PWM Signal

The script in this tutorial creates the PWM with pwm = GPIO.PWM(MOSFET_PIN, 500). This means GPIO16 jumps between 0V and 3.3V 500 times per second. Here is what follows:

  • The MOSFET follows each jump. It is ON for every HIGH part and OFF for every LOW part, so the load receives 12V pulses.
  • The duty cycle is the part of each period when the signal is HIGH. You set it with pwm.ChangeDutyCycle(), from 0 to 100.
  • The average voltage, and the average power, is the duty cycle times the full value.
pwm.ChangeDutyCycle() Signal Average on a 12V load
0 always LOW 0V, the load is OFF
10 HIGH 1/10 of the time about 1.2V, about 10% power
50 HIGH half of the time about 6V, about half power
90 HIGH 9/10 of the time about 10.8V, about 90% power
100 always HIGH 12V, full power

The script steps from 10% to 100% in 10% steps, so you can see each level. An LED strip looks dimmer at a low duty cycle, because it flickers too fast for the eye to notice. A fan or motor turns slower, because its spinning mass smooths the pulses.

You must keep the PWM frequency low, around 500 Hz to 1 kHz, because the PC817 is a slow part. If the frequency is too high, the MOSFET cannot switch fully. The load does not get clean pulses, and the MOSFET gets hot.

Other Results of This Design

  • The Raspberry Pi side and the 12V side are electrically isolated. The signal ground and the load ground are not joined on the module.
  • The gate voltage is taken from the load supply, not from the Raspberry Pi. For this reason, the load supply must be at least about 6V. If the load draws more than a few amps, use 9V or more. A 5V load supply will not work well.

The MOSFET Module Pinout

AOD4184, LR7843 and FR120N MOSFET module pinout

The control side is a 2-pin header (2.54mm pitch), so it fits on a breadboard:

  • PWM: This pin receives the control signal from a Raspberry Pi GPIO pin
  • GND: This pin must be connected to the ground (0V) of the Raspberry Pi

The power side has three screw terminals:

  • +: This terminal must be connected to the positive of the load power supply (for example 12V) and to the positive wire of the load. Both wires share this one terminal
  • LOAD: This terminal goes to the negative wire of the load. This is the switched side, and it leads to the MOSFET drain
  • −: This terminal must be connected to the negative (ground) of the load power supply

※ NOTE THAT:

The arrangement of pins on a module can differ between manufacturers, and even between batches. It is essential to always use the labels printed on the module when working with it. Be sure to take a close look!

Wiring Diagram

The first example below uses a 12V LED strip. Other devices, such as a fan, a DC motor, a pump or a solenoid, are shown further down.

The wiring diagram between Raspberry Pi and  with AOD4184, LR7843 or FR120N MOSFET module

This image is created using Fritzing. Click to enlarge image

  • MOSFET module PWM pin to Raspberry Pi GPIO16 (pin 36)
  • MOSFET module GND pin to Raspberry Pi GND (pin 34, right next to pin 36, or any other GND pin)
  • 12V power adapter positive (through the DC power jack) to MOSFET module +
  • 12V power adapter negative (through the DC power jack) to MOSFET module −
  • LED strip positive wire to MOSFET module + (the same terminal as the adapter positive)
  • LED strip negative wire to MOSFET module LOAD

WARNING

The 12V supply must stay on the screw terminal side of the module. Never connect it to any pin of the Raspberry Pi header. The Raspberry Pi GPIO pins accept 3.3V only, and 12V will destroy the board.

Wiring Other DC Devices to the MOSFET Module

The Raspberry Pi side is the same for every device: GPIO16 (pin 36) goes to PWM, and a GND pin goes to GND. What changes is the screw terminal side. The lists under each picture cover that side only.

Before you wire a new device, check these points:

  • The adapter voltage must be the same as the device voltage. For a 12V device, use a 12V adapter. The supply must also be 6V or more, as explained earlier.
  • The module must handle the device current. The FR120N is only for small devices, about 2A without a heatsink. The AOD4184 and the LR7843 are for bigger ones.
  • A device with a coil (a motor, a fan, a pump or a solenoid) is an inductive load. The module has no flyback diode on board, so you must add one yourself (1N5819 or 1N4007). Place it across the device. The cathode (the side with the stripe) goes on the device positive wire, and the anode goes on the device negative wire.

You can keep the same Python script for all of these devices. The one exception is a solenoid: drive it with fully ON and fully OFF only (100% or 0%), and leave out the fade part.

Raspberry Pi, MOSFET Module and 12V DC Motor

Raspberry Pi MOSFET module 12V DC motor wiring

This image is created using Fritzing. Click to enlarge image

  • 12V adapter positive to MOSFET module +
  • 12V adapter negative to MOSFET module −
  • Motor positive wire to MOSFET module +
  • Motor negative wire to MOSFET module LOAD
  • Flyback diode across the motor: cathode (stripe) to motor positive, anode to motor negative

The PWM duty cycle controls how fast the motor turns. The module can only drive it in one direction. If you need to reverse it, use a motor driver such as the L298N, as shown in the Raspberry Pi - DC Motor tutorial.

Raspberry Pi, MOSFET Module and 12V Water Pump

MOSFET module wiring for a 12V water pump

This image is created using Fritzing. Click to enlarge image

  • 12V adapter positive to MOSFET module +
  • 12V adapter negative to MOSFET module −
  • Pump positive wire to MOSFET module +
  • Pump negative wire to MOSFET module LOAD
  • Flyback diode across the pump, placed the same way as for the motor

The duty cycle sets the water flow. Make sure there is always water in the pump. Running it dry can damage it.

Raspberry Pi, MOSFET Module and 12V LED Strip

MOSFET module wiring for a single-color 12V LED strip

This image is created using Fritzing. Click to enlarge image

  • 12V adapter positive to MOSFET module +
  • 12V adapter negative to MOSFET module −
  • LED strip positive wire (often marked 12V) to MOSFET module +
  • LED strip negative wire to MOSFET module LOAD
  • No flyback diode, because an LED strip has no coil

The duty cycle sets the brightness. A long strip draws more current, so read the current of your strip before you choose the module. This wiring is for a single-color strip. RGB and addressable strips work in a different way.

Raspberry Pi, MOSFET Module and 12V Cooling Fan

MOSFET module wiring for a 12V cooling fan

This image is created using Fritzing. Click to enlarge image

  • 12V adapter positive to MOSFET module +
  • 12V adapter negative to MOSFET module −
  • Fan red wire to MOSFET module +
  • Fan black wire to MOSFET module LOAD
  • Fan yellow wire (speed signal), if your fan has one: leave it unconnected
  • Flyback diode across the fan, the same way as above (it is cheap and safe to add)

The duty cycle sets the fan speed. Some fans will not start when the duty cycle is very low, so begin at about 30% or higher.

Raspberry Pi, MOSFET Module and 12V Solenoid Valve

MOSFET module wiring for a normally closed 12V solenoid valve

This image is created using Fritzing. Click to enlarge image

  • 12V adapter positive to MOSFET module +
  • 12V adapter negative to MOSFET module −
  • Valve positive wire to MOSFET module +
  • Valve negative wire to MOSFET module LOAD
  • Flyback diode across the valve coil

This is a normally closed valve. When the module is fully ON, the valve opens. When it is OFF, the valve closes. Do not use PWM with it, only ON and OFF.

Raspberry Pi, MOSFET Module and 12V Solenoid Lock

MOSFET module wiring for a 12V solenoid lock

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield
  • 12V adapter positive to MOSFET module +
  • 12V adapter negative to MOSFET module −
  • Lock positive wire to MOSFET module +
  • Lock negative wire to MOSFET module LOAD
  • Flyback diode across the lock coil

Switch the lock fully ON or fully OFF, never with a PWM fade. The coil heats up if it stays ON for a long time, so turn it ON only for a short moment to unlock, then turn it OFF again.

How To Program for Raspberry Pi to Control a DC Load with MOSFET Module

No extra library is needed. The script uses the PWM feature of RPi.GPIO on GPIO16 and runs this cycle again and again:

  • The load is fully ON for 2 seconds, then fully OFF for 2 seconds.
  • The power rises from 10% to 100% in 10% steps, with 0.5 seconds per step.
  • The power falls from 90% back to 0% in the same way.
  • The script waits 1 second, then starts over.

When you press Ctrl + C, the script stops the PWM and releases the GPIO pin.

Detailed Instructions

  1. Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  2. Make sure your Raspberry Pi is connected to the same local network as your PC.
  3. Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  4. If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  5. Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows.
  6. Make sure the RPi.GPIO library is installed. If it is missing, install it with these commands:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  1. Wire the Raspberry Pi, the MOSFET module, the 12V power adapter and the LED strip as shown in the wiring diagram above. Leave the 12V adapter unplugged for now.
  2. Create a Python script file MosfetModule.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-mosfet-module """ import RPi.GPIO as GPIO import time MOSFET_PIN = 16 # GPIO16 (pin 36) connected to the PWM pin of the MOSFET module GPIO.setmode(GPIO.BCM) GPIO.setup(MOSFET_PIN, GPIO.OUT) pwm = GPIO.PWM(MOSFET_PIN, 500) # 500 Hz PWM, slow enough for the module's optocoupler pwm.start(0) # start with the load OFF def set_load(percent): pwm.ChangeDutyCycle(percent) print("Load power: {}%".format(percent)) try: while True: # turn the load fully ON, then fully OFF print("Load: ON") set_load(100) time.sleep(2) print("Load: OFF") set_load(0) time.sleep(2) # increase the power step by step (PWM) for percent in range(10, 101, 10): set_load(percent) time.sleep(0.5) # decrease the power step by step (PWM) for percent in range(90, -1, -10): set_load(percent) time.sleep(0.5) time.sleep(1) except KeyboardInterrupt: pass finally: pwm.stop() GPIO.cleanup()
  1. Plug in the 12V power adapter.
  2. Run the script with this command:
python3 MosfetModule.py
  1. Watch the LED strip. It shines at full brightness for 2 seconds and goes dark for 2 seconds. Then it slowly gets brighter, and after that it slowly gets dimmer.
  2. Check the results on the terminal.
PuTTY - Raspberry Pi
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%

The script repeats this cycle forever. During the ON and OFF part, a new line appears every 2 seconds (from the time.sleep(2) lines). During the fade up and fade down, a new line appears every 0.5 seconds (from the time.sleep(0.5) lines). The output keeps going until you stop the script by pressing Ctrl + C keys in the terminal. The script then turns the load off and cleans up the GPIO pin.

If you use a 12V fan or motor instead of the LED strip, the same output means the fan runs at full speed, stops, then speeds up and slows down.

Additional Knowledge

AOD4184 vs LR7843 vs FR120N

Let's compare the three modules to see which one suits your project:

AOD4184 LR7843 FR120N
Max load voltage 40V DC 30V DC 100V DC
On-resistance about 7 mΩ about 3.3 mΩ about 210 mΩ
Practical current without heatsink about 10–15A about 15A about 2A
Good for 12V / 24V / 36V loads 12V / 24V loads with big current 24V / 48V small loads
  • The LR7843 has the lowest resistance, so it stays the coolest at high current. Pick it for high-power LED strips or big motors at 12V or 24V. Do not go above 30V.
  • The AOD4184 is a good all-rounder. It handles up to 40V and still carries a strong current.
  • The FR120N accepts the highest voltage (up to 100V), but its resistance is much higher. It suits only small currents, about 2A without a heatsink.

Keep in mind that 50A and 161A are limits of the MOSFET chip itself. The small module cannot carry that much. The real limit is heat. After a few minutes of running, touch the MOSFET carefully. If it is too hot to touch, lower the current or add a heatsink.

MOSFET Module vs Relay

Let's compare a MOSFET module with a relay module for switching a load:

MOSFET Module Relay Module
Moving parts none yes, a mechanical contact
Noise silent clicks on every switch
Switching speed very fast slow
PWM (brightness / speed control) yes no
Lifetime long, no contact wear limited by contact wear
Load type DC only DC or AC (check the relay rating)

Evidently, the MOSFET module is the better choice for DC loads, especially when you want to control brightness or speed. For AC mains devices, a relay is still the right tool.

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!