LED Blinking with Raspberry Pi
Interfacing LED with Raspberry Pi for Controlling LED ON/OFF
Interfacing LED with Raspberry Pi for Controlling LED ON/OFF demonstrates how a Light Emitting Diode (LED) can be connected to a Raspberry Pi board and controlled using Python programming. An LED (Light Emitting Diode) is an electronic component that emits light when electric current flows through it in the forward direction. In this experiment, an LED is connected to GPIO 12 of the Raspberry Pi. The LED is turned ON for 2 seconds and OFF for 5 seconds continuously. The Raspberry Pi controls the LED using digital output signals through its GPIO pins.
Raspberry Pi → GPIO Pin → LED → Light
Theory
A Light Emitting Diode (LED) is a semiconductor device that converts electrical energy into light energy. LEDs work on the principle of electroluminescence. When a suitable forward voltage is applied across the LED, electrons and holes recombine at the semiconductor junction, releasing energy in the form of light.
An LED has two terminals:
- Anode (+): Positive terminal of the LED.
- Cathode (−): Negative terminal of the LED.
The LED glows when it is connected with the correct polarity and sufficient current flows through it. The Raspberry Pi controls the LED using its General Purpose Input/Output (GPIO) pins.
- HIGH (3.3V): Turns the LED ON.
- LOW (0V): Turns the LED OFF.
Python programming is used to configure the GPIO pin as an output and control the LED using HIGH and LOW signals.
GPIO HIGH → LED ON → Light Emission
GPIO LOW → LED OFF
LED Pins
An LED has two terminals:
| Pin | Connection | Function |
|---|---|---|
| Anode (+) | Raspberry Pi GPIO 12 through a current-limiting resistor | Positive terminal |
| Cathode (−) | GND | Ground terminal |
The longer leg of the LED is generally the Anode (+), while the shorter leg is the Cathode (−). A current-limiting resistor, typically 220 ohms or 330 ohms, must be connected in series with the LED to protect it from excessive current.
LED Control Using Raspberry Pi
The Raspberry Pi provides multiple GPIO pins that can be configured as digital outputs to control external LEDs. The GPIO.setup() function is used to configure a GPIO pin as an output. The GPIO.output() function is used to control the output state of the pin.
The syntax is:
GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.HIGH) GPIO.output(pin, GPIO.LOW)
Where:
| Function / Parameter | Description |
|---|---|
| GPIO.setup() | Configures the selected GPIO pin |
| GPIO.OUT | Configures the pin as a digital output |
| GPIO.output(pin, GPIO.HIGH) | Sets the selected pin HIGH and turns the LED ON |
| GPIO.output(pin, GPIO.LOW) | Sets the selected pin LOW and turns the LED OFF |
Applications
LEDs are commonly used in:
- Status indication systems
- Traffic signal systems
- Electronic control panels
- Decorative lighting
- Alarm and warning systems
- Digital display systems
- Home automation systems
Connections
The following connections are used to interface an LED with the Raspberry Pi.
| Device | Pin | Raspberry Pi | Function |
|---|---|---|---|
| LED | Anode (+) | GPIO 12 (BCM) through 220 ohm resistor | Positive output |
| LED | Cathode (−) | GND (Physical Pin 34) | Ground |
GPIO Pin Details:
| Parameter | Value |
|---|---|
| GPIO Numbering | BCM |
| GPIO Pin | GPIO 12 |
| Physical Pin | Pin 32 |
| Ground | Physical Pin 34 |
| Logic Voltage | 3.3V |
Note: Raspberry Pi GPIO pins operate at 3.3V logic. Always use a suitable current-limiting resistor with the LED. Never apply 5V directly to a GPIO pin.
Algorithm
The following algorithm is used to control the LED using Raspberry Pi:
- Start the Raspberry Pi.
- Open the Python programming environment or terminal.
- Import the RPi.GPIO and time libraries.
- Declare a variable to assign BCM GPIO pin 12 as the LED pin.
- Configure GPIO numbering using BCM mode.
- Disable GPIO warnings.
- Configure GPIO 12 as OUTPUT.
- Set the initial output state to LOW.
- Start an infinite while loop.
- Set GPIO 12 HIGH to turn ON the LED.
- Display the message "LED ON".
- Provide a delay of 2 seconds.
- Set GPIO 12 LOW to turn OFF the LED.
- Display the message "LED OFF".
- Provide a delay of 5 seconds.
- Repeat the process continuously.
The overall operation is:
GPIO HIGH → LED ON → Delay 2 seconds → GPIO LOW → LED OFF → Delay 5 seconds → Repeat
Working
The Raspberry Pi controls the LED through GPIO 12 using Python programming. The working sequence is:
- The RPi.GPIO library is imported to access the GPIO pins.
- The time library is imported to provide time delays.
- GPIO numbering is configured using BCM mode.
- GPIO 12 is configured as an OUTPUT pin.
- The initial state of the GPIO pin is set to LOW.
- The GPIO pin is set HIGH, turning ON the LED.
- The LED remains ON for 2 seconds.
- The GPIO pin is set LOW, turning OFF the LED.
- The LED remains OFF for 5 seconds.
- The process repeats continuously.
Raspberry Pi → GPIO HIGH → LED ON
Raspberry Pi → GPIO LOW → LED OFF
Observation
LED ON/OFF Control Program
The following Python program is used to control an LED connected to GPIO 12 of the Raspberry Pi.
import RPi.GPIO as GPIO
import time
pin = 12
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
while True:
print("LED ON")
GPIO.output(pin, True)
time.sleep(2)
print("LED OFF")
GPIO.output(pin, False)
time.sleep(5)
GPIO.cleanup()
Program Explanation
- The RPi.GPIO library is imported to control the Raspberry Pi GPIO pins.
- The time library is imported to provide delays.
- The variable pin is assigned the value 12, representing BCM GPIO 12.
- GPIO.setmode(GPIO.BCM) selects BCM GPIO numbering.
- GPIO.setwarnings(False) disables GPIO warning messages.
- GPIO.setup(pin, GPIO.OUT) configures GPIO 12 as an output pin.
- GPIO.output(pin, GPIO.LOW) sets the initial state of the LED to OFF.
- The while True loop continuously executes the LED control instructions.
- GPIO.output(pin, True) sets GPIO 12 HIGH and turns ON the LED.
- time.sleep(2) keeps the LED ON for 2 seconds.
- GPIO.output(pin, False) sets GPIO 12 LOW and turns OFF the LED.
- time.sleep(5) keeps the LED OFF for 5 seconds.
- The print() function displays the LED status in the terminal.
- GPIO.cleanup() releases the GPIO resources when the program exits. It is normally reached after the loop is interrupted.
Observation Table
| Step | GPIO State | LED Condition | Duration |
|---|---|---|---|
| 1 | HIGH (3.3V) | ON | 2 seconds |
| 2 | LOW (0V) | OFF | 5 seconds |
Result
The Raspberry Pi successfully interfaces with an LED using BCM GPIO 12 (Physical Pin 32). The LED is turned ON for 2 seconds and turned OFF for 5 seconds continuously. The LED status is also displayed on the Python terminal. Thus, the LED is successfully controlled using Raspberry Pi GPIO pins and Python programming.