Control LED Using GPIO Output Pin

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
# Set up header pin 11 (GPIO17) as an input
print "Setup Pin 11"
GPIO.setup(11, GPIO.OUT)

print "Start loop"
while var==1 :
  print "Set Output False"
  GPIO.output(11, False)
  print "Set Output True"
  GPIO.output(11, True)

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 “” you would run it by typing :

sudo python

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.


This entry was posted in Hardware, Python, Tutorials & Help and tagged , , , . Bookmark the permalink.

11 Responses to Control LED Using GPIO Output Pin

  1. XavM says:


    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 ?


    • Matt says:

      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.

  2. Menno Smits says:

    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.

    • Matt says:

      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.

  3. Gert says:

    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.

    • Gert says:

      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.

      • Matt says:

        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.

    • Matt says:

      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!

  4. Alexis says:

    Could this circuit be used to drive a relay?
    I would like to turn on and off an amplifier.

    • Matt says:

      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.

  5. Pingback: Raspberry Pi as power switch - DL-UAT

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>