0% found this document useful (0 votes)
9 views

Lab Manual MES Experiment 4 N

The document is an experiment manual for a digital timer project using an Arduino. The objectives are to: 1. Study the millis() function, which keeps track of time in milliseconds since the Arduino started running. 2. Build a digital timer that turns on LEDs sequentially every minute using millis() to check if the interval has passed. 3. Implement a sequential LED pattern controlled by a tilt switch, with the pattern resetting when the switch changes position. The experiment involves connecting 6 LEDs and a tilt switch to an Arduino, and programming the Arduino using millis() to light the LEDs sequentially every minute until all 6 LEDs have lit, at which

Uploaded by

Kazi Al - Kabid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab Manual MES Experiment 4 N

The document is an experiment manual for a digital timer project using an Arduino. The objectives are to: 1. Study the millis() function, which keeps track of time in milliseconds since the Arduino started running. 2. Build a digital timer that turns on LEDs sequentially every minute using millis() to check if the interval has passed. 3. Implement a sequential LED pattern controlled by a tilt switch, with the pattern resetting when the switch changes position. The experiment involves connecting 6 LEDs and a tilt switch to an Arduino, and programming the Arduino using millis() to light the LEDs sequentially every minute until all 6 LEDs have lit, at which

Uploaded by

Kazi Al - Kabid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 3 Lab Manual

American International University- Bangladesh


Department of Electrical and Electronic Engineering
EEE 4103: Microprocessor and Embedded Systems Laboratory

Title: Study of a Digital Timer using the millis() function of Arduino.

Introduction:

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.

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
the delay() function, it freezes its current state for the entire duration of the delay. That means,
there can be no other input or output while it is 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 (since the MSB is used for a signed bit, so number ranges
are between – 215 and 215 – 1). Those may be some large numbers, but if the Arduino is
counting 1000 times a second with millis(), you would run out of space in less than a minute.
The long data type holds a 32-bit number (between -2,147,483,648 and 2,147,483,647). Since
you can’t run time backward, 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 numbers. An unsigned long number can count up to 4,294,967,295.
That is enough space for milis() function to store time for almost 50 days. By comparing the
current millis() function to a specific value, you can see if a certain amount of time has passed.

When you turn your Digital Timer over, a tilde switch will change its state, and that will set
off another cycle of LEDs turning on. The Tilde 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.

Experimental Setup and Procedures:

1. Connect power and ground to your breadboard.


2. Connect the anode (longer leg) of six LEDs to digital pins 2-7 and connect the LEDs
to the ground through 100  resistors.
3. Connect one lead of the tilt switch to +5 V. Connect the other lead to a 10 k resistor
to the ground and connect the junction where they meet to digital pin 8.

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 1


Experiment 3 Lab Manual
Experimental Setup:

1) Connect power and ground to your


breadboard
2) Connect the anode (longer leg) of six
LEDs to six digital pins 2-7 and
connect cathodes of all LEDs to the
ground through six 100  resistors.
3) Connect one lead of the tilt switch to
the +5 V and connect the other
terminal of the tilt switch to the
ground through a 10 k resistor.
Connect the junction of the switch
and resistor to the digital input pin 8.

Fig. 1 Experimental Setup of an LED Light Pattern Control System using a Tilde 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

Experimental Procedure:
The main task of this experiment is to implement an LED light pattern control system using a
tilt switch. Connect the circuits as per the diagram of Fig. 1. Then plug in the Arduino
microcontroller board to the PC.

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 2


Experiment 3 Lab Manual
Using Arduino IDE to write code:
1. Open the Arduino Uno IDE 2.0.1 (version may have been updated automatically on your
computer) and a blank sketch will open. The window as in Fig. 2 will come up on your PC:

Fig. 2 IDE Environment (text editor)

2. Now, write the program by following the code writing steps in sequences given below on
the blank sketch for the LED pattern control.
a) Declare a named constant:
You need several global variables in your program to get this all working. To start,
create a variable named SwitchPin where the tilt switch would be connected.
b) Create a variable to hold the time
Create a variable of name unsigned long to hold the time of the LED that was changed
last time.
c) Declare variable names for the inputs and outputs
Create two variables for the current and previous switch states to compare the switch
positions from one loop to the next. Create another variable named led to store the pin
numbers of the LEDs to be stored to determine which one to be turned on next time.
Start out with pin 2.
d) Declare a variable describing the interval between events
The last variable you need to create is the interval between each LED turning on. This
will be a long data type. In 10 minutes (i.e., the time between each LED turning on),
6,00,000 milliseconds (1 minute = 60 seconds and 1 second = 1000 milliseconds) pass.
If you want the delay between lights to be longer or shorter, this is the number you
change.
e) Set the direction of your digital pins
In the void setup() function, you need to declare the LED pins 2-7 as the output pins.
A for loop declares all six pins as OUTPUT (so that the microcontroller can send
signals to these pins) with just 3 lines of code. You also need to declare SwitchPin as
an INPUT (so that the microcontroller can receive a signal from this pin).
f) Check the time since the program started running
When the void loop() starts, you are going to get the amount of time the Arduino has
been running with millis() and store it in a local variable named CurrentTime.
g) Evaluate the amount of time that has passed since the previous loop()
Using an if() statement, you’ll check to see if enough time has passed to turn on an
LED. Subtract the CurrentTime from the PreviousTime (initialized as 0 for the first time
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 3
Experiment 3 Lab Manual
during declaration) and check to see if it is greater than the interval variable. If 6,00,00
milliseconds have passed (one minute), you’ll set the variable PreviousTime to the
value of CurrentTime.
h) Turn on an LED, prepare for the next one
PreviousTime indicates the last time an LED was turned on. Once you have set
PreviousTime, turn on the LED, and increment the led variable, for example from 2 to
3. The next time you pass the time interval, the next LED will light up.
i) Check to see if all lights are on
Add one more if statement in the program to check whether the LED connected to pin
7 (next pin) is turned on or not. was turned on. However, don’t do anything with this
yet, wait for the end of an hour (set time) to decide.
j) Read the value of the switch
After checking the time, you should check the switch state by reading it and storing the
value in the SwitchState variable.
k) Reset the variables to their defaults if necessary
With an if statement, check the position or status of the switch. The != evaluation checks
to see if SwitchState variable doesn’t equal the PrevSwitchState variable. If they are
different, turn the LEDs off, return the led variable to the first pin, and reset the timer
for the LEDs by setting PreviousTime to the value of CurrentTime.
l) Set the current state to the previous state
At the end of the void loop() function, save the value of the SwitchState variable in the
value of the PrevSwitchState variable so that you can compare it to the value you get
for the SwitchState variable in the next void loop().

Code of a LED light pattern control system with millis() function:

const int SwitchPin = 8;


unsigned long PreviousTime = 0;
int SwitchState = 0;
int PrevSwitchState = 0;
int led = 2;
long interval = 60000; // 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++;
if (led == 7){
}
}
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 4
Experiment 3 Lab Manual
SwitchState = digitalRead(switchPin);
if (SwitchState != PrevSwitchState){
for (int x = 2 ; x < 8; x++) {
digitalWrite(x, LOW);
}
led = 2;
PreviousTime = CurrentTime;
}
PrevSwitchState = SwitchState;
}

3. After writing the program you must save your code file in a folder. For this purpose, please
go to the File Menu → Save As → give a File name in the file name box (for example,
Traffic_light_control) → Select Save.
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.
4. 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 ‘compilation done’ message will be
displayed, otherwise, an error message will be displayed.
5. 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.
6. 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.

Questions for report writing:

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 six
digits from your ID (if your ID is XY-PQABC-Z then consider P s delay for the 1st LED
and Z s delay for the last 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/

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 5

You might also like