Arduino101 LEDFadeControl

From Twin Cities Maker wiki
Jump to: navigation, search

Control the brightness of your LED with a potentiometer (a.k.a. “pot”). The pot works just like the resistor/photoresistor combo we used in the Photo-Blink sketch. This sketch also introduces the map function, which helps make up for the difference between what the analog inputs read (0-1023) and what the PWM pins can put out (0-255).

Breadboard layout

LED Fade with Control bb.png

Code

int ledPin = 11;
int potPin = 0;
int brt = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  brt = map(analogRead(potPin), 0, 1023, 0, 255);
  brt = constrain(brt, 0, 255);
  Serial.println(brt);
  analogWrite(ledPin, brt);
}