Professional Documents
Culture Documents
GROUP 19
INTRODUCTION
Hardware timers are used for timing and counting operations, allowing the
processor to carry on with some other process while the timer process runs.
Timers are also used to generate delays and measure time. An interrupt, if
enabled, is generated when the timer reaches its maximum value.
Arduino Uno is a popular choice for electronic hobbyists and professionals
alike due to its ease of use and versatility. One of the most important features
of the Arduino Uno is its built-in timers, which can be used to accurately
time events and control the behavior of connected components.
In this lab, we explore six timer-based projects that demonstrate the power
and flexibility of the Arduino Uno’s timers.
Each project focuses on a different aspect of timer usage, from simple LED
blinking to more complex traffic light control. In all cases, we use the timer
control registers, prescalers, and interrupt service routines to achieve the
desired behavior.
Arduino IDE
1
SYSTEM DESIGN AND IMPLEMENTATION
To implement a blinking LED with different prescalers, basically the same
circuit was used for the different values of the precalers.
2
How The Arduino Program Works
We designed Arduino sketches that demonstrate how to use a timer to gen-
erate a periodic signal that can be used to control an LED.
In the Interrupt Service Routine (ISR) function, the sketch checks the value
of a software counter (OVF count) to determine if the desired delay has
elapsed.
Once the delay has elapsed (i.e., the OVF count has reached MAX OVF),
the sketch toggles the state of the LED connected to pin 7 and then resets
the software counter and the timer register.
The MAX OVF is calculated as the number of overflows needed +1.
The sketch uses interrupts to increment the software counter (OVF count)
every time the timer overflows. The counter is then incremented and re-
turned.
3
Timer With A Prescaler of 1
Calculations
fclk = 16M Hz
ftarget = 1M Hz
P rescaler = 1
M aximum Count of T imer Register = 216 − 1 = 65535
fclk 16M Hz
fclk (P RE) = = = 16M Hz
P rescaler 1
fclk (P RE) 16M Hz
count = = = 16M
ftarget 1Hz
count 16, 000, 000
N umber of overf lows needed = = = 244
maximum count of timer register 65535
Remaining Count = count−(maximum count of timer register ∗ number of overf lows needed)
= 16, 000, 000 − (65535 ∗ 244) = 9460
T imer Register P reload V alue = maximum count of timer register−remaining count
= 65535 − 9460 = 56075
4
Arduino Code
5
Timer With A Prescaler of 8
Calculations
fclk = 16M Hz
ftarget = 1M Hz
P rescaler = 8
M aximum Count of T imer Register = 216 − 1 = 65535
fclk 16M Hz
fclk (P RE) = = = 2M Hz
P rescaler 8
fclk (P RE) 2M Hz
count = = = 2M
ftarget 1Hz
count 2, 000, 000
N umber of overf lows needed = = = 30
maximum count of timer register 65535
Remaining Count = count−(maximum count of timer register ∗ number of overf lows needed)
= 2, 000, 000 − (65535 ∗ 30) = 33950
T imer Register P reload V alue = maximum count of timer register−remaining count
= 65535 − 33950 = 31585
6
Arduino Code
7
Timer With A Prescaler of 64
Calculations
fclk = 16M Hz
ftarget = 1M Hz
P rescaler = 64
M aximum Count of T imer Register = 216 − 1 = 65535
fclk 16M Hz
fclk (P RE) = = = 0.25M Hz
P rescaler 64
fclk (P RE) 0.25M Hz
count = = = 0.25M
ftarget 1Hz
count 250, 000
N umber of overf lows needed = = =3
maximum count of timer register 65535
Remaining Count = count−(maximum count of timer register ∗ number of overf lows needed)
= 250, 000 − (65535 ∗ 3) = 53395
T imer Register P reload V alue = maximum count of timer register−remaining count
= 65535 − 53395 = 12140
8
Arduino Code
9
Timer With A Prescaler of 256
Calculations
fclk = 16M Hz
ftarget = 1M Hz
P rescaler = 256
M aximum Count of T imer Register = 216 − 1 = 65535
fclk 16M Hz
fclk (P RE) = = = 62, 500Hz
P rescaler 256
fclk (P RE) 62, 500Hz
count = = = 62, 500
ftarget 1Hz
count 62, 500
N umber of overf lows needed = = = 0.95
maximum count of timer register 65, 535
Remaining Count = count−(maximum count of timer register ∗ number of overf lows needed)
= 62, 500 − (65535 ∗ 0.9) = 3, 518
T imer Register P reload V alue = maximum count of timer register−remaining count
= 65, 535 − 3, 518 = 62, 017
10
Arduino Code
11
Timer With A Prescaler of 1024
Calculations
fclk = 16M Hz
ftarget = 1M Hz
P rescaler = 1024
M aximum Count of T imer Register = 216 − 1 = 65535
fclk 16M Hz
fclk (P RE) = = = 15, 625Hz
P rescaler 1024
fclk (P RE) 0.25M Hz
count = = = 15, 625
ftarget 1Hz
count 15, 625
N umber of overf lows needed = = = 0.238
maximum count of timer register 65535
Remaining Count = count−(maximum count of timer register ∗ number of overf lows needed)
= 15, 625 − (65535 ∗ 0.2) = 2, 518
T imer Register P reload V alue = maximum count of timer register−remaining count
= 65, 535 − 2, 518 = 63, 017
12
Arduino Code
13
Results
The results for all the above systems was the continuos blinking of the LED.
The speed at which it blinked varied where prescaler of 1 was the slowest
and the circuit with prescaler value of 1024 was the fastest.
14
Single Traffic Light System
Circuit Diagram
15
CALCULATIONS
Red & Green
We want LEDs red and green to stay on for 3 seconds when it is their turn,
hence, fclk = 16M Hz
ftarget = 1/3 = 0.3Hz
P rescaler = 64
M aximum Count of T imer Register = 216 − 1 = 65535
fclk 16M Hz
fclk (P RE) = = = 0.25M Hz
P rescaler 64
fclk (P RE) 0.25M Hz
count = = = 830, 000
ftarget 0.3Hz
count 830, 000
N umber of overf lows needed = = = 12.72
maximum count of timer register 65535
Remaining Count = count−(maximum count of timer register ∗ number of overf lows needed)
= 830, 000 − (65535 ∗ 12) = 46, 913
T imer Register P reload V alue = maximum count of timer register−remaining count
= 65535 − 46913 = 18622
16
Yellow
We want yellow LED to stay on for 1 second when it is its turn,
Register Value has also been already been calculated so we need to find the
overflow since that is distinct, hence, fclk = 16M Hz
ftarget = 1Hz
M aximum Count of T imer Register = 216 − 1 = 65535
T imerRegisterP reloadV alue = 18622
Remaining Count = 46913
fclk 16M Hz
fclk (P RE) = = = 0.25M Hz
P rescaler 64
fclk (P RE) 0.25M Hz
count = = = 0.25M
ftarget 1Hz
Remaining Count = count−(maximum count of timer register ∗ number of overf lows needed)
count − remaining count
number of overf lows needed =
maximum count of timer register
250000 − 46913
number of overf lows needed = =3
65535
17
Arduino Code
18
How The Arduino Program Works
We designed this Arduino sketch that demonstrates how to use a timer to
generate a periodic signal that can be used to control three LEDs represent-
ing a traffic light system.
In the Interrupt Service Routine (ISR) function, the sketch checks the value
of a software counter (OVF count) to determine if the desired delay has
elapsed.
Once the delay has elapsed (i.e., the OVF count has reached MAX OVF),
the sketch toggles the state of the LED connected to pin 7 and then resets
the software counter and the timer register.
The MAX OVF is calculated as the number of overflows needed +1.
The sketch uses interrupts to increment the software counter (OVF count)
every time the timer overflows. The counter is then incremented and re-
turned.
If statements are then used to toggle the state of the LEDs.
19
Results
20
RESULTS AND CONCLUSION
The implementation of the six timer-based projects presented in this lab re-
port showcases the versatility of the Arduino Uno board in controlling the
behavior of LEDs through the use of timers. The first five projects focused on
controlling the blinking frequency of an LED by varying the timer prescaler
type, with prescaler values of 1, 8, 64, 256, and 1024 being used to achieve
different blinking frequencies.
The calculations involved in determining the appropriate values for the timer
registers, prescaler type, and preload value were detailed in the materials and
methods section. The accuracy of these calculations was verified by the ob-
served behavior of the LEDs.
21
REFERENCES
[ 1 ] - Ghassaei, A. (2012). Arduino Timer Interrupts
22