The circuit below shows to turn an LED on and off using a Raspberry Pi GPIO pin configured as an output. It uses the output pin to turn on a transistor which allows the LED to draw current from the 5V supply.
The following header pins are required :
- Header Pin 2 : 5V
- Header Pin 6 : Ground
- Header Pin 11 : GPIO
The Header Pins are defined in my Raspberry Pi Header Pin page. You can use whatever GPIO pin you like but I used pin 11 for my tests.
- The LED is a standard 5mm red type with a forward voltage of 2V.
- The transistor could be a BC547, BC548 or equivalent.
- Resistor R1 limits current through the LED from the 5V pin. If the voltage drop across the LED is 2V then voltage across the resistor is 3V. So current is 3/560 = 5.4mA.
- Resistor R2 limits current from the GPIO pin. GPIO is either 0v or 3.3V so the maximum current into base of transistor is 3.3/27000 = 120uA.
- Assuming the gain of the transistor is 200 a base current of 120uA would allow a maximum of 24mA (120uA x 200) to pass through the LED.
R1 could be increased you just need to make sure you allow enough current to power your LED.
R2 could be increased and this would reduce the current drawn from the GPIO pin but it would reduce the maximum current allowed to flow through the transistor.
Note : As with all connections you make to the Pi’s PCB you must double check everything. Making a mistake in your wiring could damage the CPU as some of the GPIO pins are connected straight to the Broadcom chip.
I’ve modified some of the details on this page thanks to feedback from Gert (see comments below).
Python Code
In order to control GPIO pins in Python you must install the RPi.GPIO library. Installing the library is easy if you follow my RPi.GPIO Installation Guide. Once installed you can use the following Python script to toggle the GPIO output on header pin 11 (GPIO17) :
import RPi.GPIO as GPIO import time # Use physical pin numbers GPIO.setmode(GPIO.BOARD) # Set up header pin 11 (GPIO17) as an output print "Setup Pin 11" GPIO.setup(11, GPIO.OUT) var=1 print "Start loop" while var==1 : print "Set Output False" GPIO.output(11, False) time.sleep(1) print "Set Output True" GPIO.output(11, True) time.sleep(1)
This script will continue running until you press CTRL-C.
Python scripts must be run as the super user when they are using the GPIO library. So if your script is called “ledtest.py” you would run it by typing :
sudo python ledtest.py
The script should print text messages to the command line while toggling the state of pin 11. If your circuit is correct then the LED should turn on and off every one second.
20 Comments
Hi,
Thank you : This makes thing much easier.
The diagram is showing PIN P2 (5v Power), but the “following header pins are required” paragraph talks about header PIN P1 (3.3 v Power).
I guess the real PIN to use is P2 5v : Do you confirm ?
—–
Regarding resistor R2, the diagram shows 2,2k ohms;
Does it stands for 2 200 ohms ?
Are you sure of this value ?
Should it be around 730 ohms ?
Regards,
Well spotted. It is indeed Pin 2 – 5V.
The 2.2Kohm resistor is the same as 2200 ohms. This resistor could be a different value. It just limits the current flowing out of the GPIO pin via the base of the transistor to ground. If the GPIO pin is 3.3V it would result in 1.5mA. There is little point in driving any more current into the base of the transistor so if anything the value should be larger.
Thanks for this post. Very helpful.
It won’t make a huge difference in the end but the diagram shows R1 with a value of 560 ohms but the text mentions 460 ohms. It might be less confusing if they two values matched.
I’ve updated the text. You’re right that it doesn’t matter much. Originally I planned to use a lower value but decided to go for 560ohm as it doesn’t make any difference to the LED but reduces the current drawn from the Pi’s 5V pin.
As to the circuit: it will work but it is not a very good design.
The diagram above is using 1.5mA from the GPIO which is enough to light up a high efficiency LED directly.
The LED is driven of 5V and has 560 Ohm.
Thus it will pass about (5-2)/560 = 5mA.
Lets exaggerate and make that 10mA.
The BC547B has a minimum Alpha of 200.
So you need 10mA/200 = 50uA as current into the base.
Thus your base resistor needs to be (3.3-0.6)/50e-6 = 54K.
Again take safety factor of two: 27K.
So a base resistor of 27K is enough still giving a safety factor of four.
Sorry Alpha should be Beta (Ic/Ib).
Also the statement “it doesn’t make any difference to the LED but reduces the current drawn from the Pi.” is not correct. The Ib is set by your GPIO output voltage and the base resistor. The BE junction is in effect a diode so reducing you Ic does not reduce your Ib. Your Ic can reduce your Ib if there is a resistor between the emitter and ground. It that is the case, it does so because your Ic increases the Ve and thus raises the Vb. The effect is that you have less voltage over the base resistor and thus less current going into the base.
Sorry if I sound so critical but I received a PM as your circuit caused confusion by a Pi user.
I meant that changing R1 changes the current through the LED which changes the current drawn from the 5V rail. Not that is changes the current through the GPIO base. I’ve tweaked the comment to make this a bit clearer.
I see what you mean. Originally I chose a low value for the base resistor because I wanted to be able to replace the LED with another device without having to worry about the current being limited by the transistor. So I wanted to make sure it was saturated. I’ll update the diagram and text to make it clearer and replace with new values.
Thanks for the comments, it’s appreciated. Great to know you visited this page!
Could this circuit be used to drive a relay?
I would like to turn on and off an amplifier.
Yes it can. Rather than the LED you can pass current through a relay coil. You may need to adjust the resistor to provide the correct current.
Pingback: Raspberry Pi as power switch - DL-UAT
The comment on line 6 of your example code says “input”, but should be “output”… No functionality change, but more correct in following as a noob.
Well spotted! I’ve updated now.
I get a syntax error on line 5 at the first < any suggestions?
Take a look at the code now. The tags were added by the site’s syntax plugin and weren’t part of the python.
I am seeing a circuit with GP24 to red LED to 330R to GND. The extent of my ignorance is such that I have no idea why introducing the resistor is beneficial. Any advice?
I greatly appreciate the clarity of your tutorial.
It’s to limit the current drawn through the LED. The LED will drop approx 2V and the resistor will drop the remaining 1.3V. Using V=IR you can plug in this voltage drop and the resistance to determine the current through the resistor. This current must be the same as that drawn through the LED which is turn comes from the GPIO pin. You’ll see different values but it will be value to limit the current to 5-20mA. There no “right” value just a range of sensible values and lot of wrong ones! If you want more detail or perhaps a better explanation look up “LED current limiting resistor” and there will be plenty of info/videos out there.
Do you really need the transistor? A 330 ohm in series with the LED would be enough?
Or more importantly when should one use a transistor for the Pi Zero?
You don’t really need to use a transistor to run an LED from the GPIO pins. The only rule is to not try and draw too much current from an individual pin (~15mA) or from the GPIO as a whole (~50mA). A transistor is useful for switching power from another source, often the 5V supply where the current limits are much higher. So in this article it was more of an example. If I add a few LEDs to a project I usually connect directly with a 330ohm resistor. I tend to use 330 because I’ve got a bag of 4000 🙂
Hi ,
i tried using raspberry pi 3. LED is blinking but very dim. can any one help me to figure-out what i am doing wrong.