GOEDUHUB  Online Courses || Last Batch Student's Projects || COVID-19 Projects(AI-ML) || Universities  ||  Placement Preparation  
+91-7976731765 Free Online Tutorials ||  MACHINE LEARNING || Python || DBMS || OOPs || DSA || Java || Linux/Unix ||  C Programming
0 like 0 dislike
88 views
in Tutorial & Interview questions by Goeduhub's Expert (8k 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?

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8k 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.ChangeDutyCycle(x)

                    sleep(0.01)

pwm.stop() # Stop the PWM

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

Related questions

 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...