I was wondering if someone can tell me how Arduino changes the width of the duty cycle? How it produces an on-off pattern? I am looking for an explanation for modulation in this system.
There are several simple ways of making a PWM signal. PWM means Pulse Width Modulation which is a signal of (usually) a constant frequency in which the time that the signal is high is variable.
One way to implement this is Make a counter which continually counts from 1 to 100 and after 100 resets so the next count is "1": So: 1, 2, 3, ...,99, 100, 1, 2,...
Then use a comparator to compare that number to the Duty Cycle you want. For example, we want a 33% DuCy, then we make the counter output "1" when the counter's value is 33 or lower. The comparator will "flip" at 34 so for 34 to 100 output will be "0".
The Arduino has not 100 DuCy levels but 256 so the counter counts not to 100 but to 256.
The counter will "clock" (count up) on some clock derived from the main clock frequency.
I am not saying that this is THE way that this is implemented in the ATMega chip on the Arduino, it is an example of how it could be implemented.