Digital pins can only be in one of two states: either HIGH or LOW. An LED connected to a digital pin can also only be in one of two states: on or off, correspondingly. But is there a way to control the brightness of this LED? Yes, we can use a method called Pulse-width Modulation (PWM).
The idea behind PWM is simple. We limit the amount of power delivered by the electrical signal by turning it on or off periodically. This makes the signal pulse with some frequency and the amount of power is proportional to the width of the pulse—the time when the signal was HIGH.
For example, if we turn a pin to HIGH for 10 microseconds and then LOW for another 90 microseconds in a loop, a device connected to that pin receives 10% of the power that would be delivered if the pin were always HIGH.
In this recipe, we will learn how...