// #########################################
// Varry LED Brightness
// #########################################
//
// LED Brightness Control on Arduino UNO using PWM
//
// Check out the link for Code explanation and Hardware details
// Link:
// http://tech.arunkumarn.in/blogs/arduino-uno/led-brightness-control-on-arduino-uno-using-pwm/
//
// Arduino UNO has 6 PWM channels (D3, D5, D6, D9, D10, D11)
#define ledPin 9 // PWM capable pin
int brightness = 0; // current LED brightness
int fadeAmount = 3; // how many points to fade the LED by
void setup() {
// Set ledPin as OutPut Pin.
pinMode(ledPin, OUTPUT);
}
void loop() {
// Set the brightness of ledPin (GPIO 9)
analogWrite(ledPin, brightness);
// Change the brightness for the next loop
brightness = brightness + fadeAmount;
// Reverse the direction of the fading at the ends
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// Wait to see the dimming effect
delay(10);
}
LED