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.

enter image description here

  • \$\begingroup\$ 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. \$\endgroup\$
    – EasyOhm
    Oct 24 '18 at 13:33
  • \$\begingroup\$ 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? \$\endgroup\$
    – nikki2
    Oct 24 '18 at 13:40
  • \$\begingroup\$ No, amplitude is always high or low - no in between voltages. You can't control phase either. \$\endgroup\$
    – EasyOhm
    Oct 24 '18 at 13:45
  • \$\begingroup\$ @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. \$\endgroup\$ Oct 24 '18 at 13:48
  • \$\begingroup\$ if I want to drive the transfer function output should be the velocity and input should be pwm? or phase? which one. \$\endgroup\$
    – nikki2
    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.

  • \$\begingroup\$ Technically it goes from 0 to 255 on 8 bit timer, but it also have 16 bit timer. \$\endgroup\$
    – Rokta
    Oct 24 '18 at 13:47
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Oct 24 '18 at 13:49
  • 2
    \$\begingroup\$ @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! \$\endgroup\$
    – Neil_UK
    Oct 24 '18 at 14:06
  • \$\begingroup\$ 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. \$\endgroup\$ Oct 28 '18 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.