tone() not declared for ESP32 MicroMod

Since the MicroMod Input and Display Carrier Board has a magnetic buzzer on it, I wanted to try it out – but when I tried to compile an example for use with the SparkFun ESP32 MicroMod board, I got a nastygram from the compiler saying that

‘tone’ was not declared in this scope

Referring to the Arduino reference, it sure looks like this “Advanced I/O” function is part of the default “language” and should not require any more #includes, but if it does, I’m at a loss as to how to enable it.

I’d “swiped” some of the code from the LilyPad Buzzer Example to generate a scale in some other code I was working with. When I first tried to compile, and it failed, I then grabbed the “Examples->[built-in]02.Digital->toneKeyboard”, and got the same error message from the compiler. Changing the board to Arduino Uno made the error message go away for toneKeyboard.

I’ve done some more digging in the past week, and have come up with some code that at least appears to work.

It appears at Espressif marketing, in their infinite wizDUMB, had not envisioned that anyone MIGHT want to connect a simple magnetic buzzer to an ESP32. They did, however, envision having folks want to connect several LEDs and dim them via PWM sort of independantly. The LED PWM system is complex, and the documentation on it is confusing. I’m not going to swear that this code won’t interfere with other stuff on the ESP32, but it will get the buzzer on the MicroMod Input and Display Carrier Board to generate tones. By the way, the ESP32 also has a separate PWM system for controlling motors – and that documentation is even MORE confusing.

Please note that I have ONLY implemented the two argument version of tone(), NOT the three argument version, so you’ll need to have calls to delay() after the calls to tone().

Also note that I have NOT tested it with any hardware other than the ESP32 MicroMod on a MicroMod Input and Display Carrier Board.

To use it, you’ll need two sections of code. The first needs to appear somewhere before the first use of the tone() function.

#ifdef ESP_H

// Which pin the buzzer is attached to
int buzzerPin = PWM1;
#define PWM1_Ch    0
#define PWM1_Res   8

void tone ( int tonePin, int toneFreq ) {
  Serial.print( "my tone called for pin " );
  Serial.print( tonePin );
  Serial.print( " with frequency " );
  Serial.println( toneFreq );
  ledcSetup( PWM1_Ch, toneFreq, PWM1_Res );
  ledcWrite( PWM1_Ch, 127 ); // 50% duty cycle
}
void noTone( int tonePin ) {
  Serial.print( "my noTone called for pin " );
  Serial.println( tonePin );
  ledcWrite( PWM1_Ch, 0 ); // 0% duty cycle
}
#else
// NOT an ESP32!
// From the LilyPad example
int buzzerPin = 5;
#endif

The second bit of code needs to be included inside the setup() function:

#ifdef ESP_H
  ledcAttachPin( buzzerPin, PWM1_Ch );
#else
  // LilyPad!
  pinMode(buzzerPin, OUTPUT);
#endif

Google helped me along the way by pointing me at a couple of posts, one on the Arduino Forums at https://forum.arduino.cc/t/esp32-varyin … ncy/866125 and the other on a web site called “DeepBlue” at https://deepbluembedded.com/esp32-pwm-t … e-arduino/, though neither did what I wanted to do.

In testing out the code on the hardware, I first tried it with the [SparkFun LilyPad Buzzer Hookup Guide, and it seemed to work, so I added the pitches.h table from the Arduino built-in example called toneMelody and tried a few from that. The really low tones are, well, “buzzy”, and the high ones are uncomfortably piercing, but they do seem to work.

Again, please be aware that using this code MIGHT NOT be compatible with other functions, notably other things that use PWM on the ESP-32. One that comes to mind is the brightness control for the display backlight on the MicroMod Input and Display Carrier Board.](LilyPad Buzzer Hookup Guide - SparkFun Learn)