skip navigational linksPJRC
Shopping Cart Download Website
Home Products Teensy Blog Forum
You are here: Teensy Teensyduino Libraries FrequencyTimer2

PJRC Store
Teensy 4.1, $28.60
Teensy 4.0, $22.80
Teensy LC, $12.85
Main Page
Getting Started
How-To Tips
Code Library

FrequencyTimer2 Library

FrequencyTimer2, written by Jim Studt, lets you create a zero jitter frequency output, and run your own function on each period.

Download: Included with the Teensyduino Installer
Latest Developments on Github

FrequencyTimer2 is compatible with Teensy 3.6, Teensy 3.5, Teensy 3.2, Teensy 3.1, Teensy 3.0, Teensy++ 2.0, & Teensy++ 1.0.

FrequencyTimer2 is NOT compatible with Teensy 1.0, Teensy 2.0 or Teensy LC.

Hardware Requirements

Output Pin
PWM Pins
Teensy 3.65-
Teensy 3.55-
Teensy 3.2 & 3.15-
Teensy 3.05-
Teensy++ 2.0241, 24
Teensy++ 1.0241, 24
Sanguino1514, 15
Arduino Mega109, 10
Arduino Uno113, 11

Teensy 3.x & LC also support adjustable frequency output with analogWriteFrequency() for the PWM pins. For adjustable frequency output, analogWriteFrequency() may be simpler to use.

When running on Teensy 3.x, FrequencyTimer2 uses the CMT timer, to allow a zero-jitter frequency output separate from the PWM pins.

The IRremote library uses the CMT timer on Teensy 3.x. FrequencyTimer2 and IRremote are not compatible when used on any Teensy 3.x board.

Example Use

This example can be opened from the menu: File > Examples > FrequencyTimer2 > Test

// FrequencyTimer2 Test

#include <FrequencyTimer2.h>

void setup() {

  Serial.println("You can issue commands like:");
  Serial.println("  12345p - set period to 12345 microseconds");
  Serial.println("  o - turn on a simple counter as the overflow function");
  Serial.println("  n - turn off the overflow function");
  Serial.println("  b - print the counter from the oveflow function");

// variables shared between interrupt context and main program
// context must be declared "volatile".
volatile unsigned long burpCount = 0;

void Burp(void) {

void loop() {
  static unsigned long v = 0;
  if ( Serial.available()) {
    char ch =;
    switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
      case 'p':
        Serial.print("set ");
        Serial.print((long)v, DEC);
        Serial.print(" = ");
        Serial.print((long)FrequencyTimer2::getPeriod(), DEC);
        v = 0;
      case 'e':
      case 'd':
      case 'o':
      case 'n':
      case 'b':
        unsigned long count;
        noInterrupts();     // disable interrupts while reading the count
        count = burpCount;  // so we don't accidentally read it while the
        interrupts();       // Burp() function is changing the value!
        Serial.println(count, DEC);


Arduino Playground Page has the original version of FrequencyTimer2, and more info about the library.

This github repository is the source for new development on FrequencyTimer2. Please submit bug reports or pull requests for FrequencyTimer2 on github!