{"id":9983,"date":"2023-05-28T14:51:42","date_gmt":"2023-05-28T12:51:42","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=9983"},"modified":"2023-08-17T23:49:50","modified_gmt":"2023-08-17T20:49:50","slug":"arduino-timerone-library","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/arduino-timerone-library\/","title":{"rendered":"Arduino TimerOne Library Code Examples [Tutorial]"},"content":{"rendered":"\n
In this tutorial, we’ll discuss Arduino TimerOne Library<\/strong> how it works, and how to use it to handle periodic tasks (functions). The Arduino TimerOne library can be easily installed within Arduino IDE itself. It provides users with useful APIs to configure and use the 16-Bit Timer1 for generating & handling periodic interrupts and also to generate PWM signals with controllable frequency and duty cycle.<\/p>\n\n\n\n We’ll create a couple of Arduino TimerOne Example Projects<\/strong> in this tutorial to practice what we’ll learn all the way through. Without further ado, let’s get right into it!<\/p>\n\n\n Timer modules in Arduino provide precise timing functionality. They allow us to perform various tasks, such as generating accurate delays, creating periodic events, measuring time intervals, and meeting the time requirements of the target application.<\/p>\n\n\n\n Each Arduino board has its target microcontroller that has its own set of hardware timers. Therefore, we always need to refer to the respective datasheet of the target microcontroller to know more about its hardware capabilities and how to make the best use of it.<\/p>\n\n\n Arduino UNO (Atemga328p) has 3 hardware timers which are:<\/p>\n\n\n\n Those timer modules are used to generate PWM output signals and provide timing & delay functionalities to the Arduino core, and we can also use them to run in any mode to achieve the desired functionality as we’ll see later on in this tutorial.<\/p>\n\n\n\n Each hardware timer has a digital counter register at its core that counts up based on an input clock signal. If the clock signal is coming from a fixed-frequency internal source, then it’s said to be working in timer mode. But if the clock input is externally fed from an IO or any async source, it’s said to be working as a counter that counts incoming pulses.<\/p>\n\n\n This is a summarized table for Arduino UNO (Atmega328p) timers, differences between them, capabilities, operating modes, interrupts, and use cases.<\/p>\n\n\n\n Playing with any timer configurations can and will disrupt the PWM output channels associated with that module. Moreover, changing the configurations of timer0 will disrupt the Arduino’s built-in timing functions ( You can configure & program the Arduino timer modules in two different ways. The first of which is bare-metal register access programming using the Timer control registers & the information provided in the datasheet. And this is exactly what we’ve discussed in the Arduino Timers & Timer Interrupts Tutorial<\/a><\/strong>.<\/p>\n\n\n\n The other method to control timer modules is to use Timer Libraries like the Arduino TimerOne Library. This is the topic of this tutorial, as we’ll discuss how to install and use For more information about Arduino Timers, fundamental concepts, different timer operating modes, and code examples, it’s highly recommended to check out the tutorial linked below. It’s the ultimate guide for Arduino Timers.<\/p>\n\n\nTable of Contents<\/h2>\n
\n
\n\n\nArduino Timers<\/strong><\/h2>\n\n\n
1. Arduino Hardware Timers<\/strong><\/h3>\n\n\n
\n
2. Arduino Timers Comparison<\/strong><\/h3>\n\n\n
<\/td> Timer0<\/strong><\/td> Timer1<\/strong><\/td> Timer2<\/strong><\/td><\/tr> Resolution<\/mark><\/strong><\/td> 8 Bits<\/td> 16 Bits<\/td> 8 Bits<\/td><\/tr> Used For PWM Output Pins#<\/mark><\/strong><\/td> 5, 6<\/td> 9, 10<\/td> 11, 3<\/td><\/tr> Used For Arduino Functions<\/mark><\/strong><\/td> delay()<\/code>
millis()<\/code>
micros()<\/code><\/td>
Servo Functions<\/td> tone()<\/code><\/td><\/tr>
Timer Mode<\/mark><\/strong><\/td> \u2713<\/td> \u2713<\/td> \u2713<\/td><\/tr> Counter Mode<\/mark><\/strong><\/td> \u2713<\/td> \u2713<\/td> \u2713<\/td><\/tr> Output Compare (PWM) Mode<\/mark><\/strong><\/td> \u2713<\/td> \u2713<\/td> \u2713<\/td><\/tr> Input Capture Unit Mode<\/mark><\/strong><\/td> –<\/td> \u2713<\/td> –<\/td><\/tr> Interrupts Vectors<\/mark><\/strong><\/td> TIMER0_OVF_vect<\/code>
TIMER0_COMPA_vect<\/code>
TIMER0_COMPB_vect<\/code><\/td>
TIMER1_OVF_vect<\/code>
TIMER2_COMPA_vect<\/code>
TIMER1_COMPB_vect<\/code>
TIMER1_CAPT_vect<\/code><\/td>
TIMER2_OVF_vect<\/code>
TIMER2_COMPA_vect<\/code>
TIMER2_COMPB_vect<\/code><\/td><\/tr>
Prescaler Options<\/mark><\/strong><\/td> 1:1, 1:8, 1:64, 1:256, 1:1024<\/td> 1:1, 1:8, 1:64, 1:256, 1:1024<\/td> 1:1, 1:8, 1:32, 1:64, 1:128, 1:256, 1:1024<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n delay<\/code>,
millis<\/code>, and
micros<\/code>). So keep this in mind to make better design decisions in case you’ll be using a hardware timer module for your project. Timer1 can be the best candidate so to speak.<\/p>\n\n<\/div><\/div>\n\n
3. Arduino Timers Control<\/strong><\/h3>\n\n\n
TimerOne<\/code> library. Which can be a lot easier to use than what we’ve done in the Arduino timer interrupts tutorial previously.<\/p>\n\n\n\n