4

I'm following this tutorial on how to control Raspberry Pi GPIO pins with Python scripts. Instead of connecting the test circuit (LED + resistor) between the ground and the GPIO pin however, they connect it between the +3v pin and GPIO (see section 4, last paragraph). The author says this is to have the current come from the 3v pin rather than the GPIO pin. However, from my limited understanding of electricity, the magnitude of the current through the GPIO pin should be the same regardless of how the polarity has been set, the only difference would be its direction.

Am I wrong about the current? Is it actually better to draw power from the 3v pin instead of the GPIO pin?

user1586
  • 674
  • 4
  • 15
Carlton
  • 3,064
  • 2
  • 15
  • 28

3 Answers3

2

This also has a historic reason:

Early microcontrollers' GPIO outputs were designed as open-collector pins with pull-up resistor because the technology used back then (NMOS) did not allow a different design.

This means that inside the microcontroller there was:

  • A "large" resistor (typically much larger than 1k) between the GPIO pin to the positive supply voltage
  • A transistor between the GPIO pin and the ground

(See the picture below)

If you wanted to use the GPIO pin as output, you had two possibilities as shown in this picture:

enter image description here

(Note that the resistance of Rm is too large to use it in series to the LED. so R2 had to be in parallel of Rm.)

  • The first method is the way it is done in your Raspberry Pi circuit.
    Using the resistor R1 you can exactly control the current through the LED.
  • The second method would be to shortcut the LED using the transistor inside the microcontroller (right side of the picture).
    The value of the resistor Rm had a very broad distribution (e.g. "may be between 2k and 10k") so you had no exact control over the current through the LED.
    And when the LED was off, the current consumption of the entire circuit was even higher compared to the circuit with a LED that was on!

So for these reasons you would have preferred the method on the left side in any case.

If you use a microcontroller that has two transistors at each GPIO pin (so-called "push-pull"), you can of course also connect the LED between the GPIO pin and ground.

However, many people still prefer the "old" way of doing it - especially because microcontrollers using only one transistor at each GPIO pin are still produced!

2

I think the reason is if the GPIO is used the micro is starved of power.

In the method described 3.3V is the power source. So the micro is not starved of power. If the 3.3V was provided by the GPIO pin, the micro is sourcing the power.

I am generally used to seeing a transistor used to switch on/off elements like a LED. Below is an example schematic.

LED Schematic

Further details can be found in The bipolar transistor article.

706Astor
  • 1,064
  • 1
  • 8
  • 19
user1586
  • 674
  • 4
  • 15
1

Early TTL outputs (those typical of 74xx00 series logic gates) were capable of sinking (creating a current path to ground) more current than they could source (creating a current path to VCC). If you wanted a predictable current flow which you could set by your choice of external current limiting resistors then the output transistor path to ground did a better job.

As far as I know this is still true.

Entrepreneur
  • 446
  • 4
  • 6