"tone" for buzzer ? No acceptable?

"Identifier “tone” is unidentified
‘tone’ was not declared in this scope.

Several places say this is how you do it. How should I be activating a buzzer?

if (randomNumber > i;, i++)

  {

    if (i % randomNumber == 0)

      tone(BZR01, 1000, 1000);

    tone(BZR02, 2000, 1000);

    delay(5000);

  }

On an Arduino board with an ATmega328, the tone() function can be called in two different ways:

tone(pin, frequency) ;
// or 
tone(pin, frequency, duration);

Frequency in Hz, duration is milliseconds.

It grabs hold of Timer/counter 2, an 8 bit timer, and runs it in CTC mode (Clear Timer on Compare match) and uses the timer’s Compare Match A interrupt to do the work of toggling the pin.

Do not connect a pin directly to a sound card or amplifier. The pin is switching between 0 and 5v, which is way above “line” level. Also, connecting directly to a speaker needs a resistor to limit current flow and avoids letting the magic blue smoke out of the AVR (or whatever microcontroller is in use).

Brett Hagman, who wrote the tone library, has details and warnings about it at GitHub - bhagman/Tone: A Wiring Library to produce square wave tones on arbitrary pins. which makes for interesting reading.

Only one pin can be generating a tone at any time. If a tone is already playing on a pin, a call to tone() with a different pin number will have no effect unless noTone() was called first.

If the call is made for the same pin as the one currently playing, this will set the tone’s frequency to that specified in the most recent call.

Your code above appears to be using two different pins. I assume you are using your Espressif board?

What have you got attached to the two pins named BZR01 and BZR02? Some buzzers require just a voltage while others require a pulse - like the tone() command generates.

Cheers,
Norm.

I’m using the little piezo guys, just a + and a -.

So no chords on the Uno, but the WeMos should work.

I’m migrating this from Arduino. It’s the first thing I wrote by my very own self. It generates a random number between 1 and 1M, and then does a bunch of if tests.

Some of the random numbers are going to pass more than one test, so I want the LEDs and tones to go together. They act in series now.

It’s fun, running in the background. You get this odd buzz at different tones, durations.

I only had two buzzers. Now I have a lot.

If it’s a piezo buzzer, then tone() should work. If it’s an “active” buzzer, then just activate it.

If it’s not working, go for a simple test like:

const int buzzer = 7;

void setup(){
  pinMode(buzzer, OUTPUT);
}

void loop(){
  tone(buzzer, 1000); // 1KHz
  delay(1000);
  noTone(buzzer);     // Sssssh!
  delay(1000); 
}

Connect a piezo -ve to gnd, connect a 100 ohm current limiting resistor between pin 7 and piezo +ve.

You do use limiting resistors yes? If not, you can fry a pin. Not only that, tapping a piezo can generate many thousands of volts!

If that doesn’t work then:

  • We have a dead buzzer?
  • We have a dead pin?
  • We have a bad connection?
  • We have a microcontroller that cannot do tone().
  • We have a problem!

Cheers,
Norm.

The problem isn’t in the function. We haven’t functioned yet.

I’ll try your text, which is essentially the same as mine.

‘tone’ was the word it flagged.

Nope. It’s the word:

Processing esp32dev (platform: espressif32; board: esp32dev; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 1.12.4 > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 3.10004.200129 (1.0.4)
 - tool-esptoolpy 1.20600.0 (2.6.0)
 - toolchain-xtensa32 2.50200.80 (5.2.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 26 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\esp32dev\src\main.cpp.o
Generating partitions .pio\build\esp32dev\partitions.bin
Archiving .pio\build\esp32dev\libFrameworkArduinoVariant.a
Compiling .pio\build\esp32dev\FrameworkArduino\Esp.cpp.o
Compiling .pio\build\esp32dev\FrameworkArduino\FunctionalInterrupt.cpp.o
Compiling .pio\build\esp32dev\FrameworkArduino\HardwareSerial.cpp.o
src\main.cpp: In function 'void loop()':
src\main.cpp:11:20: error: 'tone' was not declared in this scope
   tone(buzzer, 1000); // 1KHz
                    ^
src\main.cpp:13:16: error: 'noTone' was not declared in this scope
   noTone(buzzer); // Sssssh!
                ^
*** [.pio\build\esp32dev\src\main.cpp.o] Error 1
==================================================================================================== [FAILED] T

Can you create a small sketch which recreates the problem, and post it please. tone() is an Arduino built in, so including “Arduino.h” should make it compile.

Failing that, it’s possible that there isn’t a tone() function for your microcontroller.

Cheers,
Norm.

1 Like

I used the one you gave me:

#include <Arduino.h>

const int buzzer = 7;

void setup()

{

  pinMode(buzzer, OUTPUT);

}

void loop()

{

  tone(buzzer, 1000); // 1KHz

  delay(1000);

  noTone(buzzer); // Sssssh!

  delay(1000);

}

‘tone’ is used in published examples for 8266 and 32.

I’m using the WeMos D1 R32.

I just tried to build it for an Uno, and ‘tone’ is still not acceptable.

According to this issue on GitHub, tone() does not exist for your board.

However, the issue is closed - someone posted code for tone() and noTone().

You must be doing something wrong here then. :frowning_face:

The core software for the uno has tone() built in. The code above works fine on my uno.

Cheers,
Norm.

1 Like

Recently added to the above GitHub issue, ther’es now a tone library available:

If anyone wants to try it out.

Cheers,
Norm.

2 Likes

Thanks!
The D1 Mini is the one I want to use for the final gadget. I’ll see if I can use this library.

1 Like

Butit still won’t upload because it refuses to see the library.

Gotta say, PIO library function sucks too often.

I’ve spent at least half the time I’ve spent with PIO losing my mind over these stupid libraries.
And this one was installed by the PIO library tool. And it’s in the project’s /lib folder.

Fails too often. It’s insanely frustrating. And these are Adafruit libraries. No reason they wouldn’t be just known.

I’m sick of restarting.

Seriously, the library frustration is roughly half my PlatformIO experience.
Arduino did libraries well.

Seriously sucks. I took the tones out of that simple script that just lights stinking LEDs, and it’s forced me to add BusIO to the /lib. and now this?
I hate it. Where did all of this new junk come from? None of that was a problem the first three thousand times I got a library to work.

Processing esp32dev (platform: espressif32; board: esp32dev; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 1.12.4 > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 3.10004.200129 (1.0.4)
 - tool-esptoolpy 1.20600.0 (2.6.0)
 - tool-mkspiffs 2.230.0 (2.30)
 - toolchain-xtensa32 2.50200.80 (5.2.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <Adafruit GFX Library> 1.10.0    
|   |-- <Adafruit BusIO> 1.4.1       
|   |   |-- <Wire> 1.0.1
|   |   |-- <SPI> 1.0
|   |-- <SPI> 1.0
|   |-- <Wire> 1.0.1
|-- <Adafruit SSD1306> 2.3.1
|   |-- <Adafruit GFX Library> 1.10.0
|   |   |-- <Adafruit BusIO> 1.4.1
|   |   |   |-- <Wire> 1.0.1
|   |   |   |-- <SPI> 1.0
|   |   |-- <SPI> 1.0
|   |   |-- <Wire> 1.0.1
|   |-- <SPI> 1.0
|   |-- <Wire> 1.0.1
|-- <timelib>
|-- <Tone> 0.0.1
|-- <Wire> 1.0.1
Building in release mode
Compiling .pio\build\esp32dev\src\main.cpp.o
Generating partitions .pio\build\esp32dev\partitions.bin
Compiling .pio\build\esp32dev\libba3\Wire\Wire.cpp.o
Compiling .pio\build\esp32dev\lib5a1\SPI\SPI.cpp.o
Compiling .pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_BusIO_Register.cpp.o
Archiving .pio\build\esp32dev\lib5a1\libSPI.a
Compiling .pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_I2CDevice.cpp.o
Compiling .pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_I2CRegister.cpp.o
Archiving .pio\build\esp32dev\libba3\libWire.a
Compiling .pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_SPIDevice.cpp.o
Compiling .pio\build\esp32dev\libf23\Adafruit GFX Library\Adafruit_GFX.cpp.o
Compiling .pio\build\esp32dev\libf23\Adafruit GFX Library\Adafruit_GrayOLED.cpp.o
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:3:145: error: ISO C++ forbids declaration of 'Adafruit_I2CRegister' with no type [-fpermissive]
 Adafruit_I2CRegister::Adafruit_I2CRegister(Adafruit_I2CDevice *device, uint16_t reg_addr, uint8_t width, uint8_t bitorder, uint8_t address_width) {
                                                                                                                                                 ^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:3:145: error: no 'int Adafruit_BusIO_Register::Adafruit_I2CRegister(Adafruit_I2CDevice*, uint16_t, uint8_t, uint8_t, uint8_t)' member function declared in class 'Adafruit_BusIO_Register'
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'bool Adafruit_BusIO_Register::write(uint8_t*, uint8_t)':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:14:9: error: '_device' was not declared in this scope
   if (! _device->write(buffer, len, true, addrbuffer, _addrwidth)) {
         ^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'bool Adafruit_BusIO_Register::write(uint32_t, uint8_t)':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:29:9: error: '_bitorder' was not declared in this scope
     if (_bitorder == LSBFIRST) {
         ^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'uint32_t Adafruit_BusIO_Register::read()':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:49:10: error: '_bitorder' was not declared in this scope
      if (_bitorder == LSBFIRST) {
          ^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'bool Adafruit_BusIO_Register::read(uint8_t*, uint8_t)':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:62:9: error: '_device' was not declared in this scope
   if (! _device->write_then_read(_buffer, 1, buffer, len)) {
         ^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'bool Adafruit_BusIO_Register::read(uint16_t*)':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:73:7: error: '_bitorder' was not declared in this scope
   if (_bitorder == LSBFIRST) {
       ^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: At global scope:
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:105:106: error: ISO C++ forbids declaration of 'Adafruit_I2CRegisterBits' with no type [-fpermissive]
 Adafruit_I2CRegisterBits::Adafruit_I2CRegisterBits(Adafruit_I2CRegister *reg, uint8_t bits, uint8_t shift) {
                                                                                                          ^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:105:106: error: no 'int Adafruit_BusIO_RegisterBits::Adafruit_I2CRegisterBits(Adafruit_I2CRegister*, uint8_t, uint8_t)' member function declared in class 'Adafruit_BusIO_RegisterBits'  
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:117:6: error: prototype for 'void Adafruit_BusIO_RegisterBits::write(uint32_t)' does not match any in class 'Adafruit_BusIO_RegisterBits'
 void Adafruit_I2CRegisterBits::write(uint32_t data) {
      ^
In file included from lib\Adafruit BusIO/Adafruit_I2CRegister.h:1:0,
                 from lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:1:
lib\Adafruit BusIO/Adafruit_BusIO_Register.h:66:8: error: candidate is: bool Adafruit_BusIO_RegisterBits::write(uint32_t)
   bool write(uint32_t value);
        ^
*** [.pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_I2CRegister.cpp.o] Error 1
==================================================================================================== [FAILED]

Ok, here’s how it is for me. In a project that emulates the Arduino Serial interface for a book I’m currently writing.

platform.ini
src/testAVRusart.cpp
include/
lib/cBuffer/cBuffer.h
lib/cBuffer/cBuffer.cpp
lib/AVRusart/AVRusart.h
lib/AVRusart/AVRusart.cpp
  • In src/testAVRusart.cpp I have #include "AVRusart.h".
  • In lib/cBuffer/cBuffer.cpp I have #include "cBuffer.h". That header defines a circular buffer class.
  • In lib/AVRusart/AVRusart.cpp I have #include "AVRusart.h". That header defines my USART class.
  • Finally, in lib/AVRusart/AVRusart.h I have #include "cBuffer.h". Because the USART class has a pair of cBuffer class instances internally.

pio run compiles everything in src. When it sees the #include with a header file name in double quotes, it looks in:

  • The current directory;
  • The include directory;
  • All sub-directories in lib.

The compilation works. I have nothing in platformio.ini that mentions lib_deps etc. It just works.

I do not have to restart VSCodium. I do not have to restart Linux.

In another project, migrated from the Arduino IDE, I use the “servo” library in the IDE. So, I have the option to:

  • Install it globally - not a good idea as I’m using AVR for this project, but I also have some STM32 projects, it could lead to problems. Best avoided.
  • Install it locally, just for this project. pio lib install servo
  • Install it globally (no!) Or locally, using the pio home page in VSCodium.
  • Get the compiler to install it for me, locally. lib_deps = servo.

I chose to use lib_deps.

Once again, no need to restart, pio run will find the library, or download it as required.

I do not have a “global” folder where I keep lots of useful libraries. All mine are self written so live in a subdirectory of lib, or live wherever pio run puts them if I have a lib_deps in the ini file.

  • PlatformIO version 5.0.0 - it updated itself yesterday.
  • VSCodium (not the latest) 64 bit.
  • Linux Mint 19.3 64 bit.
  • Dell Vostro 17" laptop, 8G RAM, Core duo (Yes, it’s over 10 years old!)
  • Generating code for Atmel ATmega328, Arduino Uno and/or Arduino Duemilanove, both 16MHz, or my own “design”, Normduino running at 8MHz with no crystal to free up two extra pins. (http://qdosmsq.dunbar-it.co.uk/blog/2019/02/atmega328p-8mhz-on-a-breadboard/)

With the number if problems you are having, I think there is either:

  • Something wrong with your operating system;
  • Something wrong with your PlatformIO installation;
  • Something wrong in how or where you installed the libraries;
  • Something wrong with the framework.

My advice.

  • Start simple. Try out a simple project with no libraries - blink. Get it working. So far so good!.
  • Try another project, like servo, that uses a single library. Use lib_deps = servo. Compile. Did it work? Yes, move on, No, get in touch again. Post the output from the compiler. Post your ini file. Post the structure of your project folder. Post how you installed the library etc.

Please, post everything in one post. :wink: If you need to, you can edit a post using the pencil icon at the bottom. (I have to edit my posts all the time, spelling!)

Good luck.

Right, time for breakfast! :slightly_smiling_face:

Cheers,
Norm.

1 Like

I don’t know what the cause of your latest error is - but since it didn’t happen the “first three thousand times”… and you were just making changes to the code (removing tone)… it’s probably something you changed that you shouldn’t have! :stuck_out_tongue:

tone()/noTone() does work on the AVR and ESP8266 platforms without issue - it is only missing for the ESP32 platform, with the workaround being to use the functions mentioned in that github issue, or a library.

This looks like you’re putting libraries in the /include folder. Is that better than /lib?