Interface LED with Raspberry Pi




Solution

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:

  • Breadboard and jumper wires (see “Prototyping Equipment”)
  • 1kΩ resistor (see “Resistors and Capacitors”)
  • LED (see “Opto-Electronics”)

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

Figure . Connecting an LED to a Raspberry Pi

Interface LED with 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.

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.

Interface LED with Raspberry Pi SchematicRaspberry 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  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 . Selecting series resistors for LEDs and a 3.3V GPIO pin
Red470Ω3.5
Red1kΩ1.5
Orange, yellow, green470Ω2
Orange, yellow, green1kΩ1
Blue, white100Ω3
Blue, white270Ω1

 

For more detail: Interface LED with Raspberry Pi




© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top