Select Page

Pulse Width Modulation(PWM) is a technique used for getting analog signals with the help of digital means. The digital control is used to create a square wave, a signal switched between an on and off state.

This on-off pattern is able to simulate the voltage in between full on i.e, 5 Volts, and off i.e, 0 Volts by changing the portion of the time the signal spends on versus the time that the signal spends off.

The behavior of Pulse Width Modulation is determined by the duty cycle and frequency of a PWM signal. The duration of “on time” is called the pulse width. To get varying analog values, we have to change or modulate, the pulse width.

We can use PWM for voltage regulation, audio signal generation, device control (pump, hydraulics, etc.), servo motors, etc.

Note :- The frequency of PWM in arduino is about 500Hz.

## Principle of PWM

The digital input/output pins of Arduino resides in two states:-

1. High or 1
2. Low or 0

High means the voltage is 5 volts and Low means the voltage is 0 volts.

As we know PWM is a square wave signal whose graphical representation shows rectangular pulses. The duty cycle of these rectangular pulses will be calculated by:-

Duty cycle = to/tc

Where to = It is the duration of the signal when the signal is HIGH.

Tc = It is the total duration of the signal as the sum of HIGH and LOW.

### Duty Cycle

The duty cycle of the Pulse Width Modulation signal refers to the ratio of the time that the signal is in a high(on) state divided by the total time it takes to complete one cycle.

A low-duty cycle corresponds to low power as the power was off for most of the time.

Generally, the duty cycle is expressed in percentages as follows:-

1. 100% for being fully ON.
2. 0% for being fully OFF.

To understand the function of PWM in Arduino boards let’s take an example:-

Suppose we want a LED to blow/high for a certain amount of time, so the duration to which the LED will be high depends upon the duty cycle and frequency.

If Duty cycle = 25% and Frequency = 1Hz then the LED will blow up for 250 milliseconds or we can say that it will be high for 250 milliseconds and will be low for the rest of 750 milliseconds.

### Frequency Of Signals

The frequency of a signal determines how fast the PWM completes a revolution or cycle (i.e. 1000 Hz would be 1000 cycles or revolutions per second) which means how fast it switches between ON (high) and OFF (low) states. By repeating this ON-OFF pattern at a fast-enough rate, and with a certain duty cycle, the output will appear to behave like a constant voltage analog signal when providing power to devices.

### Analog Write()

The Analog write function is used to write a PWM value to a pin. Analog Write functions can help us to light LEDs with varying brightness.

When an Analog Write function is called a stable rectangular wave of a particular duty cycle is generated by the specified PWM pin until the next analogWrite() is called on that same pin.

The difference between AnalogWrite() and AnalogRead() function is as follows:-

• The analogRead() function is used to read the analog value, while the analogWrite() function is used to write the PWM value.
• The value of analogRead() function ranges from 0 to 1023, while the value of analogWrite() function ranges from 0 to 255.

Let’s take an example.

For the PWM pin, we will now specify the value instead of High or Low.

Let, High = 255

Low = 0

## The code for lighting up the LED at full brightness is as follows

```void setup()
{
pinMode(10, OUTPUT); // the declared pin must be among the PWM pins.
}
void loop()
{
analogWrite(10, 255); // 255 is the highest value.
//We can modify the value as per the required brightness.
delay(1000);
analogWrite(10, 0);
delay(1000); // time delay of 1 second or 1000 milliseconds
}

```

Now let’s check the code to control the brightness of the LED.

Here we have connected the PWM with pin 6.

```void setup()
{
pinMode(6, OUTPUT); // the declared pin must be among the PWM pins.
}
void loop()
{
analogWrite(6, 255); // brightness increases as value increases
delay(1000);
analogWrite(6, 180);// brightness level
delay(1000);
analogWrite(6, 80);
delay(1000);
analogWrite(6, 20); // brightness decreases as value decreases
delay(1000);
}

```

### Arduino PWM pins

The Pulse Width Modulation pins are available on every Arduino board.

Arduino boards consist of special Pulse Width Modulation pins which are stated as follows:-

• Arduino UNO  – 3, 5, 6, 9, 10, 11 at 490Hz (pins 5 and 6 at 980 Hz)
• Arduino NANO – 3, 5, 6, 9, 10, 11 at 490Hz (pins 5 and 6 at 980 Hz)
• Arduino Leonardo – 3, 5, 6, 9, 10, 11, 13 at 490Hz (pins 3 and 11 at 980 Hz)
• Arduino Micro – 3, 5, 6, 9, 10, 11, 13 at 490Hz (pins 3 and 11 at 980 Hz)

The Arduino IDE has a built-in function analogWrite(pin, value) which can be used to generate a PWM signal for a given pin:-

• analogWrite(PIN, 255): to generate a signal with 100% duty cycle.
• analogWrite(PIN, 127): to generate a signal with 50% duty cycle.
• analogWrite(PIN, 0): to generate a signal with 0% duty cycle.

With the help of these above-mentioned functions we can provide any value in [0-255] range which maps to [0-100] duty cycle in percent.

### Arduino Code to generate a duty cycle

The Code for generating a 490 Hz and 50% duty cycle signal on PWM pin D6 is written below:-

```#define PWM_PIN 6
void setup()
{ pinMode(PWM_PIN, OUTPUT);
}
void loop()
{
analogWrite(PWM_PIN, 127);
}
```

## How to Calculate The PWM of Arduino

The analogWrite() function discussed before is used to generate a PWM signal in Arduino.

The value associated with the analog signal is from 0 to 255. It means 256 levels of values.

The maximum voltage read by the Arduino is 5V.

We can determine the output PWM voltage by using the formula mentioned below:-

PWM voltage = ( Duty cycle/ 256) x 5V

1. How many PWM pins does Arduino have?

The number of PWM pins depends upon the Arduino board variant. For example, Arduino UNO, and Nano have 6 PWM pins while Arduino Leonardo and Micro have 7 PWM pins.

1. When was PWM invented?

pulse-width modulation chip was invented in 1975 source.

1. When to use PWM?

PWM is used in communications to power control and conversion. For example, PWM is commonly used to control the speed of electric motors and the brightness of lights, etc.

1. How many channels are available for PWM?

The PWM has up to 4 channels.

1. Is PWM input or output?

PWM is a way to control analog devices with a digital output.

1. Does PWM work with DC?

Yes, PWM pins can be used effectively with battery or DC power-driven applications.