1
\$\begingroup\$

I am try to make an LED circuit that can be controlled with pything using a Pi Zero.

The problem is when I try to turn the LED off, it retains a faint glow (image).

Equipment:

  • Pi Zero W
  • RGB LED with common anode
  • 220 Ohm resistors

I believe the circuit is wired correctly because to turn the LED "off" the output has to be set to 5V.

Circuit image

\$\endgroup\$
3
  • \$\begingroup\$ Are those connections to GPIO12, GPIO13 and 5V? Are any of the LED leads connected to that 5V line? \$\endgroup\$ Jun 30, 2021 at 17:09
  • 1
    \$\begingroup\$ I have been surprised in the past by how little leakage is required to make a red LED glow faintly (so it is barely visible in a dark room). It is something like 10 uA if I recall. \$\endgroup\$
    – user57037
    Jun 30, 2021 at 20:11
  • 4
    \$\begingroup\$ @mkeith Oh, over in the DIY stack we get that writ large. You know those screw-in LED-based lightbulb replacements that are sold everywhere now instead of incandescents... They glow from motion sensors or dimmers that are series-wired, sometimes even from "phantom voltage" (capacitive coupling from adjacent wires in a cable). \$\endgroup\$ Jul 1, 2021 at 14:26

4 Answers 4

15
\$\begingroup\$

tl; dr: the LED has a leak path between 3.3V and 5V. Either don't use 5V for the LED, or add a transistor, MOSFET or true open-collector gate to isolate the GPIO and drive the LED.

What’s going on?

If you use a 3.3V GPIO pin to drive a 5V-tied LED, when the GPIO is high it will not be fully 'off': there will still be leakage current between the pin's IO protection and the LED.

Here's what that looks like, and how to fix it:

schematic

simulate this circuit – Schematic created using CircuitLab

You’d think that because 5V-3.3V = 1.7V is lower than the LED forward voltage, or Vf, that the LED would not conduct and thus not light up. This is not the case: there will still be some small leakage current that will result in a faint glow. This leakage has a name: subthreshold conduction, and is present in practically all semiconductor devices, including LEDs. Just tens of microamps can make it light.

A note:

  • LED forward voltage is usually stated at the LED's rated current (e.g., 20mA). So a '2.0V' red LED like this one will still be conducting at lower voltages, including 1.7V, and even well below that.
  • Higher-Vf LEDs (blue, violet, white) will still have some leakage, but less than low-Vf LEDs (red, orange, green)

How do I fix It?

The solution options depend on the LED's forward voltage:

  • Low-Vf LED (red, orange, green): use 3.3V supply and drive cathode
  • Low-Vf LED (red, orange, green): use GND and drive anode
  • High-Vf LED (blue, violet, white) or any other LED: use 5V supply and transistor, MOSFET, or true open-collector gate buffer

All these solutions eliminate the leakage path.

Why use 5V at all?

Using 3.3V for a 3V-ish Vf LED (e.g., white, blue) isn't a good idea as it's very hard to get the current-limiting resistor right: with only 300mV or less IR drop in the resistor, a very small R value must be used, and so the LED current is very sensitive to forward voltage variation.

\$\endgroup\$
5
\$\begingroup\$

The GPIO output pin can output a 3.3V logic signal.

Low level is 0V and high level is 3.3V.

If you power your LED from 5V, both 0V and 3.3V will turn it on.

But injecting current into the 3.3V GPIO pin from 5V supply can damage it, don't do that. It has some built-in protection diodes, but they are not meant to be used deliberately.

\$\endgroup\$
4
  • \$\begingroup\$ Ahhhh, this makes a lot of sense, would you recommend connecting the anode to the 3.3v power supply pin then? \$\endgroup\$ Jun 30, 2021 at 14:21
  • \$\begingroup\$ Yes, that's the only correct place for it, if you want to light up a LED by pulling the pin low. \$\endgroup\$
    – Justme
    Jun 30, 2021 at 14:36
  • 1
    \$\begingroup\$ If you substitute a white LED or a blue LED for that red one in your circuit, it should turn off completely, because white, blue LEDs drop more voltage than red. However, tying anode to 3.3V is a better solution, as long as the extra LED current doesn't heat-stress the PI's 3.3V regulator. \$\endgroup\$
    – glen_geek
    Jun 30, 2021 at 16:36
  • \$\begingroup\$ White, blue or other ~3V Vf LED will still leak some. Red will leak more. \$\endgroup\$ Jun 30, 2021 at 19:53
2
\$\begingroup\$

The GPIO pins on the Raspberry Pi 0 do not provide 5 V when driving a logic 1. They are 3.3 V signals. So, if your circuit needs 5 V it will not work.

\$\endgroup\$
0
\$\begingroup\$

I may be missing something obvious here, but instead of driving the GPIO pin as high as possible trying to match the 5V line (which clearly won't work), why not connect the other end of the LED to GND?

GPIO pin - resistor(s) - +LED- - GND

then just turn your software logic upside down so that HIGH gives LED on and LOW gives LED off.

\$\endgroup\$
3
  • \$\begingroup\$ You still have a voltage across the led, 3.3 to 5V = 1.7V. Any current flowing forward will cause the LED to illuminate even though you may not be able to see it. \$\endgroup\$
    – Gil
    Jul 1, 2021 at 14:35
  • \$\begingroup\$ That will work for a red LED. Some blue or white LED's might not work well with 3.3V drive. \$\endgroup\$
    – user57037
    Jul 1, 2021 at 16:32
  • \$\begingroup\$ You're right, but it's not clear from the question that the LED definitely needs 5V, and there are plenty of blue, white and RGB LEDs that work just fine using the Pi's 3.3V outputs. \$\endgroup\$ Jul 19, 2021 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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