toneAC Library Project Homepage**
**
Advantages over the standard tone library:
- Nearly twice the volume (because it uses two out of phase pins in push/pull fashion)
- Higher quality (less clicking)
- Capability of producing higher frequencies (even if running at a lower clock speed)
- Nearly 1.5k smaller compiled code
- Bug fixes (standard tone library can generate some odd and unpredictable results)
- Can set not only the frequency but also the sound volume
- Less stress on the speaker so it will last longer and sound better
Download:
toneAC v1.2
New in v1.2:
Fixed a counter error which went "over the top" and caused periods of silence (thanks Krodal). For advanced users needing tight code, the TONEAC_TINY switch in toneAC.h activates a version of toneAC() that saves 110 bytes. With TONEAC_TINY, the syntax is toneAC(frequency, length) while playing the note at full volume forever in the background. Added support for the ATmega 640, 644, 1281, 1284P and 2561 microcontrollers.
Connection:
Connection is very similar to a piezo or standard speaker. Except, instead of connecting one speaker wire to ground you connect both speaker wires to Arduino pins. The pins you connect to are specific, as toneAC lets the ATmega microcontroller do all the pin timing and switching. This is important due to the high switching speed possible with toneAC and to make sure the pins are alyways perfectly out of phase with each other (push/pull). See the below list for which pins to use for different Arduinos. Just as usual when connecting a speaker, make sure you add an inline 100 ohm resistor between one of the pins and the speaker wire.
- Pins 9 & 10 - ATmega328, ATmega128, ATmega640, ATmega8, Uno, Leonardo, etc.
- Pins 11 & 12 - ATmega2560/2561, ATmega1280/1281, Mega
- Pins 12 & 13 - ATmega1284P, ATmega644
- Pins 14 & 15 - Teensy 2.0
- Pins 25 & 26 - Teensy++ 2.0
Example sketch:
#include <toneAC.h>
void setup() {} // Nothing to setup, just start playing!
void loop() {
for (unsigned long freq = 125; freq <= 15000; freq += 10) {
toneAC(freq); // Play the frequency (125 Hz to 15 kHz in 10 Hz steps).
delay(1); // Wait 1 ms so you can hear it.
}
toneAC(); // Turn off toneAC, can also use noToneAC().
while(1); // Stop (so it doesn't repeat forever driving you crazy--you're welcome).
}
Version History:
01/16/2013 v1.1 - Option to play notes in background, returning control back to your sketch for processing while note plays (similar to the way the tone library works). Volume is now linear and in the range from 0-10. Now uses prescaler 256 instead of 64 for frequencies below 123 Hz so it can go down to 1 Hz no matter what speed the CPU is clocked at (helpful if using toneAC to control a two-pin dual LED).
01/11/2013 v1.0 - Initial release.
Enjoy!
Tim