ESP32 high speed pwm

I am using ESP32 and arduino to generate 1.7 MHz signal. I got a 5 kHz test code working, but I am having trouble with high frequencies. Do I need to enable some specific high speed mode or should the code below work?

const int ledPin = 32;
const int freq = 1700000;
const int ledChannel = 0;
const int resolution = 4;
 
void setup(){
  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(ledPin, ledChannel);
  ledcWrite(ledChannel, 7);
}
 
void loop(){}

I reformatted your code, please use code tags next time. (press the </> icon in the tool bar and paste your code (make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)


have a look at supported-range-of-frequency-and-duty-resolutions

enable Serial monitor and see if you get some error message

I actually tested with this code:

const byte ledPin = 32;
const uint32_t freq = 1700000; // 1.7Mhz
const uint8_t ledChannel = 0;
const uint8_t resolution = 3;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Configuring PWM");
  ledcAttachPin(ledPin, ledChannel);
  ledcSetup(ledChannel, freq, resolution);
  ledcWrite(ledChannel, 7);
}

void loop() {}

and my (poorly calibrated) scope shows

so this seems right

Hi @jargo1 ,
Formula for the correct calculation of the resolution parameter.
Resolution = log2 (Clock (80MHz) / f) + 1
f ESP Clock
ex: 5,000 HZ = 80,000,000 / 5,000 = 16,000
log2 (16,000) = ~ 14 + 1 = 15

RV mineirin

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.