Raspberry Pi Pico - MOSFET Module

In this guide, we will learn to set up and use a MOSFET module as a power switch with the Raspberry Pi Pico. We will cover the following details:

Raspberry Pi Pico and AOD4184, LR7843 or FR120N MOSFET module

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
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×Breadboard
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

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

The Raspberry Pi Pico works at 3.3 volts. Its GPIO pins cannot power a 12 volts LED strip, a DC motor, a fan, a pump, a solenoid valve or a heater. A MOSFET module solves this. The Pico sends a small signal, and the module opens or closes the path for the big current of the load.

Why not just use a relay? A MOSFET has no moving parts, so it makes no clicking sound and lasts a long time. It also switches very fast, so it can do PWM. With PWM, you can dim an LED strip or slow down a fan. A relay can only do full on or full off. The one limit: a MOSFET module works with DC loads only. Never use it with AC mains power.

You can buy this module with one of three MOSFET chips: AOD4184 (also sold as D4184), LR7843 or FR120N. The rest of the board is the same. The size, the pins, the wiring and the MicroPython code are the same for all three. This one guide works for any of them.

AOD4184 LR7843 FR120N
Chip on board AOD4184A (N-channel) IRLR7843 (N-channel) IRFR120N (N-channel)
Highest load voltage (chip rating) 40V DC 30V DC 100V DC
Highest current (chip rating) 50A 161A 9.4A
On-resistance RDS(on) about 7 mΩ about 3.3 mΩ about 210 mΩ
Real current with no 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

How do you choose? Here is a simple rule:

  • LR7843: pick this one for heavy 12 volts or 24 volts loads, like a bright LED strip or a big motor. It has the lowest resistance, so it stays cool. Do not go above 30 volts.
  • AOD4184: a good choice for most projects. It handles strong current and up to 40 volts, so 12 volts, 24 volts and 36 volts loads are all fine.
  • FR120N: pick this one only for small loads at a high voltage, like 24 volts or 48 volts. It accepts up to 100 volts, but it gets hot above about 2A.

The 50A and 161A values in the table are limits of the chip alone. The small board cannot carry that much. Heat is the real limit. Let the load run for a few minutes, then touch the MOSFET with care. If it is too hot to hold your finger on, add a heatsink or use a smaller load.

How It Works

On the input side, there is a PC817 optocoupler. The signal from GP18 goes through a small resistor into a tiny LED inside the PC817. The light from that LED reaches a phototransistor, also inside the PC817, and the phototransistor drives the MOSFET gate. When the PC817 is off, a resistor pulls the gate down to 0 volts.

Because of the optocoupler, the Pico side and the load side are not wired together. The signal GND and the load GND are separate on the module. A spike or a mistake on the 12 volts side has a hard time reaching your Pico.

The MOSFET is an N-channel type. It sits between the LOAD terminal and the − terminal, on the negative wire of the load (a "low-side" switch). The load is connected between + and LOAD.

What happens next depends on what the Pico puts on GP18.

GP18 Gives 0 Volts (LOW)

  • The LED inside the PC817 gets no current, so it stays dark.
  • The phototransistor stays off. The gate resistor keeps the MOSFET gate at 0 volts.
  • The MOSFET is off. Between LOAD and −, it is like a switch that is open.
  • The load's negative wire has no way back to the adapter. No current flows, and the load gets 0 volts. It is off.
  • In MicroPython: pwm.duty_u16(0), which is 0% duty cycle.

GP18 Gives 3.3 Volts (HIGH)

  • A few milliamps flow through the LED inside the PC817, and it lights up. You cannot see this light, because it is inside the chip.
  • That is all the Pico has to do. A 3.3 volts GPIO pin is enough, and no extra transistor is needed.
  • The phototransistor turns on. Now the MOSFET gate gets about half of the load supply, so about 6 volts from a 12 volts adapter.
  • The MOSFET turns fully on. It is now like a closed switch with almost no resistance (a few mΩ for the AOD4184 and the LR7843, about 0.2Ω for the FR120N).
  • Current goes around the loop: adapter + → + terminal → load → LOAD terminal → MOSFET → − terminal → adapter −.
  • The load gets almost the full 12 volts and works at full power.
  • In MicroPython: pwm.duty_u16(65535), which is 100% duty cycle.

GP18 Gives a PWM Signal

  • The pin goes HIGH and LOW again and again, very fast. The code in this guide uses 500 Hz, so this happens 500 times every second.
  • The MOSFET does the same. It is on in each HIGH part and off in each LOW part. The load gets pulses of 12 volts.
  • The duty cycle is the part of the time that the signal is HIGH. In MicroPython, it is the duty_u16() number divided by 65535.
  • The average voltage and the average power are the duty cycle times the full value. Some examples with a 12 volts adapter:
duty_u16() value Duty cycle Load is on Average voltage
6553 10% 1/10 of the time about 1.2 volts
32767 50% half of the time about 6 volts
58981 90% 9/10 of the time about 10.8 volts
  • An LED strip looks dimmer. In fact it blinks, but too fast for your eyes. A fan, motor or pump runs slower, because the spinning part smooths out the pulses. Note: a fan may not start at a low duty cycle.
  • Keep the PWM frequency low, around 500 Hz to 1 kHz. The PC817 is not a fast part. With a too high frequency, the MOSFET does not switch fully, the load does not get clean pulses, and the MOSFET gets hot.
  • For a solenoid lock or a solenoid valve, use only 0 volts or 3.3 volts on GP18. Do not use PWM.

The gate gets only about half of the supply voltage. This is why the load supply must be at least about 6 volts. A 5 volts load supply does not work well. If your load draws more than a few amps, use 9 volts or more.

Pinout

AOD4184, LR7843 and FR120N MOSFET Module Pinout

The module has a 2-pin header (2.54mm, fits a breadboard) for the Pico, and screw terminals for the load:

  • PWM pin: connect to a GPIO pin of the Raspberry Pi Pico (GP18 in this guide). It takes the on/off or PWM signal.
  • GND pin: connect to any GND pin of the Raspberry Pi Pico (0 volts).
  • + terminal: connect to the positive wire of the load power supply (for example, 12 volts) AND to the positive wire of the load. Both wires go into this one terminal.
  • LOAD terminal: connect to the negative wire of the load. This is the side the MOSFET switches.
  • − terminal: connect to the negative wire of the load power supply (0 volts of that supply).

The labels may change a little from batch to batch. Always read the text printed on your own board.

Wiring Diagram

The Pico's 3.3 volts signal is enough to light the optocoupler, and the 12 volts from the adapter never touches the Pico.

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

This image is created using Fritzing. Click to enlarge image

Some tips before you power it on:

  • Never connect the 12 volts wire to any pin of the Raspberry Pi Pico.
  • Use a DC load only.
  • A load with a coil inside needs a flyback diode. See the next part.

More Loads You Can Wire

The LED strip is just one choice. Below are six common 12 volts loads. On the Pico side, nothing moves: GP18 still goes to PWM and a GND pin still goes to GND. All the changes happen at the screw terminals:

  • The adapter positive wire goes to +, and the adapter negative wire goes to −
  • The load positive wire goes to the same + terminal, and the load negative wire goes to LOAD

Keep these rules in mind for every load:

  • The adapter voltage must be the same as the load voltage. For a 12 volts load, use a 12 volts adapter. Never go below 6 volts, or the module will not switch well.
  • Look at the current of your load. The FR120N is only for small loads, about 2 amps with no heatsink. For a heavier load, use the AOD4184 or the LR7843.
  • Coil loads (fan, pump, motor, solenoid valve, solenoid lock) need a flyback diode, because the module has none. Use a 1N5819 or 1N4007 across the two load wires: the end with the stripe (cathode) to the load positive, the other end (anode) to the load negative.
  • You do not need new code. The same MicroPython script runs every load. Only for the two solenoids, set the power to 100% or 0% (full on or full off) and skip the fade.

How to Wire a 12 Volts Fan

The wiring diagram between MOSFET module  for a 12 volts cooling fan

This image is created using Fritzing. Click to enlarge image

  • Red wire is positive, black wire is negative.
  • A yellow third wire is for speed reading. Do not connect it.
  • Add the diode. It is cheap and does no harm.
  • PWM controls the speed. A fan may stay still at a very low duty cycle, so start at about 30% or higher.

How to Wire a 12 Volts Water Pump

The wiring diagram between 12 volts water pump and MOSFET module

This image is created using Fritzing. Click to enlarge image

  • A pump has a coil, so add the diode.
  • PWM controls how much water flows.
  • Do not run the pump with no water in it.

How to Wire a 12 Volts LED Strip

The wiring diagram between MOSFET module and one-color 12 volts LED strip

This image is created using Fritzing. Click to enlarge image

  • Strip positive (it often says 12V) to +, strip negative to LOAD.
  • No diode here. An LED strip has no coil.
  • PWM controls the brightness. A longer strip takes more current, so check it before you choose the module.
  • Only a one-color strip works like this. RGB and addressable strips do not.

How to Wire a 12 Volts Solenoid Valve

Wiring diagram of a normally closed 12 volts solenoid valve with the MOSFET module

This image is created using Fritzing. Click to enlarge image

  • This valve is normally closed. Full on opens it, off closes it.
  • Add the diode.
  • Use on and off only. Do not use PWM.

How to Wire a 12 Volts Solenoid Lock

12 volts solenoid lock wired to the MOSFET module

This image is created using Fritzing. Click to enlarge image

  • Add the diode.
  • Use full on (100%) or full off (0%) only, never PWM dimming.
  • The lock gets hot if it stays on. Turn it on for a short time to unlock, then turn it off.

How to Wire a 12 Volts DC Motor

The wiring diagram between MOSFET module  with a 12 volts DC motor

This image is created using Fritzing. Click to enlarge image

  • Motor positive to +, motor negative to LOAD, and the diode across the motor.
  • PWM controls the speed.
  • The motor turns in one direction only. To make it go both ways, you need a motor driver such as the L298N. Check the Raspberry Pi Pico - DC Motor tutorial.

Raspberry Pi Pico Code - MOSFET Module

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-mosfet-module """ from machine import Pin, PWM import time MOSFET_PIN = 18 # Pico GP18 connected to the PWM pin of the MOSFET module pwm = PWM(Pin(MOSFET_PIN), freq=500) # 500 Hz PWM, slow enough for the module's optocoupler pwm.duty_u16(0) # start with the load OFF def set_load(percent): pwm.duty_u16(percent * 65535 // 100) # convert 0-100% to 0-65535 print("Load power: {}%".format(percent)) 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)

The code makes PWM on GP18 at 500 Hz. It turns the load fully on for 2 seconds and fully off for 2 seconds. Then it raises the power from 10% to 100% in steps of 10%, one step every 0.5 seconds. After that, it lowers the power back to 0%, waits 1 second, and starts again. The duty_u16() function takes a number from 0 to 65535, so the code changes the percent into that range.

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pi Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Wire the components according to the provided diagram. Leave the 12 volts adapter unplugged for now.
  • Connect the Raspberry Pi Pico to your computer using a Micro USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port.
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, select Raspberry Pi Pico.
    • Save the file as main.py
  • Plug in the 12 volts power adapter.
  • Click the green Run button (or press F5) to run the script.
  • Look at the LED strip. It shines at full brightness for 2 seconds and goes dark for 2 seconds. Then it gets brighter little by little, and then dimmer little by little. With a fan, you will hear the speed go up and down.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 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%
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. If the LED strip does not light up, check that the load supply is at least 6 volts and that the negative wire of the load goes to the LOAD terminal, not to −.

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!