Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
in Tutorial & Interview questions by Goeduhub's Expert (8.3k points)

Control Brightness of LED Using PWM of Raspberry PI. How does PWM control LED brightness? Can Raspberry Pi do PWM? What is Gpio in Raspberry Pi?

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-


Course Name



Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon


Natural Language Processing-NLP with Deep Learning in Python Apply Coupon


Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.3k points)
selected by
Best answer

Hardware requirement and connections:

Hardware requirement and connections are same as explained in 

 What is Gpio in Raspberry Pi? 

Turning on and off LED with your Raspberry Pi's GPIO Pins

LED to Raspberry-PI_IOT-Training-in-Jaipur

Python program

import RPi.GPIO as GPIO # Importing RPi library to use the GPIO pins

from time import sleep # Importing sleep from time library

led_pin = 21 # Initializing the GPIO pin 21 for LED

GPIO.setmode(GPIO.BCM) # We are using the BCM pin numbering

GPIO.setup(led_pin, GPIO.OUT) # Declaring pin 21 as output pin

pwm = GPIO.PWM(led_pin, 100) # Created a PWM object

pwm.start(0) # Started PWM with 0% duty cycle

while 1: # Loop will run forever

          for x in range(100): # This Loop will run 100 times

                    pwm.ChangeDutyCycle(x) # Change duty cycle

                    sleep(0.01) # Delay of 10mS

          for x in range(100,0,-1): # Loop will run 100 times; 100 to 0



pwm.stop() # Stop the PWM

GPIO.cleanup() # Make all the output pins LOW

3.3k questions

7.1k answers


4.6k users

Related questions

0 like 0 dislike
1 answer 640 views
0 like 0 dislike
1 answer 1.2k views
0 like 0 dislike
1 answer 850 views


About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © Social::   |  |