How ToIoT HardwaresRaspberry PiTutorials/DIY

Controlling LED with Raspberry Pi PART-2

In the previous article, we discussed “How to connect an LED to Raspberry Pi, switching an LED on/off from the Raspberry Pi console, Python program for blinking an LED, interfacing an LED and switch with Raspberry Pi, and sending an email on switch press.” If you haven’t read it yet, check out the previous article: Controlling LED with Raspberry Pi – Part 1. Now, let’s continue with Part 2.

Understanding LED Safety and Current Limiting Resistors

LEDs are a useful, cheap, and efficient way of producing light, but you must use them carefully. Connecting an LED directly to a voltage source (such as a GPIO output) greater than about 1.7V will cause it to draw excessive current. This may damage the LED or your Raspberry Pi GPIO pin.

To prevent this, always use a series resistor with LEDs. The resistor limits the current flowing through the LED, protecting both the LED and the GPIO pin.

Recommended Series Resistor Values for LEDs on a 3.3V GPIO Pin

LED Type Resistor Value Current (mA)
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

Leaving GPIO Pins in a Safe State

To avoid accidental short circuits that can damage your Raspberry Pi, always set GPIO pins to input mode when your program exits. Use the try...finally construction with the GPIO.cleanup() method to ensure this happens automatically.

import RPi.GPIO as GPIO
import time

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

try:
    while True:
        GPIO.output(18, True)
        time.sleep(0.5)
        GPIO.output(18, False)
        time.sleep(0.5)

finally:
    print("Cleaning up")
    GPIO.cleanup()

Now, when you press Ctrl + C to stop the program, GPIO.cleanup() will be called before the program exits, ensuring the GPIO pins are reset safely.

Controlling LED Brightness with PWM (Pulse Width Modulation)

If you’d like to adjust the brightness of an LED using Python, the RPi.GPIO library offers a PWM feature to control power output.

Example Code for LED Brightness Control:

import RPi.GPIO as GPIO

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

pwm_led = GPIO.PWM(led_pin, 500)  # 500 Hz frequency
pwm_led.start(100)  # Start at 100% brightness

while True:
    try:
        duty_s = input("Enter brightness (0 to 100): ")
        duty = int(duty_s)
        pwm_led.ChangeDutyCycle(duty)
    except KeyboardInterrupt:
        print("Exiting program.")
        pwm_led.stop()
        GPIO.cleanup()
        break

Key Notes:

  • If you’re using Python 3, replace raw_input() with input().
  • The PWM frequency can be adjusted by modifying this line:
    pwm_led = GPIO.PWM(led_pin, 500)
    

    A higher frequency offers smoother brightness control but may require tuning based on your LED type.

By utilizing PWM, you can create dynamic lighting effects and control LED intensity for various projects with your Raspberry Pi.

You may like also:

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