top of page

PWM in Arduino

Updated: Mar 1


Introduction to PWM


Pulse Width Modulation (PWM) is a technique by which width of a pulse is varied while keeping the frequency of the wave constant. It is a method for generating an analog signal using a digital source.



A PWM signal consists of two main components that define its behaviour: a duty cycle and a frequency.

 

What is the Duty Cycle of Signal


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.



E.g. A pulse with a period of 10ms will remain ON (high) for 2ms.Therefore, duty cycle will be


D = 2ms / 10ms = 20%

 

Through PWM technique, we can control the power delivered to the load by using ON-OFF signal. The PWM signals can be used to control the speed of DC motors and to change the intensity of the LED.


Pulse Width Modulated signals with different duty cycle are shown below




Frequency of Signal


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. 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.


Example: If we want to create a 2V analog signal for a given digital source that can be either high (on) at 5V, or low (off) at 0V, we can use PWM with a duty cycle of 40%. It will provide output 5V for 40% of the time. If the digital signal is cycled fast enough, then the voltage seen at the output appears to be the average voltage. If the digital low is 0V (which is usually the case) then the average voltage can be calculated by taking the digital high voltage multiplied by the duty cycle, or 5V x 0.4 = 2V.

 

Now, let's see PWM in Arduino.


PWM Pins of Arduino Uno


Arduino Uno has 6 8-bit PWM channels. The pins with symbol ‘~’ represents that it has PWM support. These PWM pins are shown in below image.





Arduino Functions for PWM


analogWrite (pin, duty cycle)


It is used to generate PWM or output analog value to a specified PWM channel.


pin – pin on which we want to generate pwm or analog signal.


duty cycle – it lies in between 0 (0%, always off) – 255 (100%, always on).

e.g. analogWrite (3, 127//generates pwm of 50% duty cycle

 

LED Fading using Arduino PWM

Let’s create small application in which led will fade continuously. This led fading application used for decoration in functions and festivals.


LED Interfacing to Arduino Uno



Sketch for LED fading using Arduino PWM

int led = 6;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup() {
  pinMode(led, OUTPUT); // declare pwm pin to be an output:
}

void loop() {
  analogWrite(led, brightness); // set the brightness of led

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }  
  delay(30);  // wait for 30 milliseconds to see the dimming effect
}

 

Control LED Brightness using a Potentiometer


Let’s build an application in which we will control the brightness of led using Arduino by varying potentiometer knob. So, when we rotate knob of potentiometer, ADC of Arduino will read this analog signal. Then we will generate PWM signal proportional to the analog signal.


Interfacing Diagram



Code for controlling LED Brightness using arduino

int ledPin = 6;      // LED connected to digital pin 9
int analogPin = A0;  // potentiometer connected to analog pin 3
int val = 0;         // variable to store the read value

void setup()
{
  pinMode(ledPin, OUTPUT);   // sets the pin as output
}

void loop()
{
  val = analogRead(analogPin); // read the input pin
  analogWrite(ledPin, val / 4);// analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}

For more implementation, you can visit


2 views0 comments

Comments


bottom of page