Should I always use a transistor for a GPIO output? - Page 1
Forum section: electronics / projects

author: laxamar
author_rank: Contributor
timestamp: September 17, 2022, 11:34:09 pm
content: 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?

author: rfclown
author_rank: Frequent Contributor
timestamp: September 17, 2022, 11:44:26 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: September 17, 2022, 11:55:29 pm
content: You can't just stick a transistor on I2C bus like this, it won't work.

author: rfclown
author_rank: Frequent Contributor
timestamp: September 18, 2022, 02:11:56 am
content: 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.

author: pcprogrammer
author_rank: Super Contributor
timestamp: September 18, 2022, 05:01:01 am
content: For I2C the clock is also bidirectional. Think of clock stretching the slave can do by keeping the clock line low.But it is possible to use mosfets like 2n7000 to make level converters for this. Might not do high speed though.Bit of google for it.https://www.google.com/search?channel=fs&sxsrf=ALiCzsYwUtcwST21lVdErlmUEnB_Tq8tIw:1663477121682&source=univ&tbm=isch&q=i2c+level+converter+2n7000&client=ubuntu&fir=O1bF3jFSBDjK4M%252CQr3uxfOIvrTgRM%252C_%253B7zPWN7a9LiqgkM%252CAbhVDv-qlRo_BM%252C_%253BNHblQAXfHOTJDM%252CTL7LWJvrglSfWM%252C_%253Bv9ksYhZBxGpAkM%252C61kFBnykqlU5rM%252C_%253BSWauhzSW9wAldM%252C068DJvmhIYsJoM%252C_%253B0EnIfwAiVl1PSM%252CAbhVDv-qlRo_BM%252C_%253B25Cwe2uwcShwcM%252Cf3oLw3x2o3aquM%252C_%253ByIC7jpziXwSx6M%252CdgwoCML49ENpvM%252C_%253BOyCntY6VkIyDjM%252CTL7LWJvrglSfWM%252C_%253BPYvW3j95AEvVZM%252CQkYgbhG--uOIdM%252C_&usg=AI4_-kRtXiuTEasaD_mbyQODsfscf1j-vA&sa=X&ved=2ahUKEwil6reXx536AhUV_BoKHZ8VAaYQjJkEegQIBBAC

author: ataradov
author_rank: Super Contributor
timestamp: September 18, 2022, 05:10:37 am
content: 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.

author: laxamar
author_rank: Contributor
timestamp: September 18, 2022, 10:09:53 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: September 18, 2022, 10:14:06 pm
content: 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.

author: John B
author_rank: Frequent Contributor
timestamp: September 19, 2022, 03:37:56 am
content: 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.

author: pcprogrammer
author_rank: Super Contributor
timestamp: September 19, 2022, 05:40:43 am
content: Quote from: ataradov on September 18, 2022, 10:14:06 pmDon't forget about the current limiting series resistor though.https://www.eevblog.com/forum/chat/software-guys-please-no/?topicseenAnd 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.

author: laxamar
author_rank: Contributor
timestamp: September 19, 2022, 06:11:37 pm
content: Quote from: John B on September 19, 2022, 03:37:56 amI can vouch, from personal experience and having built a PCB that a single blue LED will render all other blue LEDs useless

author: laxamar
author_rank: Contributor
timestamp: September 19, 2022, 06:13:25 pm
content: Quote from: pcprogrammer on September 19, 2022, 05:40:43 amThis is going on an SMD PCB. I have no experience with mosfets. Would you have an exact part no I can lookup?

author: pcprogrammer
author_rank: Super Contributor
timestamp: September 19, 2022, 06:57:38 pm
content: 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.pdfFor more current there are plenty others to be found.

author: wraper
author_rank: Unknown
timestamp: September 19, 2022, 07:04:36 pm
content: 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.

author: laxamar
author_rank: Contributor
timestamp: September 19, 2022, 09:55:41 pm
content: Quote from: pcprogrammer on September 19, 2022, 06:57:38 pmI 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

author: ataradov
author_rank: Super Contributor
timestamp: September 19, 2022, 10:07:25 pm
content: Quote from: laxamar on September 19, 2022, 09:55:41 pm280 mA is the maximum current, nothing is stopping you using that with 20 mA. Those are pretty generic low power MOSFETs.Quote from: laxamar on September 19, 2022, 09:55:41 pmGate 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.

author: John B
author_rank: Frequent Contributor
timestamp: September 19, 2022, 10:17:16 pm
content: 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.

author: rfclown
author_rank: Frequent Contributor
timestamp: September 19, 2022, 10:25:21 pm
content: 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.

author: ledtester
author_rank: Super Contributor
timestamp: September 19, 2022, 10:26:29 pm
content: 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/

author: bson
author_rank: Unknown
timestamp: September 20, 2022, 08:34:25 pm
content: 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.

author: wraper
author_rank: Unknown
timestamp: September 20, 2022, 08:41:59 pm
content: Quote from: bson on September 20, 2022, 08:34:25 pmPlain 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.

author: John B
author_rank: Frequent Contributor
timestamp: September 21, 2022, 12:02:37 am
content: 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.

author: laxamar
author_rank: Contributor
timestamp: September 21, 2022, 12:26:40 am
content: Quote from: bson on September 20, 2022, 08:34:25 pmI 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.