DEV Community

Cover image for Arduino Giga R1 WiFi MOSFET Module
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino Giga R1 WiFi MOSFET Module

A MOSFET module lets your Arduino Giga R1 WiFi switch and dim a 12V load, like an LED strip or a cooling fan, even though every Giga pin only works at 3.3V and a few milliamps. The module takes a small signal from a PWM pin and uses it to control the much bigger current coming from a separate 12V supply, so you can turn the load on and off or fade its power with analogWrite().

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

What you need

Wiring

Arduino Giga R1 WiFi MOSFET Module wiring diagram

The module has a small signal header with a PWM pin and a GND pin, which connect to a PWM-capable pin on the Giga and to any GND pin. Keep these as the only wires that touch the Giga. The load side uses a three-terminal screw block: + takes both the positive wire from the 12V adapter and the positive wire of the load, LOAD takes the negative wire of the load, and − takes the negative wire from the adapter. Keep the 12V wiring away from the Giga headers, since 12V on a board pin will damage it. If your load has a coil, like a motor, fan or pump, add a 1N5819 or 1N4007 diode across its wires, striped end toward +, to absorb the voltage spike when power is cut.

How it works

The PWM pin drives a resistor into an LED inside a PC817 optocoupler. That LED's light switches a phototransistor, which feeds the MOSFET gate, and a gate resistor pulls the gate to 0V whenever the optocoupler is dark. The MOSFET sits between LOAD and −, so it switches the negative wire of the load while the positive wire stays connected the whole time, a setup called a low-side switch. When the pin is LOW, the optocoupler stays dark, the MOSFET stays off, and the load sees 0V. When the pin goes HIGH or gets a PWM signal, the optocoupler lights up, the MOSFET turns on, and current flows through the load. This optical isolation is why no extra isolation parts are needed.

Code

The sketch needs no extra library, just functions from the Arduino core. You set the MOSFET pin as an output and start with the load off. digitalWrite() gives full on or full off, while analogWrite() with a value from 0 to 255 sets a power level in between, so map() is handy for turning a 0-100 percent value into that range. The example loop runs the load at full power, then off, then ramps up from 10% to 100% in 10% steps, ramps back down from 90% to 0%, and pauses before repeating. A solenoid is the one exception, since it should only ever get full power or none, skipping the fade steps.

arduino-giga-r1-wifi-mosfet-module.ino

/*
 * This Arduino Giga R1 WiFi code was developed by newbiely.com
 *
 * This Arduino Giga R1 WiFi code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-mosfet-module
 */

const int MOSFET_PIN = 2;  // Arduino Giga R1 WiFi 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

Match the adapter voltage to the load, a 12V device needs a 12V adapter, and don't go below 6V. Check the current draw of your device against the chip on your module: the FR120N only handles small loads, about 2A without a heatsink, while the AOD4184 or LR7843 handle more, and the LR7843 runs coolest but should stay under 30V. This module is for DC loads only, never connect AC mains. If a motor, fan or pump seems to misbehave or cause glitches, check that you added the flyback diode across its wires, since the module itself has no diode to catch that spike.

Full tutorial

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

Top comments (0)