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

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

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

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

• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)

• 1kΩ resistor (see “Resistors and Capacitors” on page 380)

• LED (see “Opto-Electronics” on page 381)

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

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

Start a Python console (Recipe 5.3) 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.

Discussion

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 9-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.

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 (Recipe 5.2) or nano (Recipe 3.6) 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.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while (True):
GPIO.output(18, True)
time.sleep(0.5)
GPIO.output(18, False)
time.sleep(0.5)

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 Recipe 8.10.

Table A-7. Opto-electronics
5mm red LED SparkFun: COM-09590, Adafruit: 299
RGB common cathode LED SparkFun: COM-11120
TSOP38238 IR sensor SparkFun: SEN-10266, Adafruit: 157
Table A-4. Resistors and capacitors
270Ω 0.25W resistor Mouser: 293-270-RC
470Ω 0.25W resistor Mouser: 293-470-RC
1kΩ 0.25W resistor Mouser: 293-1k-RC
3.3kΩ 0.25W resistor Mouser: 293-3.3k-RC
4.7kΩ 0.25W resistor Mouser: 293-4.7k-RC
10 kΩ trimpot Adafruit: 356, SparkFun: COM-09806, Mouser: 652-3362F-1-103LF
Photoresistor Adafruit: 161, SparkFun: SEN-09088
220nF capacitor MCM: 31-0610, Mouser: 80-C322C224M5U5HA
Table A-3. Prototyping equipment
Description Suppliers
M-M jumper wires SparkFun: PRT-08431, Adafruit: 759
M-F jumper wires SparkFun: PRT-09140, Adafruit: 825
F-F jumper wires SparkFun: PRT-08430, Adafruit: 794
Half-sized breadboard SparkFun: PRT-09567 Adafruit: 64
Pi Cobbler Adafruit: 1105
Table 9-1. Selecting series resistors for LEDs and a 3.3V GPIO pin
LED type Resistor Current (mA)
Red 470Ω 3.5
Red 1kΩ 1.5
Orange, yellow, green 470Ω 2
Orange, yellow, green 1kΩ 1
Blue, white 100Ω 3
Blue, white 270Ω 1
9.1

Related Answered Questions

Question: 9.11

Verified Answer:

Assuming you have a 5V volt meter, you can use a P...
Question: 9.10

Verified Answer:

The way to do this is to use a technique called Ch...
Question: 9.9

Verified Answer:

Use PWM to control the power to each of the red, g...
Question: 9.8

Verified Answer:

Using the Tkinter user interface framework, write ...
Question: 9.7

Verified Answer:

Using the Tkinter user interface framework, write ...
Question: 9.5

Verified Answer:

Use a relay and small transistor. Figure 9-5 shows...
Question: 9.3

Verified Answer:

Use a piezo-electric buzzer connected to a GPIO pi...
Question: 9.4

Verified Answer:

These high-power LEDs use far too much current to ...
Question: 9.6

Verified Answer:

Use a PowerSwitch Tail II (see “Modules” on page 3...