0
0
Iot-protocolsHow-ToBeginner · 3 min read

Raspberry Pi Project for LED Control: Simple Guide and Code

To control an LED with a Raspberry Pi, connect the LED to a GPIO pin and ground, then use Python's RPi.GPIO library to turn it on or off by setting the pin's output state. This simple project uses GPIO.setup() to configure the pin and GPIO.output() to control the LED.
📐

Syntax

Here is the basic syntax to control an LED connected to a Raspberry Pi GPIO pin:

  • GPIO.setmode(GPIO.BCM): Sets the pin numbering system to BCM (Broadcom chip-specific numbering).
  • GPIO.setup(pin_number, GPIO.OUT): Configures the chosen pin as an output.
  • GPIO.output(pin_number, GPIO.HIGH): Turns the LED on by sending a high voltage to the pin.
  • GPIO.output(pin_number, GPIO.LOW): Turns the LED off by sending a low voltage to the pin.
  • GPIO.cleanup(): Resets all GPIO pins to a safe state after use.
python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  # Use BCM pin numbering
pin = 18  # GPIO pin connected to LED
GPIO.setup(pin, GPIO.OUT)  # Set pin as output

GPIO.output(pin, GPIO.HIGH)  # Turn LED on
time.sleep(1)  # Wait 1 second
GPIO.output(pin, GPIO.LOW)  # Turn LED off

GPIO.cleanup()  # Reset GPIO pins
💻

Example

This example turns an LED on for one second, then off, using GPIO pin 18 on the Raspberry Pi. It demonstrates setting up the pin, controlling the LED, and cleaning up afterward.

python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
pin = 18
GPIO.setup(pin, GPIO.OUT)

try:
    print("LED on")
    GPIO.output(pin, GPIO.HIGH)  # LED on
    time.sleep(1)  # Wait 1 second
    print("LED off")
    GPIO.output(pin, GPIO.LOW)  # LED off
finally:
    GPIO.cleanup()  # Clean up GPIO settings
Output
LED on LED off
⚠️

Common Pitfalls

Common mistakes when controlling an LED with Raspberry Pi include:

  • Not setting the GPIO mode with GPIO.setmode(), which can cause pin numbering confusion.
  • Forgetting to call GPIO.cleanup(), which can leave pins in an unstable state.
  • Connecting the LED without a resistor, which can damage the LED or Raspberry Pi.
  • Using the wrong pin number system (BCM vs BOARD) and wiring the LED to the wrong pin.
python
import RPi.GPIO as GPIO

# Wrong: Missing GPIO.setmode()
pin = 18
GPIO.setup(pin, GPIO.OUT)  # This will cause an error or unexpected behavior

# Right:
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
📊

Quick Reference

CommandPurpose
GPIO.setmode(GPIO.BCM)Set pin numbering to BCM mode
GPIO.setup(pin, GPIO.OUT)Set a pin as output
GPIO.output(pin, GPIO.HIGH)Turn LED on
GPIO.output(pin, GPIO.LOW)Turn LED off
GPIO.cleanup()Reset GPIO pins safely

Key Takeaways

Always set GPIO mode with GPIO.setmode() before using pins.
Use GPIO.setup() to configure pins as output before controlling an LED.
Include a resistor in your LED circuit to protect components.
Call GPIO.cleanup() after your program to reset pins safely.
Double-check pin numbering (BCM vs BOARD) to avoid wiring errors.