bowen1506
Posts: 4
Joined: Mon Apr 15, 2019 5:21 pm

Controlling LED on GPIO3

Mon Apr 15, 2019 5:51 pm

Hi all, Sorry for being a beginner. I spent a good amount of my weekend confused about this problem.
I need to add a status led to my pi zero w and after taking a look at the pin out I decided to put an led between pin 5 and 6. Please see attached picture. I soldered a blue 0805 SMD led directly on the holes and it is not coming off easily...
Now I have a problem, I cannot control GPIO3 reliably. This GPIO seems to act randomly. Sometimes it is high and led is on as soon as I plug in power, and I could control it using sample python code for GPIO led. Then after a while it stops working. This pin will stay low no matter what I do...

After reading around I saw this pin is also used for i2c stuff and has 18k pull-up resistor to 3.3v. Not sure what is the implication of that for using it as a GPIO output. Does that mean I cannot control a LED with it? Then Why does it work sometimes but not other times?

Btw I2C is disabled on my board. Thanks for your help!

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(3, GPIO.OUT)
pwm = GPIO.PWM(3, 100)
pwm.start(100)

print("started.")

while 1:
	pwm.ChangeDutyCycle(10)
	time.sleep(0.01)
	pwm.ChangeDutyCycle(0)
	time.sleep(0.05)
GPIO.cleanup()
Attachments
20190415_18273111.jpg
20190415_18273111.jpg (204.8 KiB) Viewed 131 times
yHddo.jpg
yHddo.jpg (84.18 KiB) Viewed 154 times
Last edited by bowen1506 on Tue Apr 16, 2019 2:45 am, edited 3 times in total.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11570
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Controlling LED on GPIO3

Tue Apr 16, 2019 12:01 am

This is NOT possible, for several reasons, three reasons in fact!
First, You cannot just connect a LED between a GPIO pin and GND, as a LED is a diode, and needs a resistor in series to limit the current flowing through it, or it will draw an "unlimited" amount of current. meaning it draws so much current that it will probably draw the 3V3 supply down to the typical forward drop voltage of the LED, a RED LED has a forward voltage of ca 2.0Volt, and sinking 3V3 to 2V will crash or destroy your PI.

Secondly luckily you made another mistake in using a BLUE LED, blue LED's have a much higher forward voltage of 3V or so, and so are typically not suitable to be driven by a GPIO, not even with a resistor, without a resistor they will draw current until the source drops to 3V.

Thirdly the GPIO pin you are using isn't suitable for driving a LED, as its one of the two GPIO's reserved for I2C, and both of them have permanent 1600 Ohm external pullups attached to them.

Choose another GPIO pin, one not dedicated to a special function,(say GPIO 27) and put a 330 Ohm series resistor in series with a RED LED.

bowen1506
Posts: 4
Joined: Mon Apr 15, 2019 5:21 pm

Re: Controlling LED on GPIO3

Tue Apr 16, 2019 2:24 am

mahjongg wrote:
Tue Apr 16, 2019 12:01 am
This NOT possible, for several reasons, three reasons in fact!
First, You cannot just connect a LED between a GPIO pin and GND, as a LED is a diode, and needs a resistor in series to limit the current flowing through it, or it will draw an "unlimited" amount of current. meaning it draws so much current that it will probably draw the 3V3 supply down to the typical forward drop voltage of the LED, a RED LED has a forward voltage of ca 2.0Volt, and sinking 3V3 to 2V will crash or destroy your PI.

Secondly luckily you made another mistake in using a BLUE LED, blue LED's have a much higher forward voltage of 3V or so, and so are typically not suitable to be driven by a GPIO, not even with a resistor, without a resistor they will draw current until the source drops to 3V.

Thirdly the GPIO pin you are using isn't suitable for driving a LED, as its one of the two GPIO's reserved for I2C, and both of them have permanent 1600 Ohm external pullups attached to them.

Choose another GPIO pin, one not dedicated to a special function,(say GPIO 27) and put a 330 Ohm series resistor in series with a RED LED.
Thanks for the reply. I am pretty new to this stuff so sorry for the noob questions.
I specifically chose the blue LED because I know it can take 3.3v
Image
Why do you think I should use a red one instead of blue?

I don't intend on using any i2c peripherals that's why I soldered onto that pin...It is also a GPIO pin nonetheless right? Is it because of the pullup resistor it has that is preventing it from driving an LED?

User avatar
rpdom
Posts: 13733
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Controlling LED on GPIO3

Tue Apr 16, 2019 4:50 am

bowen1506 wrote:
Tue Apr 16, 2019 2:24 am
I specifically chose the blue LED because I know it can take 3.3v
Yes, and the table shows that is at 20mA current. GPIO pins can supply a maximum of 16mA, but that is not guaranteed to be at 3.3V, it is only certain that it will be at a voltage that will be considered a "logic high" which is about 2V.

With a small resistor you might get your LED to light sufficiently from a GPIO, but it would be better to use a driver circuit and run it from the 5V supply via a suitable resistor.

3.0V LED at 5mA on 3.3V GPIO: 56 Ohm resistor
3.0V LED at 20mA on 5V via transistor: 100 Ohm resistor
Signature is on holiday.

User avatar
davidcoton
Posts: 3610
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Controlling LED on GPIO3

Tue Apr 16, 2019 7:40 am

bowen1506 wrote:
Tue Apr 16, 2019 2:24 am
I specifically chose the blue LED because I know it can take 3.3v
You are misreading the spec. It says that, when the current is 20mA, the voltage will be typically 3V0, max 3V4. That means if you apply 3V3 without a resistor, typically the current will exceed 20mA. That is enough to damage your Pi. It is quite possible that, if the LED is connected the right way round and you managed to program the GPIO as output high, the GPIO is already damaged, and such damage can spread through the SOC even after the LED is removed. Alternatively the 3V3 supply may have been pulled down, limiting the current to a value that did not cause permanent damage.
bowen1506 wrote:
Tue Apr 16, 2019 2:24 am
Why do you think I should use a red one instead of blue?
Because the forward voltage drop is lower, assuring that the Pi will deliver enough voltage to turn it on. You still need a current limiting resistor, which will effectively drop the difference between 3V3 and the LED Vf, thus limiting the current according to Ohm's law.
bowen1506 wrote:
Tue Apr 16, 2019 2:24 am
Is it because of the pullup resistor it has that is preventing it from driving an LED?
No, the pull-up will actually prevent the LED turning off completely.
Signature retired

bowen1506
Posts: 4
Joined: Mon Apr 15, 2019 5:21 pm

Re: Controlling LED on GPIO3

Tue Apr 16, 2019 10:05 pm

Thanks for everyone's reply. If I understand correctly you guys are stressing that I connected the LED directly between GPIO3 and ground without a resistor, which could results in the LED taking more current than the 16MA that the GPIO can handle, this could lead to the weird situation I had where the GPIO is stuck at low. Or perhaps I have already somewhat damaged the GPIO3 pin. But my LED blink code uses pwm with a very low duty cycle, IDK if that helps....

But I have one more questions tho if you guys don't mind...At first I couldn't get the LED to light up at all so I suspected the LED may be faulty, so I applied 3v3 to the led (while pi is unplugged) to test it. (I used 3v3 pin and G from a esp8266 board) The led lit up very bright. After this when I plugin power to the pi, the LED lit up right away and the GPIO worked as well. Then after I ran the blink code for like 40 min it stops working. The GPIO is stuck at low and power cycle won't fix it either. But if I repeat the entire process again, it works again, for like 40 min. This really puzzles me...

pcmanbob
Posts: 5484
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Controlling LED on GPIO3

Wed Apr 17, 2019 12:23 pm

Have you not understood what people have been telling you , you can not drive a blue LED directly from a gpio pin with no limiting resistor , you will eventually damage you pi to the point were it will not work at all.

I suspect what you are seeing is the pi failing after 40 min due to the excessive load on the gpio resulting in the gpio output failing , applying the external 3v is not actually fixing it , it more likely the time its un-powered allowing the output driver to cool and recover.

If you are not going to listen to the excellent advice given then at some point you will be back say your pi has failed , you have be warned.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

bowen1506
Posts: 4
Joined: Mon Apr 15, 2019 5:21 pm

Re: Controlling LED on GPIO3

Thu Apr 18, 2019 5:32 pm

pcmanbob wrote:
Wed Apr 17, 2019 12:23 pm
Have you not understood what people have been telling you , you can not drive a blue LED directly from a gpio pin with no limiting resistor , you will eventually damage you pi to the point were it will not work at all.

I suspect what you are seeing is the pi failing after 40 min due to the excessive load on the gpio resulting in the gpio output failing , applying the external 3v is not actually fixing it , it more likely the time its un-powered allowing the output driver to cool and recover.

If you are not going to listen to the excellent advice given then at some point you will be back say your pi has failed , you have be warned.

Yes I fully understood that point and I said it in my last post.
What I was puzzling about was the external 3v somehow "resets" the pin. I am sure it is not the un-powered time that is allowing it to recover, I've left it off for a day and it did not recover, whereas if I apply 3.3v external power to the led and it lights up, the pin is fixed immediately.

But anyways, thanks to everyone for the help, I have already de-soldered the led and the pin works again. Next time I'll be sure to add a resistor in series when driving led from gpio.

Return to “Troubleshooting”