Controlling Fading (brightness) of LED from Potentiometer Using ARDUINO


Posting after so long, Obviously expecting a good stuff from my lab. :), but again I am posting my one more small though important experiment. Its about Controlling Intensity of a LED by varying Potentiometer (as input) using arduino board. Here is the program:

int potIn = 0;                                        // Potentiometer analog input at analog pin 0
int ledPin = 12;                                    // LED at digital Pin 12
int potVal = 0;                                     // Initializing Potentiometer’s input as zero

void setup() {
pinMode(12,OUTPUT);                  // Setting pin 12 as OUTPUT

}

void loop() {
potVal = analogRead(potIn);         // analog value read at analog pin 0 assigning to potVal
for (int brightness = potVal; brightness <= 200; brightness+=5) {             // for loop for adjusting brightness
analogWrite(ledPin, brightness);                                                                         // value of brightness is analog write to pin 12

}

}

Setting whole setup:

Potentiometer had 3 legs out of which middle one is input pin connected to pin 0, whereas pin at one end must be grounded and other end pin must be +5V.

A resistor of 220 ohm between pin 12 and LED must be connected.

LED’s positive leg to output pin 12 and negative leg must be grounded.

Inform me if anyone finds its better and shorter way. see ya guys.

Advertisements

5 thoughts on “Controlling Fading (brightness) of LED from Potentiometer Using ARDUINO

  1. Ignore the above code…this one is the complete…!

    #define LED 9 // the pin for the LED
    int i = 0; // We’ll use this to count up and down

    void setup() {
    pinMode(LED, OUTPUT); // tell Arduino LED is an output
    }

    void loop(){

    for (i = 0; i 0; i–) { // loop from 255 to 1 (fade out)

    analogWrite(LED, i); // set the LED brightness
    delay(10); // Wait 10ms
    }
    }

  2. Asim Siddiqui :

    Ignore the above code…this one is the complete…!

    #define LED 9 // the pin for the LED
    int i = 0; // We’ll use this to count up and down

    void setup() {
    pinMode(LED, OUTPUT); // tell Arduino LED is an output
    }

    void loop(){

    for (i = 0; i 0; i–) { // loop from 255 to 1 (fade out)

    analogWrite(LED, i); // set the LED brightness
    delay(10); // Wait 10ms
    }
    }

    This blogpost is about LED controlling by using a potentiometer . But in your code I don’t think you are considering any input potentiometer plus there are some syntax or may be typing error. Logic behind your code will work when you just want to fade in and out LED’s brightness w.r.t time.

  3. This works and does take the Potentiometer input into consideration.

    void setup() {
    pinMode(9, OUTPUT);
    }
    void loop() {
    potVal = analogRead(A0);
    analogWrite(9, potVal);
    }

  4. hello. I need to do the same using sharp IR sensor instead of potentiometer. That is the brightness of led should gradually decrease as an object approaches the sensor. kindly help me with the code.I am using arduino mega 2560 for this.

    • Hey,

      I might need more detail about the project with exactly what you want to do. It would be better if you email me your query. Also, I cannot help in writing the code from scratch, however I can help in debugging and correcting it if you have already written it.

      Thanks.

      Regards,
      Kunal Gupta

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s