Wiki

Clone wiki

Arduino toneAC2 / Home

toneAC2 Arduino Library for Arduino


Index


Introduction

Replacement to the standard tone library with the advantage of nearly twice the volume, 800 bytes smaller compiled code size, and less stress on the speaker. This alternate version uses timer 2 and allows for flexible pin assignment. The primary version (toneAC) allows for higher frequencies, higher quality, and even smaller code size. However, toneAC is fixed to using the PWM timer 1 pins unlike toneAC2 which can use any two pins. Both exclusively use port registers for the fast and smallest code possible.


Difference between toneAC and toneAC2

First off, toneAC is SUPERIOR to toneAC2. It's called toneAC2 only because it uses timer 2, not because it's a newer version of toneAC. toneAC2 is an alternate but INFERIOR version of toneAC that uses timer 2 instead of timer 1 and allows for any two pins to be used. You should use toneAC instead of toneAC2 if at all possible because toneAC is more accurate, higher quality, allows for higher frequencies, uses fewer CPU cycles, and creates smaller code. However, if you're having a conflict with timer 1, or just can't use the default PWM pins for timer 1, then toneAC2 may be your answer.


Download & Install

v1.1 Released

Save the .zip file to your desktop, then use the Importing a .zip Library instructions to import the library into the Arduino IDE.

(1,370 downloads on Google Code before being closed)

If you wish to fork this library, please create a private repository as to not confuse others trying to download the latest official version.


Show Your Appreciation

Help future development by making a small donation.

Donate

Supporters:

  • Jean-Christophe V. $10 on Aug 13th, 2019
  • SuperTechSales $10
  • Petr V. $5
  • D&D Archive $5
  • John C. $5
  • Christian L. $2
  • John W. $5
  • Molly N. $5
  • Erez M. $5
  • Jonny R. $1
  • Thomas W. $1
  • Louis B. $20
  • Regalia S. $5
  • Shawn C. $30

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 specified as the first two require options when you call toneAC2(). 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.

toneAC3.png


Syntax

toneAC2( pin1, pin2, frequency [, length [, background ]] ) - Play a note.

  • pin1 - Pin to attach one of the speaker wires.
  • pin2 - Pin to attach the other speaker wire.
  • frequency - Play the specified frequency indefinitely, turn off with noToneAC2().
  • 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)

noToneAC2() - Stop playing.


History

v1.1 - Released 09/15/2015: Fix a potential race condition with _tAC2_time. Moved development to Bitbucket.

v1.0 - Released 01/27/2013: Initial release.


Example

#include <toneAC2.h>

void setup() {} // Nothing to setup, just start playing!

void loop() {
  for (unsigned long freq = 125; freq <= 15000; freq += 10) {  
    toneAC2(2, 3, freq, 1); // Play the frequency (125 Hz to 15 kHz sweep in 10 Hz steps) for 1ms.
  }

  while(1); // Stop (so it doesn't repeat forever driving you crazy--you're welcome).
}

How Does It Work?

The library is named toneAC2 because it produces an alternating push/pull between two pins. It's not really AC (alternating current) as in-wall electrical wiring because it's a square wave and never produces a negative voltage. However, the effect of the alternating push/pull creates an effective double voltage differential which produces the higher volume level.

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 toneAC2, 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.

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.


Cool User Projects Using toneAC

YouTube of Arduino Soundtube (Kundt's Tube)YouTube of EE 47 Final Project
Project-HandBat

My Other Arduino Libraries

NewPing Works with many ultrasonic sensors, can communicate using only one pin, very low lag, fast (up to 30 pings per second), timer interrupt method for event-driven sketches, light code, and much more.

LCDBitmap Arduino library that allows you to create a tiny 20x16 pixel bitmap (raster) display on a normally character-only Hitachi HD44780 based LCD display. Typical drawing functions like line, rectangle, invert, etc. Control is right down to the pixel level.

toneAC Replacement to the standard tone library with the advantage of nearly twice the volume, higher quality, can produce higher frequencies, 1.5k smaller compiled code, and less stress on the speaker.

toneAC2 Replacement to the standard tone library with the advantage of nearly twice the volume, 800 bytes smaller compiled code size, and less stress on the speaker.

NewTone About 1,200 bytes smaller code size than the standard tone library, faster execution time, exclusive use of port registers for fastest and smallest code, higher quality sound output than tone library.

TimerFreeTone Replacement to the standard tone library but without using timers. Also over 1.5k smaller compiled code, exclusive use of port registers, and compatible with ATmega, ATtiny, and ARM-based microcontrollers.

Updated