Go Down

Topic: toneAC v1.2 - Twice the volume, higher quality, higher frequency, etc. (Read 17 times) previous topic - next topic

teckel

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:
Code: (Example toneAC sketch) [Select]
#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
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

teckel

Disadvantages of using toneAC compared to the standard tone library:


  • You must use certain pins (9 & 10 on Uno, 11 & 12 on Mega).

  • Uses two pins instead of one.

  • Tone duration doesn't run as an interrupt, just a simple delay(). This actually may be an advantage to some for simplicity sake.



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 also exclusively uses port registers for the fastest and smallest code possible.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

sbright33

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Magician

I 'd be cautious to drive high capacitance load like piezo w/o current limiting resistors. Though volume 'd be limited back to 1 pin level.
Don't want to discourage an author, just something to think about, may be put some kind of warning in library header, that reliability of IC could be affected. .

elac

This is great thanks alot for sharing.
**Also using your New Ping library, thanks for that too.  8)
It's all about the skills

Go Up