Home Learning Controlling the Brightness of an LED

Controlling the Brightness of an LED

by shedboy71

In this example we will show you how to control the brightness of an LED connected . The RPi.GPIO library has a PWM feature that allows you to control the power to an LED and its brightness, we will use this in this example



Fairly simple, simply connect an LED like this



This is a breadboard layout



Save the following as pwm.py

[codesyntax lang=”python”]

import RPi.GPIO as GPIO

#pin 12 on GPIO header
led_pin = 18
GPIO.setup(led_pin, GPIO.OUT)
pwm_led = GPIO.PWM(led_pin, 500)

while True:
	brightness = raw_input("Enter Brightness value (0 to 100):")
	duty_value = int(brightness)




From a terminal run the program by typing in

sudo python pwm.py

You should now see a prompt, type in a value and press enter and observe the effect on the LED



You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More