Making tones with ESP32 PWM- poorly documented

I am frustrated! I have seen lots of posts on the web for how to do it. Nobody ever mentions the header files, or if they do- the code doesn't compile. I have TCP/IP working UART comms and a bunch of other stuff, but...

There's the Ledc library, for which I've tried several header files (some didn't exist with the libraries I've downloaded- and I won't mention those). There's the "tones" library, and "Tones32", couldn't make those work (with decades of embedded systems experience). I read that some of the Ledc functions have been renamed from v2 to v3 of whatever library that is supposed to be.

What is the include file, and a couple of related function names that I can research and actually use? Should I just talk directly to the hardware by writing original code and forget the libraries? I am attempting to use an ESP32WROOM board for this.

Here are some of the current errors I am getting (but these are just with the latest attempts):

//#include "driver/ledc.h"
#include "esp32-hal.h"


.... error: 'ledcSetup' was not declared in this scope
69 | ledcSetup(PWMChannel, PWMFreq, PWMResolution);
| ^~~~~~~~~
.... error: too few arguments to function 'bool ledcAttach(uint8_t, uint32_t, uint8_t)'
71 | ledcAttach(LEDPin, PWMChannel);

which arduino ESP32 Core version are you using?

see Migration from 2.x to 3.0 - - — Arduino ESP32 latest documentation

LEDC

The LEDC API has been changed in order to support the Peripheral Manager and make it easier to use, as LEDC channels are now automatically assigned to pins. For more information about the new API, check LED Control (LEDC).

Removed APIs

  • ledcSetup
  • ledcAttachPin

New APIs

  • ledcAttach used to set up the LEDC pin (merged ledcSetup and ledcAttachPin functions).
  • ledcOutputInvert used to attach the interrupt to a timer using arguments.
  • ledcFade used to set up and start a fade on a given LEDC pin.
  • ledcFadeWithInterrupt used to set up and start a fade on a given LEDC pin with an interrupt.
  • ledcFadeWithInterruptArg used to set up and start a fade on a given LEDC pin with an interrupt using arguments.

Changes in APIs

  • ledcDetachPin renamed to ledcDetach.
  • In all functions, input parameter channel has been changed to pin

I believe that I am using the ESP32- no suffix. I bought these inexpensive modules on Amazon: 3 Set ESP32 Development Board 38Pin

Thanks for the link- it sheds some light on why things might not be working. But I'm still stuck. (I've written in C, C++) but I don't know if the Arduino IDE can use the raw header files - it seems like it can't but it could be because I am using the wrong versions (I don't get compiler errors in them).

that's the hardware. When you compile, your code + the arduino core goes onto the board.

Your IDE could be configured with ESP32 core version 2 or 3. If you had the older version 2, probably you'll find that the libraries / tutorial you were looking at will work.

Espressif's page that you kindly linked shows functions and their usage, even examples. No mention of any header files.

When you compile, the IDE injects a main.cpp that includes Arduino.h which includes many modules for you

I think you just solved a mystery (for me, anyway). Thanks again!

you can look at the main.cpp file to see what it does

I guess main.cpp isn't stored locally. But I followed the link you gave me, found the ledc stuff, and was able to call one function without any compilation errors!!!

Now I know what to do about the other functions, so you fixed me! I should be beeping in a few minutes now.

It is (you have the sane source tree in the platforms directory on your Mac/PC)

I thought I had recursively searched all the Arduino dirs (Windows), but I hadn't. Thanks to you (again), I found it. My code compiles now, but no beeps. I should be able to figure it out fairly quickly though.