Setting up PWM on Arduino

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.

Basic principle of PWM

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.

Image for post

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.

PWM Pins of Arduino

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.

Image for post

Arduino Functions for PWM

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.

Image for post

Before moving further, let’s look at the main differences between the analogRead() and analogWrite() functions:

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

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.

Components Required:

  1. Arduino Uno
  2. 1 LED
  3. 220 ohm resistor
  4. Breadboard
  5. Jumper wires

Image for post

  1. To start with, make the following connections according to the given schematic.
  2. Connect the positive leg (longer leg) of the LED to pin 6 of the Arduino. Then connect the 220 ohm resistor to the negative leg of the LED and the other end of the resistor to the ground pin of Arduino.
  3. Now, let’s write the code to change the brightness of the LED using PWM.

Image for post

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.