From SainSmart Wiki
Revision as of 07:18, 14 June 2013 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

What’s PWM?

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED. In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

For the Arduino, you write a value from 0 to 255 on a PWM pin, and the Arduino library will cause the pin to output a PWM signal whose on time is in proportion to the value written.

When it comes time for us to actually write an output voltage, the 0-255 value lacks meaning. What we want is many cases is a voltage. For our purposes, we will assume the Arduino is running at Vcc = 5 volts. In that case, a value of 255 will also be 5 volts. We can then easily convert the desired voltage to the digital value needed using simple division. We first divide the voltage we want by the 5 volts maximum. That gives us the percentage of our PWM signal. We then multiply this percentage by 255 to give us our pin value. Here is the formula: Pin Value (0-255) = 255 * (AnalogWrite / 5);

Arduino use analogWrite()

analogWrite():Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal is approximately 490 Hz. On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.The Arduino Due supports analogWrite() on pins 2 through 13, plus pins DAC0 and DAC1. Unlike the PWM pins, DAC0 and DAC1 are Digital to Analog converters, and act as true analog outputs.You do not need to call pinMode() to set the pin as an output before calling analogWrite().The analogWrite function has nothing to do with the analog pins or the analogRead function.


analogWrite(pin, value)


pin: the pin to write to.

value: the duty cycle: between 0 (always off) and 255 (always on). Notes and Known Issues

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.

Experiment component:

1. 1 x 22oΩ resistor 2. 1 x LED 3. 1 x Breadboard

Connect your circuit as the below diagram.

Example code:

int brightness = 0; //define original value of brightness, the value is brightness of LED.

int fadeAmount = 5; //define fadeAmount,the value is the amount of brightness variations’ change.

void setup()  {  
  pinMode(9, OUTPUT);//	set pin9 is output
void loop()  {  
  analogWrite(9, brightness);//write the value of brightness in pin9
   brightness = brightness + fadeAmount;//change the value of brightness
   if (brightness == 0 || brightness == 255) {
       fadeAmount = -fadeAmount ; // roll over the brightness between the highest and lowest
 delay(30); //delay 30ms