LED Fade Using Pulse Width Modulation¶
PWM is a type of Digital Signal. A Digital Signal can have only two possible states, ON or OFF, 0 or 1, or in the case of this project, 0 or 3.3 volts. That’s why making the LED blink didn’t require PWM, because the LED was simply turning ON and OFF. In PWM signals, we can have both of these two states for a specified time period.
Suppose you want to control the brightness of an LED, the possible approach is to turn on an LED for a small period of time and then turn it off again for a small period of time. So, when this ON and OFF happens at very high speed, it gives the effect of dimmed LED.
%%html
<img src="https://raw.githubusercontent.com/OnionIoT/Onion-Docs/master/Omega2/Kit-Guides/img/pwm-signals.jpg" , width=400, height=200>
CIRCUIT DIAGRAM:¶
%%html
<img src="http://razzpisampler.oreilly.com/images/rpck_0901.png" , width=400, height=200>
code
import time
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BCM)
GPIO.setup(17,GPIO.OUT) # initialize GPIO17 as an output.
p = GPIO.PWM(18,100) # 100Hz frequency
p.start(0) #start at 0% duty cycle
while True:
for x in range (50):
p.ChangeDutyCycle(x)
time.sleep(0.1)
for x in range (50):
p.ChangeDutyCycle(50-x)
time.sleep(0.1)
No comments:
Post a Comment