Tone not working on espressif32 platform

Searching for tone() in the Arduino-ESP32 framework yields no results. The equivalent solution is to use the ledc driver (general PWM LED controller).

Also see ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials

Example code

// the number of the LED pin
const int ledPin = 16;  // 16 corresponds to GPIO16

// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
 
void setup(){
  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(ledPin, ledChannel);

  //output a Middle C
  ledcWriteTone(ledChannel, 261.626);
}
 
void loop(){
  //wait 5 secs
  delay(5000);
  //disable tone
  ledcWriteTone(ledChannel, 0);
}
2 Likes