E102f23 PWM Intro
E102f23 PWM Intro
Pulse width modulation or pulse duration modulation is a technique where we vary the width of a
square pulse to control the power supplied to any connected device. Using this technique, we
can simulate an analog output using a digital output.
We are using digital control to produce a square wave. This square wave is switched between On
(digital high) and Off (digital low). Those are the only two options available to us since it is a
digital signal. So how can we attain the full range of discrete analog values using something
digital that can have only two values (High and Low)? Well, you forget an essential parameter.
Time; we can use Time to introduce the variation.
Using the time for which a pulse is HIGH and LOW, we will achieve our desired result of
creating a fake analog signal. To achieve this, we need to acquaint ourselves with two parameters
of a PWM signal: Duty cycle and frequency.
A duty cycle is also known as a power cycle that is the fraction of one period in an active signal.
That’s a bunch of complex words. Here’s what a duty cycle means in simple terms. The duty
ENGR 102 Fall 2023 What is PWM and How do you use it on Arduino Mega2560? 2
cycle is the percentage of the ratio of the pulse width of the signal to the total period (T). Here’s
a formula to further simplify things:
% Duty cycle = (TON/(TON + TOFF))
For example, if a pulse with a period of 20ms remains ON (high) for 4ms, its duty cycle will be
The PWM helps us to control the power that is delivered to the load by using zeroes and ones as
on and off signal. Therefore, we can use it to control the motor rotation speed and also the
intensity of the LED.
What is Frequency?
Frequency is defined as the number of oscillations or occurrences of a repeating wave per unit
time. The period is the duration of the time of one complete cycle. Therefore the period is the
reciprocal of the frequency. We can calculate the frequency using the following formula.
between two levels of brightness. 0 or 1. With the analog option (or simulated analog to be
pedantic), I have more options in terms of brightness control.
So to summarize:
Duty cycle: Used to control how much power is sent across the device.
Frequency: Used to control the rate at which this power is sent.
How to calculate Arduino PWM?
We can determine the PWM output voltage by using the following formula,
PWM voltage=(Duty cycle ÷ 256) x 5 V.
256 because akin to the 0 to 1 levels available with digital signals, we have 256 levels of analog
values that we can work with. 5V because that’s the maximum power you can send via an
Arduino Uno.
What are the uses of pulse width modulation?
A pulse width modulation signal is a type of analog modulation signal. We use it to generate
analog signals (make-believe) using the digital signals as input. Arduino PWM signals have a
wide range of control applications.
ENGR 102 Fall 2023 What is PWM and How do you use it on Arduino Mega2560? 4
The PWM pins in Arduino Mega2560 are pins 2 - 13, 44 - 46. Arduino has 8-bit PWM channels.
These PWM pins are represented by the symbol ‘~’. That symbol tells us that these pins have
ENGR 102 Fall 2023 What is PWM and How do you use it on Arduino Mega2560? 5
PWM support. On these PWM pins, the duty cycle of the PWM pulse, 490 Hz (pins 4 and 13:
980 Hz), is controlled by the analogWrite function.
Pins Frequency
Is the Arduino PWM output analog?The Arduino doesn’t have a built-in digital-to-analog
converter (DAC). The ‘analog signals’ are, in reality, pulse width modulated digital signals. You
can use the inbuilt function, analogWrite(pin, value), to give a PWM output signal. It has two
arguments; they are the PWM output pin, and the other one is the value that is proportional to the
duty cycle of the signal.
The signal is off when the value is 0, and the signal is on when the value is 255.
What are the Arduino functions that deal with PWM signals?
AnalogRead():
These function does not require any special header file. It reads the value from the declared PIN.
Arduino contains a 10-bit analog to digital converter. Therefore, it can represent the value that
will map input voltages between 0 and the operating voltage that may be 5v or 3.3v and converts
it to the integer values that is ranging between 0 and 1023. Arduino has a resolution between
5v/1024 units that is 4.9mV per unit.
analogRead(int pin)
Parameters
AnalogWrite():
AnalogWrite() function is also a built-in function that doesn’t require any special header file.
Gives an analog value to the pin that is PWM wave. When we call the analogWrite() function,
the pin will generate a steady rectangular wave of the specified duty cycle that remains constant
until the next call to analogWrite() on the same pin.
Syntax
Return no value
AnalogRead and AnalogWrite example
We can control the brightness of the LED using a potentiometer. you can use the analogRead()
function to read a voltage and the analogWrite() function to give the output a PWM signal.
Hardware Required
• Potentiometer
• LED
• Resistor
• Arduino Mega2560
• Breadboard
• Jumper wires
You can rotate the potentiometer, which will change the voltage on pin A0. Using the program,
you can change the duty cycle of the PWM signal on your PWM pin of choice, which will adjust
the brightness of the LED.
Without a potentiometer, you can connect an LED with the PWM pin of choice of the Arduino.
For changing the LED’s brightness, our program changes the duty cycle of the PWM signal
output at your PWM pin of choice of the Arduino.
ENGR 102 Fall 2023 What is PWM and How do you use it on Arduino Mega2560? 7
Code for controlling the brightness of an LED and Arduino’s PWM output (using a
potentiometer)
void loop()
{
/*
the above funtion scales the output of a, which is 10 bit and gives values btw 0 to 1023, in values btw 0 to 255 form
analogWrite funtion which only receives values btw this range
*/
analogWrite(pwm,a) ;
}
ENGR 102 Fall 2023 What is PWM and How do you use it on Arduino Mega2560? 8
Code for controlling the brightness of an LED and Arduino’s PWM output:
void loop()
{
analogWrite(pwm,25) ; //setting pwm to 25
delay(50) ; //delay of 50 ms
analogWrite(pwm,50) ;
delay(50) ;
analogWrite(pwm,75) ;
delay(50) ;
analogWrite(pwm,100) ;
delay(50) ;
analogWrite(pwm,125) ;
delay(50) ;
analogWrite(pwm,150) ;
delay(50) ;
analogWrite(pwm,175) ;
delay(50) ;
analogWrite(pwm,200) ;
delay(50) ;
analogWrite(pwm,225) ;
delay(50) ;
analogWrite(pwm,250) ;
}