This document discusses interfacing an RGB LED with a Raspberry Pi using pulse width modulation (PWM). It provides an overview of RGB LEDs and PWM, describing how PWM works by rapidly switching power on and off faster than what affects the load. It then gives examples of code in Python to control an RGB LED connected to GPIO pins on the Raspberry Pi by changing the duty cycle from 1-100% to gradually change the color.
5. www.techvilla.org.in
Pulse Width Modulation
Pulse-width modulation (PWM), or pulse-duration modulation
(PDM), is a commonly used technique for controlling power to
inertial electrical devices, made practical by modern
electronic power switches.
The PWM switching frequency has to be much faster than
what would affect the load, which is to say the device that
uses the power.
The main advantage of PWM is that power loss in the
switching devices is very low.
PWM has also been used in certain communication systems
where its duty cycle has been used to convey information
over a communications channel.
6. www.techvilla.org.in
Types of PWM
Three types of pulse-width modulation (PWM) are
possible:
The pulse center may be fixed in the center of the time
window and both edges of the pulse moved to
compress or expand the width.
The lead edge can be held at the lead edge of the
window and the tail edge modulated.
The tail edge can be fixed and the lead edge
modulated.
7. Principle
Pulse-width modulation uses a rectangular
pulse wave whose pulse width is
modulated resulting in the variation of the
average value of the waveform.
The simplest way to generate a PWM signal
is the intersective method, which requires
only a sawtooth or a triangle waveform
(easily generated using a simple oscillator)
and a comparator
When the value of the reference signal (the
red sine wave in figure 2) is more than the
modulation waveform (blue), the PWM
signal (magenta) is in the high state,
otherwise it is in the low state.
9. www.techvilla.org.in
Telecommunications
In telecommunications, the widths of the pulses correspond to
specific data values encoded at one end and decoded at the
other.
Pulses of various lengths (the information itself) will be sent at regular
intervals (the carrier frequency of the modulation).
The inclusion of a clock signal is not necessary, as the leading edge
of the data signal can be used as the clock if a small offset is added
to the data value in order to avoid a data value with a zero length
pulse.
10. www.techvilla.org.in
Power delivery
PWM can be used to control the amount of power
delivered to a load without incurring the losses that
would result from linear power delivery by resistive
means.
Potential drawbacks to this technique are the pulsations
defined by the duty cycle, switching frequency and
properties of the load.
With a sufficiently high switching frequency and, when
necessary, using additional passive electronic filters, the
pulse train can be smoothed and average analog
waveform recovered.
11. www.techvilla.org.in
Audio effects and amplification
PWM is sometimes used in sound (music) synthesis, in particular
subtractive synthesis, as it gives a sound effect similar to
chorus or slightly detuned oscillators played together.
he ratio between the high and low level is typically
modulated with a low frequency oscillator, or LFO.
In addition, varying the duty cycle of a pulse waveform in a
subtractive-synthesis instrument creates useful timbral
variations.
13. www.techvilla.org.in
code
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(21, GPIO.OUT) #set pin 21 to output
p = GPIO.PWM(21,50) #set the PWM on pin 21 to 50%
p.start(0)
try:
while True:
for i in range (100):
p.ChangeDutyCycle(i)
time.sleep(0.02) #These last three lines are going to loop and increase the power from 1% to
100% gradually
for i in range(100):
p.ChangeDutyCycle(100-i)
time.sleep(0.02) #These three lines loop and decrease the power from 100%-1% gradually
except KeyboardInterrupt:
pass
p.stop()