Thursday, April 25, 2024
How ToIoT HardwaresRaspberry PiTutorials/DIY

Controlling LED with Raspberry Pi PART-2

In previous article we discuss about “How to connect led to raspberry, Switching LED on/off from Raspberry Pi console, Python program for blinking LED and Interfacing an LED an Switch with Raspberry Pi and Python program for sending an email on switch press” So please read this previous article Controlling LED with Raspberry Pi. let’s begin Controlling LED with Raspberry Pi PART-2.

LEDs are a very useful, cheap, and efficient way of producing light, but you do have to be careful how you are use them. If you are connected directly to a voltage source (such as GPIO output) that is greater than about 1.7V, they will draw a very large current. This can often be enough to destroy the LED or whatever is providing the current – which is not good if your RPi is providing the current.

You should always use a series resistor with LED because the series resistor is placed between the LED and 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.

Selecting series resistors for LEDs and a 3.3V GPIO pin

LED Type                    Resistor              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 the GPIO Pins in a Safe State

You want all the GPIO pins to be set to inputs whenever your program exists so that there is less of  a chance of a chance of an accidental  short on the GPIO  header, which could damage your raspberry Pi.

So use try: finally: construction and the GPIO.cleanup method.


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 Ctrl+C the program to close it, GPIO.cleanup will be called before the program exists.


You may like also: How To Use Raspberry pi in a truely headless mode


Controlling the brightness of an LED

If you wants to vary the brightness of an LED from a python program. The RPi.GPIO library has a pulse-width modulation (PWM) feature that allows you to control power to an LED and its brightness.



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)
pwm_led.start(100)

while True:
        duty_s = raw_input("Enter brightness (0 to 100)")
        duty = int(duty_s)
        pwn_led.ChangeDutyCycle(duty)
        

if you are using python 3 rather than python 2, change the command raw_input to input.
So PWM is a clever technique where you vary the length of pulse while keeping the overall number of pulse per second (the frequency in Hz) constant.
You can change the PWM frequency by modifying this line:

 pwm_led = GPIO.PWM(led_pin,500) 

You may like also:

Buy now : Raspberry PI 3 Model B+ Motherboard


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