American International University- Bangladesh (AIUB)
Faculty of Engineering (EEE)
Microprocessor and
Course Name:
Embedded Systems
Course Code: EEE4103
Semester: Fall 24-25 Sec: F
Lab Instructor: Md Sajid Hossain
Experiment No: 04 Part 2
Digital Timer project using millis() function to avoid the problems associated
Experiment Name: with delay().
Submitted by (NAME): Gazi Korbanul islam Student ID: 22-49972-3
Group Members ID Name
1. 22-47664-2 Progga Patowary
2. 22-47692-2 Md Kamruzzamn Jubo
3. 22-47733-2 Shusmita Anjum Aziz
4. 22-47891-2 Tasnia Ahmed
5. 22-49972-3 Gazi Korbanul Islam
6.
7.
Performance Date: 13/11/2024 Due Date: 19/11/2024
Marking Rubrics (to be filled by Lab Instructor)
Proficient Good Acceptable Secured
Category Unacceptable [1]
[6] [4] [2] Marks
Theoretical Background, All information, measures All Information provided Most information correct,
Much information missing
Methods and variables are provided that is sufficient, but more but some information may
and/or inaccurate.
& procedures sections and explained. explanation is needed. be missing or inaccurate.
Most criteria are met, but Experimental results don’t
All of the criteria are met;
there may be some lack of match exactly with the Experimental results are
Results results are described
clarity and/or incorrect theoretical values and/or missing or incorrect;
clearly and accurately;
information. analysis is unclear.
Demonstrates thorough Hypotheses are clearly Conclusions don’t match
and sophisticated stated, but some Some hypotheses missing hypotheses, not supported
Discussion understanding. concluding statements not or misstated; conclusions by data; no integration of
Conclusions drawn are supported by data or data not supported by data. data from different
appropriate for analyses; not well integrated. sources.
Title page, placement of
figures and figure
Minor errors in Major errors and/or
General formatting captions, and other Not proper style in text.
formatting. missing information.
formatting issues all
correct.
Writing is strong and easy
Writing is clear and easy
to understand; ideas are Most of the required
to understand; ideas are
fully elaborated and criteria are met, but some
connected; effective
connected; effective lack of clarity, Very unclear, many
Writing & organization transitions between
transitions between typographic, spelling, or errors.
sentences; minor
sentences; no typographic, grammatical errors are
typographic, spelling, or
spelling, or grammatical present.
grammatical errors.
errors.
Total Marks
Comments:
(Out of 30 ):
Experiment Title: Study of a Digital Timer using the millis () function of Arduino.
Abstract:
This experiment focuses on designing a digital timer using the Arduino's millis() function to
eliminate issues associated with the traditional delay() function. The primary goal is to create a
six-minute timer that activates an LED each minute and resets using a tilt switch. This approach
allows non-blocking execution, enabling efficient multitasking and precise timekeeping. The
methodology involves leveraging the millis() function, which tracks elapsed time in
milliseconds, and implementing a tilt switch for reset functionality. Key apparatus include an
Arduino Uno, LEDs, resistors, and a tilt sensor. The system is programmed to activate LEDs
sequentially at one-minute intervals and resets upon tilt detection. This project demonstrates
practical applications of non-blocking timers in embedded systems, highlighting their advantages
over traditional delay-based systems. The limitations include reliance on precise hardware setup
and potential inaccuracies in long-duration timing. This experiment offers insights into advanced
Arduino programming and introduces practical methods for real-time applications.
Introduction:
The main objective of this experiment was to design a digital timer using the Arduino's millis()
function to overcome the limitations of traditional delay()-based timing. In achieving this
objective,the following steps were performed:
a) To utilize the millis() function for non-blocking and efficient time tracking in an Arduino-
based system.
b) To design and implement a system that sequentially activates six LEDs at one-minute
intervals.
c) To incorporate a tilt switch as a reset mechanism for restarting the timer cycle.
d) To analyze the functionality of the digital timer in comparison to a delay-based approach and
evaluate its advantages in real-time applications.
This experiment demonstrates a practical application of advanced timer functions in embedded
systems while addressing common issues like multitasking inefficiency and limited timing
resolution.
Theory and methodology:
Up to now, when you’ve wanted something to happen at a specific time interval with the
Arduino, you’ve used delay(). This is handy, but a little confining. When the Arduino calls
delay(), it freezes its current state for the duration of the delay. That means there can be no other
input or output while it’s waiting. Delays are also not very helpful for keeping track of time. If
you wanted to do something every 10 seconds, having a 10 second delay would be cumbersome.
The millis() function helps to solve these problems. It keeps track of the time your Arduino has
been running in milliseconds. So far you’ve been declaring variables as int. An int (integer) is a
16-bit number, it holds values between -32,768 and 32,767. Those may be some large numbers,
but if the Arduino is counting 1000 times a second with millis(), you’d run out of space in less
than a minute. The long datatype holds a 32-bit number (between -2,147,483,648 and
2,147,483,647). Since you can’t run time backwards to get negative numbers, the variable to
store millis() time is called an unsigned long. When a datatype is called unsigned, it is only
positive. This allows you to count even higher. An unsigned long can count up to 4,294,967,295.
That’s enough space for milis() to store time for almost 50 days. By comparing the current
millis() to a specific value, you can see if a certain amount of time has passed. When you turn
your Digital Timer over, a tilt switch will change its state, and that will set off another cycle of
LEDs turning on. The tilt switch works just like a regular switch in that it is an on/off sensor.
You’ll use it here as a digital input. What makes tilt switches unique is that they detect
orientation. Typically, they have a small cavity inside the housing that has a metal ball. When
tilted in the proper way, the ball rolls to one side of the cavity and connects the two leads that are
in your breadboard, closing the switch. With six LEDs, your timer will run for six minutes.
Apparatus:
● Arduino Uno/ Arduino Mega
● Tilt sensor
● LED lights (Six)
● Resistors (One 10K and six 220 ohms)
Circuit Diagram:
The Arduino platform is made up of the following components.
Fig. 1 Experimental Setup of an LED Light Pattern Control System using a Tilt switch and an
Arduino Microcontroller Board.
Code/Program:
The following is the code for the implementation of the digital timer using the millis() test with
the necessary code explanation:
const int SwitchPin = 8;
unsigned long PreviousTime = 0;
int SwitchState = 0;
int PrevSwitchState = 0;
3
4
int led = 2;
long interval = 4000; // for 1 min = 60,000 ms delay
void setup() {
for (int x = 2; x < 8; x++) {
pinMode(x, OUTPUT);
}
pinMode(SwitchPin, INPUT);
}
void loop() {
unsigned long CurrentTime = millis();
if (CurrentTime - PreviousTime > interval) {
PreviousTime = CurrentTime;
digitalWrite(led, HIGH);
led++;
}
SwitchState = digitalRead(SwitchPin);
if (SwitchState != PrevSwitchState){
for (int x = 2 ; x < 8; x++) {
digitalWrite(x, LOW);
}
led = 2;
PreviousTime = CurrentTime;
}
PrevSwitchState = SwitchState;
}
Code Explanation: This Arduino code is for creating a digital timer using the millis()
function instead of the delay() function to avoid blocking the program execution. Here's a
breakdown of each part of the code along with an explanation:
• const int SwitchPin = 8: Declares a constant integer variable named
SwitchPin and initializes it with the value 8 . This variable is used to store
the pin number connected to a switch.
• unsigned long PreviousTime = 0: Declares an unsigned long variable
named PreviousTime and initializes it with the value 0. This variable is
used to store the previous time value.
• int SwitchState = 0: Declares an integer variable named SwitchState and
initializes it with the value 0. This Variable is used to store the current state
of the switch.
• int PrevSwitchState = 0: Declares an integer variable named
PrevSwitchState and initializes it with the value 0. This variable is used to
store the previous state of the switch.
• int led = 2: Declares an integer variable named led and initializes it with the
value 2. This variable is used to control the LED.
• long interval = 4000: Declares a long variable named interval and initializes
it with the value 4000. This variable is used to specify the time interval in
milliseconds.
• void setup(): Begins the setup function where initialization of the Arduino
board is done.
• for (int x = 2; x < 8; x++) { pinMode(x, OUTPUT); }: Initializes pins 2 to 7
as output pins using a for loop.
• pinMode(SwitchPin, INPUT): Initializes the pin defined by SwitchPin (pin
8) as an input pin.
• void loop(): Begins the loop function where the main program logic resides.
• unsigned long CurrentTime = millis(): Reads the current time in
milliseconds since the Arduino board began running and stores it in the
variable CurrentTime.
• if (CurrentTime - PreviousTime > interval): Checks if the time elapsed
since the previous time stored in PreviousTime is greater than the interval
specified by interval.
• PreviousTime = CurrentTime: Updates PreviousTime with the current
time.
• digitalWrite(led, HIGH): Turns on the LED connected to the pin specified
by led.
• SwitchState = digitalRead(SwitchPin): Reads the state of the switch
connected to the pin specified by SwitchPin and stores it in the variable
SwitchState.
• if (SwitchState != PrevSwitchState) : Checks if the current state of the
switch is different from the previous state.
• for (int x = 2; x < 8; x++) { digitalWrite(x, LOW); : Turns off all LEDs by
setting their corresponding pins
• (2 to 7) to LOW.
• led = 2: Resets the led variable to the starting LED pin.
• PreviousTime = CurrentTime: Updates PreviousTime to the current time to
mark the switch state change.
• PrevSwitchState = SwitchState: Updates PrevSwitchState with the current
switch state for the next loop iteration.
Hardware Output Results:
Here is the hardware implementation of the digital timer using the millis () test and the necessary
explanation of the implementation:
Fig 2 Hardware implementation of Digital Timer using the Millis () function
Fig.3First LED is ON in the Digital Timer
Fig.4 Second LED is ON in the Digital Timer
Fig.5 Third LED is ON in the Digital Timer
Fig.6 Fourth LED is ON in the Digital Timer
Fig.7 Fifth LED is ON in the Digital Timer
Fig.8 Sixth LED is ON in the Digital Timer
Fig.9 All LEDs turned OFF after giving vibration to the Tilt Switch
Simulation and Measurement:
Here are the simulation output results of Digital Timer using the Millis () function on Proteus
simulation
Fig.10 First LED is ON in the Digital Timer
Fig. 11 Second LED is ON in the Digital Timer
Fig,12 Third LED is ON in the Digital Timer
Fig.13 Fourth LED is ON in the Digital Timer
Fig.14 Fifth LED is ON in the Digital Timer
Fig.15 Sixth LED is ON in the Digital Timer
Fig.16 All LEDs turned OFF after giving vibration to the Tilt Switch
Experimental Output Result :
Figure: 17 Program for Sequential LED Light Pattern Control System in Arduino IDE.
Explanation: This figure shows that, the Arduino IDE version 2.2.1 has been used to write and
upload the program for the sequential led light pattern control system. The Arduino UNO
microcontroller board was used for this experiment
Answer to Report Question:
Include all codes and scripts into the lab report.
const int SwitchPin = 8;
unsigned long PreviousTime = 0;
int SwitchState = 0;
int PrevSwitchState = 0;
int led = 2;
long interval = 4000; // for 1 min = 60,000 ms delay
void setup() {
for (int x = 2; x < 8; x++) {
pinMode(x, OUTPUT);
pinMode(SwitchPin, INPUT);
void loop() {
unsigned long CurrentTime = millis();
if (CurrentTime - PreviousTime > interval) {
PreviousTime = CurrentTime;
digitalWrite(led, HIGH);
led++;
SwitchState = digitalRead(SwitchPin);
if (SwitchState != PrevSwitchState){
for (int x = 2 ; x < 8; x++) {
digitalWrite(x, LOW);
led = 2;
PreviousTime = CurrentTime;
PrevSwitchState = SwitchState;
}
Implement this system using Proteus or an online simulation platform www.tinkercad.com.
Fig.18 First LED is ON in the Digital Timer
Fig. 19 Second LED is ON in the Digital Timer
Fig,20
Third LED is ON in the Digital Timer
Fig.21 Fourth LED is ON in the Digital Timer
Fig.22 Fifth LED is ON in the Digital Timer
Fig.23 Sixth LED is ON in the Digital Timer
Fig.24All LEDs turned OFF after giving vibration to the Tilt Switch
Discussion:
In the following experiment, a simple digital timer system was developed using millis
function. The necessary observation has been done about millis function from various
sources by using internet and study material. Necessary information and observation were
collected for future reference and project implementation Also, the important information
about millis function were documented properly for future work. The millis function was
used to track the current time and if the specified interval had passed, the program
advanced to the next LED. It was observed that millis was a built-in library of Arduino.
Also, it was observed that the millis function was used as an alternative of delay function.
Furthermore, it were observed that millis function allowed to execute other codes while
waiting for the specified time that needed to be waited. From various observations and
sources, it was found that millis function has some drawbacks as well. The difficulties
that were found according to the observation: this is difficult to use in precise timing.
Besides this, there were many more drawbacks that were found. Based on the logic and
working methodology that were identified and acknowledged, the digital timer system
was developed accordingly using resistors, LEDs, tilt switch, wire, Arduino UNO board
and many more. The necessary code to run the digital timer program were also developed
based on the collected information regarding the millis function and working
methodology of a digital timer. The results of the developed system were observed and
noted down for further analysis. Simulations verified that the results that were obtained
was appropriate. For further observation a different kind of digital timer was developed
using several delay duration that were obtained from a student ID. The results were
observed and evaluated manually. Multiple simulation software was used to develop the
simulation of the experiment. In this experiment, TinkerCAD and Proteus simulation
software was used. The simulated results were verified accordingly. The simulated
outcomes were observed carefully and documented for future work. From the observation
it can be said that after both hardware and software implementation showed the expected
outcomes and the experimental objectives was achieved.
Conclusion:
The experiment successfully demonstrated the design and implementation of a digital timer using
the Arduino's millis() function. The hypothesis that millis() offers a superior alternative to
delay() for non-blocking timing and multitasking in embedded systems was validated. The timer
accurately activated six LEDs sequentially at one-minute intervals and reset effectively upon
detecting a tilt switch signal. This outcome confirms the efficiency and practicality of using
millis() for time tracking in real-time applications. The experiment highlights the importance of
non-blocking functions in overcoming traditional limitations, enabling enhanced performance in
embedded system design.
References:
1.Circuit Digest Website, [Online: March 21, 2022], [Cited: November 18, 2024]
Available: https://circuitdigest.com/article/everything-you-need-to-know-about-arduino-
uno-board-hardware
2.Arduino CC Website, [Online] [Cited: November 18, 2024] Available:
https://docs.arduino.cc/hardware/uno-rev3
3.Arduino CC Website, [Online] [Cited: November 18, 2024] Available:
4. https://reference.arduino.cc/reference/en/language/functions/time/millis/
[Cited: November 18, 2024]
5.AIUB Microprocessor and Embedded Systems Lab Manual 4