ESP32-wroom cosine max frequency

I've been working on a project and it requires that I have a 60khz sine wave, I'd like to be able to adjust it a little in software, in 1khz increments would be OK, but if I can go finer than that, all the better.. however...
I made a sketch that uses the built in cosine generator and I can only get about 32.5 khz out of it according to my scope, and the waveform, while not perfect is certainly sufficient for my needs... The ESP32 literature says it can do up to 100khz.. Where am I going wrong? Is it perhaps something in the library?
32.5 khz sound suspiciously close to 2^15 so I'm wondering if there's a data conversion problem

#include <Arduino.h>
#include "DacESP32.h"

DacESP32 dac1(GPIO_NUM_25);

void setup() {
  Serial.begin(115200);

  Serial.println();
  Serial.print("Sketch started.  GPIO (Pin) number: ");
  Serial.println(GPIO_NUM_25);
}

void loop() {
  static long long  Freq = 20000;
  delay(1000);

 Serial.print("Setting Frequency to ");
      Serial.println(Freq);

      dac1.outputCW(Freq);
      Freq +=1000;
      if(Freq >= 100000){
        Freq = 20000;
      }
}

It's incrementing properly from 20khz up to 32 and then stops, I've tried a few different data types for the Freq variable and nothing changed

can you try with a uint32_t for the frequency (it should not matter but just for the sake of meeting the API)

void loop() {
  static uint32_t Freq = 20000;

  Serial.print("Setting Frequency to ");  Serial.println(Freq);
  dac1.outputCW(Freq);
  delay(1000);
  Freq +=1000ul;
  if(Freq >= 100000ul) Freq = 20000ul;
}

hum... just seen in the documentation

To assure at least 256 points per cycle the value for SW_FSTEP_MAX should not exceed 256 which still results in a possible highest output frequency of ~32kHz.

** SW_FSTEP_MAX = 64 --> voltage steps per cycle >= 1024, fmax ~7.8kHz*
** SW_FSTEP_MAX = 128 --> voltage steps per cycle >= 512, fmax ~15.6kHz*
** SW_FSTEP_MAX = 256 --> voltage steps per cycle >= 256, fmax ~31.3kHz*
** SW_FSTEP_MAX = 512 --> voltage steps per cycle >= 128, fmax ~62.6kHz*

so if you want to go above ~32KHz you'll have to sacrifice the number of steps (128 instead of 256) for a period

consider using a DDS module, e.g. ad9833
using an ESP32 driving a AD9833 generating a 100kHZ sine wave
image

If op adds a small RC filter, a 128 step cosine will be pretty smooth...
Even a square wave can be changed to something that looks like a cosine with 3 rc filters in series...

I think that's what's happening,.. now I'm really not sure how to access the register to change that, I'm assuming there's an added line needed in the setup?

Apparently you need to change SW_FSTEP_MAX directly in the source file of DacESP32.cpp

1 Like

I found it, and it's working now
I changed the Step size in the CPP file to 1024 and turned off High accuracy
Really sucks you have to go into the header and cpp files to make those changes

The calculated clock speed isn't quite right,
This fixed it
// Master clock for digital controller section of both DAC & ADC systems. // According to spec approximately 8MHz. #define CK8M 8430400UL
5% off on clock speed is pretty bad, hopefully it doesn't change too much over time and temperature

I did a little experimenting and it seems like there's about a 200hz step size at 100khz which is good enough for me

Solved! Thank you

1 Like

Great news

Have fun

1 Like

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