Arduino Library – toneAC

library icon

Installatie van Arduino IDE libraries: Arduino info

Informatie (ENG):arduino logo rond

Replacement to the standard tone library with many advantages:

  • 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 microcontroller is 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

Disadvantages are that it must use certain pins and it uses two pins instead of one. But, if you’re flexible with your pin choices, this is a great upgrade. It also uses timer 1 instead of timer 2, which may free up a conflict you have with the tone library. It exclusively uses port registers for the fastest and smallest code possible (which also makes it ONLY compatible with ATmega microcontrollers).

Connection Example

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
  • Ping 12 & 13 – ATmega1284P, ATmega644
  • Pins 14 & 15 – Teensy 2.0
  • Pins 25 & 26 – Teensy++ 2.0

Syntax

toneAC( frequency [, volume [, length [, background ]]] ) – Play a note.

  • frequency – Play the specified frequency indefinitely, turn off with toneAC().
  • volume – [optional] Set a volume level. (default: 10, range: 0 to 10 [0 = off])
  • length – [optional] Set the length to play in milliseconds. (default: 0 [forever], range: 0 to 2^32-1)
  • background – [optional] Play note in background or pause till finished? (default: false, values: true/false)

toneAC() – Stop output.

noToneAC() – Same as toneAC().

History

v1.2 – Released 01/27/2013 – 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) which plays the note at full volume in the background. Added support for the ATmega 640, 644, 1281, 1284P and 2561 microcontrollers.

v1.1 – Released 01/16/2013 – 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).

v1.0 – Released 01/11/2013 – Initial release.


Download toneAC @ bitbucket.org

[#/arduino/libraries/toneac” ]