IoT HardwaresRaspberry PiTutorials/DIY

How to Control a Red LED on Raspberry Pi Using GPIO

Introduction

The Raspberry Pi is a powerful single-board computer that allows users to interface with various hardware components using its GPIO (General Purpose Input/Output) pins. One of the most common beginner projects is controlling an LED, specifically a red LED, using the Raspberry Pi.

This article will guide you through:

  • Understanding the GPIO pin configuration.
  • Wiring a red LED correctly.
  • Using Python and Bash commands to control the LED.
  • Different ways to implement LED blinking using GPIO Zero and RPi.GPIO libraries.

Understanding Raspberry Pi GPIO Pins

Raspberry Pi models come with a 40-pin GPIO header, where certain pins provide power (3.3V, 5V), ground (GND), and programmable GPIOs.

For LED control, we focus on:

  • A GPIO pin (e.g., GPIO 17) to send HIGH (ON) or LOW (OFF) signals.
  • GND (Ground) to complete the circuit.

Raspberry Pi GPIO Pinout

Here is the standard Raspberry Pi GPIO pinout for reference:

 3.3V   (1) (2)  5V
 GPIO2   (3) (4)  5V
 GPIO3   (5) (6)  GND
 GPIO4   (7) (8)  GPIO14
  GND    (9) (10) GPIO15
GPIO17  (11) (12) GPIO18
GPIO27  (13) (14) GND
GPIO22  (15) (16) GPIO23

Example GPIO selection:

We will use GPIO 17 to control the LED.

Wiring the Red LED to the Raspberry Pi

To connect a red LED safely to your Raspberry Pi, follow these steps:

Required Components:

  • Raspberry Pi (Any model with GPIO headers)
  • Red LED
  • 330Ω resistor (to limit current and prevent damage)
  • Breadboard & jumper wires

Wiring Diagram:

  1. Connect one leg of the red LED (longer leg – anode) to GPIO 17.
  2. Connect the other leg (shorter leg – cathode) to one side of a 330Ω resistor.
  3. Connect the other side of the resistor to GND.

Method 1: Controlling Red LED Using Python (GPIO Zero Library)

The GPIO Zero library simplifies LED control in Python.

Installation of GPIO Zero:

sudo apt update
sudo apt install python3-gpiozero

Python Code to Turn LED On/Off:

from gpiozero import LED
from time import sleep

led = LED(17)  # GPIO 17

while True:
    led.on()  # Turn LED ON
    sleep(1)
    led.off()  # Turn LED OFF
    sleep(1)

This script will blink the LED every second.

To stop, press CTRL+C.

Method 2: Using Python (RPi.GPIO Library)

If you prefer RPi.GPIO, use the following method.

Installation of RPi.GPIO:

sudo apt install python3-rpi.gpio

Python Code Using RPi.GPIO:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

try:
    while True:
        GPIO.output(17, GPIO.HIGH)  # Turn LED ON
        time.sleep(1)
        GPIO.output(17, GPIO.LOW)   # Turn LED OFF
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()

This program also blinks the LED every second.

Method 3: Controlling LED via Terminal (Shell Commands)

If you prefer not to use Python, you can control the LED using sysfs.

Enable GPIO 17:

echo 17 > /sys/class/gpio/export

Set GPIO 17 as Output:

echo out > /sys/class/gpio/gpio17/direction

Turn LED ON:

echo 1 > /sys/class/gpio/gpio17/value

Turn LED OFF:

echo 0 > /sys/class/gpio/gpio17/value

Clean Up GPIO:

echo 17 > /sys/class/gpio/unexport

This method is useful for quick testing without Python.

Method 4: Using Raspberry Pi with Bash Script

You can automate LED control using a Bash script.

Create a Bash Script:

nano led_control.sh

Add the following lines:

#!/bin/bash
echo 17 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio17/direction

while true
do
    echo 1 > /sys/class/gpio/gpio17/value
    sleep 1
    echo 0 > /sys/class/gpio/gpio17/value
    sleep 1
done

Make the script executable:

chmod +x led_control.sh

Run the script:

./led_control.sh

To stop the script, press CTRL+C.

Troubleshooting Tips

  • LED is not lighting up?
    • Check if you’ve used the correct GPIO pin.
    • Verify that the resistor is not too high (330Ω is recommended).
    • Make sure the LED polarity is correct (long leg to GPIO, short leg to GND).
  • Python script gives permission error?
    • Run the script as root (sudo python3 script.py).
  • LED remains ON or OFF permanently?
    • Ensure the script is running properly.
    • Double-check the GPIO pin configuration.

Conclusion

Controlling a red LED using Raspberry Pi’s GPIO pins is a fundamental hardware project that introduces you to electronic circuit design and programming. You can control the LED using Python (GPIO Zero or RPi.GPIO), shell commands, or Bash scripting.

Read This: How To Configure WiFi on Raspberry Pi

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

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