Arduino Lab - 2019
Arduino Lab - 2019
This lab will teach you a little about basic electronics circuits and how to use a
small computer to control lights and make music. The small computer is called an
Arduino.
Plug in USB cable here.
Plug the other end
into the PC.
Check that one end of the USB cable is plugged into the Arduino and the other end into
your laptop..
1
Go to the File menu and then scroll down to Examples, over to 1.0 Basics, and then select
the Blink program.
In the dialog box which opens, select Libraries at the left, then the subdirectoriesin turn:
Documents, Arduino, Blink. Finally select the program, Blink or Blink.ino. (It will have
an Arduino icon next to the name.)
1. Blink Program
This will load the Blink program, which will make the light emitting diode (LED), which you
connected to Pin 13, turn on for a second and then turn off for a second. Read the program and
see if it makes sense. If not, please ask! The part inside setup is executed once, at the beginning.
It initializes pin 13 to be an output pin. The part inside loop executes over and over again,
forever, or until the Arduino is reset or reprogrammed. It turns the LED on (sends a high voltage
out to pin 13), waits 1000 mS=1 sec, turns the LED off (sends a low voltage out to pin 13), and
waits 1000 mS.
To load this program into the Arduino, put your mouse over the button which looks like a right
arrow. You will see the white word “Upload.” Click this Upload button. You should see the
program say “Uploading” followed by a message about the number of bytes (>1000) uploaded
into the Arduino.
Then the program should start running and blinking the LED!
2
2. TriColor LED
Now let’s try using a tricolor LED, which has 3 LEDs (Red, Green, Blue, i.e., RGB) all inside
the same package.
Then open the program TriColorLED using the same procedure as before.
Read the program. The part inside loop calls 2 subroutines: mainColors, and showSpectrum.
red, green, blue, then yellow, cyan, purple, and finally white.
showSpectrum changes the intensity (brightness) of the LED and steps through all of the colors
of the rainbow.
Upload the program into the Arduino. Enjoy the colors! You can try to modify this program also.
3
3. Making Music!
Download the 2 music programs, Song1 and Melody from the Class Smartsite under Resources,
Arduino Programs.
We can try 2 different music programs. Each plays a different melody. Open each program in
turn, upload each, and listen to the melody it produces. Now try to modify one of the programs to
play a different melody!
A. First try Song1.This program has 2 arrays to keep track of the notes. The notes array lists
the names of the notes, with a space to indicate a rest (i.e., no tone). The array beats lists
the durations for each note, with 1 meaning quarter note, 2 meaning half note, etc. The
duration of the rests must be specified also. The subroutine frequency takes the name of
the note as an argument and returns the frequency. The song only plays once, but you can
play it again by pushing the Reset button on the Arduino.
B. Then try Melody. This program works in a completely different way. The note
frequencies are specified in the file “pitches.h,” which must be in the same directory as
Melody.ino. A preprocessor substitutes the frequencies for the names of the notes before
the program is compiled. Then the compiler takes the code and makes the executable
program which you upload. Pitches.h defines 8 octaves of note frequencies, together with
sharps, so you can play any note which would occur on a piano keyboard. You specify
the sequence of notes in the array melody, and the lengths of the notes in the array
noteDurations, where 4 = quarter note, 8 = eighth note, etc.