Built-in project · Arduino Uno R3

3 W power LED on pin 13, on an Arduino Uno

A 3 W white power LED, switched by a MOSFET module whose trigger is on pin 13, blinks once a second from the Uno’s 5 V through a 4.7 ohm resistor. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Why a pin cannot drive a power LED directly
  • A MOSFET module as a switch
  • Setting LED current with a series resistor
  • Blinking with millis() instead of waiting
  • Sharing ground between module and board
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A 3 W power LED needs hundreds of milliamps, far more than an Uno pin can give. So pin 13 only drives the trigger of a D4184 MOSFET module, and the module switches the LED's current straight from the 5 V supply.

A 4.7 ohm resistor sets that current. The sketch's math gives about (5 - 3.4) / 4.7, or 340 mA, which is half the LED's rating.

The LED starts on. Every second, loop() flips the gate pin and prints whether the gate is high or low. The result is a steady one-second blink.

Parts list

5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
D4184 dual MOSFET switch module pin GNDArduino Uno R3 pin GND
D4184 dual MOSFET switch module pin VIN-Arduino Uno R3 pin GND
D4184 dual MOSFET switch module pin TRIGArduino Uno R3 pin 13
D4184 dual MOSFET switch module pin VIN+Arduino Uno R3 pin 5V
Resistor, 4.7 Ω pin 1D4184 dual MOSFET switch module pin OUT+
Power LED, 1 W or 3 W star pin -D4184 dual MOSFET switch module pin OUT-
Power LED, 1 W or 3 W star pin +Resistor, 4.7 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A 3 W power LED, blinking on pin 13.
//
//   MOSFET module TRIG -> pin 13   GND -> GND
//   module VIN+ -> 5V   VIN- -> GND
//   module OUT+ -> 4.7 ohm -> LED +   LED - -> module OUT-
//
// A power LED wants hundreds of milliamps, far more than the 20 mA a pin can
// give, so the pin only drives the gate of a MOSFET and the MOSFET switches
// the LED's current straight from the 5 V supply. The 4.7 ohm resistor sets
// that current: about (5 - 3.4) / 4.7, or 340 mA, half the LED's rating,
// which is how a small 3 W module runs it from 5 V.

// The MOSFET's gate is on pin 13. Instead of delay(), the sketch keeps the
// time it last switched and flips the gate once a second has passed, so
// loop() is free for anything else a lamp might need, like a button.

const int GATE_PIN = 13;
const unsigned long PERIOD_MS = 1000;

bool lit = true;
unsigned long switchedAt = 0;

void setup() {
  pinMode(GATE_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: 3 W LED on pin 13, through a MOSFET");
  digitalWrite(GATE_PIN, HIGH);
  switchedAt = millis();
}

void loop() {
  if (millis() - switchedAt < PERIOD_MS) return;
  switchedAt += PERIOD_MS;
  lit = !lit;
  digitalWrite(GATE_PIN, lit ? HIGH : LOW);
  Serial.println(lit ? "gate high: the LED draws about 340 mA" : "gate low: the LED is dark");
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

It makes GATE_PIN (pin 13) an output and opens serial at 9600. Then it sets the pin HIGH so the LED starts lit, and saves the current millis() as the time of the last switch.

void setup() {
  pinMode(GATE_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: 3 W LED on pin 13, through a MOSFET");
  digitalWrite(GATE_PIN, HIGH);
  switchedAt = millis();
}

loop sketch.ino

If less than PERIOD_MS (1000 ms) has passed since the last switch, it returns right away. Otherwise it moves switchedAt forward by one period, flips lit, and writes the new level to the gate. It prints what happened. No blocking wait is used, so the loop stays free for other jobs like a button.

void loop() {
  if (millis() - switchedAt < PERIOD_MS) return;
  switchedAt += PERIOD_MS;
  lit = !lit;
  digitalWrite(GATE_PIN, lit ? HIGH : LOW);
  Serial.println(lit ? "gate high: the LED draws about 340 mA" : "gate low: the LED is dark");
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED never lights This project
    Check that TRIG goes to pin 13, VIN+ to 5V, and both GND and VIN- to ground.
  • You want it brighter This project
    A smaller resistor passes more current. Stay within the LED's 3 W rating, and remember the resistor and the 5 V supply must handle the heat.
  • The load never turns on, or it is only dim. D4184 dual MOSFET switch module
    Check the load's own supply is wired to the screw terminals and the control pin gets 3.3 V or 5 V. Join the grounds of the controller and the load supply together.
  • Parts get hot or wires melt at high current. D4184 dual MOSFET switch module
    The board can carry high current only with thick wire and good connections. Keep the load supply within the module's rated voltage, and use a flyback diode across motors and other coils.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • A power LED is wired straight to a microcontroller pin or with no resistor. It draws 350 or 700 mA and burns up or kills the pin. Power LED
    Drive it with a logic level MOSFET and a current limiting resistor sized for the rated current, from a proper supply. Join the grounds.
  • The LED or the resistor overheats and it fades or fails. A 1 W or 3 W LED makes a lot of heat on a small area. Power LED
    Mount the star on a heatsink with thermal paste. Rate the resistor for the power: I squared times R. Never run it without cooling for more than a moment.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.