0
\$\begingroup\$

I was trying to understand GPIO open drain configuration. As far as I understood, to avoid floating output, one should use internal or external pull-up resistors. This is OK. However, I saw multiple times this driving LEDs example. I am kind of stuck here, because;

In this video and other sources, I generally see that internall pull-up resistors are 30-50k ohms. I also confirmed this from the datasheet of one of the MCUs of STM. I know that classic 5mm LEDs require approximately 20mA to give decent light output. Now, I am confused because considering 5V for Vcc, 5V - 2V(LED on) / 30k ohms = 0.1 mA of current, which looks to me that it won't be enough to drive an LED.

So, what do I miss / don't know / know but wrong here?

Thanks.

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ Is this a dedicated open-drain output? Or, is this a programmable pin that can also be an input? If the the pin is a GPIO (General Purpose), then it usually can be programmed to be an input. Input circuits should not be left floating, even if they are not being used. \$\endgroup\$ – Mattman944 Apr 28 '20 at 22:58
  • \$\begingroup\$ @Mattman944 it is not specific to a pin actually, it was a general question regarding GPIO pins(so, yes programmable). However, I don't understand the relation between my question and configuring the pin as input. \$\endgroup\$ – muyustan Apr 28 '20 at 23:02
  • \$\begingroup\$ If it is a GPIO, the input circuit is still there even if it is ignored by the internal circuits. The input should not be left floating because if it drifts to the middle, it will unnecessarily draw current. The TI MSP430 MCUs that I use specifically warn about this issue. \$\endgroup\$ – Mattman944 Apr 28 '20 at 23:07
1
\$\begingroup\$

'As far as I understood, to avoid floating output, one should use internal or external pull-up resistors'

There is no problem with leaving an open-drain output floating.

I generally see that internal pull-up resistors are 30-50k

The LED will not be very bright but it will generally be visible. 10mA or 20mA is often way too much these days with LEDs being so efficient.

Normally you would use the output transistor to sink current and add a series resistor to the LED, if you happened to have an open-drain output (and also for a more usual push-pull output). Otherwise you're wasting power when the LED is off.

\$\endgroup\$
4
  • \$\begingroup\$ hmm, ok I got it, thanks. One more question regarding your paranthesis mentioning push-pull configuration; I would connect LED between GND and GPIO pin intuitively. Is connecting the LED between Vcc and Pin more preferable? Or no difference? Thanks. \$\endgroup\$ – muyustan Apr 28 '20 at 22:58
  • 1
    \$\begingroup\$ With a series resistor, of course. Often the voltage drop of a push-pull output is lower when sinking current, so it's better to put the LED + resistor between Vdd and the output. It's a consequence of solid-state physics- electron mobility beats hole mobility so the same size of MOSFET has lower Rds(on) if it is N-channel (sinking current). Sometimes an MCU manufacturer will compensate for the carrier mobility difference by making the P-channel MOSFET larger so there is little difference in sink vs. source. You have to read the datasheet. You'll almost never go wrong using output to Vdd. \$\endgroup\$ – Spehro Pefhany Apr 28 '20 at 23:05
  • 1
    \$\begingroup\$ well, brilliant explanation, thanks! Just one more question, in case of connecting the LED between Vcc and gpio pin, against the other method(between gnd and gpio pin), one should write 0 to gpio pin in order to activate LED, right? \$\endgroup\$ – muyustan Apr 28 '20 at 23:08
  • 2
    \$\begingroup\$ @muyustan: That's correct. Inverted logic when done that way. \$\endgroup\$ – Transistor Apr 29 '20 at 18:07
2
\$\begingroup\$

Based on your diagrams...

  • Internal pull-up: The weak (10-50K) pull-up will source a small current when the OD FET is off. The LED would light, but only dimly
  • External pull-up: If the pull-up is low enough (less than 1K), the LED will light when the FET is off, and will be dark when the FET is on.

I don't know if that was your intention, but that's what those two circuits will do.

The second case has an implication for I/O voltage which I will discuss below.


Why use open-drain drivers for LEDs

LEDs have a forward threshold voltage (Vf) that varies with color. The shorter the wavelength, the higher the threshold. If you use a 3.3V supply, this is marginal for some LEDs (blue, violet, some white) that require 3.1V or more to achieve their Vf. So it's often necessary to use a higher Vcc (e.g., 5V) to drive them.

Open-drain drivers can translate lower MCU I/O voltage to higher LED voltage. But there's a catch.

The Problem with GPIOs: the Protection Racket

An MCU chip GPIO, programmed as 'open drain' like you show without an internal pull-up, will indeed only sink current. It can drive an LED, but only if the LED Vcc is no higher than the chip power GPIO supply.

So it's ok to drive a 5V LED if your MCU is also 5V, but not if the MCU is 3.3V.

Why?

The MCU's GPIO voltage, even open-drain programmed one, must not exceed its datasheet spec vs. the power supply. There is a breakdown limit set by the chip I/O structure; higher voltages will damage the chip.

Typically the MCU datasheet sets the I/O voltage limit to be between GND-0.5V to Vcc + 0.5V. That 0.5V comes in for a reason: there are ESD protection diodes to enforce that limit.

Here's a typical MCU I/O protection scheme (this one for PIC):

enter image description here

From here: http://embedded-lab.com/blog/tag/esd-protection/

So what happens with a 3.3V MCU and 5V LED Vcc? As shown in your second diagram, when the GPIO is off, the pull-up to 5V will continue to source current to the GPIO high-side protection diode which will clamp it to about 3.8V, or the LED will clamp it to somewhat above its Vf depending on the current.

While the I/O is protected, kind of, the LED won't light like you expect it to if it needs a higher Vf than 3.8V. Even worse, if Vf is above 3.8V, you've created a leakage path from 5V to 3.3V via the protection diode. You don't want that.

So, how to drive LEDs at higher-than-MCU voltage?

  • Use an open-drain buffers. Some allow a higher voltage on the open-drain output. For example, the LVC2G07 supports translating a lower MCU I/O voltage (e.g., 1.8V) up to a higher LED voltage (up to 5V.) These devices not only have sturdier FETs that can take the voltage, but also have have different ESD protection schemes for the output that make them 5V tolerant.

  • Use a discrete N-FET. These are true open-drain. As the FET has no ESD diodes, the only LED voltage limit is the max drain-source voltage (Vds) it can tolerate.

\$\endgroup\$
1
  • \$\begingroup\$ This is a nice answer, thanks. \$\endgroup\$ – muyustan Apr 29 '20 at 18:54
1
\$\begingroup\$

Modern SMD LEDs can be bright enough even with fed with current from internal pull-ups. However, in general that is not how to drive a LED. You would connect the LED to VCC via suitable resistor, and activate the FET to pull current into the IO pin to turn the LED on. That is how open drain outputs can drive loads.

\$\endgroup\$
4
  • \$\begingroup\$ Vcc -- (+)LED(-) -- current limiting resistor -- Pin this is the way of connection you are saying, right? \$\endgroup\$ – muyustan Apr 28 '20 at 22:51
  • 1
    \$\begingroup\$ Yes. With the resistor. \$\endgroup\$ – Justme Apr 28 '20 at 22:54
  • \$\begingroup\$ OK, but, then, should I write 0 to GPIO pin to light the LED? It seems counter intuitive. Am i right? \$\endgroup\$ – muyustan Apr 28 '20 at 23:00
  • 1
    \$\begingroup\$ You are right, logic zero lights up the LED. \$\endgroup\$ – Justme Apr 28 '20 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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