Electronic – Driving a LED with an open drain digital output

ledmicrocontrolleropen-drainstm32f2

I need to drive an LED from an STM32F217ZG.

So I was thinking about a classic circuit with a BJT.
But then I saw that GPIO outputs of the microcontroller are configurable as open drain.
Would this circuit work?

enter image description here

The LED has a 20mA forward current and a 2V forward voltage. Considering \$R=(3.3-2)/0.02\$ it should be on, if GPIO is Hi-Z and off, if it is GND, right?

I don't see why this circuit would not work but I've never seen it before, so I just want to be sure before designing it.

Edit:
Then you mean it is better to do this:

enter image description here

Edit 2:
Finally here is the schematic I'll be using (the output GPIO LED6FAIL is 3.3V and it's a push-pull):

enter image description here

Best Answer

This circuit will work, but not be efficient, power-wise. If the output is high the LED is on and you'll have 20 mA current through LED and resistor.

But with the output low, the LED is short-circuited and therefore off, but the resistor current will be even higher: 50 mA!, which is not only bad for your wallet and the environment, but also for the output pin; most I/Os will only allow 25 mA.

Place the LED in series with the resistor. Then a high output won't light the LED because the output FET is switched off. A low output will draw the 20 mA through LED, resistor and open-drain output.

Note that this will invert the logic: in your first schematic the LED will be off when the output is active, in the second circuit it will be on with active output.

edit
If the output can't sink enough current you'll need an external transistor to increase that.

enter image description here

If the output is active (low) there will flow a 1.5 mA from +3.3 V through Q2's emitter-base junction, R2 and Q1. That 1.5 mA will allow Q2 to source more than 100 mA for a typical general-purpose transistor. You will only get about 22 mA, though, because R1 won't allow more.

If Q1 is off there won't be any base current through Q1, so the LED will remain off. Q1 will have a small leakage current, and to avoid that this would get amplified by Q2 I added R3. As long as the leakage current is less than 0.7 V / 15 kΩ = 50 µA all the leakage current will flow through R3, so that will ensure Q2 will be completely off.