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

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.

[member]

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) [/member]

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

Source : http://razzpisampler.oreilly.com/

Source: Interface LED with Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top