How to Control LED Brightness – Arduino Tutorial

LED Brightness control

I assume you have already accomplished how to turn a LED on and off. Wouldn’t it be great if we are also able to control its brightness? Today we are going to do just that.

We know that in digitalWrite() function accept two parameter (HIGH / LOW) but if we use PWM pin then we can use analogWrite() function then we can we can control voltage by passing parameter between 0 – 255 . 0 means off & 255 means highest value.

 

Components :

  • Arduino uno
  •  LED
  •  220-ohm resistor

Connections:

LED Brightness control

Join together LED long pin to 220-ohm resistor then connect resistor another pin to arduino 9 pin and LED another pin to GND. Now your connection is complete.

 

Example Code

int led = 9;   

void setup() {
    pinMode(led ,OUPUT);
}

void loop() {
    analogWrite(led ,255);
    delay(100);
    analogWrite(led ,150);
    delay(100);
    analogWrite(led ,100);
    delay(100);
    analogWrite(led ,50);
    delay(100);
    analogWrite(led ,10); 
    delay(100);
    analogWrite(led ,0);
    delay(100);
}

 

you can also write this simply like this,

int led = 9;
int i;  
void setup() {
  pinMode(led ,OUPUT); 
}  
void loop() {
  for(i= 0; i<=255 ; i++){
      analogWrite(led ,i);
      delay(100);
 }
}

 

Now run the code and enjoy !!!!

 

Leave a Comment

(0 Comments)

Your email address will not be published. Required fields are marked *