What is PWM? Arduino and Pulse Width Modulation
In general, Pulse Width Modulation (PWM) is a modulation technique
used to encode a message into a pulsing
signal. A PWM is comprised of two key components: frequency and duty cycle. The PWM frequency
dictates how long it takes to complete a single cycle (period) and how quickly
the signal fluctuates from high to low. The duty cycle determines how long a
signal stays high out of the total period. Duty cycle is represented in
percentage.
In Arduino, the PWM enabled pins produce a
constant frequency of ~ 500Hz, while the duty cycle changes according to the
parameters set by the user. See the following illustration:
Basic Principle of PWM
Pulse Width Modulation or PWM is a common
technique used to vary the width of the pulses in a pulse-train. PWM has many
applications such as controlling servos and speed controllers, limiting the
effective power of motors and LEDs.
Pulse width modulation is basically, a
square wave with a varying high and low time. A basic PWM signal is shown in
the following figure.
There are various terms associated with PWM −
On-Time − Duration of time signal is high.
Off-Time − Duration of time signal is low.
Period − It is represented as the sum
of on-time and off-time of PWM signal.
Duty Cycle − It is represented as the
percentage of time signal that remains on during the period of the PWM signal.
Period
As shown in the figure, Ton denotes
the on-time and Toff denotes the off-time of signal. Period is the sum of
both on and off times and is calculated as shown in the following equation −
T_{total} = T_{on}+T_{off}
Duty
Cycle
Duty cycle is calculated as the on-time of
the period of time. Using the period calculated above, duty cycle is calculated
as −
D = {T_{on}}/{T_{on}+T_{off}} = {T_{on}}/{T_{total}}
AnalogWrite() Function
The analogWrite() function writes
an analog value (PWM wave) to a pin. It can be used to light a LED at varying
brightness or drive a motor at various speeds. After a call of the
analogWrite() function, the pin will generate a steady square wave of the
specified duty cycle until the next call to analogWrite() or a call to
digitalRead() or digitalWrite() on the same pin. The frequency of the PWM
signal on most pins is approximately 490 Hz. On the Uno and similar boards,
pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the
Leonardo also run at 980 Hz.
On most Arduino boards (those with the
ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On
the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards
with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
analogWrite()
Function Syntax
analogWrite
( pin , value ) ;
value −
the duty cycle: between 0 (always off) and 255 (always on).
Example
/*
Fade
This example shows how to
fade an LED on pin 9
using the analogWrite() function.
The analogWrite() function
uses PWM, so if you
want to change the pin
you're using, be sure to
use another PWM capable pin.
On most Arduino,
the PWM pins are identified
with a "~" sign,
like ~3, ~5, ~6, ~9, ~10 and
~11.
*/
int brightness = 0;
void setup()
{
pinMode(9, OUTPUT);
}
void loop()
{
for (brightness = 0;
brightness <= 255; brightness += 5) {
analogWrite(9,
brightness);
delay(30); // Wait for 30
millisecond(s)
}
for (brightness = 255;
brightness >= 0; brightness -= 5) {
analogWrite(9,
brightness);
delay(30); // Wait for 30
millisecond(s)
}
}
No comments:
Post a Comment