Lab Manual MES Experiment 3
Lab Manual MES Experiment 3
Lab Manual MES Experiment 3
Title: Familiarization with the Timers of an Arduino Microcontroller Board, the study of LED
blink test, and implementation of a simple traffic control system using its Timer0 function.
Introduction:
Every electronic component of a sequential logic circuit works on a time base. This time base
helps to keep all the work synchronized. Without a time base, devices would have no idea as
to when to execute a particular action or task. Thus, the timer is an important concept in the
field of electronics.
A timer/counter is a piece of hardware built into the Arduino controller. It is like a clock and
can be used to measure time events. A timer is a fixed-width register whose value increases/
decreases automatically with the clock signal.
In AVR, timers are of two types, such as 8-bit and 16-bit timers. In an 8-bit timer, the register
used is 8-bit wide whereas, in a 16-bit timer, the register width is 16 bits. This means that the
8-bit timer can count 28 = 256 steps starting from 0 to 255. Similarly, a 16-bit timer can count
216 = 65536 steps starting from 0 to 65535.
Experimental Setup:
Anode
Cathode
(a) (b)
Fig. 1 Experimental Setup of a Traffic Control System using an Arduino Microcontroller Board.
Experimental Procedure:
The main task of this experiment is to understand and implement an LED blinking and traffic
light control system. Connect the circuits as per the diagram of Fig. 1. Then plug in the Arduino
microcontroller board to the PC.
2. Now, write the following program on the blank sketch for the LED blink test.
// Declaring the timer function to count 1 ms of delay instead of calling the delay function
int delay_timer (int milliseconds){
int count = 0;
while(1)
{
if(TCNT0 >= 16) // Checking if 1 millisecond has passed
{
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 2
Experiment 3 Lab Manual
TCNT0=0;
count++;
if (count == milliseconds) //checking if required milliseconds delay has passed
{
count=0;
break; // exits the loop
}
}
}
return 0;
}
void setup() {
//define pins connected to LEDs as outputs
pinMode(RED_PIN, OUTPUT);
void loop() {
//to make the red LED turned on
digitalWrite(RED_PIN, HIGH);
delay_timer(red_on);
void setup() {
//Define pins connected to LEDs as outputs
pinMode(RED_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
void loop() {
//to make the green LED turned on
digitalWrite(GREEN_PIN, HIGH);
delay_timer(green_on);
//to make the green LED turned off
digitalWrite(GREEN_PIN, LOW);
N.B. After saving your code, a sketch file with the desired file name will be stored in a
folder with the same file name.
3. Now you need to verify/compile your code to find out and correct the errors. For this, please
go to the Sketch Menu → Verify/Compile or press Ctrl+R or click on this button just
below the menu bar. If the code is correct then the code compilation done message will be
displayed, otherwise, an error message will be displayed.
4. After the compilation is done successfully, you need to upload your code onto the Arduino
Uno board. To upload the program, connect your Arduino Uno R3 board to your PC with
a USB cable. Before uploading the code, select the board type and port at your Arduino
IDE. For this purpose, please go to the Tools Menu → Board: “Arduino Uno” → Arduino
Uno. Then again go to the Tools Menu → Ports → COMx, here x means an integer number
that corresponds to the Arduino Uno board that is automatically detected by the IDE.
5. After you have selected the board and port, you must upload the code. For this, please go
to the Sketch Menu → Upload or press Ctrl+U or click on this button just below the
menu bar. If the code is correct then the code upload-done message will be displayed,
otherwise, an error message will be displayed. Based on the error message (if any), please
go to the code, and correct them. Usually, students forget to set up the Tools or select an
inappropriate board or COMx port number to get such kinds of error messages.
1) Include all codes and scripts in the lab report following the lab report writing template.
2) Show the output/results in the form of images. Give their captions and descriptions.
3) Configure the system to have delays for outputs according to your ID. Consider the last
three digits from your ID (if your ID is XY-PQABC-Z then consider A s delay for the RED
LED, B s delay for the YELLOW LED, and C s delay for GREEN LED). In case any digit
is zero then use Z s delay by default. Include the program and results within your lab report.
4) Include the Proteus simulation of the LED blink program and traffic light control system.
Explain the simulation methodology. You may learn the simulation from the following
video link: https://www.youtube.com/watch?v=yHB5it0s2oU
Reference(s):
[1] https://www.arduino.cc/.
[2] ATMega328 manual
[3] https://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-timers
[4] http://maxembedded.com/2011/06/avr-timers-timer0/