Source: https://mokxi.com/libraries/timerone
Updated: 2026-11-02

Libraries / Data and time
Arduino library · Data and time

# TimerOne: a periodic interrupt and custom PWM from Timer1

TimerOne, by Paul Stoffregen, puts the ATmega’s 16-bit Timer1 under a few plain calls. initialize() sets a period in microseconds, from about 1 microsecond to 8.3 seconds on a 16 MHz board, and attachInterrupt() runs one of your functions every time that period ends, whatever loop() happens to be doing.

Open the example in the editor See the calls

Built in on Mokxi: #include <TimerOne.h> and it builds, with nothing to install. Free, and no account needed.

## What TimerOne does

The same timer drives PWM on its own output pins, 9 and 10 on an Uno or Nano (11, 12 and 13 on a Mega), at a frequency you choose instead of analogWrite()’s fixed 490 Hz. pwm(pin, duty) takes a duty from 0 to 1023. That is handy for driving a fan at 25 kHz so it does not whine, or for making a clock signal for another chip.

Timer1 does other jobs too: it is analogWrite()’s timer on pins 9 and 10, and the Servo library uses it on an Uno. Once TimerOne owns it, those stop working the usual way. The function you attach runs inside an interrupt, so keep it short and make any variable it shares with loop() volatile.

Library Manager name | TimerOne

Author | Paul Stoffregen

Include | #include <TimerOne.h>

Boards | Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V

In the simulator | Simulated: it runs against the part the way it does on the bench.

## Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V.

In Mokxi it is built in: #include <TimerOne.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "TimerOne" by Paul Stoffregen from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add TimerOne to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1.1, so a sketch written here builds there too.

What to know in the simulator: Timer1 is also analogWrite’s on pins 9 and 10, as on the real board. clockSelectBits and pwmPeriod are public, as upstream, for sketches that pause the timer through TCCR1B.

Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · Arduino Pro Mini 3.3 V

## The calls you will use

In the order a sketch meets them.

### Timer1.initialize(150000)

Starts Timer1 with a period of 150,000 microseconds (150 ms). The default with no argument is one second.

### Timer1.attachInterrupt(blinkLED)

Calls the function once every period, from the timer interrupt.

### Timer1.pwm(9, 512)

Starts PWM on pin 9 at the timer’s period with a duty of 512 out of 1023 (about 50 percent).

### Timer1.setPwmDuty(9, 256)

Changes the duty on a pin already set up with pwm(), without restarting it.

### Timer1.setPeriod(40)

Changes the period (here 40 microseconds, 25 kHz) for both the interrupt and the PWM pins.

### Timer1.detachInterrupt()

Stops calling your function; the timer keeps counting.

### Timer1.stop()

Halts the timer. start() runs it from zero again and resume() carries on from where it stopped.

### Timer1.disablePwm(9)

Returns a pin from PWM to ordinary use.

## Example: Satellite tracker

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

The sketch is cut short here; the editor has every line of it.

Open it in the editor, wired and ready to run · Satellite tracker: the project page

## Common errors and how to fix them

### TimerOne.h drives the ATmega’s Timer1, so it is for the Uno, Nano, Pro Mini, Mega and Leonardo.

Mokxi stops the build with this on boards without an ATmega Timer1, and upstream fails too. On an ESP32 use its hardware timers (hw_timer) or the Ticker library; on other boards time with millis() or micros() in loop().

### A servo or analogWrite() on pins 9 and 10 stopped working

Those use Timer1 as well. Move the servo to a library or board that uses another timer, or move the PWM output to pins 3, 5, 6 or 11, which run from Timer0 and Timer2.

### The program freezes or millis() stops while the timer is running

The interrupt function is too slow or calls things that wait for interrupts, like Serial.print() or delay(). Set a volatile flag in the interrupt and do the slow work in loop() when the flag is set.

## Keep going

### Learn

- Arduino Timer Interrupt: Timer1 Every Second
- Arduino Interrupts: attachInterrupt on Pin 2 and 3
- Interrupts vs Polling on Arduino, Side by Side

### More data and time libraries

- QRCode
- Madgwick
- PID
- MsTimer2
- Low-Power
- RTClib
- DS1307RTC
- DS3231 (NorthernWidget) and DS3231 (Rinky-Dink Electronics)

Every built-in library · Open the editor

## Try TimerOne without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
