Arduino TimerOne Timer Interrupt: Wiring, Code and Simulator
To build this Arduino TimerOne Timer Interrupt project you need an Arduino Uno and nothing but the board itself. TimerOne calls a function from Timer 1 every 100 ms, independent of loop(). Make the 0 connections in the wiring table, upload the Arduino C++ code, and change initialize(100000) to 10000 and the ticks go ten times faster.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
Wiring
No wiring: this project uses only the Arduino Uno.
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add nothing but the board itself from the parts palette.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Change initialize(100000) to 10000 and the ticks go ten times faster.
Arduino C++ code
// TimerOne: run a function from a hardware-timer interrupt every 100 ms.
#include <TimerOne.h>
volatile unsigned int ticks = 0;
void tick() { ticks++; }
void setup() {
Serial.begin(9600);
Timer1.initialize(100000);
Timer1.attachInterrupt(tick);
}
void loop() {
static unsigned long last = 0;
if (millis() - last >= 1000) {
last += 1000;
noInterrupts();
unsigned int t = ticks;
interrupts();
Serial.print("Ticks: ");
Serial.println(t);
}
}
What to try
Change initialize(100000) to 10000 and the ticks go ten times faster.
Frequently asked questions
How do I wire the TimerOne Timer Interrupt project on an Arduino Uno?
No extra wiring is needed: it uses only the Arduino Uno itself.
What parts do I need for this Arduino project?
An Arduino Uno and nothing but the board itself.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
TimerOne calls a function from Timer 1 every 100 ms, independent of loop().
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator