In the previous lesson, we used digitalWrite() to turn an LED fully on or off. But what if we want the LED to slowly get brighter and then dim again? To do this, we use analogWrite() to control how much voltage is sent to the LED using a method called Pulse Width Modulation (PWM).
PWM turns a pin on and off very quickly. If it stays on longer, the LED looks brighter. If it stays off longer, the LED looks dimmer. This happens so fast that our eyes only see a smooth change in brightness.
Think of PWM like flipping a light switch really fast. If it's on most of the time and off just a little, the room looks bright. If it's off most of the time and only on briefly, it looks dim. This is how PWM works behind the scenes.