0
\$\begingroup\$

I've been reading through the ESP32 hardware documentation, but I'm not entirely clear about the capabilities/limits of its high-speed PWM. Specifically, its granularity, precision, and stability.

Let's start with precision. According to the ESP_IDF docs, the frequency in hz is specified by a uint32_t, and has a maximum of 40MHz when using high-speed and a 80MHz clock source.

Does this mean you can literally specify some precise arbitrary frequency like 10,138,842hz and reasonably expect it to be within a few hz of that over the span of, say, 120 seconds? Or is the software documentation a bit (cough) "optimistic" about the degree to which the actual hardware carries out the software's requests?

For example, can you REALLY specify the frequency with 1hz granularity, or is there some underlying limit that forces the frequency to be some whole multiple of 80MHz/n, where n is probably some power of 2 that's way less than 80 million?

How much accuracy and stability can you assume a random ESP32 module bought for $8-12 from Amazon is actually likely to HAVE? For example, if you can truly set the frequency to some whole multiple of 1hz, and you set it to 10,138,842hz, what range of frequencies would you be likely to see if you ran the software on 100 random ESP32 modules and weeded out any that appeared to be really, blatantly bad?

Likewise, how stable is that frequency over time? For example, if you were outputting a nominally 10,138,842hz square wave that was initially measured to be 10,138,869hz for 120 seconds, would it remain within a hz or two of 10,138,869hz the whole time, would it drift slowly over the span of a few seconds, or would it thrash all over the place from millisecond to millisecond, to the point where anything observing the generated PWM waveform, doing FFTs on it, and EXPECTING to find something recognizable as a 10,138,842 (or 10,138,869hz) square wave would likely see only noise?

\$\endgroup\$
4
  • \$\begingroup\$ I don't know the answers to these questions. Other people might. However, I would say to pick up a few $8-12 modules and test them out yourself! Not only would the results be specifically tailored to your questions, but you'd have the peace of mind of having seen it firsthand plus the fun of testing this stuff out. \$\endgroup\$
    – InBedded16
    Mar 4 at 20:44
  • 1
    \$\begingroup\$ it cannot hit every frequency discreetly. the return value gives you the actual frequency applied. the square waveform seems to distort after a 100 KHz or so, but that could be my hobby-level equipment, or a cheap devboard with extra capacitance. \$\endgroup\$
    – dandavis
    Mar 4 at 21:12
  • \$\begingroup\$ An alternative may be one of the DDS (direct-digital synthesis) chips out there. Spit that a config packet and away it goes. Those can output MHz-range signals with fine granularity, but of course are rather complex and not exactly cheap. \$\endgroup\$
    – rdtsc
    Mar 4 at 21:46
  • \$\begingroup\$ I totally respect the mindset of precision. I don't have answers either, but will add this in case it's helpful: One thing that is definitely optimistic is the "40Mhz PWM". At that frequency you have 1-bit precision. In other words you can set ON and OFF, but nothing in between. IMO that completely defeats the purpose of a PWM in the first place. \$\endgroup\$
    – joshfindit
    Mar 7 at 17:35

1 Answer 1

2
\$\begingroup\$

The frequency reference is derived from a crystal, so you could expect the accuracy to be pretty darn good as well as drift. The datasheet has the requirements for this specified. These specs are critical for the operation of WiFi.

The basic operation of pwm is to have a counter that counts up to a given value then resets to zero. Rinse and repeat.

If you want 10 bit pwm resolution, then the counter will count from 0 to 1023. Assuming a clock of 40MHz, this gives you a PWM frequency of 40MHz / 1024 which would be around 38kHz.

So, higher resolution, less frequency.

On top of that, there is most likely a clock prescaler. This divides the 40MHz clock by a given value. The IDF function can only achieve certain frequencies due to integer division etc. you give it the parameters you’d like and it finds to closest settings that satisfy your request.

I’d suggest having a read of the datasheet and hardware manuals to get a complete picture.

In short, the clock accuracy and stability should not be an issue. The pwm freq granularity will depend on the hardware and what you require. If you want good frequency resolution, PWM mode is probably not what you want.

\$\endgroup\$

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.