You are on page 1of 23

LAB SIX & SEVEN

USING TIMERS TO REGULATE LEDs

GROUP 19

ADUFUL LENNOX KOJO ANIM - 10821816


ABDUL NASSER ABDUL HAKEEM MAKUSE - 10834697
ADUAMA DANIEL NATOMAH - 10854793

MARCH 14, 2023


ABSTRACT
This lab report presents the implementation of six timer-based projects. The
first project through to the fifth, each demonstrates how to use a timer to
control the blinking frequency of an LED using prescalers 1, 8, 64, 256, and
1024 respectively. The sixth project shows how to use a timer to imple-
ment a traffic light system with three LEDs. All projects involve configuring
the timer control registers, enabling timer overflow interrupts, selecting a
prescaler type, and preloading the timer register with a value to achieve the
desired delay. The report details the calculations involved and the actual
code implementation for each project.
The result of each project is shown through the LEDs’ behavior, which fol-
lows the expected blinking frequency and traffic light sequence.

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.

MATERIALS AND RESOURCES


ˆ Proteus

ˆ 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.

Figure 1: Circuit Diagram Of Blinking LED

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.

The sketch sets up a timer (Timer 1) to generate an interrupt every time


it overflows.
The frequency of the overflow interrupt is determined by the prescaler value,
which is set in every sketch.
The initial value of the timer register (TCNT1) is set to a calculated value,
the timer register preload value.
This corresponds to a delay of approximately 1 second, assuming a clock
frequency of 16MHz.

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

Figure 2: Program Implementing Blinking With Prescaler Of 1

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

Figure 3: Program Implementing Blinking With Prescaler Value Of 8

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

Figure 4: Program Implementing Blinking With Prescaler Value Of 64

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

Figure 5: Program Implementing Blinking With Prescaler Value Of 256

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

Figure 6: Program Implementing Blinking With Prescaler Value Of 1024

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

Figure 7: Circuit Diagram Of Traffic Light System

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

Figure 8: Program Implementing Traffic Light System With Prescaler Value


Of 64

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.

The sketch sets up a timer (Timer 1) to generate an interrupt every time


it overflows.
The frequency of the overflow interrupt is determined by the prescaler value,
which is set for each LED in this sketch.(13 for Red & Green LEDs and 3
for the Yellow LED).
The initial value of the timer register (TCNT1) is set to a calculated value,
the timer register preload value.
We assume a clock frequency of 16MHz.

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

Figure 9: Snippet Of Result Of Proteus Simulation

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 experiments were carried out in a simulation environment using Pro-


teus software, and the results showed that the LED’s behavior followed the
expected blinking frequency and traffic light sequence.

The sixth project demonstrated the implementation of a traffic light sys-


tem using three LEDs, where the behavior of each LED follows a specific
sequence to simulate a real-life traffic light. The implementation involved
configuring the timer control registers, enabling timer overflow interrupts,
selecting a prescaler type, and preloading the timer register with a value to
achieve the desired delay. The LED behavior followed the expected sequence,
indicating the successful implementation of the traffic light system.

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.

In conclusion, the successful implementation of the timer-based projects pre-


sented in this lab report highlights the potential of the Arduino Uno board
in controlling the behavior of LEDs through the use of timers.

21
REFERENCES
[ 1 ] - Ghassaei, A. (2012). Arduino Timer Interrupts

[ 2 ] - Blum, C. (2016). Arduino: A quick start guide. Pragmatic Book-


shelf.

[ 3 ] - Margolis, M. (2016). Arduino cookbook. O’Reilly Media, Inc.

[ 4 ] - Monk, S. (2012). Programming Arduino: Getting started with sketches.


McGraw Hill Professional.

22

You might also like