Facebook Twitter Instagram Pinterest YouTube
    Trending
    • Pi Pico W Launched
    • Add Kodi to RetroPie Menu
    • Disable Auto-login in Raspberry Pi OS
    • Raspberry Pi Cloud Storage with MEGA
    • RetroPie Temperature Monitor from Menu
    • Pi Pico Pinout and Power Pins
    • Install Arduino IDE on Raspberry Pi
    • Raspberry Pi 400 SSD Upgrade
    Facebook Twitter Instagram Pinterest YouTube RSS
    Raspberry Pi SpyRaspberry Pi Spy
    • Home
    • Categories
      • General
      • Hardware
      • Programming
      • Python
      • Software
      • Tutorials & Help
    • BerryClip
      • BerryClip Instructions
      • BerryClip Plus Instructions
      • Videos & Reviews
    • Buy
      • Buy Pi
      • Buy Pi Accessories
      • Buy Books
    • Tools
      • Ultimate Raspberry Pi Alexa Skill
      • Pi Power Estimator App
      • Pi-Lite 14×9 LED Matrix Sprite Editor
      • RPiREF Pin-out Reference App
      • Simple Ohm’s Law Calculator
      • Web Sites & Links
    • Tutorials & Help
        Featured
        November 9, 20200

        Raspberry Pi Temperature Monitoring

        Recent
        May 6, 2022

        Add Kodi to RetroPie Menu

        February 26, 2022

        Disable Auto-login in Raspberry Pi OS

        February 2, 2022

        Raspberry Pi Cloud Storage with MEGA

      1. Contact Us
      2. Site Map
      Raspberry Pi SpyRaspberry Pi Spy
      You are at:Home»Hardware»Control LED Using GPIO Output Pin

      Control LED Using GPIO Output Pin

      20
      By Matt on June 10, 2012 Hardware, Python, Tutorials & Help

      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.

       

      Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
      Previous ArticleSimple Guide to the Raspberry Pi GPIO Header
      Next Article Overclocking & Benchmarking the Raspberry Pi

      Related Posts

      Pi Pico W Launched

      Add Kodi to RetroPie Menu

      Disable Auto-login in Raspberry Pi OS

      20 Comments

      1. XavM on June 15, 2012 9:36 am

        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,

        Reply
        • Matt on June 15, 2012 9:37 pm

          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.

          Reply
      2. Menno Smits on July 4, 2012 1:21 pm

        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.

        Reply
        • Matt on July 7, 2012 9:48 pm

          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.

          Reply
      3. Gert on July 9, 2012 5:17 pm

        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.

        Reply
        • Gert on July 9, 2012 5:34 pm

          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.

          Reply
          • Matt on July 11, 2012 10:16 am

            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.

            Reply
        • Matt on July 11, 2012 10:37 am

          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!

          Reply
      4. Alexis on September 25, 2014 6:46 pm

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

        Reply
        • Matt on September 25, 2014 8:15 pm

          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.

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

      6. Ken on April 15, 2015 9:45 pm

        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.

        Reply
        • Matt on April 16, 2015 11:31 am

          Well spotted! I’ve updated now.

          Reply
      7. joel kemp on October 11, 2015 4:33 pm

        I get a syntax error on line 5 at the first < any suggestions?

        Reply
        • Matt on October 11, 2015 8:03 pm

          Take a look at the code now. The tags were added by the site’s syntax plugin and weren’t part of the python.

          Reply
      8. Paul on January 17, 2016 4:52 pm

        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.

        Reply
        • Matt on January 18, 2016 7:39 pm

          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.

          Reply
      9. Don DeGregori on March 18, 2016 4:38 am

        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?

        Reply
        • Matt on March 19, 2016 11:52 am

          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 🙂

          Reply
      10. Cometoviv on November 10, 2016 5:04 am

        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.

        Reply

      Leave A Reply Cancel Reply

      This site uses Akismet to reduce spam. Learn how your comment data is processed.

      Recent Posts
      June 30, 2022

      Pi Pico W Launched

      May 6, 2022

      Add Kodi to RetroPie Menu

      February 26, 2022

      Disable Auto-login in Raspberry Pi OS

      February 2, 2022

      Raspberry Pi Cloud Storage with MEGA

      January 7, 2022

      RetroPie Temperature Monitor from Menu

      Categories
      • 1-wire
      • 3D Printing
      • Add-ons
      • BBC Micro:bit
      • BerryClip
      • Books
      • Camera Module
      • Cases
      • Events
      • General
      • Hardware
      • I2C
      • Infographics
      • Interfaces
      • Minecraft
      • Model A+
      • Model B+
      • News
      • Pi Models
      • Pi Pico
      • Pi Zero
      • Power
      • Programming
      • Python
      • Raspberry Pi OS
      • Raspbian
      • RetroGaming
      • Robotics
      • Sensors
      • Software
      • SPI
      • Tutorials & Help
      Tags
      3D Printing Arduino audio battery berryclip Birthday bluetooth cambridge camera CamJam DigiMakers display games GPIO I2C interface Kickstarter LCD LED Linux media Minecraft Model A Model B motionEyeOS PCB photography photos Pi-Lite portable power python Raspberry Jam Raspberry Pi Bootcamp raspbian Retrogaming retroPie screen SD card security sensor SPI temperature ultrasonic video
      Raspberry PI Related
      • Adafruit Blog
      • Average Maker
      • Official RaspBerry Pi Site
      • Raspberry Pi Pod
      • RasPi.tv
      • RaspTut
      • Stuff About Code
      Tech Resources
      • MattsBits – Pi Resources
      • Microbit Spy
      • Technology Spy
      Archives
      About

      Unofficial site devoted to the Raspberry Pi credit card sized computer offering tutorials, guides, resources,scripts and downloads. We hope to help everyone get the most out of their Pi by providing clear, simple articles on configuring, programming and operating it.

      Popular Posts
      September 19, 2014

      Top 5 Reasons The Raspberry Pi Sucks

      July 27, 2012

      16×2 LCD Module Control Using Python

      October 20, 2013

      Analogue Sensors On The Raspberry Pi Using An MCP3008

      Recent Posts
      June 30, 2022

      Pi Pico W Launched

      May 6, 2022

      Add Kodi to RetroPie Menu

      February 26, 2022

      Disable Auto-login in Raspberry Pi OS

      Facebook Twitter Instagram Pinterest YouTube RSS

      Entries RSS | Comments RSS

      This site is not associated with the official Raspberrypi.org site or the Raspberry Pi Foundation. Raspberry Pi is a trademark of the Raspberry Pi Foundation.

      Copyright © 2022 - All Rights Reserved - Matt Hawkins

      Type above and press Enter to search. Press Esc to cancel.