Need help implement this code on ESP32

Hi! I have been trying to create a sliding sound.
As I show in this video.

I search a lot of blogs and documentation. I tried using ledc library using Arduino framework it created strange metallic sound and weird harmonics.

I know tone library is not available for ESP32.
Tone32 library also doesn't work like Arduino and does require delay().

Ledc library has a function called "ledc_freq_set" as far as I understand every time this function is called it resets the timer resulting in glitches and skipping sound from speaker.
Then I tried using esp-idf on PlatformIO I tried to access low level code tried to modified ledc library. I failed.

I even tried to create sine wave using SigGen. It also didn't work.

My Arduino code is here.

void setup() {
  pinMode(11,OUTPUT);

  for( int i=400; i<2000;i++){ //sliding freq from 400hz to 2000hz
    tone(11,i);
    delay(2);
  }
  
  noTone(11);
  
}

void loop() {}

first--what do you have working hardware wise? ie are you sure your hardware is setup properly? Posting your circuit might be helpful.

so you do it manually, by setting a timer & gradually reducing or increasing the frequency. essentially, you need a variable PWM output. I imagine thats what good sound libs implement anyways.

You also could check out bitluni & fabgl (both listed in platformIO libs) These libs are primarily graphics, but people often emulate old gaming systems using them, that includes sound. I've used fabgl extensively, but just for the display. Its well written & well documented. I imagine the sound classes are written well too. You may find ultimately that fabgl is too much for your application, you still might be able to learn something from how they make sounds.

http://www.fabglib.org/annotated.html
https://github.com/fdivitto/FabGL

re LEDC lib: to make sound, dont you need to push AND pull the speaker? While the ledc library has all the stuff to vary frequency, it looks like it only produces the positive side of the wave as thats all thats needed to turn a light on.

Before void setup() add:

Serial.begin(9600);

Then inside the "for" loop, add

Serial.println(analogRead(11));

You should see the values being used by tone() in your monitor (CTRL-SHFT-M)

ESP32 tone() - Google Search

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