# PWM Pins

PWM stands for Pulse Width Modifier. It is a technique to get analog value using digital pins.

When we set a digital pin to HIGH, the pin will output 5 volts for a full duty cycle. The duty cycle on a microcontroller is dependent on the frequency of the board.

When we output an analog value to a PWM pin, the output will be HIGH for a percentage of the time of a full duty cycle. The analog value goes from 0 to 255. At 0 the output is always LOW. And at 255, the output is always HIGH. If the analog value we are writing to the pin is 127, then the output of the pin will be HIGH for half the duty cycle and LOW for the other half.

Some sensors would measure the width of the pulse to read the analog value that we output.

pwm

If we connect an LED to a PWM pin, we can control the brightness of the LED by writing an anlog value to the pin from 0 to 255.


This program is incrementing the value of brightness every 20 milleseconds. So and writing the analog value to pin 11. To reach 255 from 0, it will take 255*20 = 5100 milleseconds. Which is around 5 seconds.

What happens after reaching 255?

The variable brightness is of type byte. A byte consists of 8 bits. What is the maximum number we can store in 8 bits?

In C++, if we increment a byte of value 11111111, the result will be 00000000.