How to Control the Brightness of LED in Raspberry pi
You want to vary the brightness of an LED from a Python program. Today I show you How to Control the Brightness of LED in Raspberry piRequired Component connect LED Raspberry Pi :
1.Raspberry pi 5. 5mm LED
This book will help you to gain more knowledge of Raspberry pi Software and Hardware Problems and Solutions
Raspberry Pi Cookbook
Circuit diagram Control the Brightness of LED in Raspberry pi :
shows how you can wire this LED using a solderless breadboard and male-to-female jumper leadsCode Control the Brightness of LED in Raspberry pi:
import RPi.GPIO as GPIO led_pin = 12 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) pwm_led.ChangeDutyCycle(duty)The RPi.GPIO library has a pulse-width modulation (PWM) feature that allows you to control the power to an LED and its brightness If you are using Python 3 rather than Python 2, change the command raw_input to input. Run the Python program, and you will be able to change the brightness by entering a number between 0 and 100: pi@raspberrypi ~ $ sudo python led_brightness.py Enter Brightness (0 to 100):0 Enter Brightness (0 to 100):20 What is PWM : PWM is a technique where you vary the length of pulses while keeping the overall number of pulses per second (the frequency in Hz) constant.
At high frequencies, the measured PWM frequency varies somewhat from the frequency supplied as an argument. This might change in later versions of the PWM feature of
RPi.GPIO
. More about PWM Click Here
No comments