LightingHow-to

Drive an LED from a GPIO pin with a MOSFET: resistor current sizing

A GPIO pin supplies a few milliamps, not enough for a bright LED. Drive it from a MOSFET switch on an external rail; the resistor sets the LED current.

LumaSpec Vision10 min read Share on LinkedIn

On this page 9 sections
  1. 01Why the pin is dim: the numbers
  2. 02The circuit: a low-side MOSFET switch
  3. 03Resistor math: two worked examples
  4. 04Wire it: step by step
  5. 05Verify it with a meter
  6. 06What overloading the pin really does
  7. 07When the load is a machine-vision light
  8. 08Pre-power checklist
  9. 09Questions

An indicator LED wired straight to a controller's GPIO comes up dim, and no firmware setting brightens it. The pin is not broken: its output stage is rated for a few milliamps, while a standard 5 mm LED needs about 20 mA for full brightness. The fix is to stop asking the pin to act as a power supply. Drive a logic-level MOSFET from the pin, feed the LED from an external rail, and let one series resistor set the current. This page gives the pin limits, the switch circuit, the resistor math at 5 V and 24 V, the wiring order and the meter checks that prove it.

Why the pin is dim: the numbers

Data sheets rate GPIO pins in milliamps, not in lamp loads. Many classic 8-bit microcontrollers allow 20 mA continuous per pin with a 40 mA absolute maximum. Some 32-bit families allow 25 mA per pin but cap a whole port bank at around 120 mA. The headers on small single-board computers are typically limited to 16 mA per pin and 50-100 mA per header in total. Compact vision controllers are often lower still: expansion pins are specified at a few milliamps, and 4-8 mA is a common figure.

A standard 5 mm indicator LED is specified at 20 mA, with a forward voltage near 2.0 V for red and 3.1-3.3 V for white or blue. At these levels brightness tracks current almost linearly, so 5 mA on a 20 mA part looks about a quarter as bright. That is the symptom you are seeing.

The sag happens inside the pin. Its output transistor has tens of ohms of on-resistance, so the pin voltage drops under load: 10 mA through 50 Ω loses 0.5 V before the LED sees any of it. Less voltage means less branch current, which means less light. For the same limits tabulated with the matching resistor arithmetic, the GPIO current limits and LED resistor sizing reference goes deeper.

The circuit: a low-side MOSFET switch

The standard answer is a low-side N-channel MOSFET switch. The LED anode goes to the positive rail. The cathode goes through the series resistor to the MOSFET drain, the source goes to ground, and the gate goes to the GPIO. When the pin goes high, the MOSFET conducts and the rail, not the pin, supplies the LED current.

Size the part with three numbers:

  • Gate drive: a logic-level part, meaning RDS(on) is specified at a gate voltage of 2.5-3 V. A low threshold voltage alone proves nothing; the on-resistance test condition is what matters.
  • Voltage: VDS rated at least twice the rail. Every small-signal part clears 5 V easily; pick 60 V or more for a 24 V rail.
  • Current: continuous drain current at least three times the LED current, so a 1 A part covers indicators up to roughly 300 mA.

Put a 100 Ω resistor between the GPIO and the gate to keep the switching edge tidy, and a 10 kΩ resistor from gate to ground to hold the MOSFET off while the controller boots with its pins floating. The gate is a small capacitor: the pin shifts a few nanocoulombs per edge and the steady-state gate current is zero. All of the LED current flows through the drain; none of it passes through the pin.

If your parts bin is full of NPN transistors, the same layout works with a BJT. Saturation wants roughly a tenth of the collector current as base drive, so 20 mA of LED current needs 2 mA into the base: (3.3 - 0.7) V / 2 mA = 1.3 kΩ. The MOSFET stays the default because it loads the pin with nothing at all in steady state.

Resistor math: two worked examples

R = (V_supply - V_f) / I_LED P_R = I_LED² × R

Example 1, a red indicator on a 5 V rail with Vf = 2.0 V and a 15 mA target:

  • R = (5.0 - 2.0) V / 0.015 A = 200 Ω, a standard value
  • P = (0.015 A)² × 200 Ω = 45 mW, comfortable on a 0.125 W part

The MOSFET's on-drop is a few millivolts at this current, so it does not move the result.

Example 2, a white indicator on a 24 V cabinet rail with Vf = 3.2 V and a 20 mA target:

  • R = (24.0 - 3.2) V / 0.020 A = 1040 Ω, so fit the standard 1.1 kΩ
  • I = 20.8 V / 1100 Ω = 18.9 mA, inside the target
  • P = (0.0189 A)² × 1100 Ω = 0.39 W, so fit a 0.5 W resistor

At 24 V the resistor turns roughly 0.4 W into heat. That is fine for an indicator and is exactly why a lamp is not run this way.

Wire it: step by step

  1. Pick the rail. One 15-20 mA indicator can run from the board's own 5 V pin: the total budget on a USB-powered controller is roughly 500 mA and the board spends most of it. Anything brighter, several loads or a buzzer belongs on an external supply. The trade-offs are compared in Camera power: USB vs external 5 V, which should you use?
  2. Tie the grounds. Run the external supply's 0 V terminal to a ground pin on the controller. Without a common reference the gate voltage is undefined and the LED glows faintly or flickers on its own.
  3. Place the MOSFET low-side: source to the shared ground, drain toward the LED branch. Gate, drain and source ordering changes between packages, so read it off the part's pinout diagram once and label the wiring.
  4. Add the gate network: 100 Ω from the GPIO to the gate, 10 kΩ from gate to ground.
  5. Build the LED branch: anode to the positive rail, then cathode, series resistor and drain, in that order.
  6. Wire a buzzer into the same circuit unchanged. A piezo type needs nothing extra; a magnetic (coil) type gets a flyback diode across it, cathode toward the positive terminal, so the collapsing field cannot spike the drain.
  7. Set the boot state. Initialize the pin as a push-pull output driving LOW in the first lines of firmware setup. The 10 kΩ pulldown already holds the LED off through reset and boot; the firmware default keeps it dark afterwards too.

The firmware side is two calls in any SDK:

/* pseudo-code: use your own SDK's GPIO calls */
int led_gate = 7;             /* output, initialized LOW */
gpio_set_output(led_gate, LOW);
gpio_write(led_gate, 1);      /* gate high: LED on */

Verify it with a meter

Four readings confirm the circuit:

  • Gate to ground with the pin commanded high: about 3.3 V, or your controller's I/O voltage. Pin low: 0 V, held by the pulldown.
  • Drain to ground with the LED on: a few millivolts. A reading of a volt or more means the MOSFET is not fully on: check gate drive or the pinout.
  • Current with the meter in series in the LED branch: within about 10% of the design value, with resistor tolerance covering the difference.
  • Rail voltage at the LED anode while on: equal to the supply setpoint. If it sags under load, the supply is undersized for everything hanging on it.

An indicator that measures correct current but still looks dim is being pulsed with a low duty cycle; check the firmware timing before the hardware. The same habits apply to every trigger and I/O line on the machine: GPIO wiring damage prevention and fault triage covers the fault patterns.

What overloading the pin really does

Nothing dramatic at first, which is what makes it dangerous. The output stage heats, its effective on-resistance climbs, and drive strength quietly degrades. Bond wires inside the package fuse in the tens of milliamps on small parts. Exceed a port bank's total on several pins at once and the internal rail droops far enough to reset the processor mid-run. Pins also die outright, showing up later as an I/O line that no longer toggles; the walkthrough of a GPIO pin killed by LED drive shows that failure from the bench side.

When the load is a machine-vision light

Scale the same idea up to a machine-vision light and the current moves tens to hundreds of times higher. A ring, bar or coaxial light draws hundreds of milliamps to several amps, pulsed bright and short inside the camera exposure, under duty-cycle and thermal limits that keep the LEDs alive. Building that stage by hand means solving LED overdrive and current control, strobe timing and droop compensation on your own board. A light controller is the engineered version of the same three functions: constant-current regulation replaces the resistor, a strobe input fires the pulse inside the exposure, and the duty-cycle matching that prevents thermal foldback is designed in. The startup droop you would otherwise chase on a scope is handled inside the controller design.

For a 24 V panel with one or two strobed lights, a 4-channel constant-current unit rated 120 W in a 89 x 147 mm housing covers the case:

Light controllersConstant current controller, 4 ch, 120 W, 89 x 147 mm, for 24 V lights (LS-CTL-0180)LS-CTL-0180
Output
Constant current
Channels
4 channels
Control
Digital
Feature
Constant current
$359.00View partBuild a system with it

If the trigger arrives from a PLC over a serial link, a digital box with the same 4-channel, 200 W envelope plus RS232 and strobe support wires straight in:

Light controllersDigital control box, 4 ch, 200 W, RS232 (LS-CTL-0070)LS-CTL-0070
Output
Constant voltage 24 V
Channels
4 channels
Control
Digital
Feature
Strobe
$549.00View partBuild a system with it

High-power strobing needs current headroom from the first design pass: a 2-channel high-power constant-current unit with 240 W per channel, 320 W total, is sized for it:

Light controllersHigh power constant current controller, 2 ch, max. output power 240 W per channel, 320 W (LS-CTL-0054)LS-CTL-0054
Output
Constant current
Channels
2 channels
Control
Analog
Feature
Constant current
$549.00View partBuild a system with it

Whichever controller you choose, feed it from an enclosed industrial supply rather than a bench unit. The shop's power supplies are enclosed 12 / 24 / 48 V units for cameras, lights and controllers; the LRS-75-48, for example, outputs 48 V at 76.8 W.

Power suppliesMEAN WELL LRS-75-48 Industrial Power Supply, 48 V 76.8 WLRS-75-48
Type
Power cords and supplies
$35.00View part

Between controller and light, run a proper light extension cable instead of loose hookup wire. The connector standard and the current rating have to agree at both ends, which the controller-to-light cable and adapter guide walks through.

Pre-power checklist

  • Grounds tied: 0 Ω measured between supply 0 V and controller GND with everything off. The gate has no reference without it.
  • Dark at boot: 10 kΩ pulldown fitted and the pin initialized LOW, so reset and crashes leave the LED off.
  • Resistor sized for the rail: 200 Ω at 5 V, or 1.1 kΩ at 0.5 W at 24 V, per the worked examples above.
  • Flyback diode on every coil: magnetic buzzers and relays spike the drain without one.
  • Current measured, not assumed: a reading within ±10% of design proves the resistor, not the pin, sets the brightness.

The decision in one line: the pin switches, the rail powers, the resistor sets the current. An indicator needs two resistors, one MOSFET and one LED. When the load grows into a vision light, start at the light controller finder or the controller configurator instead of etching a driver board, and the lighting guides collect the exposure-side technique. To have a channel count and power budget checked before you order, contact the applications team.

Questions

Why is my LED dim when I drive it straight from a GPIO pin?

The pin's output stage is current-limited to a few milliamps and its voltage sags under load, so the LED lands near 4-8 mA against a 20 mA rating, roughly a quarter of full brightness. The pin is healthy; it was never meant to be a power supply. Move the load to an external rail behind a MOSFET and a series resistor and the brightness returns.

What MOSFET should I use with a 3.3 V logic pin?

A logic-level N-channel type: one whose RDS(on) is specified at a gate voltage of 3 V or less, not merely one with a low threshold. Rate VDS at twice your rail and current at three times the LED current; a 60 V, 1 A small-signal part clears any indicator with margin. Keep the 100 Ω gate resistor and the 10 kΩ pulldown.

Can I power the LED from the board's 5 V pin instead of an external supply?

For one 15-20 mA indicator, usually yes: a USB-powered controller has roughly a 500 mA total 5 V budget and the LED is a small slice of it. For anything brighter, for several indicators or for a buzzer, take an external rail. The board's regulator also feeds the processor, the camera and every transceiver, so its margin is worth keeping.

Do I still need the series resistor if the MOSFET is fully on?

Yes. The MOSFET is a switch, not a current regulator. With the gate fully enhanced, current is set only by the internal resistances of the LED and the MOSFET, and both run past their ratings until something fails. The series resistor stays the only part that sets the LED current, no matter how hard the gate is driven.

How do I drive a buzzer from the same kind of output?

Use the same low-side MOSFET circuit. A piezo buzzer needs nothing beyond its rated voltage. A magnetic buzzer or any relay coil is inductive: add a flyback diode directly across it, cathode to the positive terminal, so the collapsing field circulates through the diode instead of spiking the MOSFET drain. Any small-signal diode handles a buzzer's flyback pulse.