Pulse Width Modulation or PWM is a technique for obtaining analog outputs with digital means. It used to vary the width of the pulses in a pulse-train and has many applications such as controlling the brightness of LEDs, voltage regulation, audio signal generation, speed control of servo motors etc. In this blog, we will learn about PWM and how you can get the PWM output from the digital pins of Arduino. If you’re new to the Arduino, you can have a look at its basic setup and a beginner’s project out here.
The state of the Digital Input/Output pins in Arduino is either High(1) or Low(0), where High implies the voltage is approx 5V and Low for 0V. But what do you do, if you want a voltage between these? Well, we use the PWM technique to achieve this.
TON (On Time): The time when the signal is high.
TOFF (Off Time): The time when the signal is low.
Period: It is the sum of on time and off time.
Duty cycle: A period of a pulse consists of an ON cycle(5V) and an OFF cycle(0V). The fraction for which the signal is ON over a period is known as a duty cycle.
Frequency: The frequency of a signal determines how fast the PWM completes a cycle (i.e. 1000 Hz would be 1000 cycles per second) which means how fast it switches between ON (high) and OFF (low) states.
Through PWM, we can control the power delivered to the load by using an ON-OFF signal.
Arduino Uno consists of 14 digital Input/Output pins and has six 8-bit PWM which are the- 11, 10, 9, 6, 5, and 3 pins. The pins with symbol ‘~’ represent that it has PWM support. These PWM pins are shown in the following image.
analogWrite (pin, duty cycle) -
pin — pin on which we want to generate pwm or analog signal
duty cycle — the value lies in between 0 (0%, always off) — 255 (100%, always on)
This function is used to generate PWM or an output analog value to a specified PWM channel. When an analogWrite() 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 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.
Before moving further, let’s look at the main differences between the analogRead() and analogWrite() functions:
Now that we’re familiar with the functions, let’s learn how to calculate the PWM values for the Arduino:
The analogWrite() function discussed above is used to generate a PWM signal in Arduino. The value associated with the analog signal is from 0 to 255 consisting of 256 levels of values.
We can determine the output PWM voltage by using the below formula:
PWM voltage = ( Duty cycle/ 256) x 5V
Here’s a simple example to demonstrate the application of the PWM technique in the Arduino. We’ll be learning to control the brightness of a LED using the Arduino.
Once you upload the code to the board and run, you should be able to control the brightness of the LED and witness it fade gradually.
With this easy-to-execute example, we can get started with using PWM to control the outputs in an Arduino. In this blog we looked at what PWM is, its main features and how it can be implemented in applications of the Arduino.