Experiment 7
Experiment 7
La Paz, Iloilo
Experiment 7
Name : ___________________________________________________Year & Sec.:________________
Date Performed: ______________________________________________Rating: ___________________
Date finished: ________________________________________________Instructor’s signature : ______
Discussion
Digital Inputs
Digital input and output are the most fundamental physical connections for any microcontroller.
The pins to which you connect the circuits shown here 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.
When you’re trying to sense activity in the physical world using a microcontroller, the simplest
activities you can sense are those in which you only need to know one thing about the physical world:
Whether something is true or false. Is the viewer in the room or out? Are they touching the table or not?
Is the door open or closed? In these cases, you can determine what you need to know using a digital
input, or switch.
Digital or binary inputs to microcontrollers have two states: off and on. If voltage is flowing, the
circuit is on. If it’s not flowing, the circuit is off. To make a digital circuit, you need a circuit, and a
movable conductor which can either complete the circuit, or not. Figure 7.1 configure as digital input for
a microcontroller.
25
Figure 7.1.Circuit Diagram for Digital Input
Figure 7.1 shows the electrical schematic for a digital input to a microcontroller. The current has
two directions it can go to ground: through the resistor or through the microcontroller. When the switch
is closed, the current will follow the path of least resistance, to the microcontroller pin, and the
microcontroller can then read the voltage. The microcontroller pin will then read as high voltage or
HIGH. When the switch is open, the resistor connects the digital input to ground, so that it reads as zero
voltage, or LOW.
Digital Output
Just as digital inputs allow you to sense activities which have two states, digital or binary outputs
allow you to control activities which can have two states. With a digital output you can either turn
something off or on. Figure 7.2 show s the schematic diagram for digital output controlling an LED.
Digital outputs are often used to control other electrical devices besides LEDs, through
transistors or relays. On an Arduino module, you declare the pin an output at the top of the program
just like you did with inputs. Then in the body of the program you use the digitalWrite() command with
the values HIGH and LOW to set the pin high or low.
26
Procedures:
void loop() {
// read pin 2:
if (digitalRead(2) == 1)
{
// if pin 2 is HIGH, set pin 3 HIGH:
digitalWrite(3, HIGH);
}
else
{
// if pin 2 is LOW, set pin 3 LOW:
digitalWrite(3, LOW);
}
2. Prepare the breadboard. Connect power and ground on the breadboard to power and ground of the
microcontroller as shown on Figure 4.3. This time we will be using Arduino Board, use the 5V and any of the
ground connections.
3. Add a digital input and refer to Figure 7.1 with pull down resistors. It provides the digital input
pin with a reference to ground. Without it, the input will behave unreliably.
27
Figure 7.4.Microcontroller with switch on digital pin 2
28
Figure 7.6. Adding Output on Pins 3 and 4.
5. Once the program has been successfully verified, connect the Arduino to the computer using
a USB standard A-B cable. Specify the COM port where the Arduino is connected in Tools >
Serial Port > [COM port used].
TIP: You could find which COM port the Arduino is connected by expanding the Ports (COM &
LPT) item in the Windows Device Manager. The Arduino will be connected to Prolific USB-to-
Serial Comm Port (COM#).
29
4. Specify the board to use by checking the appropriate Arduino board under Tools >
Board.
5. Click the (Upload) button. This will now compile and burn the program to the
Arduino.
TIP: You could actually skip Step 2 since the Upload button automatically verifies your program
before uploading it to your Arduino board.
6. Remove first the 220 ohms resistor on pin 4 since pin 4 was not been programmed.
8. Observe the LED that you connected to the Arduino through pin 3 as you change the status
of your switch.
Guide Questions:
1. Describe the behavior of the LED that you connected to the Arduino as you change the status of the
switch.
Answer:
_____________________________________________________________________________________ _
____________________________________________________________________________________
_____________________________________________________________________________________
2. What functions and commands cause the LED to exhibit this kind of behavior? Elaborate your answer.
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Review Questions:
1. What is digital input? How about digital output?
Answer:
_____________________________________________________________________________________ _
____________________________________________________________________________________
_____________________________________________________________________________________
2. How can we assign pins on the Arduino board? For example we will be using specific pin.
Answer:
_____________________________________________________________________________________ _
____________________________________________________________________________________
_____________________________________________________________________________________
3. What is the purpose of the pull down resistor?
Answer:
_____________________________________________________________________________________ _
____________________________________________________________________________________
30
4. Where can we find the correct serial port in the Arduino IDE window?
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
5. What decision statement we used inside our void loop? Explain each case.
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
6. What does the function digitalWrite() do? Briefly explain where it is applied.
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Challenge Yourself:
1. Connect the 220 ohms resistor on pin 4. Now it’s time to write a program that reads the digital input
on pin 2. When the switch is pressed, turn the LED on pin 3 and the LED on pin 4 off. When the switch is
released, turn the LED on pin 4 on and the LED on pin 3 off. Present this to your instructor when it’s
done.
Conclusion:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
31