Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ir. Amran: University Malaysia Sabah Faculty of Engineering

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

University Malaysia Sabah

Faculty of Engineering

KM42801 Laboratory V

Semester 8 2018/2019

Lecturer: IR. AMRAN

LAB A3: ARDUINO INPUTS AND OUTPUTS

Arapat Bin Syamsuddin

BK15110032
1.0 Introduction

The pins on the Arduino can be configured as either inputs or outputs. In this section, the
functioning of the pins (digital) in those modes are explained. it is important to note that the
majority of Arduino (Atmega) analog pins, may be configured, and used in the exact manner as
digital pins.

1.1 Properties of Pin configured as INPUT

Arduino (Atmega) pins default to inputs, therefore it is not explicitly declared as inputs with
pinMode(). Pins configured this way are in a high-impedance state. Input pins exert extremely
small demands on the circuit that is sampled, equivalent to a series resistor of 100 megaohm 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.

However, this also implies that the 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.

1.2 Pulled up Resistors with Pin configured as INPUT

It is often 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.
1.3 Properties of Pin 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, the datasheet of the microcontroller on
your board is consulted.

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(). Prior to Arduino 1.0.1, it was possible to configure the internal pull-ups in the following
manner:

The digital pin 13 is harder to use as a digital input than the other digital pins because it has an
LED and resistor attached to it that's soldered to the board on most boards. If you enable its
internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the
onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If
you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down
resistor.
1.4 Properties of Pin configured as OUTPUT

Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means
that they can provide a substantial amount of current to other circuits. Atmega pins can source
(provide positive current) or sink (provide negative current) up to 40 mA (milliamps) of current
to other devices/circuits. This current is sufficient to brightly light up an LED (don't forget the
series resistor), or run many sensors, for example, but not enough current to run most relays,
solenoids, or motors.

Short circuits on Arduino pins, or attempting to run high current devices from them, can
damage or destroy the output transistors in the pin, or damage the entire Atmega chip. Often
this will result in a "dead" pin in the micro-controller but the remaining chip will still function
adequately. Hence, it is a good idea to connect OUTPUT pins to other devices with 470Ω or 1k
resistors, unless maximum current draw from the pins is required for a particular application.

2.0 Objectives

The objectives of the experiment are to:


a) To control the blinking and delay response for LEDs of the Arduino system.
b) To control the intensity of the outputs for LEDs from the Arduino system.
c) To control multiple LEDs in the Arduino system.
3.0 Methodology

This section describes the materials needed and the procedures to achieve the objectives of the
experiment.

3.1 Materials

The materials needed for the experiment are listed below.

a) Arduino Uno
b) Breadboard
c) LED (x3)
d) Resistors anything between 220 ohms to 1K ohm (x3)
e) Resistor, 10K ohm (x3)
f) Jumper/Wire

Figure 3.1 : Circuit connection (left) and the schematic (right) for single LED
3.2 LED Blink and Delay Procedures

1) The circuit is connected as shown in Figure 3.1.


2) The anode of the LED is connected to the digital output pin 13 on the Arduino Uno
through a 220 ohms resistor.
3) The cathode is connected directly to the ground.
4) The Arduino IDE is started and the “Blink” example code is uploaded after the circuit
plug of the Arduino board is built into the computer (saved as Exp1_1).
5) The coding is modified that the LED is blinked faster (saved as Exp1_2).
6) The coding is modified that the LED is blinked slower (saved as Exp1_3).

3.3 LED Fading or Intensity Control Procedures

This experiment demonstrates the use of an analog output (Pulse Width Modulation (PWM)) to
fade an LED. PWM is a technique for getting an analog-like behavior from a digital output by
switching it off and on very fast and with different ratio between the on and off times. Listed
below are the procedures to achieve the objective.

1) The circuit is connected as shown in Figure 3.1.


2) The anode of the LED is connected to the digital output pin 11 on the Arduino Uno
through a 220 ohms resistor.
3) The cathode is connected directly to the ground.
4) The Arduino IDE is started and the “Blink” example code is uploaded after the circuit
plug of the Arduino board is built into the computer (saved as Exp2_1).
5) The coding is modified that the LED starts dark and slowly becomes brighter (saved as
Exp2_2).
6) The coding is modified that the LED starts bright and slowly becomes fades and then
from dark to gradually brightens until it is fully bright (saved as Exp2_3).
3.4 Control Multiple LED Procedures

Figure 3.2 : Circuit connection (left) and the schematic (right) for multiple LED controls

1) The circuit is connected as shown in Figure 3.2.


2) The anode of the LED is connected to the digital output pin 11, 10 and 9 on the Arduino
Uno through a 220 ohms resistor respectively.
3) The cathode is connected directly to the ground.
4) The Arduino IDE is started and the “Blink3” example code is uploaded after the circuit
plug of the Arduino board is built into the computer (saved as Exp3_1).
5) Step 4 is repeated for the Run3 code (saved as Exp3_2).
6) The multiple blink LEDs and running LEDs coding are combined into 1 (saved as
Exp3_3).
7) The coding is modified that the LED starts by blinking, running and then fading (saved
as Exp3_4).
5.0 Programme Flow Charts

The programme flow charts are constructed for each experiment to show the general program
flow process that is conducted to achieve the objectives of the experiment.

5.1 Flow Chart for LED Blink and Delay

Figure 5.1 : Flow Chart for Exp1_1, Exp1_2 and Exp1_3 (left to right)
5.2 Flow Chart for LED Fading or Intensity Control

Figure 5.2 : Flow Chart for Exp2_1 (top left)


Figure 5.3 : Flow Chart for Exp2_2 (top right)
Figure 5.4 : Flow Chart for Exp2_3 (bottom)
5.3 Flow Chart for Control Multiple LED Procedures

Figure 5.3 : Flow Chart for Exp3_1 (left)


Figure 5.4 : Flow Chart for Exp3_2 (right)
Figure 5.4 : Flow Chart for Exp3_3
6.0 Programme Coding

The MATLAB coding of Experiment 1, 2 and 3 are listed in this section.

6.1 Programme Coding of Experiment 1 (LED Blink and Delay)

Figure 6.1 : Programme Coding for Experiment1_1, Experiment1_2 and Experiment1_3


(from top to bottom)
6.2 Programme Coding of Experiment 2 (LED Fading and Intensity Control)

Figure 6.2 : Programme Coding for Experiment2_1, Experiment2_2 and Experiment2_3 (top
left, top right, bottom)
6.2 Programme Coding of Experiment 3 (Control Multiple LED)

Figure 6.3 : Programme Coding for Experiment3_1, Experiment3_2 and Experiment3_3 (top
left, top right, bottom)
Figure 6.4 : Programme Coding for Experiment3_4
7.0 Results

The results of Experiment 1, 2 and 3 are obtained by observing the output from the Arduino
board. The observations are then recorded in this section.

7.1 Results of Experiment 1 (LED Blink and Delay)

The connection of the Arduino board circuit is as shown in Figure 7.1 below.

Figure 7.1 : Connection of the Arduino Board Circuit for Experiment 1

Table 7.1: Observations of Experiment 1

Experiment 1 Observations
Experiment1_1 After the code is uploaded, the LED lights up for 1 second. Then, the LED
fades for 1 second. The whole process then repeats itself until the programme
is turned off.
Experiment1_2 After the code is uploaded, the LED lights up for 5 seconds. Then, the LED
fades for 5 seconds. The whole process then repeats itself until the
programme is turned off.
Experiment1_3 After the code is uploaded, the LED lights up for 0.5 second. Then, the LED
fades for 0.5 second. The whole process then repeats itself until the
programme is turned off.
7.2 Results of Experiment 2 (LED Fading and Intensity Control)

The connection of the Arduino board circuit is as shown in Figure 7.1 above.

Table 7.2: Observations of Experiment 2

Experiment 2 Observations
Experiment2_1 After the code is uploaded, it is observed that the brightness of the LED
steadily decreases from its maximum to zero from t=0s to t=3s in steps of
0.5 second. After the end of the 3 seconds, the whole process repeats itself.

Experiment2_2 After the code is uploaded, it is observed that the brightness of the LED
steadily increases from zero to maximum from t=0s to t=3s in steps of 0.5
second. After the end of the 3 seconds, the whole process repeats itself.

Experiment2_3 After the code is uploaded, it is observed that the brightness of the LED
steadily decreases from maximum to zero from t=0s to t=3s in steps of 0.5
second. Then, after the initial 3 seconds, the brightness of the LED steadily
increases from zero to its maximum (not as bright as in t=0s) from t=3s to
t=5s in steps of 0.5 second. After the end of the 5 seconds, the whole process
repeats itself.

7.3 Results of Experiment 3 (Control Multiple LED)

The connection of the Arduino board circuit is as shown in Figure 7.2 and 7.3 below.

Figure 7.2 : Connection of the Arduino Board Circuit for Experiment 3 (left)

Figure 7.3 : Connection of the Arduino Board Circuit for Experiment 3 with outputs (right)
Table 7.3: Observations of Experiment 3

Experiment 3 Observations
Experiment3_1 After the code is uploaded, it is observed that three of the LEDs light up
simultaneously for 0.1 second. The, three of the LEDs stop lighting up
simultaneously for 0.1 second. The whole process repeats itself after 0.2
second. Every ‘blink’ occurs in the 0.2 second of one cycle.

Experiment3_2 After the code is uploaded, it is observed that only LED1 lights up for 0.1
second. Then, LED1 stops to light up. After 0.1 second, LED2 lights up.
Subsequently, LED2 stops to light up after 0.1 second. After 0.1 second, LED3
lights up. Subsequently, LED3 stops to light up after 1 second. After the end
of 0.6 second, the whole process repeats itself. Every ‘running’ occurs in the
0.6 second of one cycle.

Experiment3_3 After the code is uploaded, it is observed that three of the LEDs light up
simultaneously for 0.1 second. The, three of the LEDs stop lighting up
simultaneously for 0.1 second. Then, only LED1 lights up for 0.1 second.
Then, LED1 stops to light up. After 0.1 second, LED2 lights up. Subsequently,
LED2 stops to light up after 0.1 second. After 0.1 second, LED3 lights up.
Subsequently, LED3 stops to light up after 0.1 second. After the end of 0.8
second, the whole process repeats itself. Both the ‘blinking’ and ‘running’
occurs in the 0.8 second of one cycle.

Experiment3_4 After the code is uploaded, it is observed that three of the LEDs light up
simultaneously for 1 second. The, three of the LEDs stop lighting up
simultaneously for 1 second. Then, only LED1 lights up for 1 second. Then,
LED1 stops to light up. After 1 second, LED2 lights up. Subsequently, LED2
stops to light up after 1 second. After 1 second, LED3 lights up. Subsequently,
LED3 stops to light up after 1 second. Then, all three of the LEDs light up,
and subsequently the brightness of the LED steadily decreases from its
maximum to zero from t=8s to t=14s in steps of 1 second. After the end of
the 14 seconds, the whole process repeats itself.
8.0 Discussion

The experimental results and the mechanisms of the coding are obtained are then discussed for
Experiment 1, 2 and 3.

8.1 Discussion for Experiment 1 (LED Blink and Delay)

Based on the coding for Experiment 1, it is can be observed that no LED is initialized with a pin.
The reason to this is that Arduino boards are equipped with a built-in LED, whereby LED_BUILTIN
is set to the correct LED pin independent of which board is used. Therefore, the built-in LED can
be directly assigned as OUTPUT instead of needing to be initialized to another pin since it is
already connected to a pin (pin 13). Besides, a 220Ohm resistor is connected in series with the
LED on the breadboard to reduce the potential difference on the LED. This is because Arduino
boards have an operating voltage of 5V whereas for LED is within the range of 1.8V to 3.3V. The
void loop() is then assigned to enable all the coding written in it to loop endlessly. When the
second parameter of the digitalWrite command is set to HIGH, a voltage of 5V passes through
the circuit and 0V when is set to LOW.

The ‘blinking’ of the LED can be achieved by assigning the digitalWrite commands with
system delay response time at intervals of HIGH and LOW. The rate at which the LED blinks can
be adjusted by assigning the desired number within the command delay(). Therefore, when
delay(5000) is assigned as in Experiment1_2, the blinking period is 10 seconds (LED lights up for
5 seconds, LED does not light up for 5 seconds), which is at a slower rate than Experiment1_1.
Hence, when delay(500) is assigned as in Experiment1_2, the blinking period is 1 seconds (LED
lights up for 0.5 seconds, LED does not light up for 0.5 seconds)., which is at a faster rate than
Experiment1_1.
8.2 Discussion for Experiment 2 (LED Fading and Intensity Control)

Based on the coding for Experiment 2, it can be observed that the LED is initialized at pin 11.
This is because pin 11 is one of the pins (3, 5, 6, 9, 10) in the Arduino board with the PWM
function. The void loop() is then assigned to enable all the coding written in it to loop endlessly.
Pin 11 is then assigned as OUTPUT.

The intensity at which the LED lights up is controlled by the PWM enabled command only
available in PWM capable pins, which is analogWrite(). For instance, the brightness is set to
maximum by assigning analogWrite(led, 255) for Experiment2_1 and the ‘fading’ LEDs are
achieved by lowering the value of the second parameter of the command analogWrite().
Therefore, the LED stops to light up at all when analogWrite(led, 0) is assigned as in
Experiment2_1. The rate at which the LED ‘fades’ is controlled by the delay() command, which
assigns the system delay response time as discussed in the previous sub-chapter above. As the
PWM value is steadily decreased from 255, 200, 150, 100, 50 and 0, a considerable drop of light
intensity is observed for the LEDs after each time interval of 0.5 second, creating the ‘fading’
phenomena. Such is also the case for Experiment2_2, whereby the PWM value is steadily increase
from 0, 50, 100, 150, 200 and 255, whereby a considerable increase of light intensity is observed
for the LED for each time interval of 0.5 second, creating the ‘brighten’ phenomena. For
Experiment2_3, the ‘Fade-Bright’ phenomena is achieved by assigning the PWM values first in
descending order and then in ascending order, which coding is done by simply stacking the PWM
codes in Experiment2_1 and Experiment2_2.

8.3 Discussion for Experiment 3 (Control Multiple LED)

Based on the coding in Experiment3_1, it can be observed that three LEDs are used instead of
one as in Experiment 1 and 2. Three of the LEDs are initialized with the PWM capable pins, which
are LED1, LED2 and LED3 to pin 9, 10 and 11 respectively. The reason for this configuration is
for the LEDs to achieve the ‘brightening’ and ‘fading’ effect which is to be achieved in
Experiment3_4. LED1, 2 and 3 are then assigned as OUTPUTs using the pinMode() command,
that voltage from the board can be discharged to the LEDs are it to light up. The void loop() is
assigned subsequently so that all the commands can be repeated endlessly until the programme
is switched off.

To achieve ‘blinking’ effect for three of the LEDs, the digitalWrite() command is simply
assigned to each of the LEDs with the second parameter as HIGH, e.g [digitalWrite(LED1, HIGH)],
with a system delay response time of 0.1 second achieved by the delay(100) command. Similarly,
the remaining codes are similar, except that the second parameters are changed to LOW instead.
Therefore, the resultant result is that all three LEDs light up and successively does not light up
simultaneously for each respective 0.1 second, until the process repeats itself at the end of 0.2
second.

To achieve the ‘running’ effect for three of the LEDs, the digitalWrite() command is simply
assigned to each of the individual LEDs in steps of HIGH and LOW as shown in the coding of
Experiment3_2. From the coding, LED1 is set to HIGH for 0.1 second and then LOW for 0.1
second. Then, the sets of HIGH and LOW is assigned instead to only LED2 and likewise to LED3.
Therefore, the the individual LED lights up and then off when another LED is lighted in succession
at regular intervals of 0.2s until the whole process repeats itself at the end of 0.6s (completion
of 3 sets for the lighting on and off for the 3 LEDs). For Experiment3_3, the ‘blink’ and subsequent
‘running’ effect on the three LEDs can be run by simply combining the codes from Experiment3_1
and Experiment3_2 into the void loop() command.

For Experiment3_4, the LEDs are programmed to carry out the following commands in
sequence, beginning with ‘blinking’, ‘running’ and eventually ‘fading’. The first and second
sequence of ‘blinking’ and ‘running’ are simply achieved using the combined codes from
Experiment3_3, which is the stacking of coding from Experiment3_1 and Experiment3_2.
However, the system delay response time is set to 1 second for each transition of command to
allow any change in responses to be observed with relative ease using the delay(1000) command.
The final sequence ‘fading’ is achieved by simply stacking the coding from Experiment2_1 on the
Experiment3_3 coding. Therefore, the ‘blinking’ of the three LEDs happen under a duration of 2
seconds (light up for 1s, light off for 1s) and the ‘running’ of the LEDs 6 seconds (individual LED
light up for 1 second and then off for the subsequent 1 second, the other two LEDs repeats the
sequence). The ‘fading’ happen under the period of 6 seconds, with the PWM values assigned in
descending order of 255, 200, 150, 100, 50, 0 at intervals of 1 second for all three of the LEDs
simultaneously.

9.0 Conclusion

In conclusion, the blink and delay effect of LEDs can be achieved by turning the LEDs on and off
with the assigning of system delay response time at the end of the execution of each on off
command. The control of the intensity of the voltage supplied to the LEDs from the Arduino board
is accomplished by connecting the outputs to the PWM enabled analogue pins, which allows PWM
values to be assigned to the pin, which gives out the voltage output. Multiple LEDs can be
controlled by assigning pin-LED pairs by initializing it together. The objectives are achieved in this
experiment.

You might also like