GPIO pins with transistors


5 posts
by superroxfan » Wed Jul 24, 2013 3:41 am
Hello fellow Pi lovers,

I'm currently having some trouble getting by raspberry pi to work with a NPN transistor. All I am trying to do is turn an L.E.D. (which has a forward voltage of 3V) on from an external battery and have my Pi control when to allow the battery current to flow through the L.E.D. via transistor. Here's my problem: I can't get the Pi to turn the transistor into the "off" state to turn off the L.E.D. I know my code is working properly, as I made a circuit without the transistor, but I can't get the darn L.E.D. to turn off with the transistor in the circuit. Here's my circuit:

Image

My Code:

Code: Select all
import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BCM)
gpio.setup(14, gpio.OUT)
while True:
  gpio.output(14, gpio.HIGH)
  time.sleep(1)
  gpio.output(14, gpio.LOW)
  time.sleep(1)


I have tried several other GPIO pins but I still can't get the L.E.D. to turn off. The weird part is that if I replace pin 8 in my circuit with the ground pin, the L.E.D. will still stay on! If I completely unplug the Pi from the circuit, the L.E.D. is off. Do the pins on the Raspberry Pi constantly give off a small amount of current constantly or something? Please help me as I have tried several hours to figure this out to no avail.

Thank You,
Superroxfan
Posts: 2
Joined: Tue Jul 23, 2013 11:09 pm
by mbaird » Wed Jul 24, 2013 6:02 am
Have you got a wire going from the -ve battery terminal to the 0v pin on the GPIO?
Posts: 4
Joined: Tue Jul 23, 2013 6:54 am
by Etienne » Wed Jul 24, 2013 6:43 am
If ground of your Led circuit and ground of Pi are well connected together as suggested by mbaird, you can try to add resistor between the transistor base and ground (same value as the one between the Pi GPIO and the transistor base).
This should avoid that any residual voltage from the GPIO at low state keeps the transistor saturated.
Well, this does not explain why the LED remain on when the circuit is open...
Posts: 44
Joined: Wed Jul 24, 2013 6:23 am
by superroxfan » Wed Jul 24, 2013 2:13 pm
Thank you mbaird and etienne. My problem was, in fact, that I didn't have my raspberry pi's ground connected to the battery. I'm really just starting to work with hardware (I'm more of a software kind of guy) and foolishly assumed that the negative battery terminal would act as a ground for both currents. Thank you for solving my problem!
Posts: 2
Joined: Tue Jul 23, 2013 11:09 pm
by beny » Mon Sep 23, 2013 5:40 pm
@superroxfan it's a common problem that people forget to use common grounds and end up looking for potential differences at places which are basically part of two different circuit. When you hadn't made the ground common, you were essentially dealing with two circuits operating in isolation which didn't recognize the voltage levels of each other. Anyways, it's good that you're learning from your mistakes, we all do. Just a tip, you can simply use a low pin to switch on an LED by pulling it up. This makes the pins of Raspberry act as sinks and it doesn't have to supply power to the LED. Saves you the trouble of using a transistor for this purpose.

pcb assembly
Posts: 2
Joined: Mon Sep 23, 2013 3:28 pm