# Arduino PWM pin vs digital pin PWM simulation

Do I understand it correctly from this thread, Why do some pins have ~ signs next to their numbers?, that the PWM pins generate a square wave signal independently from what the code uploaded to the chip does after starting the PWM output? In other words, if my code contains all sorts of delay() functions that pause longer than the period of the PWM signal, the PWM signal will still be generated properly, correct?

On the other hand, if I decided to implement my own PWM on a digital pin instead by flipping the state between HIGH and LOW in my code at regular intervals, the presence of other delay() calls in other parts of the code would directly impact my ability to generate the desired consistent PWM signal. Is this correct? I hope this question makes sense (a bit hard to ask the question).

## 1 Answer

You understood right. The pins with the tilde (~) symbol are pins which are linked to an hardware PWM module (i.e. timer). Since it is a hardware module, it is independent of whatever your CPU does. As such, if you are stuck in a delay, your hardware PWM will still continue to generate the proper output signal. On the other hand, a software PWM (one that you would generate with a manual pin toggling) would be interrupted by your delay.

Most of the time, you should prefer hardware PWM over software PWM for many reason:

1. It's more reliable;
2. It consumes less power (you could sleep your MCU while still generating PWMs);
3. It allows you to do something else instead of servicing the switching;
4. It allows for larger frequencies, because you can typically get frequencies up to your MCU's operating frequency, while a software emulation involves a few operations, so you are not able to keep up;
5. It's more accurate: the hardware PWM is simply a divider on your MCU's operating frequency, while software PWM uses CPU instructions, which require branching. Since it is compiled code, it is more or less deterministic, so your duty cycle would not be exactly the one you expect (near, but not perfect nor stable if doing something else at the same time).
• I have a follow up question. If I used registers to flip digital port HIGH and LOW, like PORTB = 0b00000001;, will that also go through CPU as if I was using digitalWrite(1, HIGH) or would that be as efficient as using PWM port? I assume it's also inefficient, right? – pkout Jun 20 '14 at 22:30
• This actually is using the CPU! When you use built-in libraries from the Arduino stack, you are using registers to toggle the I/Os. This fact is only abstracted away from you by the libraries. This is exactly why Arduino libraries (and mostly any high-level hardware abstraction layer) is much slower than toggling the GPIOs using such assignments. When you read the datasheet, if you feel like putting the MCU into sleep mode would prevent you from doing it, then it is very likely to be CPU-related. Timers and DMA (on other MCUs) even when CPU is in sleep mode. Hope this helps you. – Mishyoshi Jun 23 '14 at 15:18
• Makes sense. I learned important things from you! Thank you! – pkout Jun 23 '14 at 15:25