Table of Contents

LED Fader

Shows the most basic use of the PWM pins – make lights brighter and dimmer. Also introduces the for loop.

Breadboard Layout

Code

LED_Fader.ino
int ledPin = 11;
int inc = 1;
 
void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  // put your main code here, to run repeatedly: 
  for (int i=0; i>-1; i=i+inc){
    analogWrite(ledPin, i);
    if (i==255){
    	inc = -inc;
    }
    if (i==0){
    	inc = -inc;
    }
// Here's another way to find out if you've hit the ends of the range.
// This takes care of it with a single "if" statement:
//    if (i==255 || i==0) inc = -inc;
    delay(10);
  }
}
Print/export
Toolbox