Wiki

Clone wiki

Arduino New Tone / Home

NewTone Library for Arduino


Index


Introduction

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. Plug-in replacement for Tone. Uses timer 1 which may free up conflicts with the tone library.


Download & Install

v1.0 Released

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

(6,355 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


Syntax

NewTone( pin, frequency [, length ] ) - Play a note on pin at frequency in Hz.

  • pin - Pin speaker is wired to (other wire to ground, be sure to add an inline 100 ohm resistor).
  • frequency - Play the specified frequency indefinitely, turn off with noNewTone().
  • length - [optional] Set the length to play in milliseconds. (default: 0 [forever], range: 0 to 2^32-1)

noNewTone(pin) - Stop playing note (pin is optional, will always stop playing on pin that was last used).


History

v1.0 - Released 01/20/2013 - Initial release.


Support Forum

NewTone support forum


Example

#include <NewTone.h>

#define TONE_PIN 2 // Pin you have speaker/piezo connected to (be sure to include a 100 ohm resistor).

// Melody (liberated from the toneMelody Arduino example sketch by Tom Igoe).
int melody[] = { 262, 196, 196, 220, 196, 0, 247, 262 };
int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };

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

void loop() {
  for (unsigned long freq = 125; freq <= 15000; freq += 10) {  
    NewTone(TONE_PIN, freq); // Play the frequency (125 Hz to 15 kHz sweep in 10 Hz steps).
    delay(1); // Wait 1 ms so you can hear it.
  }
  noNewTone(TONE_PIN); // Turn off the tone.

  delay(1000); // Wait a second.

  for (int thisNote = 0; thisNote < 8; thisNote++) { // Loop through the notes in the array.
    int noteDuration = 1000/noteDurations[thisNote];
    NewTone(TONE_PIN, melody[thisNote], noteDuration); // Play thisNote for noteDuration.
    delay(noteDuration * 4 / 3); // Wait while the tone plays in the background, plus another 33% delay between notes.
  }

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

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