LED fading
You can fade out and fade in the light of an LED using Arduino's analogWrite(pin, value)
function. Before we get into using the analogWrite()
function, we will understand the concept behind the analogWrite()
function. To create an analog signal, Arduino uses a technique called Pulse width modulation (PWM).
Pulse width modulation (PWM)
PWM is a technique for getting an analog signal using digital means. By varying the duty cycle (duty cycle is the percentage of a period, when a signal is active.), we can mimic an "average" analog voltage. As you can see in the following image, when we want medium voltage, we will keep the duty cycle as 50%. Similarly, if we want to achieve low voltage and high voltage, we will keep the duty cycle as 10% and 90% respectively. In this application, PWM is the process to control the power sent to the LED:
Using PWM on Arduino
Arduino UNO has 14 digital I/O pins. As mentioned in Chapter 1, Getting Started with Arduino and LEDs, we can use...