Tone() does not accept two arguments

Calling tone() without the third duration argument gives the error:

macro "tone" requires 3 arguments, but only 2 given

Program to demonstrate:

void setup() {
  tone(8, 262); // NOTE_C4
  delay(1000);
  noTone(8);
}

void loop() {}

The third argument should be optional:

tone() - Arduino Reference

Tested with Arduino Nano ESP32 core 2.0.11.

1 Like

It is compiled in UNO without any error. It is also compiled in ESP32 Dev Module. I have no NANO ESP32.

Hello @johnsondavies, thanks for spotting this! it will be fixed in the next ESP core release.

If you feel OK with editing core files, you can fix it directly in your own installation: look in your Arduino15 folder for the file [edit: fixed path]

packages/arduino/hardware/esp32/2.0.11/cores/esp32/io_pin_remap.h

(what a mouthful!) and edit the line

#define tone(_pin, frequency, duration)     tone(digitalPinToGPIONumber(_pin), frequency, duration)

so it reads

#define tone(_pin, args...)                 tone(digitalPinToGPIONumber(_pin), args)

This functionality is famously unavailable in Espressif’s arduino-esp32 library and members of the community have found various work-arounds such as using the native LED Control functions to generate PWM signals.

However, looking more closely at arduino-esp32 library, not only has Espressif provided a clean API for generating tones, they’ve provided an interface for generating specific PWM frequencies for specific notes on the chromatic scale in different octaves.

@lburelli Thanks, but I don't seem to have io_pin_remap.h. Just io_pin_remap.cpp.

@johnsondavies check at this path:

packages/arduino/hardware/esp32/2.0.11/cores/esp32/io_pin_remap.h
1 Like

My bad, thanks! Edited the post for future reference :slight_smile:

1 Like