Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

toneAC

toneAC Library for Arduino
Author:  Tim Eckel
Contact: tim@leethost.com

Introduction

Replacement to the standard Arduino tone library with twice the volume, higher quality and higher frequency.


Navigation


History

ReleaseDateChanges
1.21/27/2013Fixed bug that caused silence, added support for ATmega 640, 644, 1281, 1284P and 2561 microcontrollers.
1.11/16/2013Play in background, linear volume, frequency as low as 1 Hz.
1.01/11/2013Initial Release.

Purpose

The library is named toneAC because it produces an alternating current (AC) between two pins. The ATmega's PWM takes care of this so the accuracy is exact. When you send a tone to a speaker with the standard tone library, the loudest is at 50% duty cycle (only on half the time). Which at 5 volts, is like sending only 2.5v to the speaker. With toneAC, we're sending out of phase signals on two pins. So in effect, the speaker is getting 5 volts instead of 2.5, making it nearly twice as loud. The sound quality difference has to do with allowing the Arduino's PWM to take care of everything and careful programming. Longer piezo life happens because instead of driving the transducer disc only ever in one direction (deforming the disc and reducing sound and quality), it drives it in both directions keeping the disc uniform.


Features

  • 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

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.


Download

Download here: Download toneAC Library


Installation

There are two options for installation.

* .zip file installation:
  • "Sketch->Import Library...->Add Library..." (Arduino IDE 1.6.1 and older) or "Sketch->Include Library->Add .ZIP Library..." (Arduino IDE 1.6.2 and newer).
  • Select the downloaded file.
  • Click "OK".
* Manual installation:
  • Put the toneAC folder found in the downloaded file in "{sketchbook folder}/libraries". The sketchbook folder location can be found at "File->Preferences->Sketchbook location:".

Including the library in your sketch
  • In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch->Import Library->toneAC" (Arduino IDE 1.6.1 and older) or "Sketch->Include Library->toneAC" (Arduino IDE 1.6.2 and newer).


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().


Example

toneAC_demo Sketch

  1. #include <toneAC.h>
  2.  
  3. void setup() {} // Nothing to set up, just start playing!
  4.  
  5. void loop() {
  6.   for (unsigned long freq = 150; freq <= 15000; freq += 10){  
  7.     toneAC(freq); // Play the frequency (150 Hz to 15 kHz).
  8.     delay(1);     // Wait 1 ms so you can hear it.
  9.   }
  10.   toneAC(0); // Turn off toneAC, can also use noToneAC().
  11.  
  12.   while(1); // Stop.
  13. }


Information about this page

Last Modified: April 17, 2017, at 02:46 AM
By: pert