Exercise 2 Blink an LED
Objective: Use Arduino to turn an LED on and off.
To turn in: Your working Arduino code and the Blink Worksheet (handed out in class).
Filename Example: name1-name2_blink.ino
Introduction
This exercise will explore two concepts: (1) connecting devices to the Arduino and (2) the structure of the Arduino program used to control those devices. As a first example, we’ll connect an LED (light-emitting diode) to the Teensy and write a program to turn it on and off.
How are things connected to a Teensy?
The Teensy can control all sorts of devices, including lights, motors, and various sensors. The device to be controlled must be connected to one of the Teensy’s pins. You can think of a pin as a unique port or connection on the Teensy. Pins can send and receive information, so communication between the pin and the sensor goes both ways. One pin might be used to control a heater, while another might be used to read data from a temperature sensor. The pins that are available on the Teensy are shown in the figure below. Some pins provide power (3.3 V), some pins provide a connection to the circuit ground (GND), and some function to send and receive data. A lot of pins have several functions, each represented by a different color rectangle on the diagram. Note, though, that each pin can only have one function at any given time. For now, we’ll just focus on the pin numbers. Those labeled 0-33 (shown in grey) are called digital pins; they function as both inputs (to receive data, or sense) and outputs (send data, or control), but we’ll use just the output functionality in this exercise.
There are several ways to connect something to the pins of the Teensy. One way is to directly solder wires to them, in which a soft metal alloy is melted onto two the wire and the Teensy pins as they are connected to make a permanent, metal connection. This is best saved for a final, fully vetted design that won’t ever need to be changed. We’ll use another method for this exercise called a solderless breadboard.
The solderless breadboard
A breadboard is shown in the figure below. The holes in the breadboard are spaced in such a way that a Teensy can be plugged directly into the board. Columns, labelled A, B, C, …, are connected across the board electrically, such that A1 and B1 are connected; rows are insulated from each other, such that A1 and A2 are not connected. The connections labeled with red and blue lines on each edge are connected together and are sometimes called rails; they provide an easy way to distribute power (e.g. the red rail) and ground (e.g. the blue rail) connections around the board. If that seems confusing, try watching this video this video.
What is a program?
A program, or sketch in Arduino jargon, is a set of commands stored on the Teensy that tell it what to do. Although a Teensy can do most anything a computer would do, it can only run a single program over and over. For example, this exercise involves programming the Teensy to turn an LED on and off so that it blinks. Other options would be to collect data from a temperature sensor, or turn a motor on a off to move a robot (or to do all of those things at once).
An Arduino program always consists of two parts: (1) a setup()
function and (2) a loop()
function (more details about what a function is will come in future exercises). The setup()
function runs once every time the Teensy is powered on. After that, the Teensy runs the loop()
function over and over until it’s turned off. Usually, some initial lines of code are included above the setup()
function provide definitions used throughout the program. A “bare minimum” program with the setup()
and loop()
functions is shown below. Note that this program does nothing other than run.
void setup() {
// anything written here will execute exactly once on startup
}
void loop() {
// anything written here will execute over and over indefinitely until the power is turned off
}
Arduino sketches (programs) are written with the Arduino IDE (see Chapter 1). To begin, you open the IDE and write your code in the text area of the program. When your’re done writing your code, you compile it and upload it to the board to run it. If it compiles and uploads correctly, you will see a “Success” message in the message are of the IDE; if not, you’ll see an error message and will need to troubleshoot your code.
You should use comments when writing your program to clairfy what each part does. Comments are not read by the compiler/board, and do not execute. In other words, they are only to help humans reading the code understand it more easily. To add a comment, just use the \\
symbol; anything after that symbol won’t be read by the program. For example, you can make an entire line a comment, like the first line below.
You could also make a portion of a line a comment, like the second half of the line below.
Finally, note that every line in the program must end with a semi-colon, ;
.
In the exercise, we’ll connect an 3-color LED to a Teensy and write a sketch to control it.
2.1 Connecting the LED
Push your Teensy into the breadboard provided and connect the USB cable. Try not to bend the pins as you push it in. Refer to the diagram below if necessary.
REMEMBER, BE EXTREMELY CAREFUL WITH THE USB CONNECTION ON THE TEENSY. THEY BREAK VERY EASILY!!
Push the LED into the breadboard such that none of the leads (wires) are connected to anything else (each one is in it’s own row). Make sure you note which row the longer lead goes into.
Always work with the Teensy unplugged from the computer. This will help ensure no “magic smoke” escapes.
Use a jumper wire to connect the longer lead on the LED to the GND pin on the Teensy.
Use 3 more jumper wires to connect the remaining three leads to pins 13, 14, and 15.
2.2 Program the Teensy
2.2.1 One Color
Open the Arduino IDE and copy the Arduino code you created in Tinkercad when completing the extra credit exercise. If you did not complete the exercise, instead go to File > Examples > Basics > Blink.
Go to
File > Save As
to save the example under a new name in the default location (Documents/Arduino
). Call it groupname_blink.ino.Remember, files that don’t follow the naming convention exactly will incur point deductions.
You should see the
setup()
function at the top of the sketch. In this case, the only setup necessary is to set the mode of the LED pin (pin 13) as an output. If yoursetup()
code is slightly different from what’s shown below, change it to match. Remember that anything after a//
is a comment and isn’t compiled.
/ / the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(13, OUTPUT);
}
- Below that you should see the
loop()
function, which runs over and over again.
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
- After checking to make sure those sections look good, press the Upload to button (right arrow in top left) to save, compile, and upload your sketch.
2.2.2 Three Colors
You should have seen a single color blinking on and off. Now, make some modifications so that all three colors blink on and off.
- Set each of the LED pins to outputs in the
setup()
function. You will have to fill in the blanks for the red and green pins below.
// the setup routine runs once when you press reset:
void setup() {
pinMode(13, OUTPUT); // initialize pin 13 as an output.
pinMode(____, OUTPUT); // initialize pin 12 as an output.
______________________ // initialize pin 11 as an output.
}
Have you saved your work yet? The IDE doesn’t autosave, so make sure you save it frequently!
- Finally, add the extra colors to the
loop()
function.
// blue
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
// green
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
// red
digitalWrite(___, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(___, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
- Make sure you show your code and blinking LED to your instructor before you move on.
2.2.3 Make Your Own Changes
- Before you leave, modify your code to change the blink pattern. You can change the sequence of the colors, the time between blinks, the number of blinks, or the amount of time each color is on.
2.2.4 Turn in
A hardcopy of your completed Blink Worksheet.
An electronic copy of your final Blink code (submit to Dropbox).
Remember, files that don’t follow the naming convention exactly will incur point deductions.