Arduino Fading LED tutorial
FADE-
This example demonstrates the use of analogWrite() function in fading a LED on and off.
AnalogWrite uses pulse width modulation(PWM), turning a digital pin on and off very fast with a different ratio between on and off.
DUTY CYCLE-
Duty cycle is defined as the percentage of time a digital signal is ON over a time period. This period is the inverse of the frequency of waveform.
If a digital signal spends half of the time as ON and the other half as OFF, then it has a duty cycle of 50% and that is the case of an ideal square wave system.
PWM AS IN AnalogWrite() function -
Arduino circuit for fading LED-
Components Required-
1. Arduino or Genuine board.
2. 220 Ohm resistor.
3. LED bulb.
4. Connection wires and Breadboard.
CODE-
/* Made by Nishant kr */
int led = 6;
int brightness = 0;
int fadeAmount = 5;
void setup() {
// declare pin 6 to be an output:
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255)
{ fadeAmount = -fadeAmount; }
delay(30);
}
Connections-
Working model-
No comments