Discuss on GitHub

Connecting an LED


You want to know how to connect an LED to the Raspberry Pi.


Connect an LED (see “Opto-Electronics”) to one of the GPIO pins using a 470Ω or 1kΩ series resistor (see “Resistors and Capacitors”) to limit the current. To make this recipe, you will need:

Figure 3-1 shows how you can wire this using solderless breadboard and male-to-female jumper leads.

Figure 3-1. Connecting an LED to a Raspberry Pi

Having connected the LED, we need to be able to turn it on and off using commands from Python. To do this, follow “Installing RPi.GPIO” to install the RPi.GPIO Python library.

Start a Python console (“Using the Python Console”) from the Terminal with superuser access and enter these commands:

$ sudo python
>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)
>>> GPIO.setup(18, GPIO.OUT)
>>> GPIO.output(18, True)
>>> GPIO.output(18, False)

This will turn your LED on and off.


LEDs are a very useful, cheap, and efficient way of producing light, but you do have to be careful how you use them. If they are connected directly to a voltage source (such as a GPIO output) that is greater than about 1.7 volts, they will draw a very large current. This can often be enough to either destroy the LED or whatever is providing the current—which is not good if your Raspberry Pi is providing the current.

You should always use a series resistor with an LED because the series resistor is placed between the LED and the voltage source, which limits the amount of current flowing through the LED to a level that is safe for both the LED and the GPIO pin driving it.

Raspberry Pi GPIO pins can only provide about 3 mA of current. LEDs will generally illuminate with any current greater than 1 mA, but will be brighter with more current. Use Table 3-1 as a guide to selecting a series resistor based on the type of LED; the table also indicates the approximate current that will be drawn from the GPIO pin.

Table 3-1. Selecting series resistors for LEDs and a 3.3V GPIO pin







Orange, yellow, green



Orange, yellow, green



Blue, white



Blue, white



As you can see, in all cases, it is safe to use a 470Ω resistor. If you are using a blue or white LED, you can reduce the value of the series resistor considerably. If you want to play it safe, use 1kΩ.

If you wanted to extend the experiments that you made in the Python console into a program that makes the LED blink on and off repeatedly, you could paste the following code into the IDLE (“Editing Python Programs with IDLE”) or nano (“Editing a File”) editors. Save the file as led_blink.py. You can also download the program from the Raspberry Pi Cookbook website. Follow the link to this book and then the Downloads section.

import RPi.GPIO as GPIO
import time

GPIO.setup(18, GPIO.OUT)

while (True):
    GPIO.output(18, True)
        GPIO.output(18, False)

Remember that to run the program, you must have superuser privileges for the RPi.GPIO library, so you need to use this command:

$ sudo python led_blink.py

See Also

Check out this handy series resistor calculator.

For more information on using breadboard and jumper wires with the Raspberry Pi, see “Using a Breadboard with Jumper Leads”.