Ir. Amran: University Malaysia Sabah Faculty of Engineering
Ir. Amran: University Malaysia Sabah Faculty of Engineering
Ir. Amran: University Malaysia Sabah Faculty of Engineering
Faculty of Engineering
KM42801 Laboratory V
Semester 8 2018/2019
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.
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.
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
This section describes the materials needed and the procedures to achieve the objectives of the
experiment.
3.1 Materials
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
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.
Figure 3.2 : Circuit connection (left) and the schematic (right) for multiple LED controls
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.
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 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.
The connection of the Arduino board circuit is as shown in Figure 7.1 below.
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.
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.
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.
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.
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.