LED BRIGHTNESS CONTROL USING ARDUINO

Led brightness control using Arduino, rather than just turning LEDs on and off rapidly using digitalWrite(), we can define the level of brightness of an LED by adjusting the amount of time between each LED’s on and off states using PWM (pulse-width modulation).

If you don’t know about PWM take a brief – PWM stands for pulse width modulation, we know led is a diode and works only in forward direction, but there is a twist, although it works is forward direction but if I give the led a less voltage it will be appears to be dim.

Same as this if I vary this voltage level then the intensity of the Led will also vary. Means, in simple, Led receives a DC cycle of different duty cycles (ON period). See figure

PWM signals of different duty cycles
PWM signals of different duty cycles

Only digital pins 3, 5, 6, 9, 10, and 11 on a regular Arduino UNO board can be used for PWM. They are marked on the Arduino board with a tilde (~). Which means these pins support PWM.

Required hardware or components for PWM in LED using Arduino

S.N.ComponentQuantity
1.Arduino Uno1
2.Breadboard1
3.LED1
4.Resistor 220 or 280 ohm1

CIRCUIT DIAGRAM : LED BRIGHTNESS CONTROL USING ARDUINO

Circuit diagram Arduino led connection for PWM
Circuit diagram Arduino led connection for PWM

ARDUINO PROGRAMMING CODE : LED BRIGHTNESS CONTROL USING ARDUINO

int del = 5;
int a = 0;

void setup() {
  pinMode(3, OUTPUT); // LED control pin is 3, a PWM capable pin
}

void loop() {
  for (a = 0 ; a < 256 ; a++) {
    analogWrite(3, a);
    delay(del);
  }
  for (a = 255 ; a >= 0 ; a--) {
    analogWrite(3, a);
    delay(del);
  }
  delay(200);
}

CODE EXPLANATION

First we define two integer variables that are del and a, and we use ‘del’ for delay and ‘a’ in for loop to vary intensity of LED.

int del = 5;
int a = 0;

In Setup()

As we know pin number 3 can support pwm, so we use pin number 3 as output by using function pinMode()

void setup() {
/* LED control pin is 3, a PWM capable pin */  pinMode(3, OUTPUT); }

In Loop()

To create a PWM signal, we use the function analogWrite(x, y), 
where x is the digital pin and y is a value for the “duty cycle”, between “0 and 255” where 0 indicates 0% duty cycle and 255 indicates 100% duty cycle. 

The 1st for loop intensity or brightness of LED will start to increase, starting from 0 intensity and after a delay of 5 milliseconds intensity will increase by 1 till its maximum intensity that is 255.

Then this for loop gets completely executed and we will reach to our next instruction that is another for loop

void loop() {
  for (a = 0 ; a < 256 ; a++) {
    analogWrite(3, a);
    delay(del);
  }

In 2nd for loop intensity of LED will start to decrease from intensity 255 and after a delay of 5 milliseconds intensity will decrease by 1 till its minimum intensity that is 0.

Then this for loop gets completely executed and we will reach to our next instruction that is delay of 200 milliseconds to our next cycle of void loop and the process continues

  for (a = 255 ; a >= 0 ; a--) {
    analogWrite(3, a);
    delay(del);
  }
  delay(200);
}

And the void loop() gets executed continuously and follows the same pattern.


READ NEXT
HOW TO TAKE INPUT TO CONTROL OUTPUT IN ARDUINO or TURN ON LED USING BUTTON AS INPUT


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x