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)
{
}
}