Question: how pwm works in microcontroller?
Answer: Most microcontrollers have a built-in timer that can be used to generate a PWM signal. PWM outputs can be converted to a dc voltage by using a series of RC low pass filters to average the pulses. Thus, a filtered PWM circuit is a simple low cost method to convert digital to analog (i.e. create a DAC).