I need to control a LED with a Raspberry Pi!

The LED is a white one and it has to be PWM driven, in order to make it show a sort of "breathing effect".

Unfortunately I don't have the LED datasheet so I've empirically (using my bench power supply) found that the LED forward voltage is 3V approx. and that the brightness I want is obtained with 50mA approx.

So I can't drive the LED attaching it directly to a GPIO pin: Raspberry Pi is a 3.3V logic and I've read indeed that a GPIO pin can give a maximum of 16mA to them :(
I had to find another way.

I eventually come up with a circuit like the following one:

circuit image

I'm using an IRL540N MOSFET (link) to drive the LED through the Raspberry 5V power supply pin.

The MOSFET gate is attached to the GPIO pin through a resistor to reduce the (theoretically minimal already) current required from the GPIO.

I've also added a pull-down resistor in order for the gate charge to discharge to ground when GPIO is LOW.

Resistor R1 and R2 have respectively 10k and 100k Ohm values (I put them empirically).

I chose a 2.2 Ohm R3 resistor for the LED, this way I've found current values are pretty near to the one I want.


Now I have created 4 copies of the circuit mounted on a Raspberry Pi Adafruit Perma-Proto HATs (link).

Now I'm experiencing different behaviours for each of them. While two of them are working the way I wanted to be, the others are not.


  1. In one case the LED has been burnt after working for a while;
  2. The other last one showed a strange flickering pattern (not related to the PWM). It worked like that for a day then stopped working at all.

I'm supposing that in the first case, maybe the current through the LED was much higher than expected (maybe because of resistors, transistors tolerances?)

In the second situation, I'm not able to figure out what the problem can be. It seems a transistor problem but I not sure (I have only a limited knowledge of electronics).

What am I doing wrong?

Has someone already some experience in setups like that?

Any help will be much appreciated


  • 1
    \$\begingroup\$ "The MOSFET gate is attached to the GPIO through a resistor to reduce the ... current required from the GPIO" No. I'm afraid you are wrong. It's used to damp parasitic oscillation, which is not good. \$\endgroup\$
    – Long Pham
    Commented Aug 28, 2018 at 17:31
  • \$\begingroup\$ What frequency and duty cycle are you running your PWM? With only a 2.2Ω resistor, you could be over-currenting your LED by quite a bit. \$\endgroup\$ Commented Aug 28, 2018 at 17:37
  • \$\begingroup\$ @evildemonic "quite a bit" is pretty modest. \$\endgroup\$
    – Long Pham
    Commented Aug 28, 2018 at 17:47
  • \$\begingroup\$ LED's should not be so hot that it burns your fingers. Do you know how to describe the case size? or buy a LED with a datasheet? Rpi can drive 50 mA to cathode easily with anode + R to 5V \$\endgroup\$ Commented Aug 28, 2018 at 19:21
  • \$\begingroup\$ Note to other readers here on Stack Exchange: The same question has also been asked by the OP at the All About Circuits forum, so more information or other answers may appear there instead of here. \$\endgroup\$
    – SamGibson
    Commented Aug 28, 2018 at 21:59

2 Answers 2


So many things wrong.

For one, the led probably isn't suited to 50mA unless it's not a standard 5mm led. Typical led forward current is 20mA. Or even if your type of led is suited to 50mA, it requires heat sinking or you got a counterfeit that won't last at 50mA like a real one.

Second, at a Vf of 3V with an If at 0.05A, and a source voltage of 5V, you have I = (Vs - Vf) / R, or ( 5 - 3 ) / 2.2 = 2 V / 2.2 ohms = 0.9 Amps. Or almost 20 times the current going through that resistor, and such the led.

  • 1
    \$\begingroup\$ thank you @Passerby, unfortunately I've not such an experience in using transistors so thank you for your math! Yes indeed I've placed a much greater resistor in series with the LED and the circuit seems to work well, without problems. -best regards- \$\endgroup\$
    – moscardo
    Commented Sep 3, 2018 at 8:57

I recently needed to drive a high current output and used a uln 2803a. These are 8 darlington drivers in one dil package. There is no need of any resistors to drive the input and the output will sink up to 500 mA. If you use a reasonable wattage led resistor you should be able to pulse it ok. Although the uln 2803a is an older product, it is reliable and fast enough for your application. I bought 50 for £5 on ebay so they are also really cheap.

  • \$\begingroup\$ Thank you @peewee for your useful informations. best regards \$\endgroup\$
    – moscardo
    Commented Sep 3, 2018 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.