by

Using Pulse-Width Modification to Simulate Analog Outputs with Digital Signals

Pulse-width modulation (PWM) is the act of using a digital ‘on’ or ‘off’ signal (all or none, high or low, 5 V or 0 V) to simulate an analog graded signal. This is done by rapidly flipping the digital signal between high and low voltage so that the duty cycle (defined as the amount of time the signal is high divided by the period of the signal) multiplied by the voltage when the signal is high is equal to the analog voltage you are trying to simulation. This practice is called pulse-width modulation because one modulates the width (duration) of the portion of the signal that is high to change analog value you wish to simulate. Consider the following example. Suppose you want to simulate an analog output voltage of 3.5 V using PWM between 0 and 5 V. To do this your duty cycle should be 70% (0.7*5 V = 3.5 V). In general the following formula (where Vcc is the voltage of your digital signal when high) relates the duty cycle of the PWM to the analog value you wish to simulate.

 Simulated Analog Voltage = Vcc * Duty Cycle
         = Vcc * (Time PWM Signal is High) / (Period of PWM Signal)

Arduino has digital pins that are capable of generating PWM outputs. On the Arduino Uno these are digital pins 3, 5, 6, 9, 10, and 11. At the same time, the Arduino IDE has a function called analogWrite() that makes it easy to generate PWM signals. This function takes two arguments; the first is the pin on which to generate the output and the second takes a value between 0 and 255 and determines the duty cycle of PWM signal you wish to generate. For example, analogWrite(9,0), analogWrite(9,127), and analogWrite(9,255) will generate signals with duty cycles of 0%, 50%, and 100%, respectively, on digital pin 9.

A Breathing LED

The Arduino IDE has an example program called Fade that uses PWM to make an LED appear to be “breathing” like the LED on MacBooks when they are asleep. This program is shown below.

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */
int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // 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 ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

Attach and LED to pin 9 (don’t forget a current limiting resistor) and upload and run this program on your Arduino. Experiment with changes to the fadeAmount and delay values.

Assignment

Write a Program that mimics the flickering of a real candle using an LED and Arduino PWM.

Leave a Reply

Your email address will not be published.