0% found this document useful (0 votes)
27 views11 pages

Activity 2 - Digital Input MTS-100

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

Activity 2 - Digital Input MTS-100

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

Course: CPE405 Section: CPE41S2

Name/s: Date Performed: 10/04/2024


Ocariza, Aaron Kyle Date Submitted: 10/04/2024
Sandoval, Samuel
Viernes, Joice Ann Eisley
Vinas, Gabriel Edmund
Justimbaste, Allen Hendrix G.
Instructor: Engr. Mon Malbog

Laboratory Activity No. 2


Arduino Digital Input, Looping and Control
Statements

1. Objective(s):

The activity aims to covers the discussions on Arduino digital input. It also
includes the discussions on topic about Arduino looping and control statements.

2. Intended Learning Outcomes (ILOs):

At the end of the activity student shall able to:

● Create a program that uses digital input.


● Use looping and control statements in the implementation of sequence output.
● Manipulate C/C++ code to implement the required output sequence
3. Materials

● MTS-100 Development Kit


● Connecting Wires
4. Discussion

Digital Input

Digital input is one of the most fundamental physical connections for any
microcontroller. The pins to which you connect the circuits shown in the activity are
called General Purpose Input-Output, or GPIO, pins. Even if a given project doesn’t
use digital in and out, you’ll often use LEDs and switches during the development for
testing whether everything’s working.
Properties of Pins Configured as INPUT

Arduino (Atmega) pins default to inputs, so they don't need to be explicitly


declared as inputs with pinMode() when you're using them as inputs. Pins configured
this way are said to be in a high- impedance state. Input pins make extremely small
demands on the circuit that they are sampling, equivalent to a series resistor of 100
megohm in front of the pin. This means that it takes very little current to move the
input pin from one state to another, and can make the pins useful for such tasks as
implementing a capacitive touch sensor, reading an LED as a photodiode, or reading
an analog sensor with a scheme such as RCTime.

This also means however, that pins configured as pinMode(pin, INPUT) with
nothing connected to them, or with wires connected to them that are not
connected to other circuits, will report seemingly random changes in pin state,
picking up electrical noise from the environment, or capacitively coupling the
state of a nearby pin.

Pullup Resistors with pins configured as INPUT


Often it is useful to steer an input pin to a known state if no input is present.
This can be done by adding a pullup resistor (to +5V), or a pulldown resistor
(resistor to ground) on the input. A 10K resistor is a good value for a pullup or
pulldown resistor.

Properties of Pins Configured as INPUT_PULLUP


There are 20K pullup resistors built into the Atmega chip that can be accessed
from software. These built-in pullup resistors are accessed by setting the
pinMode() as INPUT_PULLUP. This effectively inverts the behavior of the INPUT
mode, where HIGH means the sensor is off, and LOW means the sensor is on.

The value of this pullup depends on the microcontroller used. On most


AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ. On the
Arduino Due, it is between 50kΩ and 150kΩ. For the exact value, consult the
datasheet of the microcontroller on your board.
When connecting a sensor to a pin configured with INPUT_PULLUP, the other
end should be connected to ground. In the case of a simple switch, this causes the
pin to read HIGH when the switch is open, and LOW when the switch is pressed.

The pullup resistors provide enough current to dimly light an LED connected to
a pin that has been configured as an input. If LEDs in a project seem to be
working, but very dimly, this is likely what is going on.

The pullup resistors are controlled by the same registers (internal chip memory
locations) that control whether a pin is HIGH or LOW. Consequently, a pin that is
configured to have pullup resistors turned on when the pin is an INPUT, will have
the pin configured as HIGH if the pin is then switched to an OUTPUT with
pinMode(). This works in the other direction as well, and an output pin that is left in
a HIGH state will have the pullup resistors set if switched to an input with
pinMode().

Digital Read()
Description
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead(pin)

Parameters
pin: the number of the digital pin you want to read (int)
Returns
HIGH or LOW

Arduino Control & looping Statements

No matter what type programming languages you are using, e.g. C/C++,
Visual Basic, PHP and etc, there are always some logic paths you want your
software to decide how it should react based on certain criteria. This is where
control structure needed.
if and if…else… statements
if and if…else… are the most straight forward condition checking control
statement. The syntax of the if statement should be as below:

if (myVar> 0)
{
// do something here if myVar is greater than 0
}

Sometime you may need an if…else… control statement for more than 1
condition.
if (myVar< 10)
{
// do Thing A
}
else if (myVar>= 100)
{

}
else
{

for statement
// do Thing B

Another useful control structure is the for loop. It is used to repeat a block of
statements between its curly braces. An increment/decrement counter is usually
used to increment/decrement and terminate the loop. Normally the for statement is
used in combination with arrays to operate on collections of data/pins in Arduino.

for (int iBright=0; iBright<= 255; iBright++){


analogWrite(PWMpin, iBright);
delay(50);
}

The above for loop Arduino programming example fades a LED up slowly from
off to full brightness.

Switch…case… statement
Another control statement for Arduino programming that quite similar
toif…else… is switch…case… statement. Like if statements, switch…case…
controls the flow of programs by allowing programmers to specify code that
should be executed in various conditions. Below is the syntax example go the
Arduino programming switch…case… statement.

switch (MyVar) {
case 1:
//do something when MyVar equals 1
break;
case 2:
//do something when MyVar equ

als 2
break;
default:
// if nothing else matches, do the default
// default is optional
}

You can see the above switch…case… Arduino codes are very similar to the
if…else…
statement.

while and do…while… statements


while and do…while… control statements are used to loop a block of code
until reach certain conditions. Here is a while loop statement example.

myVar = 0;
while(var < 50){
// do something repetitive 50 times
myVar=myVar + 1;
}

Below is the example of the do…while… loop.


Do

{
delay(50); // wait for sensors to stabilize
sensorVal = readSensors(); // check the sensors
} while (sensorVal< 100);

The main different between while loop and do…while… loop is while loop
check the condition before executes the action, butdo…while… loop is executing
the action at least once before exiting the loop by meeting the exit condition.

MTS-100 Keypad/Switch
MTS-100 is equipped with configurable Keypad. The Keypad is can function as
individual switch or a keypad itself.
As individual switch below is the configuration:
5. Procedures

Preparing the Hardware


1. Prepare your connections.
2. Connect the 2 LEDS of the MTS-100 Led Barto Arduino digital pins (pin 2 to
3).
3. ConnectC0 of the Keypad pins to Arduino pin8

Coding the Arduino

In your setup() method, you need to set the three pins you’re using as inputs or
outputs,
appropriately.

voidsetup() {
pinMode(2, OUTPUT); // set the yellow LED pin to be an output
pinMode(3, OUTPUT); // set the yellow LED pin to be an output
pinMode(8, INPUT); // set the switch pin to be an input
}

In the main loop, first you need an if-then-else statement to read the switch.

voidloop() {
// read the switch input:
if(digitalRead(8) == LOW) { //Check if Button1 is pressed
// if the switch is closed: digitalWrite(2,
HIGH); // turn on 1STLED
digitalWrite(3, LOW); // turn off 2NDLED
}
else{
// if the switch is open:
digitalWrite(2, LOW); // turn off 1ST LED
digitalWrite(3, HIGH); // turn on the 2ND LED
}
}

Running the code


Compile and upload the code in the Arduino board.
Run the code
What happens to the LEDs whenever the switch is being pressed?
The first LED lights on when the switch is being pressed then if not, the second LED
turns on. Pressing the switch makes the LEDs light alternately.
Why the output above behaves like that?
The output behaves like that because it mentions the if else statement. If else
statement allows you to make decisions based on certain conditions. For example,
the code used the if statement to light the first LED then the second LED is off but,
else statement is used to turn off the first LED then the second LED lights on.

6.Supplemental Activity
1. Create a program that will output the sequence 8-bits LED below whenever
the corresponding switch is being pressed.
Arduino Switch No. Outpu
Pin t
8 1 Continuous Blinking lights
9 2 Continuous Running Lights (Back & Forth)
10 3 Continuous Alternate low and high nibble

SUPPLEMENTAL ACTIVITY 1
2. Create a program that simulates a car counter on a parking area. The program
should count all the cars that comes in and comes out on the parking area. 2
switches should represents the sensor for sensing the incoming car and
outgoing car. Seven segments should be the output. For reference refer to the
figure below MTS-100 Seven Segment connection.

MICROPROCESSOR-ACTIVITY 2
3. In a certain car, there are two lights to light the interior and one light for the
trunk. The first (LED0) is in front of the car, the second (LED1) is in the back of
the car, and the third (LED2) is in the trunk. There are five switches that control
these three lights: SW0-SW3 is the switches that indicate if one of the four doors
is opened or not. SW4 indicates if the trunk is opened or not. Let's suppose that
the switches are closed if the doors are open. When one of the four car doors is
opened, both lights in the interior needs to light up. When the trunk is opened,
only the trunk-light needs to light up. (Assuming the LEDs on the car needs 4.8
volts to light up). If the switches are connected to pin 8-12 respectively (pin8 to
SW0, pin 9 to SW1 …) and the LEDs are connected to pin3 and pin4 (LED0
connected to pin 3 and LED1 connected to pin 4) of the Arduino, create a
complete C code for Arduino that will simulate the given machine problem.

SUPPLEMENTAL ACTIVITY 3.mp4

7. Conclusion:

What conclusion can you make based on the activity?


Based on the activity, we therefore identified how to implement the if else statement with
looping on the code. On the supplemental activity, we created a program that used LEDs,
switches, and 7 segments. We find it difficult to program the 7 segments because code
errors occur.

8. Assessment (Rubric for Laboratory Performance):


Intended Unsatisfact Satisfacto Exempla
Score
Learning ory 1 ry 2 ry 3
Outcomes
Create a Unable to Able to create a Able to create a
program that create a program that program that
uses digital program that uses digital uses digital
input. uses digital input. input. without
input. but with errors errors
Use looping Unable to Able to use Able to use
and control use looping looping and looping and
statements in and control control control
the statements statements in statements in
implementation in the the the
of sequence implementati implementation implementation
output on of of sequence of sequence
sequence output but with output without
output errors errors
Manipulate Unable to Able to Able to
C/C++ code to manipulate manipulate manipulate
implement the C/C++ code C/C++ code to C/C++ code to
to implement implement the implement the
required output
the required required required output
sequence output output sequence
sequence sequence without
but with errors errors
Other comments/observation: Total Score
RATING = (total score) x
100%
9

Evaluated by: Date:

Printed Name and Signature of Faculty Member

You might also like