Monday, January 31, 2011

PWM LED Brightness Control

Pulse Width Modulation at last! In our last example, we read a potentiometer (pot) to get a analog value of 0-1023, and used that number to delay the digital on and off times of a LED. This example will do the same read, but output a analog value (PWM) of 0-255 to control the brightness of a LED. It's still controlling the on and off times, but so quickly the eye doesn't see off, it just sees decreased or increased brightness. this is much more power efficient than using a resistor to shed voltage, dimming a bulb. We will use this same method in an upcoming post to control the speed of a motor.

(video uploaded to youtube)

int sensorPin = A0;    // Analog input pin to pot
int ledPin = 7;      // PWM pin to LED
int sensorValue = 0;  // variable to store pot value


void setup() {}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);  
  // converts 0-1023 to 0-255
  sensorValue /=4;   
  // outputs PWM signal to LED
  analogWrite(ledPin, sensorValue);  
  
                   
}

No comments:

Post a Comment