School of the Future Name: Mr. Z / Physics II Date:

Arduino Exercise #2: Making an LED Blink

In this activity we will connect an LED to an Arduino, and then program the Arduino such that it causes an LED to blink.

Construct the Hardware

1. Gather the following materials: a. One Arduino & USB Cable b. One breadboard & two jumper wires c. One LED & One 220Ω Resistor 2. Construct the following setup:

a. Connect one jumper wire from GND (“Ground”) to one of the (-) columns. b. Place one leg of the resistor in the same column as the GND wire. Place the other leg of the resistor in a new row. c. Place the short leg of the LED to the same row as resistor leg. Place the long leg in a new row. d. Connect one jumper wire from the previous row to Digital Pin 10. e. Check that your setup matches the setup in the diagram above. If it does, put your yellow cup up for Mr. Z to check it. Create the Program 3. Type up the associated program for Exercise #2 (see next page). a. Keep in mind that capitalization and spelling matter. Also, most lines of the code end in a semi-colon. 4. Save the file as LED_Blink 5. To check that the Arduino can compile your code, “Verify” it. You can do this by either pressing Command + R or by clicking on the Verify button (it looks like a check mark). a. NOTE: This does not upload the program to the Arduino board. This operation only checks to make sure that the Arduino will be able to execute it. b. NOTE: If the file does compile without errors, this does not necessarily mean that there are no issues with your code.

Troubleshoot Errors 6. At the bottom of the IDE window, you’ll see error messages pop up in orange font. The computer will tell you where (what lines) it believes the errors in the program are (though it is not always right). 7. Start by reading the first error message and checking that line of code. Compare what you’ve written to the original program. Common errors: a. If you get the message “______was not declared in this scope”, it is possible you misspelt one of the variables or functions. b. If you get the message “expected ‘;’ before ____”, you forgot to add a semi-colon at the end of the previous line.

Run The Program

8. Using the USB cable, connect your Arduino to your computer. You should see some lights on the Arduino board flash. The Arduino will start to execute the last program that was uploaded to it. 9. From the “Tools” menu in the Arduino IDE, make sure the appropriate Serial Port is selected. 10. Now upload the “LED_Blink” sketch. To do this, hit Command + U. (Or, click on the Upload Button in the Arduino IDE, which looks like an arrow pointing to the right.) 11. After a few seconds you should notice the LED start to blink! If it is not blinking, check your hardware first, and then your code. /* Arduino Exercise 2: LED_Blink Turns on an external LED on for one second, then off for one second, repeatedly. */

// The setup() routine runs only once: void setup() {

// Set the digital pin as an output. pinMode(10, OUTPUT);

}

// The loop routine runs over and over again forever: void loop() {

digitalWrite(10, HIGH); delay(1000);

digitalWrite(10, LOW); delay(1000);

} Reflection

1. Figure out how to change the blink-rate of the LED. For example, try to get the LED to blink twice as fast or twice as slow. Describe what you had to change in the program to do this.

2. What do you think the function delay( ) does? What does the number in the parentheses mean? Once you’ve written a description, Google it (“Arduino delay”) and see what you find out. Add to/modify your description as needed.

3. Make the LED blink very slowly (like 5s on, 5s off). Grab a multi- meter. Measure the potential difference (voltage) between: a. Pin 10 and GND when the LED is on. b. Pin 10 and GND when the LED is off. (You will need to pull the jumper wires slightly upwards out of Pin 10 and GND so you can touch the exposed metal on each wire.)

4. What do you think the function digitalWrite( ) does? What information must go in its parentheses for it to work? Once you’ve written a description, Google it (“Arduino digitalWrite”) and see what you find out. Add to/modify your description as needed.

5. What do you think the function pinMode( ) does? What information must go in its parentheses for it to work? Once you’ve written a description, Google it (“Arduino pinMode”) and see what you find out. Add to/modify your description as needed.

Trouble Shooting

6. SOFTWARE: Make the following simple errors SEPARATELY (one at a time) in your code. For each one, record the general error message that pops up. Explain how the IDE is trying to convey the problem to you. Then correct that error and go on to the next one. a. Remove a semi-colon from one line. b. Remove a bracket from one line. c. Misspell “setup”. d. Delete “10,” from inside one of the digitalWrite( ) functions. e. Make some other error of your choosing in the code.

7. HARDWARE: What mistakes could you have made in your physical setup? Identify and write about at least two mistakes. Add drawings to make it clear as to what the error is.

Alternatives (PICK TWO)

1. Figure out how to change the blink pattern of the LED. For example, get the LED to blink twice fast and then twice slowly (repeatedly). a. Describe the significant changes you made to the program to accomplish this in your notebook. b. Save the new program with a different name and store it in your folder.

2. Figure out how to add a second LED and get it to blink opposite the first LED (when the 1st one is on, the 2nd one should be off). a. Sketch the new setup in your notebook. b. Describe the significant changes you made to the program to accomplish this in your notebook. c. Save the new program with a different name and store it in your folder.

3. Figure out how to add a third LED. Then make each LED blink with a different pattern. For example, make the first LED blink twice fast, then the second LED blink twice at a medium speed, and then third LED blink twice at a slow speed.