Wiki

Clone wiki

Arduino Timer Free Tone / Home

TimerFreeTone Library for Arduino


Index


Introduction

Advantages over the standard tone library:

  • Doesn't use timers which frees up conflicts with other libraries.
  • Compatible with all ATmega, ATtiny and ARM-based microcontrollers.
  • About 1,500 bytes smaller binary sketch size than the standard tone library.
  • Exclusive use of port registers for AVR-based microcontrollers for fastest and smallest code.
  • Optional volume parameter.
  • Close to a plug-in replacement for the standard Tone library.

The disadvantage of using TimerFreeTone is that while playing a tone, the sketch will be in blocking mode instead of parallel mode like the default tone library. Basically, you call TimerFreeTone() where it plays the tone for the specified duration, after which it executes the next command. In some ways, this is easier to understand and program. However, this is different than the default tone library which executes in parallel using a timer interrupt to play the tone. This is a fundamental difference between the default tone library and TimerFreeTone.


Download & Install

v1.5 Released

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

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 (latest: Feb 8th, 2018):

Nikolai R. $5

Syntax

TimerFreeTone( pin, frequency, duration [, volume ] ) - Play a note on pin at frequency in Hz for duration in milliseconds.

  • pin - Pin speaker is wired to (other wire to ground, be sure to add an inline 100 ohm resistor).
  • frequency - Play the specified frequency (should work fairly well in the 100 to 15000 Hz range).
  • duration - Set the duration to play in milliseconds. Range: 0 to 65535 (65.5 seconds).
  • volume - Optionally set the tone volume level (from 1 to 10), defaults to full volume (10).

History

v1.5 - Released 09/12/2016 - Fixed problem with latest release of the Arduino IDE which caused the library to totally stop functioning. Adjusted note duration to not fail on timer rollover. Now delays for note duration when frequency or volume are zero.

v1.4 - Released 08/05/2016 - Added optional volume parameter.

v1.3 - Released 07/23/2016 - Fixed problem with long tone play durations. Changed the way the note duration is calculated from a suggestion by Paul Stoffregen.

v1.2 - Released 01/14/2015 - Calculates duration differently for higher tone accuracy and smaller code size.

v1.1 - Released 04/30/2014 - Automatically sets mode of pin to OUTPUT as does the standard Tone library. Sets pinOutput variable to volatile to work with certain microcontrollers. Removed overhead parameter and calculation, fairly accurate anyway at audible frequencies. Even smaller binary sketch size.

v1.0 - Released 04/25/2014 - Initial release.


Support Forum

TimerFreeTone support forum


Example

#include <TimerFreeTone.h>

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

int melody[] = { 262, 196, 196, 220, 196, 0, 247, 262 };
int duration[] = { 250, 125, 125, 250, 250, 250, 250, 250 };

void setup() {
  for (int thisNote = 0; thisNote < 8; thisNote++) { // Loop through the notes in the array.
    TimerFreeTone(TONE_PIN, melody[thisNote], duration[thisNote]); // Play thisNote for duration.
    delay(50); // Short delay between notes.
  }
}

void loop() {}

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