Sunday, December 25, 2016

Control Brightness of LED Using Raspberry Pi

In the previous post we see how to turn on and off Led's connected to Raspberry Pi, and now in this post i will show you how to control the brightness of a LED using Raspberry Pi.
The idea here is that you can switch an led on and off very fast and trick your eye thinking it’s on all the time. Human Eyes has the persistence of vision of 1/16, which means if something is turning on and off at a rate greater than 16 times in a second, then our eyes will see it on, the most common example of this is lights at our home, which turns on and off 50 times per second but we never see this fluctuation because of the limitation of our eyes.
Control Brightness of LED using PWM
For circuit construction you can use any GPIO pin of Raspberry Pi, just make sure to add current limiting resistor in series with Led in such a way that current from the GPIO pin doesn't exceed 16mA value.
PWM Signals
From the above figure, it is more clear, like what we are going to do, by varying the duration of on and off pulses we can adjust the brightness of the Led, obviously 100% means full brightness or completely on and 0% means completely off.
Now after the connection is completed it's time to write the program for controlling the brightness of Led in Python, before proceeding further make sure that Raspberry Pi GPIO library is installed, although this library is installed by default but still it is better to update this library and this can be done by running the command "sudo apt-get install rpi-gpio", once the library is updated we can use the nano editor to write and run the Python program.
Lets discuss some of the important functions which we are going to use in our program.
  • Initialize and Start the PWM
    • pwm = GPIO.PWM(Pin Number, Frequency), this function will configure the Raspberry Pi pin as PWM pin with the entered frequency (Frequency value should be greater than Persistence Of Vision value of Human Eyes, otherwise you will see flickering).
    • pwm.Start(Duty Cycle), this function will start the PWM pulse on selected with the entered duty cycle value, valid values are from 0 to 100 where 0 means completely off and 100 means completely on.
  • Change Duty Cycle
    • pwm.ChangeDutyCycle(Duty Cycle), this function will change the duty cycle after the PWM is started.
  • Stop PWM
    • pwm.Stop(), this function will stop the PWM on the selected pin.
So by using the above function, now we can write the program to control the brightness of Led, the program is given below.
 import RPi.GPIO as GPIO  
 import time  
 # Led1 on my Board  
 led = 11  
 GPIO.setmode( GPIO.BOARD)  
 GPIO.setup( led, GPIO.OUT)  
 # 50Hz PWM Frequency  
 pwm_led = GPIO.PWM( led, 50)  
 # Full Brightness, 100% Duty Cycle  
 pwm_led.start(100)  
 try:  
   while True:  
     duty_s = raw_input("Enter Brightness Value (0 to 100):")  
     # Convert into Integer Value  
     duty = int(duty_s)  
     pwm_led.ChangeDutyCycle(duty)  
     time.sleep(0.5)  
 except KeyboardInterrupt:  
   print "Exiting Program"  
 except:  
   print "Error Occurs, Exiting Program"  
 finally:  
   GPIO.cleanup()  

The above program is self explanatory, but if still you don't understand it either watch the video given below or comment below in case of any problem.



Note: RPi.GPIO provides a built-in function GPIO.cleanup() to clean up all the ports you’ve used. But be very clear what this does. It only affects the ports you have set in the current program. It resets any ports you have used in this program back to input mode. This prevents damage from, the situation where you have a port set HIGH as an output and you accidentally connect it to GND (LOW), which would short-circuit the port and possibly fry it. Inputs can handle either 0V (LOW) or 3.3V (HIGH), so it’s safer to leave ports as inputs.

4 comments:

  1. hello!
    what if i want to control analog output(220 volts AC) in the form of lights?

    ReplyDelete
  2. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. wista floodlights

    ReplyDelete