I plan to drive a 51 ohm LED using the pi's 5v pin using a 2n2222 transistor connected to the GPIO. Do I need a resistor on the base pin of the transistor or can I connect it directly to a GPIO pin?



There are two answers to your question of "need":

  1. Yes - you need a resistor between the base junction of the transistor and the GPIO pin. Otherwise, the current flowing into the base junction of the transistor will exceed the GPIO's "safe" limits. In other words you may damage your RPi - you'll certainly not be following best practices.

  2. No - you can connect the GPIO directly to the base of your transistor. If you review a data sheet for the 2N2222, and you care to muddle through all the vagariessee below in the published GPIO specifications, it seems likely (to me) that you can drive the base of a 2N2222 transistor directly from a GPIO pin without destroying them immediately. But given that a resistor can be bought for a few cents, you should ask yourself if that's a risk you want to take.

In other words, this is the same situation you probably face every day with your car, your hob and all the other appliances you use. You have a choice: you can operate them within suggested limits, or you can push them to their limits.

Here are some references if you're interested in further reading:

  1. How a bipolar junction transistor operates.

  2. A similar Question & Answer on Electronics SE

The vagaries of GPIO current limits:

The "Official GPIO Documentation" on the RPi GPIO is written for the BCM2835 - the Broadcom processor used in Raspberry Pi 1 and Zero; ca. 2011. The Foundation has not yet updated their "Official GPIO Documentation" for the RPi 2, 3 or 4. This may mean the BCM2835 GPIO documents remain valid - or it may not. The same can be said for the explicit "Gert van Loo documents" - which are far more enlightening, but also dated.

The CM4 documentation addresses the BCM2711 (the RPi 4B SoC), and covers some of the details in the "Official GPIO Documentation", but not all. You can follow this question if you're interested in the possible update and clarification of the GPIO electrical characteristics.

What does all of this mean? While the ultimate limits of the Raspberry Pi GPIO are not as clear as some of us would like, there does seem to be general agreement that a GPIO pin can safely source or sink 16 mA.

  • GPIO current is NOT "limited". 16mA is the "safe" maximum, but if abused will supply much more. All models before the Pi4 have the same GPIO. – Milliways Jun 9 at 23:15
  • @Milliways: As a practical matter, all current is limited eventually, but if you've got a specific reference to support your comment, I'll gladly update my answer. – Seamus Jun 10 at 6:22
  • The linked document (authored by Gert Van Loo who is/was a Broadcom engineer) states "It is not: The current that the pad will deliver. It is not: A current limit so the pad will not blow up." Some of the links in raspberrypi.stackexchange.com/a/60219/8697 show that GPIO can deliver 200mA to a short, and my own tests show that much higher currents are possible, but not wishing to damage my Pi in didn't push it that far. The current is limited by the shortcomings of the drive circuitry but not to 16mA – Milliways Jun 10 at 6:59
  • Incidentally the GPIO-Pads-Control2.pdf document, which was once available for download and is now hidden behind a paywall can still be accessed GPIO-Pads-Control2.pdf – Milliways Jun 10 at 7:12
  • @Milliways: Thanks for the "van Loo Document" - my copy was lost. It does clarify the word salad in the "official" documentation. But there remains a bit of a question: It's written for the 2011-vintage BCM2835, instead of the current BCM2711. The only GPIO specs I've found on the BCM2711 are in the CM4 Datasheet (ref para 3.3). It does make sense to me that the "van Loo Document" may still be accurate, but I'd like to see that confirmed - perhaps it will be? – Seamus Jun 10 at 17:12

The short answer is YES. The longer answer is it depends.

In a common emitter configuration (the most common) you NEED to limit current. This needs to be sufficient to saturate the transistor. For a light load of 20 mA this is not critical. A value of 1kΩ would limit current to the nominal 3.3mA and drive loads of several hundred mA with most transistors.

In emitter follower configuration no resistor is needed and the emitter voltage would track base voltage. This has the disadvantage that excess voltage would be dissipated by the transistor as heat.

Incidentally there is no such thing as a 51 ohm LED.

  • 1
    Thanks for the answer, that's very helpful. You're right about the LED, I meant to say LED with a 51 ohm resistor – Nujra Jun 11 at 14:33

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