Arduino Labs – Lab 4 v.2University of Hartford CS 110
Lab 4 – Arduino and output to components
Discussion
We can use the output pins on the Arduino board to
control electrical components. Look at the “Digital”
side of the Arduino board. Digital pins can output 5
volts (when “on”) and 0 volts (when “off”). In
addition to the pins labeled 0 through 13, we have a
GND pin. We’re not going to go into the theory
behind “ground” as it applies to electronics, but
essentially for this board, the ground pin is the return
from all circuits from the other pins on the board.
Pins on the Arduino board can be used for input
(software reads the status of the pin) or output
(software turns the pin on/off). Thus, we need the
program to define each pin we’re using, except for
GND, as being Input or Output. We do this with the
pinMode() function, which requires us to specify
which pin, and then whether it’s output or input. For example, pinMode(2, OUTPUT) sets pin 2 to be
output, and pinMode(5, INPUT) sets pin 5 to be input. If we want to write to a pin using software,
we use the function digitalWrite(), which requires us to specify which pin, and then the value we
want to write, which must be either HIGH or LOW. If we write HIGH, it is essentially “turning on” the
pin, whereas if we write LOW, it’s like turning the pin off. For example, digitalWrite(2, HIGH)
makes pin 2 into a 5-volt battery, but digitalWrite(2, LOW) turns it off.
Objective
Use Arduino to control LEDs.
What you need
1) Arduino software running on computer, with computer connected to Elegoo UNO R3 via USB
cable. Don’t forget to configure the port to which the UNO is connected (Tools Menu →
Port → Choose port) if you didn’t already do this today.
2) Red, green, and blue LEDs.
3) three 220 resistors (red-red-black-black-brown).
4) Protoboard
5) A bunch of wires
Outline
1) Execute program to blink one LED.
2) Execute program to blink three LEDs.
3) Execute program to sequentially blink LEDs
Page 1
Arduino Labs – Lab 4 v.2
University of Hartford CS 110
Directions
1) Execute program to blink one LED.
We’re going to connect one LED (with its resistor) to the Arduino board and run a program that
turns it off and on repetitively, on for 1 second, off for 1 second, etc.
a) Connect a 220 resistor and the longer lead (anode) of
the blue LED into one 5-hole row on the protoboard.
b) Connect the shorter lead (cathode) of the blue LED
along with one end of a black wire to one of the 25-hole
(negatively labeled) rows on the protoboard.
c) Connect the other end of the resistor along with one end
of a blue wire to an unused 5-hole row on the
protoboard.
d) Connect the other end of the black wire to GND on the
protoboard and the other end of the blue wire to pin 2 on
the protoboard.
e) Create a program in the Arduino editor with the following
code:
Page 2
Arduino Labs – Lab 4 v.2
University of Hartford CS 110
Look at each line of the program and from what we’ve already learned, try to understand what
each function is doing.
f) Run the program. The LED should turn on and off, each for one second.
g) Let’s re-write this program to follow some coding best practices. For variables that don’t
change, it’s good practice to define them at the top of the program. Change the code as
below, and re-run the program. Validate it’s still blinking, exactly as before.
h) Experiment with different on- and off-times and re-run the program. Note the changes in the
blinking of the light.
2) Execute program to blink three LEDs.
Now we’re going to add another two LEDs (with resistors) using the same pattern as we
implemented with the blue on.
a) For both the red LED, connect its anode (as well
as a 220 resistor) into an unused 5-hole row on
the protoboard.
b) Connect the opposite end of that resistor along
with a red wire into an unused 5-hole row on the
protoboard.
c) Connect the opposite end of the red wire to pin 3
on the protoboard.
Page 3
Arduino Labs – Lab 4 v.2
University of Hartford CS 110
d) For both the green LED, connect its anode (as well as a
220 resistor) into an unused 5-hole row on the
protoboard.
e) Connect the opposite end of that resistor along with a
green wire into an unused 5-hole row on the protoboard.
f) Connect the opposite end of the green wire to pin 4 on
the protoboard.
g) Connect the negative terminals (cathodes) of the red and
green LEDs to the same 25-hole row on the protoboard
as has the cathode of the blue LED.
h) Try to improve the code from our last program to make
all three LEDs blink. Compile and execute it.
i) When you’re ready, use the code below. We have
created some reusable functions for turning the LEDs on
and off in this version. Try out this one. Compile and
execute it.
j)
Adjust the previous program to make the LEDs stay on for a half second but off for 2
seconds.
Page 4
Arduino Labs – Lab 4 v.2
University of Hartford CS 110
3) Execute program to sequentially blink LEDs
We want to use the same wiring from the last exercise, but this time make the LEDs blink
sequentially for 1 second each with a half-second between each.
a) Try to adjust the code yourself first, thinking about just changing the code in the loop()
function.
b) Is this how you did it? If not, or you couldn’t figure it out, try this:
c) There are typically multiple ways to solve most programs in code. Here is another solution to
that problem. See what’s different? See how it makes the loop() function shorter?
Page 5
Arduino Labs – Lab 4 v.2
University of Hartford CS 110
Questions
1) Why do you think it’s good to define the pins and other non-changing variables at the top?
2) Why did we choose the wire colors we chose?
3) Why is it better to separate out the turnOn() and turnoff() functions than to repeatedly call
the digitalWrite() function from within the loop function?
4) How could you add a fourth LED? What else would you need to do?
5) What if we wanted to make different LEDs blink for different periods of time? What would you
have to do?
6) What problems did you encounter with this lab and how did you resolve them?
Page 6