Author Topic: Should I always use a transistor for a GPIO output?  (Read 5170 times)

0 Members and 2 Guests are viewing this topic.

Offline laxamarTopic starter

  • Contributor
  • Posts: 37
  • Country: us
Should I always use a transistor for a GPIO output?
« on: September 17, 2022, 11:34:09 pm »
I'm designing for a Raspberry Pi(4). When using a GPIO pin for output, I've always used a transistor to propagate the signal and "prevent reverse current". Honestly, I just copied stuff on the internet and since I've always added an LED to monitor the status of the signal, it always made sense.

My new project is rather higher speed and I won't be using an LED to visually monitor the signal. I'll be using an oscilloscope instead. And the GPIO output is going straight to an IC.

My question is should I continue using a transistor in front of the output or can I safely skip it? It would simplify the design (and marginally the cost)

I'm also going to be using I2C programming and I'm worried the transistor is actually going to degrade and slow the signal on those 2 pins.

What are the pros and cons?
« Last Edit: September 17, 2022, 11:35:40 pm by laxamar »
 

Offline rfclown

  • Frequent Contributor
  • **
  • Posts: 461
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #1 on: September 17, 2022, 11:44:26 pm »
You only need to use a transistor if it is needed. To determine that you need to look at the data sheets of the two devices that you are interfacing. Are the voltage levels the same? What current is required? I2C requires a open drain (or open collector) output. The two most common reasons for needing to use a transistor are the need for voltage level translation, and the need for a higher current than the device can source.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12455
  • Country: us
    • Personal site
Re: Should I always use a transistor for a GPIO output?
« Reply #2 on: September 17, 2022, 11:55:29 pm »
You can't just stick a transistor on I2C bus like this, it won't work.
Alex
 

Offline rfclown

  • Frequent Contributor
  • **
  • Posts: 461
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #3 on: September 18, 2022, 02:11:56 am »
like Alex says, I2C in particular won't work like you have it in the schematic. Google I2C app note and you will find a TI publication that goes over that interface. The SDA line is both an output and an input. If you need to do a voltage level translation between two I2C devices, there are ICs made for that function.
 


Online ataradov

  • Super Contributor
  • ***
  • Posts: 12455
  • Country: us
    • Personal site
Re: Should I always use a transistor for a GPIO output?
« Reply #5 on: September 18, 2022, 05:10:37 am »
But in this case all of this was done not for level shifting, but to reduce pin load. And none of this is necessary, GPIOs can drive I2C bus directly. 
Alex
 

Offline laxamarTopic starter

  • Contributor
  • Posts: 37
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #6 on: September 18, 2022, 10:09:53 pm »
Thanks for the info on I2C. I am reading something about open collector that I don't understand yet, but if the signals are bidirectional, definitively no transistor there. The OLED I will be using on I2C works with 3.3V.

As for the rest, the IC is a shift register and doesn't need any signal cleanup. Direct makes sense again.

All that's left is for the LEDs. Again, I copied this form the internet when I started. They suggested not to connect an LED directly, but through a transistor. I wonder if that's needed on GPIO also. Much simpler and cleaner design.

 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12455
  • Country: us
    • Personal site
Re: Should I always use a transistor for a GPIO output?
« Reply #7 on: September 18, 2022, 10:14:06 pm »
You need to find the GPIO drive strength (maximum current it can source or sink safely) and compare it to the current you want to drive though the LED. Most modern general purpose indication LEDs can be directly driven by the GPIO pins. More bright lighting LEDs would need a separate buffer.
Alex
 

Offline John B

  • Super Contributor
  • ***
  • Posts: 1051
  • Country: au
Re: Should I always use a transistor for a GPIO output?
« Reply #8 on: September 19, 2022, 03:37:56 am »
Personally I do use a transistor for LEDs. It's much easier to ensure you don't exceed GPIO bank limits that might catch you out once you start adding a lot of IO. Even a SOT-23 3904 will do.

But keep in mind you're using NPN transistors in an emitter follower configuration. So you'll drop another 0.6-0.7V off the RPis 3.3V output, which won't be enough voltage for any and every LED colour.
 

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 5965
  • Country: nl
Re: Should I always use a transistor for a GPIO output?
« Reply #9 on: September 19, 2022, 05:40:43 am »
You need to find the GPIO drive strength (maximum current it can source or sink safely) and compare it to the current you want to drive though the LED. Most modern general purpose indication LEDs can be directly driven by the GPIO pins. More bright lighting LEDs would need a separate buffer.

Don't forget about the current limiting series resistor though. :-DD  https://www.eevblog.com/forum/chat/software-guys-please-no/?topicseen

And the total current limit for all the GPIO's together.

And instead of transistors use mosfets to do the heavy lifting. Then there is almost no loss and no current drawn from the gpio, except for switching current into the gate capacitor, but that is not an issue here.
« Last Edit: September 19, 2022, 05:46:21 am by pcprogrammer »
 

Offline laxamarTopic starter

  • Contributor
  • Posts: 37
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #10 on: September 19, 2022, 06:11:37 pm »
But keep in mind you're using NPN transistors in an emitter follower configuration. So you'll drop another 0.6-0.7V off the RPis 3.3V output, which won't be enough voltage for any and every LED colour.

I can vouch, from personal experience and having built a PCB that a single blue LED will render all other blue LEDs useless  :D

 

Offline laxamarTopic starter

  • Contributor
  • Posts: 37
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #11 on: September 19, 2022, 06:13:25 pm »
And instead of transistors use mosfets to do the heavy lifting. Then there is almost no loss and no current drawn from the gpio, except for switching current into the gate capacitor, but that is not an issue here.

This is going on an SMD PCB. I have no experience with mosfets. Would you have an exact part no I can lookup?
 

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 5965
  • Country: nl
Re: Should I always use a transistor for a GPIO output?
« Reply #12 on: September 19, 2022, 06:57:38 pm »
2n7000, 2n7002 or 2n7002A are available in smd and work very well for up to 280mA continuous and can handle 60V max. On resistance about 1.5 ohms typical. Gate threshold 2.1V typical.

https://www.onsemi.com/pdf/datasheet/nds7002a-d.pdf

For more current there are plenty others to be found.

Offline wraper

  • Supporter
  • ****
  • Posts: 19401
  • Country: lv
Re: Should I always use a transistor for a GPIO output?
« Reply #13 on: September 19, 2022, 07:04:36 pm »
Using emitter followers like this reduces output voltage by about 0.7V which may be a problem. Simply using a series resistor (if strong output is not required) is enough of pin protection in most cases.
 

Offline laxamarTopic starter

  • Contributor
  • Posts: 37
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #14 on: September 19, 2022, 09:55:41 pm »
2n7000, 2n7002 or 2n7002A are available in smd and work very well for up to 280mA continuous and can handle 60V max. On resistance about 1.5 ohms typical. Gate threshold 2.1V typical.

https://www.onsemi.com/pdf/datasheet/nds7002a-d.pdf

For more current there are plenty others to be found.

I am not good at reading datasheets, but isn't this for higher voltage and current designs? I'm actually using very low power (20mA) and source V is 3.3V.  Doesn't 2.1V gate threshold pretty much kill off my signal?
I'm sorry if I'm misunderstanding this
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12455
  • Country: us
    • Personal site
Re: Should I always use a transistor for a GPIO output?
« Reply #15 on: September 19, 2022, 10:07:25 pm »
I am not good at reading datasheets, but isn't this for higher voltage and current designs?
280 mA is the maximum current, nothing is stopping you using that with 20 mA. Those are pretty generic low power MOSFETs.

Doesn't 2.1V gate threshold pretty much kill off my signal?
Gate threshold is the minimal voltage between the gate and the source that is needed to turn the transistor on. You would have 3.3 V, which is greater than 2.1 V, so you are good.


Alex
 

Offline John B

  • Super Contributor
  • ***
  • Posts: 1051
  • Country: au
Re: Should I always use a transistor for a GPIO output?
« Reply #16 on: September 19, 2022, 10:17:16 pm »
Perhaps you're thinking of arranging the MOSFET like you did with the NPN transistor, ie in a "source follower" configuration.

Generally you will use the MOSFETs in a low side arrangement, where the LED and resistor will connect to the drain of an N channel MOSFET, then the source will connect to ground. Then you only need to ensure that the driving signal is capable of at least the gate threshold voltage above ground. In this case your 3.3V signal is fine for the 2.1V threshold.

For powering LEDs, there's no appreciable efficiency benefit between MOSFETs or BJTs, though BJTs are generally cheaper. If you have a few LEDs to light you can even consolidate the transistors into a single IC. I use ULN200x darlington arrays. They're cheap, easy to obtain, made by different manufacturers and it simplifies the circuit as they have built in resistors and a common emitter connection. The ULN2003 is generally driven by a 5V signal, although it will work fine at 3.3V if you are sinking small current loads like an LED.
 

Offline rfclown

  • Frequent Contributor
  • **
  • Posts: 461
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #17 on: September 19, 2022, 10:25:21 pm »
An emitter follower (what you show) does not pull the output low. It can only source current, not sink current. If you are interfacing to a CMOS input (implied by your use of the term "gate threshold"), you want to be able to supply voltage to the gate (to turn it on), and sink current out of the gate (to turn it off). You would not use a follower circuit for this.

If your signals are going to other digital i/o of the same 3.3v level, then remove the transistors. If you want an LED you can still do that without the transistor if you keep the current low by putting in a series resistor. Most LEDs are plenty easy to see only 1mA of current. The default drive strength of a Pi i/o is 8 mA. You find your LED voltage drop, say it is a 2.0v red LED. Then the current would be I=(3.3-2.0)/R, or written to solve for R, R=(3.3-2.0)/I. So if you want 1mA, R would be (3.3-2.0)/0.001 = 1300 ohms. Or you just grab a 1k resistor since that's more likely in your stash, and the current would be 1.3 mA.
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3993
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #18 on: September 19, 2022, 10:26:29 pm »
Here's a previous thread on what "jellybean" transistors are useful for a hobbyist to have:

https://www.eevblog.com/forum/beginners/jellybean-hobbyist-general-purpose-transistors/
 

Offline bson

  • Supporter
  • ****
  • Posts: 2753
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #19 on: September 20, 2022, 08:34:25 pm »
They're not used as emitter followers as there is no emitter resistor, they're just plain high-side switches.

MOSFETs make better switches than BJTs.

I2C is pull-up, so a P-FET follower would work better.  When released (high) the line floats up.  When the gate is low it pulls the output down.  Something like a BSS84 will do the job.  You need pull-ups for the I2C bus anyway.  Obviously this is unidirectional, so you can't receive data from a device, and it can't clock stretch, but it gives you some minimal pin isolation, especially if combined with a 3.3V TVS diode across source and drain.

If it were me I'd find something that has an SPI interface instead.
 

Offline wraper

  • Supporter
  • ****
  • Posts: 19401
  • Country: lv
Re: Should I always use a transistor for a GPIO output?
« Reply #20 on: September 20, 2022, 08:41:59 pm »
They're not used as emitter followers as there is no emitter resistor, they're just plain high-side switches.
Plain high side switches would be with PNP transistors and collector/emitter swapped. No resistor in the circuit shown? Does not mean there will be no resistor in practice. They simply cannot work without a load between emitter and GND. Also there already is such load for Q3 and Q4.
 

Offline John B

  • Super Contributor
  • ***
  • Posts: 1051
  • Country: au
Re: Should I always use a transistor for a GPIO output?
« Reply #21 on: September 21, 2022, 12:02:37 am »
A small benefit with using an NPN as a high side switch, as long as you have sufficient voltage at the emitter for an LED, is that you can omit a base resistor and just have a current limiting resistor on the LED. The comparable counterpart would be a PNP as a low side switch. Saves a fraction of board space.
 

Offline laxamarTopic starter

  • Contributor
  • Posts: 37
  • Country: us
Re: Should I always use a transistor for a GPIO output?
« Reply #22 on: September 21, 2022, 12:26:40 am »
If it were me I'd find something that has an SPI interface instead.

I had 16 pins bus across multiple boards that I was using. I changed the design and used shift register ICs that actually simplifies the design and "liberated" a couple of bus lines. I suddenly have the bandwidth and luxury to do I2C. Really don't need SPI for my needs, just going to use a 0.9" I2C OLED circuit ( a whopping $1.25) to display stuff I used to use 7SEG and LEDs. Of course, with feature creep, I will add a logo and the IP of the device cause I'm a sucker for punishment.

The I2C display board I'm using seems to handle all the signal needs and just wants to be connected to I2C bus. I'm keeping my fingers crossed.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf