Pulsewidth Modulation (PWM) is a way to fake an analog output by pulsing the
output. This could be used to dim and brighten an LED or later to control a servo
motor. The following example slowly brightens and dims an LED using for loops.

int ledPin = 9; // PWM pin for the LED
void setup(){} // no setup needed
void loop()
{
 for (int i=0; i<=255; i++) // ascending value for i
 {
 analogWrite(ledPin, i); // sets brightess level to i
 delay(100); // pauses for 100ms
 }
 for (int i=255; i>=0; i--) // descending value for i
 {
 analogWrite(ledPin, i); // sets brightess level to i
 delay(100); // pauses for 100ms
 }
}
author avatar
Aravind S S