Lab 4 - MES - Group 8
Lab 4 - MES - Group 8
BANGLADESH
Faculty of Engineering
Lab Report
Experiment # 03
Experiment 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.
* Student(s) must complete all details except the faculty use part.
** Please submit all assignments to your course teacher or the office of the concerned teacher.
Group # 05
1
FACULTY COMMENTS
Marks Obtained
Total Marks
Table of Contents
Title 03
Objectives 03
Equipment List 03
Circuit Diagram 03
Code / Program 04
Experimental Output Results (Color Photographs) 06
Simulation Output Results (Color Photographs) 07
Answers to the Questions in the Lab Manual 09
Discussion 09
References 09
2
Title: Study of a Digital Timer using the millis() function of Arduino.
Objective:
The objectives of this experiment are to
1. Study the application of the millis() function of Arduino.
2. Build a digital timer to turn on several LEDs every minute sequentially on an Arduino Microcontroller Board.
3. Know the working duration of a project run by an Arduino’s built-in Timer.
4. Implement a sequential LED light pattern control system using an input switch and an Arduino
Microcontroller Board.
Apparatus:
1) Arduino IDE (2.0.1 or any recent version)
2) Arduino Microcontroller board
3) LED lights (Red, Green, and Yellow- each 2)
4) Six 100 resistors and one 10 k resistor
5) One tilt switch
6) Jumper wires
Circuit Diagram:
Fig:1
Arduino board
3
Code/Program:
4
// Initialize LED pins (2 to 7)
for (int x = 2; x < 8; x++) {
pinMode(x, OUTPUT); }
pinMode(SwitchPin, INPUT_PULLUP); // Switch pin as input with pull-up resistor}
void loop() {
Serial.println(cnt); // Print the counter to serial monitor for debugging
unsigned long CurrentTime = millis();
// Execute the LED behavior based on the counter value
if (CurrentTime - PreviousTime > interval) {
PreviousTime = CurrentTime;
// If counter is even, call type_2 (LED forward), else call type_1 (LED reverse)
if (cnt % 2) {
type_1(); } else {
type_2(); } }
// Read switch state
SwitchState = digitalRead(SwitchPin);
// Check for a button press (falling edge detection)
if (SwitchState != PrevSwitchState) {
// Simple debounce: delay for 50ms to avoid multiple reads
delay(50);
// If the button is pressed (switch is LOW in active-low configuration)
if (SwitchState == LOW) {
cnt++; // Toggle the counter on a button press (flip the LED pattern)
5
Simulation Result:
Experimental Output:
a.
b.
7
c.
Discussion:
In this experiment, we explored the utility of the millis() function in Arduino for constructing a digital timer.
Our findings underscored the remarkable accuracy and precision achieved with millis(), vital for tasks
requiring precise timing. Moreover, its non-blocking nature enables concurrent execution of other program
tasks, enhancing overall efficiency. We addressed potential overflow issues through effective handling
mechanisms, ensuring uninterrupted timer operation over extended periods. Practical applications span
various domains, including photography, automation, and IoT projects. Future improvements could focus on
integrating advanced features like interrupts for enhanced functionality and exploring power optimization
strategies. In summary, our study demonstrates the efficacy of millis() in facilitating precise, non-blocking
digital timers, empowering Arduino projects with versatile timing capabilities.
References:
1. https://www.arduino.cc/.
2. https://www.coursera.org/learn/arduino/lecture/ei4ni/1-10-first-glance-at-a-program
3. Jeremy Blue; Exploring Arduino: Tools and Techniques for Engineering Wizardr
4. https://internetofthingsagenda.techtarget.com/
5. AIUB Microprocessor and Embedded Systems Lab Manual 3