Programming Logic and Design Workbook

Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

President Ramon Magsaysay State University

Iba, Zambales, Philippines


Tel/Fax No.: (047) 811-1683

College of Engineering

PROGRAMMING USING ARDUINO


LABORATORY WORKBOOK

COURSE CODE:
CpE 122
COURSE DESCRIPTION:
Programming Logic and Design

Dionisio M. Martin Jr., CpE


Instructor
IMPORTANT REMINDER

Diagram shows in each activity is the guided wiring connection to realize the
output of activities. Component required may differ in the technology used, it is
advised to be guided in every activity with appropriate data sheets for proper pin
layout and connections.
The accomplishment is based on the Performance Observation Checklist.
The activity is strictly not permitted to used 220-volt directly from the power outlet to
energized the ICs. Used necessary power converter or adapter that is regulated to
COURSE LABORATORY WORKBOOK

energized the circuit requiring different voltage value. Only power source coming
from the mobile device or laptop are only allowed to use for Arduino board.

Intended Learning Outcomes

Upon completing this workbook, student should be able to:


A. Understand the importance of programming and hardware in a modern
technology.
B. Understand the concept and application of programming with Arduino Uno
board in an intelligence system and embedded technology.
C. Modify and construct the activity in their own knowledge and strategy.
D. Develop his/her skills in constructing electronic circuit requirement in each
activity.

1
Table of Activities

No. Title

1 Basic Output
2 Basic Input
3 Control Structure
4 Loop Structure
COURSE LABORATORY WORKBOOK

5 Procedure and Label


6 Data Types and Array
7 Multiple Array
8 Motor Programming
9 Ultrasonic Programming
10 LED Matrix Programming

Appendices

A Using Arduino IDE


B Using ArduinoDroid IDE
C Arduino Uno

2
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 01
BASIC OUTPUT
LEARNING OBJECTIVES

The student should be able to:


 understand the pinMode function and digitalWrite function as primary instruction for
COURSE LABORATORY WORKBOOK

basic output of Atmega controller.


 identify the function pin of Atmega controller as an output pin.
 perform the output function of Atmega pin using LEDs.
 utilize in more complex application using LEDs.

COMPONENTS REQUIRED

 Arduino Uno with USB cable


 8 pcs. - LED (Red)
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

pinMode
Configures the specified pin to behave either as an input or an output.
Syntax:
pinMode(pin, mode);
Parameter:
pin – the number of the pin on Arduino Uno to use
mode – INPUT, OUTPUT or INPUT_PULLUP
Sample usage:
The code makes the digital pin 13 OUTPUT.
vo i d se t u p () {
p i n Mo d e( 1 3 , O UT PUT ) ; / / se t s t h e dig it al pi n 1 3 a s o u tp u t
}

digitalWrite
Write a HIGH or a LOW value to a digital pin.
Syntax:
digitalWrite(pin, value);
Parameter:
pin – the number of the pin on Arduino Uno to use
value – HIGH or LOW

3
Sample usage:
The code makes the digital pin 13 toggles between HIGH and LOW.
vo i d l oo p () {
d i gi talW rit e ( 13 , HI G H) ; // se t s t h e d igi t al pi n 13 O N
d e la y( 1 00 0 ) ; // wa i t s f o r a se co n d
d i gi talW rit e ( 13 , L OW ) ; // se t s t h e d igi t al pi n 13 O FF
d e la y( 1 00 0 ) ; // wa i t s f o r a se co n d
}

SAMPLE PROGRAM: Blinking an LED


This will turn on and off an LED connected at digital pin 13 for durations of 1 second.
COURSE LABORATORY WORKBOOK

Code:
vo i d se t u p () {
p i n Mo d e( 1 3 , O UT PUT ) ; / / se t s t h e dig it al pi n 1 3 a s o u tp u t
}

vo i d l oo p () {
d i gi talW rit e ( 13 , HI G H) ; // se t s t h e d igi t al pi n 13 O N
d e la y( 1 00 0 ) ; // wa i t s f o r a se co n d
d i gi talW rit e ( 13 , L OW ) ; // se t s t h e d igi t al pi n 13 O FF
d e la y( 1 00 0 ) ; // wa i t s f o r a se co n d
}

WIRING DIAGRAM:

PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the LEDs needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno.

4
ACTIVITIES

ACTIVITY 1.1
Make a circuit connection of Arduino and LEDs that will move the light the LEDs from
left to right.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
COURSE LABORATORY WORKBOOK

same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LED as required
from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended LED?
5. get a consistent output/result?

ACTIVITY 1.2
Make a circuit connection of Arduino and LEDs that will perform the following
sequence of light continuously:
10101010
01010101
00001111
11110000
11001100
00110011

5
PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
COURSE LABORATORY WORKBOOK

downloaded programs on the Arduino Uno.


7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LED as required
from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. digital Pin of Arduino is properly connected to the
intended LED?
c. displays the required output continuously?
5. get a consistent output/result?

6
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 02
BASIC INPUT
LEARNING OBJECTIVES

The student should be able to:


 understand the digitalRead function as primary instruction for basic input of
COURSE LABORATORY WORKBOOK

Atmega controller.
 understand the conditional statement using If statement in identifying input on
Arduino Uno.
 identify the function pin of Atmega controller as an input pin.
 perform the input function of Atmega pin using simple contact switch.
 utilize switches and LEDs in more complex application.

COMPONENTS REQUIRED

 Arduino Uno with USB cable


 8 pcs. – LED (Red)
 1 pc. – DIP switch (8 position)
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

digitalRead
Read the value from a specified digital pin and the result is either HIGH or LOW.
Syntax:
digitalRead(pin);
Parameter:
pin – the number of the digital pin wanted to read on Arduino Uno
Sample usage:
Sets pin 13 to the same value as pin 7, declared as an input.
i n t val = 0 ;

vo i d se t u p () {
p i n Mo d e( 7 , I NPUT) ; / / se t s t h e dig it al pi n 7 a s i n p u t
p i n Mo d e( 1 3 , O UT PUT ) ; / / se t s t h e dig it al pi n 1 3 a s o u tp u t
}

vo i d l oo p () {
va l = d ig it al Re a d (7 ) ; / / r e a d th e i n p ut val u e i n di gi t al pi n 7
d i gi talW rit e ( 13 , val ) ; / / se t s t h e d igi t al pi n 13 val u e
}

7
== (Comparison Operator)
Compares the variable on the left with the value or variable on the right of the
operator. Returns true when the two operands are equal. The opposite operator of ==
(equal to) is != (not equal to). Other comparison operators are < (less than), <= (less
than or equal to), > (greater than), and >= (greater than or equal to).
Syntax:
x == y; //is true if x is equal to y and it is false if x is not equal to y
Parameter:
x – variable
COURSE LABORATORY WORKBOOK

y – variable or constant
Sample usage:
To tests if x is equal to y.
i f ( x==y) {
/ / d o so me t h i n g o nl y i f t h e co mp a r i so n r e sul t i s tr u e
}

SAMPLE PROGRAM: Blinking an LED when switch is ON


This will turn on and off an LED connected at digital pin 13 for durations of 1 second
when the switch is ON at digital pin 7.
Code:
i n t val = 0 ;

vo i d se t u p () {
p i n Mo d e( 7 , I NPUT _ PUL L UP ) ; / / se t s t h e dig it al pi n 7 a s i n p u t
p i n Mo d e( 1 3 , O UT PUT ) ; / / se t s t h e dig it al pi n 1 3 a s o u tp u t
}

vo i d l oo p () {
va l = d ig it al Re a d (7 ) ; // r e a d th e i n p ut val u e i n di gi t al pi n 7
i f( val == L OW ){ // t e st i s val i s e qu al t o LOW
di gi t alW ri t e (1 3 , HI G H); // se t s t h e d igi t al pi n 13 O N
d el a y( 10 0 0 ) ; // wa i t s f o r a se co n d
di gi t alW ri t e (1 3 , L OW ); // se t s t h e d igi t al pi n 13 O FF
d el a y( 10 0 0 ) ; // wa i t s f o r a se co n d
}
d i gi talW rit e ( 13 , L OW ) ; / / i f pi n 7 i s HI G H th e o u tp u t a t
} / / d ig it al pi n 13 i s al wa ys O F F

WIRING DIAGRAM:

8
PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the LED and DIP switch needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
COURSE LABORATORY WORKBOOK

6. Download the program to the Arduino Uno.


7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.

ACTIVITIES

ACTIVITY 2.1
Make a circuit connection of Arduino and LEDs that will move the light the LEDs from
left to right when switch is ON.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LED as required
from the activity?
4. check the circuit if:

9
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended LED?
c. switch is ON, the required output is displayed?
d. switch is OFF, NO LEDs are lighting?
5. get a consistent output/result?

ACTIVITY 2.2
Make a circuit connection of Arduino and LEDs that will perform the following
COURSE LABORATORY WORKBOOK

sequence of light continuously based on the following setting of switches:


Switch No. is ON Output
10101010
1
01010101
00001111
2
11110000
11001100
3
00110011
10101010
01010101
00001111
4
11110000
11001100
00110011

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?

10
3. display the correct output to the LED as required
from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. digital Pin of Arduino is properly connected to the
intended LED?
c. displays the required output continuously?
d. switch 1 is ON, the required output is displayed?
e. switch 2 is ON, the required output is displayed?
f. switch 3 is ON, the required output is displayed?
COURSE LABORATORY WORKBOOK

g. switch 4 is ON, the required output is displayed?


h. all switches is OFF, NO LEDs are lighting?
5. get a consistent output/result?

11
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 03
CONTROL STRUCTURE
LEARNING OBJECTIVES

The student should be able to:


 understand the if and switch control structures and its application.
COURSE LABORATORY WORKBOOK

 identify the function pin of Atmega controller as an input pin.


 perform the input function of Atmega pin using simple contact switch.
 utilize switches and LEDs in more complex application.

COMPONENTS REQUIRED
 Arduino Uno with USB cable
 1 pc. – seven segment display (common cathode)
 7 pcs. – 220-ohms resistor (1/4 W)
 1 pc. – DIP switch (8 position)
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

if
The if statement checks for a condition and executes the proceeding statement or set
of statements if the condition is 'true'
Syntax:
if (condition) {
//statements;
}
or:
if (condition) { statement; }
or:
if (condition)
statement;
or:
if (condition) statement;
Parameter:
condition – a Boolean expression, i.e., can be true or false
Sample usage:
To tests if x is equal to y.
i f ( x==y) {
/ / d o so me t h i n g o nl y i f t h e co mp a r i so n r e sul t i s tr u e
}

12
if…else
The if…else allows greater control over the flow of code by allowing multiple tests to
be grouped together. An else clause (if at all exists) will be executed if the condition in
the if statement results in false. The else can proceed another if test.
Syntax:
if (condition1) {
//statement(s);
}
else if (condition2) {
//statement(s);
COURSE LABORATORY WORKBOOK

}
else {
//statement(s);
}

switch…case
Like if statements, switch case controls the flow of programs by allowing programmers
to specify different code that should be executed in various conditions. When a case
statement is found whose value matches that of the variable, the code in that case
statement is run.
The break keyword exits the switch statement, and is typically used at the end of each
case. Without a break statement, the switch statement will continue executing the
following expressions ("falling-through") until a break, or the end of the switch
statement is reached.
Syntax:
switch (var) {
case label1:
//statement(s);
break;
case label2:
//statement(s);
break;
default:
//statement(s);
break;
}
Parameter:
var – a variable whose value to compare with various cases
label – constant value
Sample usage:
To tests if x is equal to possible value.
swi t ch ( x) {
ca se 1 :
/ / d o so me t h i n g wh e n x e q u al s 1
b r e a k;
ca se 2 :
/ / d o so me t h i n g wh e n x e q u al s 2
b r e a k;
d e f a ul t :

13
/ / d o so me t h i n g i f n o t hi ng ma t ch e s
/ / d e f a ul t i s o p ti o n al
b r e a k;
}

SAMPLE PROGRAM: Decimal Number Display


This will display the decimal number “1” if the switch one is ON.
Code:
i n t val = 0 ;
COURSE LABORATORY WORKBOOK

vo i d se t u p () {
p i n Mo d e( 2 , O UT PUT ) ; // se t s the d igi t al pi n 2 as o u t pu t
p i n Mo d e( 3 , O UT PUT ) ; // se t s the d igi t al pi n 3 as o u t pu t
p i n Mo d e( 4 , O UT PUT ) ; // se t s the d igi t al pi n 4 as o u t pu t
p i n Mo d e( 5 , O UT PUT ) ; // se t s the d igi t al pi n 5 as o u t pu t
p i n Mo d e( 6 , O UT PUT ) ; // se t s the d igi t al pi n 6 as o u t pu t
p i n Mo d e( 7 , O UT PUT ) ; // se t s the d igi t al pi n 7 as o u t pu t
p i n Mo d e( 8 , O UT PUT ) ; // se t s the d igi t al pi n 8 as o u t pu t
p i n Mo d e( 9 , I NPUT _ PUL L UP ) ; // se t s the d igi t al pi n 8 as input
}

vo i d l oo p () {
i f ( val = di gi t al Re ad ( 9 ) { / / r e a d th e i n p ut val u e i n di gi t al pi n 9
di gi t alW ri t e (2 , L OW ) ; / / i f e q u al , “ 1 ” will di spl a y
di gi t alW ri t e (3 , HI G H) ;
di gi t alW ri t e (4 , HI G H) ;
di gi t alW ri t e (5 , L OW ) ;
di gi t alW ri t e (6 , L OW ) ;
di gi t alW ri t e (7 , L OW ) ;
di gi t alW ri t e (8 , L OW ) ;
}
e l se {
di gi t alW ri t e (2 , L OW ) ; / / e l se , n o li g ht
di gi t alW ri t e (3 , L OW ) ;
di gi t alW ri t e (4 , L OW ) ;
di gi t alW ri t e (5 , L OW ) ;
di gi t alW ri t e (6 , L OW ) ;
di gi t alW ri t e (7 , L OW ) ;
di gi t alW ri t e (8 , L OW ) ;
}
}

WIRING DIAGRAM:

Arduino 7 Segment
pins pins
6 1 (e)
5 2 (d)
3 and 8
GND
(COM)
4 4 (c)
- 5 (dp)
3 6 (b)
2 7 (a)
7 9 (f)
8 10 (g)

14
PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the seven segment display and DIP switch needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
6. Download the program to the Arduino Uno.
COURSE LABORATORY WORKBOOK

7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.

ACTIVITIES

ACTIVITY 3.1
Make a circuit connection of Arduino and seven segment that will display 1 to 9
continuously when switch 1 is ON.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LED as required
from the activity?
4. check the circuit if:
c. supplied by the correct voltage?

15
d. Digital Pin of Arduino is properly connected to
the intended pin of seven segment display?
e. switch is ON, the required output is displayed?
f. switch is OFF, NO LEDs are lighting?
5. get a consistent output/result?

ACTIVITY 3.2
Make a circuit connection of Arduino and seven segment that will display the
following decimal number based on the following setting of switches:
COURSE LABORATORY WORKBOOK

Switch No. is ON Output in Decimal


1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LED as required
from the activity?
4. check the circuit if:
a. supplied by the correct voltage?

16
b. digital Pin of Arduino is properly connected to the
intended seven segment display?
c. displays the required output according to the
number of switch?
d. all switches is OFF, NO LEDs are lighting?
5. get a consistent output/result?
COURSE LABORATORY WORKBOOK

17
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 04
LOOP STRUCTURE
LEARNING OBJECTIVES

The student should be able to:


 understand the different loop structures and its application.
COURSE LABORATORY WORKBOOK

 identify the function pin of Atmega controller as an input pin.


 perform the output function of Atmega pin using different examples of loop
application.

COMPONENTS REQUIRED

 Atmega 2560 with USB cable


 2 pcs. – seven segment display (common cathode)
 14 pcs. – 220-ohms resistor (1/4 W)
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

for
The for statement is used to repeat a block of statements enclosed in curly braces. An
increment counter is usually used to increment and terminate the loop.
The for statement is useful for any repetitive operation.
Syntax:
for ( initialization ; condition ; increment ) {
//statements;
}
Parameter:
initialization – the initial value of variable to be incremented
condition – a Boolean expression, i.e., can be true or false
increment – variable will be incremented or plus one from the current value
Sample usage:
It will increment the variable x from 1 to 10.
f o r ( x=1 ; x<=1 0 ; x++) {
/ / d o so me t h i n g o nl y i f t h e co mp a r i so n r e sul t i s tr u e
}

while
A while loop will loop continuously, and infinitely, until the expression inside the
parenthesis, () becomes false. Something must change the tested variable, or the

18
while loop will never exit. This could be in your code, such as an incremented variable,
or an external condition.
Syntax:
while (condition) {
//statement(s);
}
Parameter:
condition –a Boolean expression that evaluates to true or false
Sample usage:
COURSE LABORATORY WORKBOOK

To make a repetitive operation 100 times.


va r =0 ;
wh i l e ( va r <1 0 0) {
/ / d o so me t h i n g r e p e ti ti ve 1 0 0 ti me s
va r ++;
}

do…while
The do…while loop works in the same manner as the while loop, with the exception
that the condition is tested at the end of the loop, so the do loop will always run at
least once.
Syntax:
do {
//statement(s);
} while (condition);
Parameter:
condition –a Boolean expression that evaluates to true or false
Sample usage:
To make a repetitive operation 100 times.
va r =0 ;
do {
/ / d o so me t h i n g r e p e ti ti ve 1 0 0 ti me s
va r ++;
} wh i l e( va r <10 0 ) ;

SAMPLE PROGRAM: Display Decimal 1 and Decimal 2 Alternately


This will display decimal 1 and 2 alternately.
Code:
i n t cou n t = 1 ;

vo i d se t u p () {
f o r (i nt i =2 ; i <9 ; i ++) {
pi n Mo d e (i , O UT PUT ) ; / / se t s t h e dig it al pi n 2 to pi n 8 a s o u tp u t
}
}

vo i d l oo p () {
swi t ch ( co u n t) { / / t a ke t h e val u e o f co u n t

19
ca se 1 : / / i f co u n t eq u al t o 1 , “ 1” wi ll di spl a y
di gi t alW ri t e (2 , L OW ) ;
di gi t alW ri t e (3 , HI G H) ;
di gi t alW ri t e (4 , HI G H) ;
di gi t alW ri t e (5 , L OW ) ;
di gi t alW ri t e (6 , L OW ) ;
di gi t alW ri t e (7 , L OW ) ;
di gi t alW ri t e (8 , L OW ) ;
b r e a k;
ca se 2 : / / i f co u n t eq u al t o 2 , “ 2” wi ll di spl a y
di gi t alW ri t e (2 , HI G H) ;
di gi t alW ri t e (3 , HI G H) ;
COURSE LABORATORY WORKBOOK

di gi t alW ri t e (4 , L OW ) ;
di gi t alW ri t e (5 , HI G H) ;
di gi t alW ri t e (6 , HI G H) ;
di gi t alW ri t e (7 , L OW ) ;
di gi t alW ri t e (8 , HI G H) ;
b r e a k;
d e f a ul t : / / e l se , n o li g ht
in t x=2 ; / / u sin g d o wh il e lo o p
do {
di gi t alW ri t e( x, L OW ) ;
x++;
} wh il e ( x<9 ) ;
b r e a k;
}
co u n t ++;
i f( co u n t ==3 ) co u n t =1 ;
}

WIRING DIAGRAM:

Arduino 7 Segment
pins pins
6 1 (e)
5 2 (d)
3 and 8
GND
(COM)
4 4 (c)
- 5 (dp)
3 6 (b)
2 7 (a)
7 9 (f)
8 10 (g)

PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the seven segment display and resistor needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.

20
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.

ACTIVITIES

ACTIVITY 4.1
COURSE LABORATORY WORKBOOK

Make a circuit connection of Arduino and seven segment display that will count from
0 to 9 repeatedly.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the seven segment
display as required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended segment of display?
c. displays the required output continuously?
d. the output starts from zero?
e. the output able to display up to nine?
5. get a consistent output/result?

21
ACTIVITY 4.2
Make a circuit connection of Arduino and seven segment display that will count from
0 to 99 repeatedly.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
COURSE LABORATORY WORKBOOK

4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LED as required
from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended segment of display?
c. displays the required output continuously?
d. the output starts from zero?
e. the output able to display two-digit number?
5. get a consistent output/result?

22
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 05
PROCEDURE AND LABEL
LEARNING OBJECTIVES

The student should be able to:


 understand the usage of function and label.
COURSE LABORATORY WORKBOOK

 identify the difference of procedure and label in Arduino programming.


 use appropriate purpose of procedure and label.
 perform the procedure and label using seven segment for displaying character and
for more other complex application.

COMPONENTS REQUIRED
 Atmega Uno with USB cable
 1 pc. – seven segment display (common cathode)
 8 pcs. – 220-ohms resistor (1/4 W)
 1 pc. – DIP switch (8 position)
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

Functions
Segmenting code into functions allows a programmer to create modular pieces of
code that perform a defined task and then return to the area of code from which the
function was “called.” The typical case for creating a function is when one needs to
perform the same action multiple times in a program.
Structure:
v o id se tup () {
/ / m o re state m en ts .. .
Dash e dLin e ();
/ / m o re state m en ts .. .
}

v o id l o o p() {
}

v o id Dash e dL in e () {
/ / m o re state m en ts .. .
}
Sample usage:
Multiplying two numbers using function parameter.

23
vo i d se t u p () {
Se r i al .b e gi n( 9 6 0 0) ;
}

vo i d l oo p () {
int i = 2;
int j = 3;
i n t k;
k = my Mu l t i pl yF u n cti o n(i , j) ; / / k n o w co n t a in s 6
se ri al . p ri nt ln ( k) ;
d e la y( 5 00 ) ;
}
COURSE LABORATORY WORKBOOK

i n t myMu l t i pl yF u n cti o n(i n t x, i n t y) {


i n t r e sul t ;
r e sul t = x * y;
r e t u rn r e sul t;
}

goto
Transfers program flow to a labeled point in the program.
Structure:
goto label; // sends program flow to the label

label:
Sample usage:
f o r ( b yte r = 0 ; r < 2 5 5; r ++) {
f o r (b yt e g = 25 5 ; g > 0 ; g - -) {
f o r ( b yt e b = 0 ; b < 2 55 ; b ++) {
i f ( a nal o g Re a d( 0 ) > 2 5 0 ) {
g o to b ail o u t ;
}
/ / mo r e st a t e me n t s . . .
}
}
}

b a il ou t :
/ / mo r e st a t e me n t s . . .

continue
Skips the rest of the current iteration of a loop.
Sample usage:
The following code writes the value of 0 to 255 to the PWM pin, but skips the values
in the range of 41 to 119.
f o r ( x = 0 ; x <= 2 5 5 ; x ++) {
i f ( x > 4 0 && x < 1 2 0 ) { / / cr e a t e ju mp i n va lu e s
co n ti n u e;
}
a n al o gW ri t e( PW Mpi n , x) ;
d e la y( 5 0) ;
}

24
return
Terminate a function and function a value from a function to the calling function, if
desired.
Syntax:
return;
or
return value; // sends program flow to the label
Parameters:
value – any variable or constant type
COURSE LABORATORY WORKBOOK

Sample usage:
A function to compare a sensor input to a threshold
i n t che ckSe n so r( ) {
i f ( a n al og Re a d (0 ) > 4 0 0) {
r e t ur n 1 ;
}
e l se {
r e t ur n 0 ;
}
}
The return keyword is handy to test a section of code without having to "comment out"
large sections of possibly buggy code.
vo i d l oo p () {
/ / b rilli a n t cod e i d e a t o t e st h e r e
r e t u rn ;

/ / t h e re st o f a d ysf u n cti on al ske t ch h e r e


/ / t hi s co d e wi ll ne ve r be e xe cu t e d
}

SAMPLE PROGRAM: Counts from 5 down to 0


This will display decimal from 5 down to 0 once.
Code:
i n t cou n t = 1 ;

vo i d se t u p () {
f o r (i nt i =2 ; i <9 ; i ++) { / / se t s t h e dig it al pi n 2 to pi n 8 a s o u tp u t
pi n Mo d e (i , O UT PUT ) ;
}
f o r (i nt i =5 ; i >=0 ; i - -) {
n u mb e r ( i ) ;
}
}

vo i d l oo p () {
}

vo i d n u mb e r ( i n t co u n t) {
swi t ch ( co u n t) { / / t a ke t h e val u e o f co u n t
ca se 0 : / / i f co u n t eq u al t o 0 , “ 0” wi ll di spl a y
ze r o( ) ;

25
b r e a k;
ca se 1 : / / i f co u n t eq u al t o 1 , “ 1” wi ll di spl a y
o n e () ;
b r e a k;
ca se 2 : / / i f co u n t eq u al t o 2 , “ 2” wi ll di spl a y
t wo ( ) ;
b r e a k;
ca se 3 : / / i f co u n t eq u al t o 3 , “ 3” wi ll di spl a y
o n e () ;
b r e a k;
ca se 4 : / / i f co u n t eq u al t o 4 , “ 4” wi ll di spl a y
o n e () ;
COURSE LABORATORY WORKBOOK

b r e a k;
ca se 5 : / / i f co u n t eq u al t o 5 , “ 5” wi ll di spl a y
o n e () ;
b r e a k;
d e f a ul t : / / e l se , n o li g ht
in t x=2 ; / / u sin g d o wh il e lo o p
do {
di gi t alW ri t e( x, L OW ) ;
x++;
} wh il e ( x<9 ) ;
b r e a k;
}
r e t u rn ;
}

vo i d ze r o () {
d i gi talW rit e ( 2, HI G H) ;
d i gi talW rit e ( 3, HI G H) ;
d i gi talW rit e ( 4, HI G H) ;
d i gi talW rit e ( 5, HI G H) ;
d i gi talW rit e ( 6, HI G H) ;
d i gi talW rit e ( 7, HI G H) ;
d i gi tal W rit e ( 8, L OW ) ;
r e t u rn ;
}

vo i d o n e () {
d i gi talW rit e ( 2, L OW ) ;
d i gi talW rit e ( 3, HI G H) ;
d i gi talW rit e ( 4, HI G H) ;
d i gi talW rit e ( 5, L OW ) ;
d i gi talW rit e ( 6, L OW ) ;
d i gi talW rit e ( 7, L OW ) ;
d i gi talW rit e ( 8, L OW ) ;
r e t u rn ;
}

vo i d t wo ( ) {
d i gi talW rit e ( 2, HI G H) ;
d i gi talW rit e ( 3, HI G H) ;
d i gi talW rit e ( 4, L OW ) ;
d i gi talW rit e ( 5, HI G H) ;
d i gi talW rit e ( 6, HI G H) ;
d i gi talW rit e ( 7, L OW ) ;
d i gi talW rit e ( 8, HI G H) ;

26
r e t u rn ;
}

vo i d th r e e( ) {
d i gi talW rit e ( 2, HI G H) ;
d i gi talW rit e ( 3, HI G H) ;
d i gi talW rit e ( 4, HI G H) ;
d i gi talW rit e ( 5, HI G H) ;
d i gi talW rit e ( 6, L OW ) ;
d i gi talW rit e ( 7, L OW ) ;
d i gi talW rit e ( 8, HI G H) ;
r e t u rn ;
COURSE LABORATORY WORKBOOK

vo i d fo u r( ) {
d i gi talW rit e ( 2, L OW ) ;
d i gi talW rit e ( 3, HI G H) ;
d i gi talW rit e ( 4, HI G H) ;
d i gi talW rit e ( 5, L OW ) ;
d i gi talW rit e ( 6, L OW ) ;
d i gi talW rit e ( 7, HI G H) ;
d i gi talW rit e ( 8, HI G H) ;
r e t u rn ;
}

vo i d fi ve ( ) {
d i gi talW rit e ( 2, HI G H) ;
d i gi talW rit e ( 3, L OW ) ;
d i gi talW rit e ( 4, HI G H) ;
d i gi talW rit e ( 5, HI G H) ;
d i gi talW rit e ( 6, L OW ) ;
d i gi talW rit e ( 7, HI G H) ;
d i gi talW rit e ( 8, HI G H) ;
r e t u rn ;
}

WIRING DIAGRAM:

Arduino 7 Segment
pins pins
6 1 (e)
5 2 (d)
3 and 8
GND
(COM)
4 4 (c)
- 5 (dp)
3 6 (b)
2 7 (a)
7 9 (f)
8 10 (g)

PROCEDURE:
1. Write the program using any form of PC or a mobile phone.

27
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the seven segment display and resistors needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.
COURSE LABORATORY WORKBOOK

ACTIVITIES

ACTIVITY 5.1
Make a circuit connection of Arduino and seven segment display that will count from
0 to 9 repeatedly with 1 second delay using procedure/label.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the seven segment
display as required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended segment of display?
c. displays the required output continuously?

28
d. the output starts from zero?
e. the output able to display up to nine?
5. get a consistent output/result?

ACTIVITY 5.2
Make a circuit connection of Arduino and seven segment that will display the
following decimal number based on the following setting of switches using
procedure/label:
COURSE LABORATORY WORKBOOK

Switch No. is ON Output in Decimal


1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LED as required
from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. digital Pin of Arduino is properly connected to the
intended seven segment display?

29
c. displays the required output according to the
number of switch?
d. all switches is OFF, NO LEDs are lighting?
5. get a consistent output/result?
COURSE LABORATORY WORKBOOK

30
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 06
DATA TYPES AND ARRAY
LEARNING OBJECTIVES

The student should be able to:


 understand the different usage of variables and data types.
COURSE LABORATORY WORKBOOK

 identify the different function and commands in displaying messages into the LCD.
 use appropriate data types of variables in Arduino programming.
 perform the array function using LCD module for displaying text and for more other
complex application.

COMPONENTS REQUIRED
 Arduino Uno with USB cable
 1 pc. – 16x2 LCD Display (compatible with Hitachi HD44780 driver)
 1 pc. – 220-ohm resistor (1/4 W)
 1 pc. – 10k-ohm potentiometer
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

Variables
A way of naming and storing a value for later use by the program. Some variable
types are char, byte, int, unsigned int, long, unsigned long, float, double, string and
also array.
Another important choice is where to declare variables. The specific place that
variables are declared influences how various functions in a program will see the
variable. This is called variable scope.
Sample usage:
 Declaring variable i n t i np u t Va ri a bl e1 ;
 Initializing variable i n t cali br a ti o n Val = 17 ;
 Variable rollover x = x – 1;
t o t a l = t o t al + n [ i ] ;
 Using variable i n p ut Va ri a bl e = 7 ;
i n p ut Va ri a bl e = di gi tal Re a d (2 ) ;

Data Types
The type of a variable determines how much space it occupies in the storage and how
the bit pattern stored is interpreted.

31
The following are all the data types in Arduino programming:
 boolean – simple logical true/false
 byte – unsigned number from 0-255
 char – used to store a character value. Character literals are written in single
quotes: 'A' and for multiple characters – called strings - use double quotes: "ABC"
 unsigned char – same as 'byte'; if this is what you're after, you should use 'byte'
instead, for reasons of clarity
 word – unsigned number from 0-65535
 int – signed number from -32768 to 32767. This is primary data-type for number
COURSE LABORATORY WORKBOOK

storage
 unsigned int – the same as int in that they store a 2-byte value. Instead of storing
negative numbers however they only store positive values, yielding a useful
range of 0 to 65,535
 long – signed number from -2,147,483,648 to 2,147,483,647
 unsigned long – unsigned number from 0 to 4,294,967,295
 float – signed number from -3.4028235E38 to 3.4028235E38

array
Collection of variables that are accessed with an index number.
Syntax:
Declaring array
type arrayName [arraySize];
Accessing array
arrayName [index];
Parameter:
arraySize – the size allocated for memory
index – specifies the number of elements from the beginning
Sample usage:
int x [12]; / / i nt e g e r x i s a n ar r a y o f 1 2 i n te g e r s
i n t n [ 5 ] = { 3 2 , 2 7, 6 4 , 18 , 9 5 } / / i nt e g e r n wi t h a li st o f el e me n t co n t e n t s
n [2] = 100; / / se t el e me n t a t l o ca ti o n 2 t o 1 0 0

LiquidCrystal Library
This library allows an Arduino board to control LiquidCrystal displays (LCDs) based on
the Hitachi HD44780 (or a compatible) chipset, which is found on most text-
based LCDs. The library works with in either 4- or 8-bit mode (i.e. using 4 or 8 data lines
in addition to the rs, enable, and, optionally, the rw control lines).
To use this library:
# i n cl ud e <Li q ui d Cr yst al . h >

The following are the functions of this library:

Statement Description Usage


begin() needs to be called before any other l cd . be gi n ( col s,r o ws) ;
LCD library commands / / t h e n u mb e r o f

32
co l u mn s a n d r o ws t h a t
t h e di spl a y h a s
clear() clears the LCD screen and positions l cd . cl e ar () ;
the cursor in the upper-left corner
home() positions the cursor in the upper-left of l cd . ho me ( ) ;
the LCD
setCursor() use to reposition the cursor l cd . se t Cu r so r( col , ro w) ;
/ / t h e p o si ti on o f
cu r so r t o t h e di spla y
print() prints text to the LCD l cd . pri n t ( da t a ) ;
COURSE LABORATORY WORKBOOK

write() write character to the LCD l cd . wr i te ( d a ta ) ;

cursor() display the LCD cursor at the position l cd . cur so r( ) ;


to which the next character will be
written
noCursor() hides the LCD cursor l cd . no Cu r sor ( ) ;
blink() display the blinking LCD cursor l cd . bli n k( ) ;
noBlink() turns off the blinking LCD cursor l cd . no Bli n k( ) ;
display() turns on the LCD display and will l cd . di spla y( ) ;
restore the text (and cursor) that was
on the display
noDisplay() turns off the LCD display without losing l cd . no Di sp la y( ) ;
the text currently shown on it
scrollDisplayLeft() scrolls the contents of the display (text l cd . scr oll Di spla yL e f t( ) ;
and cursor) one space to the left. l cd . scr oll Di spla yRig h t () ;
Opposite is scrollDisplayRight().
autoscroll() turns on automatic scrolling of the LCD l cd . au t o scr oll ( ) ;
causing to push previous characters l cd . no Au t o scr oll () ;
over by one space. Opposite is
noAutoscroll() which turns off
automatics scrolling of the LCD.
leftToRight() set the direction for text written to the l cd .l e f tT o Ri g h t( ) ;
LCD to left-to-right, the default. l cd .r ig h t To L e f t( ) ;
Opposite is rightToLeft().

Sample usage:
co n st in t r s = 12 , e n = 11 , d 4 = 5, d 5 = 4 , d 6 = 3 , d7 = 2 ;
L i q uid Cr yst al l cd ( r s, e n, d 4 , d5 , d 6 , d7 ) ;

vo i d se t u p () {
l cd . b eg in ( 1 6 , 2 ) ; / / se t u p th e L CD' s n u mb e r o f co lu mn s a n d r o ws
l cd .p ri n t (" h ell o , wo rl d! ") ; / / Pr i nt a me ssa g e t o t h e L CD
}

vo i d l oo p () {
l cd .n o Bli n k( ) ; / / T u rn o f f t he bli n ki n g cur so r
d e la y( 1 00 0 ) ;
l cd .b lin k( ) ; / / T u rn o n t he bli n ki n g cur so r
d e la y( 1 00 0 ) ;
}

33
SAMPLE PROGRAM: Numbers incremented by 5
The program declares a 5-element integer array n.
Code:
co n st in t r s=1 2, e n =1 1 , d 4 =5 , d 5 =4, d6 =3 , d 7 =2
L i q uid Cr yst al l cd( r s, e n, d 4 , d5 , d 6 , d7 ) ;
i n t n[ 5 ] ; / / n i s a n ar r a y o f 5 i n t eg e r s
int x = 0;

vo i d se t u p () {
l cd .b e gi n (1 6 , 2) ; / / se t u p th e L CD' s n u mb e r o f co lu mn s a n d r o ws
COURSE LABORATORY WORKBOOK

f o r ( x = 0 ; x < 5 ; x++) {
n[ x ] = x * 5; / / st o ri n g 0 , 5 , 10 , 1 5 , 2 0
}
}

vo i d l oo p () {
l cd . se t Cu r sor ( 0 ,1 ) ;
f o r ( x = 0 ; x < 5 ; x++) {
l cd . p ri n t( n [ x ] ) ; / / d i sp la yi n g 0 , 5 , 1 0 , 1 5 , 2 0
}
d e la y( 1 00 0 ) ;
}

WIRING DIAGRAM:

Arduino LCD
pins pins
12 RS
11 Enable
5 D4
4 D5
3 D6
2 D7
Gnd R/W
Gnd VSS
5V VCC

PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the LCD, potentiometer and resistor needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.

34
ACTIVITIES

ACTIVITY 6.1
Make a circuit connection of Arduino and LCD module that will display the following
messages blinking:
WELCOME
P R M S U

PROCEDURE:
COURSE LABORATORY WORKBOOK

1. Recode the program based on the task requirement.


2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LCD display as
required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended pin of LCD display?
c. displays the required output continuously?
d. the characters displayed is visible?
5. get a consistent output/result?

ACTIVITY 6.2
Make a circuit connection of Arduino and LCD module that will count from 0 to 9
repeatedly with 1 second delay.

PROCEDURE:

35
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
COURSE LABORATORY WORKBOOK

7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw


the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LCD display as
required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended pin of LCD display?
c. displays the required output continuously?
d. the output starts from zero?
5. get a consistent output/result?

ACTIVITY 6.3
Make a circuit connection of Arduino and LCD module that will count from 5 down to
0 and display the following messages, and repeat this task continuously:
GROUP # - static on Line 1
NAME OF MEMBERS) - scrolling from right to left on Line 2

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.

36
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
COURSE LABORATORY WORKBOOK

successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LCD display as
required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended pin of LCD display?
c. displays the required output continuously?
d. the output starts from 5?
5. get a consistent output/result?

37
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 07
MULTIPLE ARRAY
LEARNING OBJECTIVES

The student should be able to:


 understand the usage of multiple array as data types.
COURSE LABORATORY WORKBOOK

 identify the different function and commands in displaying number into the seven
segment display using numeric keypad.
 use array data types of variables in Arduino programming.
 perform the array function in detection of number position and for more other
complex application.

COMPONENTS REQUIRED

 Arduino Uno with USB cable


 1 pc. – 4x3 numeric keypad
 4 pcs. – 220-ohms resistor (1/4 W)
 4 pc. – LED (Red)
 1 pc. – 16x2 LCD Display (compatible with Hitachi HD44780 driver)
 1 pc. – 10k-ohm potentiometer
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

Multi-Dimensional Array
Arrays with two dimensions (i.e., subscripts) often represent tables of values consisting
of information arranged in rows and columns.
Following are the key features of multidimensional arrays −
 To identify a particular table element, we must specify two subscripts.
 By convention, the first identifies the element’s row and the second identifies the
element’s column.
 Arrays that require two subscripts to identify a particular element are called two-
dimensional arrays or 2-D arrays.
 Arrays with two or more dimensions are known as multidimensional arrays and
can have more than two dimensions.
The following figure illustrates a two-dimensional array, a. The array contains three rows
and four columns, so it is a 3-by-4 array. In general, an array with m rows
and n columns is called an m-by-n array.

38
COURSE LABORATORY WORKBOOK

Every element in array a is identified by an element name of the form a[i][j]. Here, a is
the name of the array, and i and j are the subscripts that uniquely identify each
element in a. Notice that the names of the elements in row 0 all have a first subscript
of 0; the names of the elements in column 3 all have a second subscript of 3.
A multidimensional array can be initialized in its declaration much like a one-
dimensional array.
Sample usage:
co n st in t r o ws = 2 ;
co n st in t col u mn s = 3 ;
i n t ar r a y1 [ r o ws ] [ col u mn s ] = { { 1 , 2, 3 } , { 4 , 5 , 6 } } ;
i n t ar r a y2 [ r o ws ] [ col u mn s ] = { 1 , 2 , 3 , 4 , 5 };
i n t ar r a y3 [ r o ws ] [ col u mn s ] = { { 1 , 2 } , { 4 } } ;
And thus,
Values in array1 by row are:
1 2 3
4 5 6
Values in array2 by row are:
1 2 3
4 5 0
Values in array3 by row are:
1 2 0
4 0 0

Keypad programming
Matrix keypads are the kind of keypads you see on cell phones, calculators,
microwaves ovens, door locks, etc. They’re practically everywhere. In electronic
projects and programming, they are a great way to let users interact with your
hardware and are often needed to navigate menus, punch in passwords and control
robots.
Membrane keypads are made of a thin, flexible membrane material. They do come in
may sizes 4×3, 4×4, 4×1 etc. Regardless of their size, they all work in the same way.

39
COURSE LABORATORY WORKBOOK

In order to determine which key was pressed, we need to continuously scan rows &
columns. Fortunately, keypad.h was written to hide away this unnecessary complexity
so that we can issue simple commands to know which key was pressed.
To use this library:
# i n cl ud e <ke yp a d. h >
The constructor Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS ) takes five
parameters as follows:
 makeKeymap(keys) initializes the internal keymap to be equal to the user
defined keymap.
 rowPins and colPins are the arduino pins to which rows & columns of keypad
connected.
 ROWS and COLS are the number of rows & columns of the keypad.
There are a few useful functions you can use with Keypad object. Few of them are
listed below:
 getKey() – check which key is pressed, if any.
 char waitForKey() – waits forever until someone presses a key. Warning – It blocks
all other code until a key is pressed.
 KeyState getState() – returns the current state of any of the keys. The four states
are IDLE, PRESSED, RELEASED and HOLD.
 boolean keyStateChanged() – Let’s you know when the key has changed from
one state to another. For example, instead of just testing for a valid key you can
test for when a key was pressed.
 setHoldTime(unsigned int time) – sets the amount of milliseconds the user will
have to hold a button until the HOLD state is triggered.

40
 setDebounceTime(unsigned int time) – sets the amount of milliseconds the
keypad will wait until it accepts a new keypress/keyEvent.
 addEventListener(keypadEvent) – triggers an event if the keypad is used.
Sample usage:
# i n cl ud e <Ke yp a d . h >

co n st b yt e ROW S = 4 ; / / f o u r r o ws
co n st b yt e CO L S = 3 ; / / t h r e e co lu mn s
ch a r ke ys[ ROW S] [ CO L S] = {
{ ' 1' , ' 2' , '3 ' } ,
COURSE LABORATORY WORKBOOK

{ ' 4' , ' 5' , '6 ' } ,


{ ' 7' , ' 8' , '9 ' } ,
{ ' *' ,' 0 ' ,' # '}
};
b yt e r o wPi n s[ ROW S] = { 9 , 8 , 7 , 6 }; / /co n n e ct t o t h e ro w p i n o u t s o f t h e ke yp a d
b yt e col Pi n s[ CO L S] = { 5 , 4, 3 } ; / / co n n e ct t o t h e col u mn p i n ou t s o f th e ke yp a d

/ / Cr e a t e an o b je ct o f ke yp ad
Ke yp a d ke yp a d = Ke ypa d ( ma ke Ke y ma p ( ke ys ) , r o wPi n s, col Pi n s, ROW S, CO L S ) ;

vo i d se t u p () {
Se r i al .b e gi n( 9 6 0 0) ;
}

vo i d l oo p () {
ch a r ke y = ke yp a d . ge t Ke y( ) ; / / Re a d t h e ke y
i f ( ke y){ / / Pr i nt i f ke y pr e sse d
Se ri al . p ri n t( " Ke y Pr e ssed : " );
Se ri al . p ri n tln ( ke y);
}
}

SAMPLE PROGRAM: Pressing Odd Numbers to light LED


The program will turn “On” the LED if the pressed key in the numeric keypad is odd
numbers only.
Code:
co n st b yt e ROW S = 4 ; / / f o ur r o ws
co n st b yt e CO L S = 3 ; / / th r ee co lu mn s
ch a r ke ys[ ROW S] [ CO L S] = {
{ ‘1 ’, ’2 ’, ’3 ’} ,
{ ‘4 ’, ’5 ’, ’6 ’} ,
{ ‘7 ’, ’8 ’, ’9 ’} ,
{ ‘# ’, ’0 ’, ’*’}
};
b yt e r o wPi n s [ ROW S] = { 9 , 8 , 7 , 6} ;
b yt e col Pi n s [ CO L S] = { 1 2, 1 1 , 10 } ;

Ke yp a d kp d = Ke yp a d( ma ke Ke y ma p ( ke ys) , r o wPi n s, col Pi n s, ROW S, CO LS) ;

# d e fi n e l e dp in 1 3

vo i d se t u p () {
p i n Mo d e(l e d pi n , O UT PUT ) ; / / se t u p th e L CD' s n u mb e r o f co lu mn s a n d r o ws

41
d i gi talW rit e (l ed pi n , L OW );
Se r i al .b e gi n( 9 6 0 0) ;
}

vo i d l oo p () {
ch a r ke y = kpd . g e t Ke y() ;
i f ( ke y ) {
swi t ch ( ke y)
{
ca se ‘1 ’, ‘3 ’, ‘5 ’, ‘7 ’, ‘9 ’;
di gi t alW ri te (l e d pi n, HI G H) ;
b re a k;
COURSE LABORATORY WORKBOOK

d e f au lt :
di gi t alW ri te (l e d pi n, L OW ) ;
}
}

WIRING DIAGRAM:

PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the LCD, LEDs and resistors needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
6. Download the program to the Arduino Uno.

42
7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.

ACTIVITIES

ACTIVITY 7.1
Make a circuit connection of Arduino, LEDs and keypad that will display a binary
number on LED once it pressed on the corresponding decimal number only.
COURSE LABORATORY WORKBOOK

Example:
Pressed Number Expected Output in LEDs
0 NO LIGHT
1 0001
2 0101
⁞ ⁞
9 1001
Other keys, except decimal ALL LIGHT
Reminder: All LEDs are OFF as initial output.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended pin of Keypad display?
4. display the initial output correctly?

43
5. display the appropriate lighting of LEDs based on its
corresponding pressed number?
6. lighting all the LEDs when special characters are
pressed from the keypad?
7. get a consistent output/result?

ACTIVITY 7.2
Make a circuit connection of Arduino, LCD module and keypad that will display a
number or special character on LCD once pressed on keypad steadily. If no keys are
COURSE LABORATORY WORKBOOK

pressed, a “PRESS ANY KEY” message shall appear on the LCD screen.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LCD display as
required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended pin of LCD display?
5. display the message appropriately based on the
given condition?
6. display the number or special character once it
pressed steadily on the keypad?
7. get a consistent output/result?

44
ACTIVITY 7.3
Make a program for Arduino, LCD module and keypad that will allow the user to enter
the numbers consecutively. And clear the entered number/s when “#” key is press.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
COURSE LABORATORY WORKBOOK

4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the correct output to the LCD display as
required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. Digital Pin of Arduino is properly connected to
the intended pin of LCD display?
5. displays number, including repeated number,
correctly?
6. clear the screen once the “#” key is pressed?
7. get a consistent output/result?

45
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 08
MOTOR PROGRAMMING
LEARNING OBJECTIVES

The student should be able to:


 understand the programming of motor.
COURSE LABORATORY WORKBOOK

 identify the different function and commands in controlling different kinds of motors.
 use techniques in controlling speed and rotation direction of different kinds of
motors.
 perform motor control for more other complex application.

COMPONENTS REQUIRED
 Arduino Uno with USB cable
 Connecting wires (pref. AWG No.24)
 For DC motor requirements:
1 pc. – Small 6V DC Motor
1 pc. – PN2222 Transistor
1 pc. – 1N4001 diode
1 pc. – 270 Ω Resistor
 For servo motor requirements:
1 pc. – Servo Motor
1 pc. – ULN2003 driving IC (for more than two or more servo motors)
 For stepper motor requirements:
1 pc. – small bipolar stepper Motor
1 pc. – LM298 driving IC

DISCUSSION

Motor
There are three different type of motors −
a. DC Motor
A DC motor (Direct Current motor) is the most common type of motor. DC motors
normally have just two leads, one positive and one negative. If you connect
these two leads directly to a battery, the motor will rotate. If you switch the leads,
the motor will rotate in the opposite direction.

46
Warning: Do not drive the motor directly from Arduino board pins. This may damage
the board. Use a driver Circuit or an IC.
b. Servo Motor
A Servo Motor is a small device that has an output shaft. This shaft can be
positioned to specific angular positions by sending the servo a coded signal. As
long as the coded signal exists on the input line, the servo will maintain the
angular position of the shaft. If the coded signal changes, the angular position of
the shaft changes. In practice, servos are used in radio-controlled airplanes to
position control surfaces like the elevators and rudders. They are also used in
radio-controlled cars, puppets, and of course, robots.
COURSE LABORATORY WORKBOOK

c. Stepper Motor
A Stepper Motor or a step motor is a brushless, synchronous motor, which divides
a full rotation into a number of steps. Unlike a brushless DC motor, which rotates
continuously when a fixed DC voltage is applied to it, a step motor rotates in
discrete step angles.

Servo Library
To use this library
# i n cl ud e <Se r vo . h >
This library allows an Arduino board to control RC (hobby) servo motors. Servos have
integrated gears and a shaft that can be precisely controlled. Standard servos allow
the shaft to be positioned at various angles, usually between 0 and 180 degrees.
Continuous rotation servos allow the rotation of the shaft to be set to various speeds.
Functions are:
 attach() – attach the Servo variable to a pin. Note that in Arduino 0016 and
earlier, the Servo library supports only servos on only two pins: 9 and 10.
Syntax:
servo.attach(pin);
servo.attach(pin, min, max);
 write() - Writes a value to the servo, controlling the shaft accordingly. On a
standard servo, this will set the angle of the shaft (in degrees), moving the
shaft to that orientation. On a continuous rotation servo, this will set the speed

47
of the servo (with 0 being full-speed in one direction, 180 being full speed in
the other, and a value near 90 being no movement).
Syntax:
servo.write(angle);
 writeMicroseconds() – Writes a value in microseconds (uS) to the servo,
controlling the shaft accordingly. On a standard servo, this will set the angle of
the shaft. On standard servos a parameter value of 1000 is fully counter-
clockwise, 2000 is fully clockwise, and 1500 is in the middle.
Syntax:
COURSE LABORATORY WORKBOOK

servo.writeMicroseconds(uS);
 read() – Read the current angle of the servo (the value passed to the last call to
write()).
Syntax
servo.read();
 attached() – Check whether the Servo variable is attached to a pin.
Syntax
servo.attached();
 detach() – Detach the Servo variable from its pin. If all Servo variables are
detached, then pins 9 and 10 can be used for PWM output with
analogWrite().
Syntax
servo.detach();

SAMPLE PROGRAM: Sweeping the shaft of a Servo Motor


The program will sweep the shaft of a servo motor back and forth across 180 degrees.
Code:
# i n cl ud e <Se r vo . h >

Se r vo myse r vo ; / / cr e a t e se r vo o b je ct t o co n t r ol a se rvo

i n t po s = 0 ; / / va ri a bl e to st o re t h e ser vo po sit io n

vo i d se t u p () {
myse r vo . a t t a ch ( 9 ) ; / / a t t a ch e s th e se r vo o n pin 9 t o th e se r vo o b je ct
}

vo i d l oo p () {
f o r ( po s = 0 ; p o s <= 18 0 ; p o s += 1) { / / f r o m 0 d e g r e e t o 1 8 0 de g r ee s
myse r vo . wr i t e ( p o s) ; / / i n st e p s o f 1 d e gr e e
d el a y( 15 ) ;
}
f o r ( po s = 1 8 0 ; po s >= 0 ; p o s - = 1 ) { / / g o e s f ro m 1 8 0 d e g r e e s to 0 d eg r e e
myse r vo . wr i t e ( p o s) ;
d el a y( 15 ) ;
}
}

48
WIRING DIAGRAM:
COURSE LABORATORY WORKBOOK

PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno and connecting wires.
3. Prepare the motor needed.
4. Insert the wires needed for motor and Arduino. Follow the wiring diagram on the
Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to motor.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.

ACTIVITIES

ACTIVITY 8.1
Make a program for Arduino and servo motor that will rotate its shaft 360 degrees
clockwise and going back (counter clockwise) to its initial position. This will be
performed for 5 repetitions only, then stop.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno and connecting wires).
3. Make the necessary connection to accomplish the task.

49
4. Insert a piece of paper or place any indicator to the shaft (or any mark) to indicate
its initial position.
5. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to motor.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
8. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.
COURSE LABORATORY WORKBOOK

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. show the correct output based from the servo motor
shaft rotation as required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. required Pin of Arduino is properly connected to
the intended pin of servo motor?
5. show that it rotates clockwise 360 degrees from its
initial position?
6. show that the shaft rotates back (counter clockwise)
to its initial position?
7. show that the movements required performed only 5
times?
8. get a consistent output/result?

ACTIVITY 8.2
Make a program for Arduino and servo motor that will rotate its shaft clockwise
continuously but stops momentarily for about 5 seconds for every 45-degree angle
movement.

PROCEDURE:

1. Recode the program based on the task requirement.


2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Insert a piece of paper or place any indicator to the shaft (or any mark) to indicate
its orientation.

50
5. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to motor.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
8. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:


COURSE LABORATORY WORKBOOK

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. show the correct output based from the servo motor
shaft rotation as required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. required Pin of Arduino is properly connected to
the intended pin of servo motor?
5. shows that the servo motor shaft stopped for every
45 degrees clockwise movement?
6. show that the servo motor able to rotate its shaft
clockwise more than 180 degrees?
7. get a consistent output/result continuously?

51
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 09
ULTRASONIC PROGRAMMING
LEARNING OBJECTIVES

The student should be able to:


 understand the programming of ultrasonic sensors.
COURSE LABORATORY WORKBOOK

 identify the different function and commands in using ultrasonic sensors.


 use techniques in detecting objects using ultrasonic.
 perform object detection for more other complex application.

COMPONENTS REQUIRED

 Arduino Uno with USB cable


1 pc. – ULTRASONIC Sensor (HC-SR04)
1 pc. – 16x2 LCD Display (compatible with Hitachi HD44780 driver)
1 pc. – 220-ohm resistor (1/4 W)
1 pc. – 10k-ohm potentiometer
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

Ultrasonic Sensor
The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just
like the bats do. It offers excellent non-contact range detection with high accuracy
and stable readings in an easy-to-use package from 2 cm to 400 cm or 1” to 13 feet.
The operation is not affected by sunlight or black material, although acoustically, soft
materials like cloth can be difficult to detect. It comes complete with ultrasonic
transmitter and receiver module.

52
Technical Specifications
 Power Supply − +5V DC
 Quiescent Current − <2mA
 Working Current − 15mA
 Effectual Angle − <15°
 Ranging Distance − 2cm – 400 cm/1″ – 13ft
 Resolution − 0.3 cm
 Measuring Angle − 30 degree

SAMPLE PROGRAM: Distance measurement in Inch and Centimeter


The program will able to measures certain distance and display in inch and
COURSE LABORATORY WORKBOOK

centimeter unit to the LCD.


Code:
co n st in t pi n g Pi n = 7 ; / / T ri g ge r Pi n o f u lt r a so ni c se n so r
co n st in t e ch o Pi n = 6; / / Ech o Pi n o f ul t r a so ni c se n sor
co n st in t r s = 12 , e n = 11 , d 4 = 5, d 5 = 4 , d 6 = 3 , d7 = 2 ;
L i q uid Cr yst al l cd( r s, e n, d 4 , d5 , d 6 , d7 ) ;

vo i d se t u p () {
l cd .b e gi n (1 6 , 2) ;
}

vo i d l oo p () {
l o ng d ur a ti o n , i n ch e s, cm;
p i n Mo d e( pi n g Pi n , O UT PUT ) ;
d i gi talW rit e ( pin g Pi n, L OW ) ;
d e la yMi cr o se co n d s( 2 ) ;
d i gi talW rit e ( pin g Pi n, HI G H) ;
d e la yMi cr o se co n d s( 1 0 ) ;
d i gi talW rit e ( pin g Pi n, L OW ) ;
p i n Mo d e( e ch o Pi n , I NPUT) ;
d u r a ti on = p ul se I n( e ch o Pi n , HI G H) ;
i n che s = mi cr o se co n d sT o I n che s( d ura t i on ) ;
cm = mi cr o se co n d sT o Ce n ti me t e r s( du r a ti o n );
l cd . se t Cu r sor ( 1 ,1 ) ;
l cd .p ri n t (in ch e s) ;
l cd .p ri n t ("i n " );
l cd . se t Cu r sor ( 1 ,2 ) ;
l cd .p ri n t ( cm) ;
l cd .p ri n t (" cm" ) ;
d e la y( 1 00 ) ;
}

l o n g mi cr o se co n d sT o I n ch e s(l o n g mi cro se co n d s) {
r e t u rn mi cr o se co n d s / 7 4 / 2;
}

l o n g mi cr o se co n d sT o Ce n ti me t e r s( lo ng mi cr o se co n d s) {
r e t u rn mi cr o se co n d s / 2 9 / 2;
}

WIRING DIAGRAM:
The Ultrasonic sensor has four terminals - +5V, Trigger, Echo, and GND connected

53
as follows −
 Connect the +5V pin to +5v on your Arduino board.
 Connect Trigger to digital pin 7 on your Arduino board.
 Connect Echo to digital pin 6 on your Arduino board.
 Connect GND with GND on Arduino.
COURSE LABORATORY WORKBOOK

PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the LCD, Ultrasonic sensor and resistors needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.

ACTIVITIES

ACTIVITY 9.1
Make a program for Arduino and ultrasonic sensor that will able to measure a height
of a human in foot/feet and meter unit.

PROCEDURE:
1. Recode the program based on the task requirement.

54
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
COURSE LABORATORY WORKBOOK

the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to
breadboard properly?
3. show the measured height generated by Ultrasonic
sensor as required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. required Pin of Arduino is properly connected to
the intended pin of ultrasonic sensor?
5. show the measured value is in feet and meter
correctly?
6. get a consistent output/result?

ACTIVITY 9.2
Make a program for Arduino and ultrasonic sensor that will detect and count an
object passing in front of it with a maximum distance of 1 foot.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.

55
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to
breadboard properly?
COURSE LABORATORY WORKBOOK

3. show the counted object by Ultrasonic sensor as


required from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. required pin of Arduino is properly connected to
the intended pin of ultrasonic sensor?
5. show the counted value correctly from the actual
counted value?
6. get a consistent output/result?

56
Student Name: Date Started: Date Finished: Grade:

ACTIVITY # 10
LED MATRIX PROGRAMMING
LEARNING OBJECTIVES

The student should be able to:


 understand the programming of LED matrix.
COURSE LABORATORY WORKBOOK

 identify the different function and commands in using ultrasonic sensors.


 use techniques in detecting objects using ultrasonic.
 perform object detection for more other complex application.

COMPONENTS REQUIRED

 Arduino Uno with USB cable


 8 x 8 LED Matrix
 2 pcs. –10k ohm potentiometer
 Breadboard
 Connecting wires (pref. AWG No.24)

DISCUSSION

LED Matrix
LED displays are often packaged as matrixes of LEDs arranged in rows of common
anodes and columns of common cathodes, or the reverse.

To control an individual LED, you set its column LOW and its row HIGH. To control
multiple LEDs in a row, you set the row HIGH, then take the column high, then set the
columns LOW or HIGH as appropriate; a LOW column will turn the corresponding LED
ON, and a HIGH column will turn it off.
Tip: Pins set to OUTPUT by use of the PinMode command are set to LOW if not otherwise
stated

57
Here's a matrix of the pin connections, based on the diagram above:

Matrix pin no. Row Column Arduino pin no.


1 5 13
2 7 12
3 2 11
4 3 10
5 8 16(analog pin 2)
6 5 17(analog pin 3)
7 6 18(analog pin 4)
COURSE LABORATORY WORKBOOK

8 3 19(analog pin 5)
9 1 2
10 4 3
11 6 4
12 4 5
13 1 6
14 2 7
15 7 8
16 8 9

SAMPLE PROGRAM: Row-Column Scanning to control an 8x8 LED Matrix


The program will able to measures certain distance and display in inch and
centimeter unit to the LCD.
Code:
/*
Ro w- Co l u mn Sca n n i n g a n 8 x8 L ED ma t r i x wi t h X - Y i n p ut

T hi s e xa mp l e wo r ks f o r t h e L u me x L DM - 2 4 4 8 8 NI Ma t ri x.

r o ws a r e t h e an o d e s
co l s a r e th e ca t ho d e s
- - -- - -- - -
Pi n n u mb e r s:
Ma t r i x:
- di gi t al p in s 2 t hr o u g h 1 3 ,
- a n alo g pi n s 2 t h r ou g h 5 u se d a s d i gi t al 1 6 t hr o u g h 1 9
Po t e n ti o me t e r s:
- ce n te r pi n s a r e at t a ch e d t o a na log p in s 0 a nd 1 , re sp e cti vel y
- si d e pin s a t t a che d t o +5 V a nd g ro u n d , re sp e cti vel y
*/

co n st in t r o w[ 8 ] = { 2 , 7 , 1 9 , 5 , 1 3 , 1 8 , 1 2 , 1 6 }; / / r o w p i n nu mb e r s
co n st in t col [ 8 ] = { 6 , 11 , 1 0 , 3 , 1 7 , 4 , 8 , 9 } ; / / co lu mn p i n n u mb e r s

i n t pi xe l s[ 8 ] [ 8 ] ; / / 2 - di me n si o n al ar r a y o f pi xe l s

int x = 5; / / cu r so r p o si ti o n
int y = 5;

vo i d se t u p () {
f o r (i nt t hi sPin = 0 ; t hi sPi n < 8 ; thi sPi n ++) { / / i ni ti ali ze t h e ou t p u t pin s
pi n Mo d e ( col [ t hi sPi n ] , O UT PUT ) ;

58
pi n Mo d e ( ro w[ t h i sPi n] , O UT PUT) ;
di gi t alW ri t e ( col [ t hi sPin ] , HI G H) ; / / t o e n sur e t h at t h e L EDs ar e OF F
}

f o r (i nt x = 0 ; x < 8 ; x++) { / / i ni ti ali ze t h e pi xe l ma t r i x


f o r ( in t y = 0 ; y < 8; y++) {
pi xe l s[ x] [ y] = HI G H;
}
}
}

vo i d l oo p () {
COURSE LABORATORY WORKBOOK

r e a d Se n sor s( ); / / r e a d i np u t
r e f r e sh Scr e e n( ) ; / / d r a w t h e scr e en
}

vo i d r e a d Se n so r s() {
p i xe l s[ x] [ y] = HI G H; / / t u r n of f t h e l a st p o si ti o n
/ / r e a d t h e sen so r s fo r X a n d Y val ue s
x = 7 - ma p ( a n al o g Re a d( A0 ) , 0 , 1 0 23 , 0 , 7 ) ;
y = ma p ( a n a l o g Re ad ( A1 ) , 0 , 1 0 23 , 0, 7 ) ;
p i xe l s[ x] [ y] = L OW ; / / se t t h e n e w p i xe l p o si ti on l o w so t h a t t h e
} / / L ED wi ll t u rn o n i n th e n e xt scr e en re f r e sh

vo i d r e f re sh Scr e e n( ) {
/ / it e r a te o ve r t h e ro ws ( a n o d e s)
f o r (i nt t hi sRo w = 0 ; t h i sRo w < 8 ; t hisRo w++) {
/ / t a ke th e ro w p i n ( an o d e) hi g h
di gi t alW ri t e (r o w[ t h i sRo w] , HI G H) ;
/ / i t e r at e o ver t h e co l s ( ca t ho d e s)
f o r ( in t t hi sCol = 0 ; t hi sCol < 8 ; t hisCo l ++) {
/ / g e t t he st a t e o f t h e cu r r en t pi xe l
i n t t hi sPi xe l = pi xe l s[ t hi sRo w] [ t hisCo l ] ;
/ / wh e n t h e r o w i s HI G H an d t he co l i s L OW ,
di gi t alW ri t e( col [ t hi sCol ] , t hi sPi xe l) ; / / t h e L ED wh e r e t h e y me e t t u r n s O N
i f ( t hi sPi xe l == L OW ) { / / t u r n th e pi xe l OF F
di gi t alW ri te ( col [ t hi sCol ], HI G H) ;
}
}
/ / t a ke th e ro w p i n l o w t o t u r n OF F t h e wh o l e ro w
di gi t alW ri t e (r o w[ t h i sRo w] , L OW ) ;
}
}

WIRING DIAGRAM:
The 16 pins of the matrix are hooked up to 16 pins of the Arduino board. Four of the
analog pins are used as digital inputs 16 through 19. The order of the pins is assigned in
two arrays in the code.
Two potentiometers, connected to analog pins 0 and 1, control the movement of a
lit LED in the matrix.

59
COURSE LABORATORY WORKBOOK

PROCEDURE:
1. Write the program using any form of PC or a mobile phone.
2. Once the program is free from error and meet the objective requirement, prepare
the Arduino Uno, breadboard and connecting wires.
3. Insert the LED matrix and potentiometers needed to the breadboard.
4. Insert the wires needed that will provide the connection between Arduino and
breadboard. Follow the wiring diagram on the Circuit.
5. Make a connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
6. Download the program to the Arduino Uno.
7. Press the RESET button of Arduino Uno if necessary to see the proper start-up of
downloaded programs on the Arduino Uno and test.

ACTIVITIES

ACTIVITY 10.1
Make a program for Arduino and LED matrix that will display the letters of the word
“PRMSU” in one at a time with 2 seconds delay, repetitively. It shall start with all LEDs
OFF before displaying the first letter.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.

60
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

PERFORMANCE OBSERVATION CHECKLIST:


COURSE LABORATORY WORKBOOK

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the required output from the activity?
4. check the circuit if:
a. supplied by the correct voltage?
b. required Pin of Arduino is properly connected to
the intended pin of LED matrix?
5. start always with all LEDs turned OFF before
displaying the first letter?
6. display the letter one at a time with 2 seconds
delay?
7. get a consistent output/result?

ACTIVITY 10.2
Make a program for Arduino and LED matrix that will display a scrolling text (from right
to left) the word PRMSU.

PROCEDURE:
1. Recode the program based on the task requirement.
2. Once the program is free from error and meet the task requirement, prepare the
same materials used before (Arduino Uno, breadboard and connecting wires).
3. Make the necessary connection to accomplish the task.
4. Make the connection of VCC (+5V) and Gnd (which or both are needed) from
Arduino to breadboard.
5. Download the program to the Arduino Uno.
6. Press the RESET button of Arduino Uno, if necessary, to see the proper start-up of
downloaded programs on the Arduino Uno.
7. If done successfully, fill-up the PERFORMANCE OBSERVATION CHECKLIST and draw
the flowchart of your code at the back of this page.

61
PERFORMANCE OBSERVATION CHECKLIST:

Did you… Yes No


1. download the program to the Arduino Uno
successfully?
2. connect the power supply of Arduino to breadboard
properly?
3. display the required output from the activity?
4. check the circuit if:
COURSE LABORATORY WORKBOOK

a. supplied by the correct voltage?


b. required pin of Arduino is properly connected to
the intended pin of LED matrix?
5. display the scrolling word of PRMSU from right to left
correctly?
6. display the scrolling word of PRMSU from right to left
visibly?
7. get a consistent output/result?

62
APPENDIX A
Using Arduino IDE
Arduino IDE for Windows
Arduino programs are written in the Arduino Integrated Development Environment (IDE).
Arduino IDE is a special software running on your system that allows you to write sketches
(synonym for program in Arduino language) for different Arduino boards. The Arduino
programming language is based on a very simple hardware programming language
called processing, which is similar to the C language. After the sketch is written in the
Arduino IDE, it should be uploaded on the Arduino board for execution.
COURSE LABORATORY WORKBOOK

The Arduino development environment contains a text editor for writing code, a
message area, a text console, a toolbar with buttons for common functions, and a series
of menus. It connects to the Arduino hardware to upload programs and communicate
with them.
The first step in programming the Arduino board is downloading and installing the
Arduino IDE. The open source Arduino IDE runs on Windows, Mac OS X, and Linux.
Download the Arduino software (depending on your OS) from the official website and
follow the instructions to install.

Arduino IDE

Getting Started with Arduino on Windows


This procedure explains how to connect your Arduino board to the computer and
upload your first sketch.
1. Get an Arduino board and USB cable.
2. Open the Arduino software application.
3. Connect the board using USB cable. The green power LED (labelled PWR) should go
on.

63
Illustration of Arduino Uno -
Laptop connection using
the USB cable and the USB
OTG adapter.
COURSE LABORATORY WORKBOOK

4. Create your program or open your recent created program.


5. Select your board. From Tools > Board menu, select among the list that corresponds
to your Arduino.
6. Select your serial port. From the Tools | Serial Port menu, select the serial device of
the Arduino board. This is likely to be COM3 or higher (COM1 and COM2 are usually
reserved for hardware serial ports). To find out, you can disconnect your Arduino
board and re-open the menu; the entry that disappears should be the Arduino
board. Reconnect the board and select that serial port.
7. Upload the program. Click the "Upload" button in the environment. Wait a few
seconds - you should see the RX and TX LEDs on the board flashing. If the upload is
successful, the message "Done uploading." will appear in the status bar.

Writing Sketches
Software written using Arduino are called sketches. These sketches are written in the text
editor. Sketches are saved with the file extension .ino. It has features for cutting/pasting
and for searching/replacing text. The message area gives feedback while saving and
exporting and also displays errors. The console displays text output by the Arduino
environment including complete error messages and other information. The bottom right-
hand corner of the window displays the current board and serial port. The toolbar
buttons allow you to verify and upload programs, create, open, and save sketches, and
open the serial monitor.
1. Verify. Checks your code for errors.
2. Upload. Compiles your code and uploads it to the Arduino I/O board.
3. New. Creates a new sketch.
4. Open. Presents a menu of all the sketches in your sketchbook. Clicking one will
open it within the current window.
5. Save. Saves your sketch.
6. Serial Monitor. Opens the serial monitor.
Additional commands are found within the five menus: File, Edit, Sketch, Tools, Help. The
menus are context sensitive which means only those items relevant to the work currently
being carried out are available.

Edit

64
1. Copy for Forum. Copies the code of your sketch to the clipboard in a form suitable
for posting to the forum, complete with syntax coloring.
2. Copy as HTML. Copies the code of your sketch to the clipboard as HTML, suitable
for embedding in web pages.

Sketch
1. Verify/Compile. Checks your sketch for errors.
2. Show Sketch Folder. Opens the current sketch folder.
3. Add File... Adds a source file to the sketch (it will be copied from its current
COURSE LABORATORY WORKBOOK

location). The new file appears in a new tab in the sketch window. Files can be
removed from the sketch using the tab menu.
4. Import Library. Adds a library to your sketch by inserting #include statements at the
code of your code.

Tools
1. Auto Format. This formats your code nicely: i.e. indents it so that opening and
closing curly braces line up, and that the statements instead curly braces are
indented more.
2. Archive Sketch. Archives a copy of the current sketch in .zip format. The archive is
placed in the same directory as the sketch.
3. Board. Select the board that you're using.
4. Serial Port. This menu contains all the serial devices (real or virtual) on your machine.
It should automatically refresh every time you open the top-level tools menu.
5. Programmer. For selecting a hardware programmer when programming a board or
chip and not using the onboard USB-serial connection.
6. Burn Bootloader. The items in this menu allow you to burn a bootloader onto the
microcontroller on an Arduino board. This is not required for normal use of an
Arduino board but is useful if you purchase a new ATmega microcontroller (which
normally come without a bootloader).

Libraries
Libraries provide extra functionality for use in sketches, e.g. working with hardware or
manipulating data. To use a library in a sketch, select it from the Sketch > Import Library
menu. This will insert one or more #include statements at the top of the sketch and
compile the library with your sketch. Because libraries are uploaded to the board with
your sketch, they increase the amount of space it takes up. If a sketch no longer needs a
library, simply delete its #include statements from the top of your code.

Boards
The board selection has two effects: it sets the parameters (e.g. CPU speed and baud
rate) used when compiling and uploading sketches; and sets and the file and fuse
settings used by the burn bootloader command. Some of the board definitions differ only
in the latter, so even if you've been uploading successfully with a particular selection,
you'll want to check it before burning the bootloader.

65
APPENDIX B
Using ArduinoDroid IDE
ArduinoDroid for Android
ArduinoDroid is a free app that will let you edit, compile and upload sketches to
your Arduino board directly from an Android phone or tablet.
COURSE LABORATORY WORKBOOK

ArduinoDroid Environment

It has the following features:


 works offline (internet connection is not required)
 open/edit Arduino sketches
 example sketches and libraries included
 code syntax highlighting
 compile sketches (no root required)
 upload sketches

Getting Started with Arduino on Android Device


This procedure explains how to connect your Arduino board to the computer and
upload your first sketch.
1. Get an Arduino board and USB cable with USB OTG (on-the-go) adapter.
2. Open the Arduino software application.
3. Connect the board using USB OTG adapter to your Android device. The green
power LED (labelled PWR) should go on.
4. Create your program or open your recent created program.

5. Select your board. From Options ( ) > Settings > Board Type > Arduino menu,
select among the list that corresponds to your Arduino.

66
6. Compile your program by tapping the Compile icon.
7. Upload the program. Tap the Upload icon in the environment. Wait a few
seconds - you should see the RX and TX LEDs on the board flashing. If the upload is
successful, the message "Done uploading." will appear in the Output window.
COURSE LABORATORY WORKBOOK

Illustration of Arduino Uno -


Android device
connection using the USB
cable and the USB OTG
adapter.

67
APPENDIX C
Arduino Uno Board
Arduino Uno
The Arduino Uno is an open-source microcontroller board based on the Microchip
ATmega328P microcontroller and developed by Arduino.cc. The board is equipped with
sets of digital and analog input/output (I/O) pins that may be interfaced to various
expansion boards (shields) and other circuits. The board has 14 digital I/O pins (six
capable of PWM output), 6 analog I/O pins, and is programmable with the Arduino
IDE (Integrated Development Environment), via a type B USB cable. It can be powered
COURSE LABORATORY WORKBOOK

by the USB cable or by an external 9-volt battery, though it accepts voltages between 7
and 20 volts.
The word "uno" means "one" in Italian and was chosen to mark the initial release
of Arduino Software. The Uno board is the first in a series of USB-based Arduino boards
which have now evolved to newer releases. The ATmega328 on the board comes
preprogrammed with a bootloader that allows uploading new code to it without the use
of an external hardware programmer.

Arduino Uno Board Description


Some boards look a bit different from the one given below, but most Arduinos have
majority of these components in common

68
Power USB
Arduino board can be powered by using the USB cable from your computer. All
you need to do is connect the USB cable to the USB connection.

Power (Barrel Jack)


Arduino boards can be powered directly from the AC mains power supply by
connecting it to the Barrel Jack.

Voltage Regulator
COURSE LABORATORY WORKBOOK

The function of the voltage regulator is to control the voltage given to the Arduino
board and stabilize the DC voltages used by the processor and other elements.

Crystal Oscillator
The crystal oscillator helps Arduino in dealing with time issues most specifically in
processing time. The number printed on top of the Arduino crystal is 16.000H9H. It
tells us that the frequency is 16,000,000 Hertz or 16-MHz.

Arduino Reset
You can reset your Arduino board, i.e., start your program from the beginning. You
can reset the UNO board in two ways. First, by using the reset button (17) on the
board. Second, you can connect an external reset button to the Arduino pin
labelled RESET.

Pins (3.3, 5, GND, Vin)


• 3.3V (6): Supply 3.3 output volt
• 5V (7): Supply 5 output volt. Most of the components used with Arduino
board works fine with 3.3 volt and 5 volt.
• GND (8)(Ground): There are several GND pins on the Arduino, any of which
can be used to ground your circuit.
• Vin (9): This pin also can be used to power the Arduino board from an
external power source, like AC mains power supply.

Analog pins
The Arduino UNO board has five analog input pins A0 through A5. These pins can
read the signal from an analog sensor like the humidity sensor or temperature
sensor and convert it into a digital value that can be read by the microprocessor.

Main microcontroller
Each Arduino board has its own microcontroller. You can assume it as the brain of
your board. The main IC (integrated circuit) on the Arduino is slightly different from
board to board. The microcontrollers are usually of the ATMEL Company. You must
know what IC your board has before loading up a new program from the Arduino
IDE. This information is available on the top of the IC. For more details about the IC
construction and functions, you can refer to the data sheet.

69
ICSP pin
Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of
MOSI, MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial
Peripheral Interface), which could be considered as an "expansion" of the output.
Actually, you are slaving the output device to the master of the SPI bus.

Power LED indicator


This LED should light up when you plug your Arduino into a power source to indicate
that your board is powered up correctly. If this light does not turn on, then there is
COURSE LABORATORY WORKBOOK

something wrong with the connection.

TX and RX LEDs
On your board, you will find two labels: TX (transmit) and RX (receive). They appear
in two places on the Arduino UNO board. First, at the digital pins 0 and 1, to
indicate the pins responsible for serial communication. Second, the TX and RX led.
The TX led flashes with different speed while sending the serial data. The speed of
flashing depends on the baud rate used by the board. RX flashes during the
receiving process.

Digital I/O
The Arduino UNO board has 14 digital I/O pins (of which 6 provide PWM (Pulse
Width Modulation) output. These pins can be configured to work as input digital
pins to read logic values (0 or 1) or as digital output pins to drive different modules
like LEDs, relays, etc. The pins labeled “~” can be used to generate PWM.

AREF
AREF stands for Analog Reference. It is sometimes, used to set an external reference
voltage (between 0 and 5 Volts) as the upper limit for the analog input pins.

70
COURSE LABORATORY WORKBOOK
NOTES

71
COURSE LABORATORY WORKBOOK
NOTES

72

You might also like