# What is the physics behind width of the duty cycle in Arduino?

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.

• What do you mean modulation of the system? Timer interrupts switch a totem pole output between high and low. It really is as simple as a MCU keeping track of time to turn switches on and off. The analogWrite(255) refers to 8 bits of resolution on the duty cycle. Oct 24 '18 at 13:33
• there is no phase and amplitude? unlike the analog one? let's consider that I am controlling the position of dc motor with pwm. what is the input of this system ? am I really controlling the phase and amplitude? Oct 24 '18 at 13:40
• No, amplitude is always high or low - no in between voltages. You can't control phase either. Oct 24 '18 at 13:45
• @nikki2 I think you're confusing (analog) modulation schemes used for data transmission with PWM. PWM is not for data transmission. OK, you could do it but it is not intended for that. PWM is for controlling power to motors and LEDs, lamps etc. It is efficient as it switches on/off very rapidly instead of regulating which is much less power efficient. Oct 24 '18 at 13:48
• if I want to drive the transfer function output should be the velocity and input should be pwm? or phase? which one. Oct 24 '18 at 16:03

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.

• Technically it goes from 0 to 255 on 8 bit timer, but it also have 16 bit timer. Oct 24 '18 at 13:47
• Of course I know that (I have designed counters) but I wanted to keep things simple so I lied and started counting from 1 on purpose. Sometimes not being technically accurate helps in getting the message across. Oct 24 '18 at 13:49
• @Bimpelrekkie normally I'd agree with 'lies to children', but in the case of counters, I'd think I'd try to normalise children to count from 0, especially when talking about real hardware. That way, any confusion is informative. If you use Matlab, you'll know the frustration of only having 1-based arrays. Why not zero based? It was written by a chemist! Oct 24 '18 at 14:06
• Different Arduino models are built around different microcontrollers. The overall method of generating PWM in the microcontrollers is usually the same. The details (such as number of bits in the counter, number of counters, number of PWM channels) may vary from one microcontroller to another. Oct 28 '18 at 17:05