ESP32-WROOM PWM output jitter

Vernonvand
Posts: 4
Joined: Sat Jan 07, 2023 4:47 am

ESP32-WROOM PWM output jitter

Postby Vernonvand » Sat Jan 07, 2023 4:58 am

Hi all,
I am trying to produce a square wave output from the ESP32-WROOM, but the resulting output jitters, rather than being stable.

The desired frequency is around 134.2kHz and I am using the code below to produce it. I am checking the output on an oscilloscope and it appears that the frequency (or possibly the duty cycle) changes constantly. It isn't a huge change, but enough to cause me issues with the application I am using it for.

I have confirmed that the oscilloscope is functioning correctly, as I can switch to a wave generator that produced the same waveform (frequency and amplitude) and it shows up as stable.

Can anyone offer advice on if/how I can produce a stable waveform with the ESP32?

Thanks in advance.

Code: Select all

#define TransmitPin 2 //transmitter for RF

// setting PWM properties
const int TransFreq = 134300;//124300;//134300; //actually 134200Hz
const int TransmitChannel = 1;
const int TransmitResolution = 1; //must be 1 ....range is 1-14 bits (1-20 bits for ESP32)

//=======================================================================
//                    Power on setup
//=======================================================================
void setup() {
  Serial.begin(115200);
  
  pinMode(TransmitPin,OUTPUT);

  float frq = ledcSetup(TransmitChannel, TransFreq, TransmitResolution);
  log_i("freq: %f", frq);
  ledcAttachPin(TransmitPin, TransmitChannel);
}



//=======================================================================
//                    Main Program Loop
//=======================================================================
void loop() {
  ledcWrite(TransmitChannel, 1);
  while(1)
  {
  }
  
}

ESP_Sprite
Posts: 9589
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-WROOM PWM output jitter

Postby ESP_Sprite » Sat Jan 07, 2023 6:17 am

The LEDC does not have a PLL, instead it uses a fractional divider (which means it jitters between two frequencies to globally get the frequency you want). I don't know what the Arduino driver uses as the clock to derive the signal from, but it clearly doesn't divide cleanly into the frequency you need.

Vernonvand
Posts: 4
Joined: Sat Jan 07, 2023 4:47 am

Re: ESP32-WROOM PWM output jitter

Postby Vernonvand » Sat Jan 07, 2023 6:37 am

Thanks for the response.

I don't believe the issue is anything to do with using the Arduino IDE, as the "ledc" functions map to the native LEDC functions in the ESP ... as far as I can tell.

The frequency returned from the ledcSetup() function is not the frequency I set, but is instead slightly lower (but close enough for my application) and I assume that is because that is the closest frequency the PWM can get to by clean division ... but again I could be wrong.

Maybe I have misunderstood, but you seem to be suggesting that the ESP32 alters the frequency as it goes to give an "average" that is close to what I have set ... is that what you are saying?

Cheers.

Vernon.

ESP_Sprite
Posts: 9589
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-WROOM PWM output jitter

Postby ESP_Sprite » Sat Jan 07, 2023 8:17 am

ledcSetup is an Arduino-specific call: the ESP-IDF equivalent is documented here.

Vernonvand
Posts: 4
Joined: Sat Jan 07, 2023 4:47 am

Re: ESP32-WROOM PWM output jitter

Postby Vernonvand » Sat Jan 07, 2023 8:39 pm

Yes I realise it's an Arduino specific call, but what I am saying is that it is mapped to the ESP LEDC calls, so I don't believe that the issue I am experiencing is because I'm using Arduino.

Do you have any feedback on this ...
Maybe I have misunderstood, but you seem to be suggesting that the ESP32 alters the frequency as it goes to give an "average" that is close to what I have set ... is that what you are saying?
Thanks.

Vernon.

ESP_Sprite
Posts: 9589
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-WROOM PWM output jitter

Postby ESP_Sprite » Sun Jan 08, 2023 2:58 am

More-or-less, yes. (More specifically, it varies the length of each cycle a bit)

Vernonvand
Posts: 4
Joined: Sat Jan 07, 2023 4:47 am

Re: ESP32-WROOM PWM output jitter

Postby Vernonvand » Wed Jan 11, 2023 8:23 am

Just to circle back on this.
I also tried setting the frequency to be a whole multiple of the clock frequency and it still produced jitter.

My conclusion is that, at the frequencies I am working, the ESP32 is not capable of producing a jitter free PWM output.

I tried a few external clock sources, which ultimately worked, but I had a niggling thought in the back of my head that I shouldn't need all those additional components and complexity, and that the ESP should be able to do it.

Long story short, I found a way to do it with the ESP32, although I still do need some additional components for post processing of the signal.
I am using the ESP32's DAC continuous wave feature. This produced a stable sine wave at the desired frequency (well very close to), which I then feed into an opamp to buffer and amplify.

If anyone has any other thoughts or suggestions, I still open ... but for the moment I'm going with the above setup.

Cheers.

Vernon.

Who is online

Users browsing this forum: Bing [Bot] and 84 guests