PWM In Arduino

Pulse-width modulation (PWM), is a modulation technique that changes the width of the pulse, or the pulse duration or the duty cycle, based on a modulator/modulating signal information. One of its main use is to allow the control of the power supplied to electrical devices, especially to inertial loads such as motors. 
The average value of voltage fed to the load is controlled by turning the switch on/off between supply and load at a fast pace. The longer the switch is on compared to the off periods, the higher the power supplied to the load is.
The term duty cycle describes the proportion of 'on' time to the regular interval or 'period' of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.
The main advantage of PWM is that power loss in the switching devices is very low. When a switch is off there is practically no current, and when it is on, there is almost no voltage drop across the switch. Power loss, being the product of voltage and current, is thus in both cases close to zero. PWM also works well with digital controls, which, because of their on/off nature, can easily set the needed duty cycle.
PWM has also been used in certain communication systems where its duty cycle has been used to convey information over a communications channel.

PWM Applications In Arduino

Such as:
  • **Fading the LED experiment
  • **Providing an analog output to a device
  • **Audio signals generation.
  • **Providing variable speed control for motors.
  • **Generating a modulated signal, for example to drive an infrared LED for a remote control.
PWM In Arduino:
The Arduino's language makes PWM very easily usable, call analogWrite(pin, dutyCycle), where dutyCycle lies between 0 to 255, and pin is one of the PWM pins (3, 5, 6, 9, 10, or 11). The analogWrite function doesn't provide any control over frequency of the PWM wave thus generated by the board's pin.

You Can Make Any Digital Output Pin As PWM:

We can "manually" implement PWM on any pin by repeatedly turning the pin on and off for the desired duty cycle. e.g.
void setup()
  pinMode(13, OUTPUT);

void loop()
  digitalWrite(13, HIGH);
  delay(10); // Approx. 10% duty cycle
  digitalWrite(13, LOW);
The above code will generate a PWM wave of duty cycle 10% ( nearly).
The above technique has an advantage that it can use any digital output pin. In addition, you have full control the duty cycle and frequency. One major disadvantage is that any interrupts will affect the timing, which can cause considerable jitter unless you disable interrupts. It's difficult to determine the appropriate constants for a particular duty cycle and frequency unless you either carefully count cycles, or study the values from an oscilloscope.

More Elaborated Example Is Given Below, To generate a sine wave fading pattern through an LED:
code courtesy:
 Paul Badger 2007
 A program to illustrate one way to implement a PWM loop.
 This program fades LED's on Arduino digital pins 2 through 13 in a sinewave pattern.
 It could be modified to also modulate pins 0 & 1 and the analog pins.
 I didn't modulate pins 0 & 1 just because of the hassle of disconnecting the LEDs on the RX and TX pins (0 & 1).

 The PWM loop, as written, operates at about 175 HZ and is flicker-free.
 The trick to this of course is not doing too much math in between the PWM loop cycles.
 Long delays between the loops are going to show up as flicker. This is true especially of "Serial.print" debug statements
 which seem to hog the processor during transmission. Shorter (timewise) statements will just dim the maximum brightness of the LED's.
 There are a couple of lines of code (commented out) that implement a potentiometer as a speed control for the dimming.

 How it works: The PWM loop turns on all LED's whose values are greater than 0 at the start of the PWM loop.
 It then turns off the LED's as the loop variable is equal to the channel's PWM modulation value.
 Because the values are limited to 255 (just by the table), all LED's are turned off at the end of the PWM loop.
 This has the side effect of making any extra math, sensor reading. etc. will increase the "off" period of the LED's duty cycle. Consequently
 the brightest LED value (255), which is supposed to represent a "100%" duty cycle (on all the time), dosesn't really do that.
 More math, sensor reading etc will increase the "off time", dimming the LED's max brightness. You can (somewhat) make up for this dimming with
 smaller series resistors, since LED's can be overrated if they aren't on all of the time.

 The up side of this arrangement is that the LED's stay flicker free.
 Note that this program could easily be used to modulate motor speeds with the addition of driver transistors or MOSFET's.

long time;                                                                    // variable for speed debug
float pwmSpeed[14] = {
 0, 0, 1.2, 1.3, 1.4, 1.9, .9, .8, .5, 1.2, 1.37, 1.47, .3, 3.2};             // these constants set the rate of dimming
int pwmVal[14];                                                               // PWM values for 12 channels - 0 & 1 included but not used
float pwmFloats[14];
int i, j, k, l, x, y, z, bufsize, pot;                                        // variables for various counters

unsigned char sinewave[] =        //256 values

















void setup(){
  DDRD=0xFC;      // direction variable for port D - make em all outputs except serial pins 0 & 1
  DDRB=0xFF;      // direction variable for port B - all outputs


void loop(){

  // time = millis();               // this was to test the loop speed
  // for (z=0; z<1000; z++){        // ditto

  //  pot = analogRead(0);          // this implemented a potentiometer speed control to control speed of fading

  for (y=0; y<14; y++){             // calculate one new pwm value every time through the control loop
    j = (j + 1) % 12;              // calculate a new j every time - modulo operator makes it cycle back to 0 after 11
    k = j + 2;                      // add 2 to the result - this yields a cycle of 2 to 13 for the channel (pin) select numbers

    pwmFloats[k] =  (pwmFloats[k] + pwmSpeed[k]);
    // pwmFloats[k] =  (pwmFloats[k] + ((pwmSpeed[k]  * 15 * (float)pot) / 1023));    // implements potentiometer speed control - see line above

      if (pwmFloats[k] >= 256){                  // wrop around sinewave table index values that are larger than 256
      pwmFloats[k] = pwmFloats[k] - 256;
    else if  (pwmFloats[k] < 0){
      pwmFloats[k] = pwmFloats[k] + 256;        // wrop around sinewave table index values that are less than 0

    pwmVal[k] = sinewave[(int)pwmFloats[k]];                   // convert the float value to an integer and get the value out of the sinewave index

  PORTD = 0xFC;              // all outputs except serial pins 0 & 1
  PORTB = 0xFF;              // turn on all pins of ports D & B

for (z=0; z<3; z++){         // this loop just adds some more repetitions of the loop below to cut down on the time overhead of loop above
                             // increase this until you start to preceive flicker - then back off - decrease for more responsive sensor input reads
  for (x=0; x<256; x++){
    for( i=2; i<14; i++){    // start with 2 to avoid serial pins
      if (x == pwmVal[i]){
        if (i < 8){    // corresponds to PORTD
          // bitshift a one into the proper bit then reverse the whole byte
          // equivalent to the line below but around 4 times faster
          // digitalWrite(i, LOW);
          PORTD = PORTD & (~(1 << i));
          PORTB = PORTB & (~(1 << (i-8)));         // corresponds to PORTB - same as digitalWrite(pin, LOW); - on Port B pins

  //    }
  //    Serial.println((millis() - time), DEC);     // speed test code


Use the ATmega AVR PWM registers directly

The ATmega168P/328P chip has three PWM timers, controlling 6 PWM outputs. By manipulating the chip's timer registers directly, we can obtain more control than the analogWrite function provides. The AVR ATmega328P datasheet provides a detailed description of the PWM timers.