You are on page 1of 9

MAE 334: Mechanical and Aerospace Engineering Lab I

Department of Mechanical and Aerospace Engineering

University at Buffalo
Spring 2015

Arduino: Tutorial 1
Tutorial Overview
In this tutorial, you will learn the basics of using an Arduino board. The Arduino board contains
a microprocessor and connection points. The microprocessor allows you to control what happens
with the outputs and inputs which can be hooked up to the connection points. The Arduino
board can be used to interface with different hardware setups to implement control of systems,
data collection, etc.

Tutorial Purpose and Outcomes


The purpose for this tutorial is to familiarize students with the basic process of using the Arduino
board. There are two phases to using the Arduino board. The first is generating the logic (called
a sketch) that will be used to control the hardware. The second is connecting the hardware to
the board. In completing the tutorial, you should be able to achieve the following outcomes:
Outcome 1: Understand the fundamental components of an Arduino Sketch.

Understand what is required for the Arduino Sketch to work

Understand how the Arduino Sketch can be modified to change overall system behavior

Outcome 2: Understand how basic hardware is interfaced with the Arduino board.
The two tutorials are not intended to cover all aspects of using an Arduino board. These tutorials
are intended to provide you with enough knowledge to comfortably work with the Arduino
board in the final lab and the course project. Students interested in further pursuing this subject
are encouraged to seek further information through books, the online Arduino community, or
fellow students.
References:
http://arduino.cc/en/Reference/
http://playground.arduino.cc/Projects/Ideas

System Setup
The physical system consists of an Arduino board coupled with a breadboard. The breadboard
allows electrical components to be easily and temporarily connected (no soldering required). The
Arduino board and breadboard are shown in Figure 1. The software environment used to create
sketches is shown in Figure 2.

Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

MAE 334: Mechanical and Aerospace Engineering Lab I


Department of Mechanical and Aerospace Engineering

Figure 1: Arduino Mega 2560 Board and Bread Board

Figure 2: Arduino Sketch Environment

Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

University at Buffalo
Spring 2015

MAE 334: Mechanical and Aerospace Engineering Lab I


Department of Mechanical and Aerospace Engineering

University at Buffalo
Spring 2015

Connecting the Board and Uploading a Sketch


The process of uploading your Arduino Sketch to the board consists of two steps. The first step
is to verify the sketch; the icon is the check mark (see Figure 3). Verifying the sketch compiles
it, checking for syntax errors and turning the code into something the microcontroller can process.
The second step is to upload the sketch; the icon for upload is the right facing arrow (see Figure
3).

Figure 3: Controls for Uploading an Arduino Sketch

To upload your sketch you need to ensure that the correct port and board are selected in the
Arduino environment. To select the correct board type go to Tools >> Board >> Arduino Mega or
Mega 2560 (see Figure 4). Also ensure the correct processor (i.e. 2560) is selected (see Figure 4a).
To select the correct port go to Tools >> Serial Port then select the port indicated in the lower right
of the Arduino environment (see Figure 5).
Once you have selected the correct board and serial port, you will be able to upload sketches to
your Arduino board. If you perform this process incorrectly, you will be able to verify your
sketch, but you will get an error when you try and upload your sketch.
Incorrect Board:
avrdude: stk500_getsync(): not in sync: resp=0x00.
Incorrect Serial Port:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

MAE 334: Mechanical and Aerospace Engineering Lab I


Department of Mechanical and Aerospace Engineering

University at Buffalo
Spring 2015

Figure 4: Select the Correct Board Type (Arduino Mega or Mega 2560)

Figure 4a: Ensure the 2560 processor is selected

Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

MAE 334: Mechanical and Aerospace Engineering Lab I


Department of Mechanical and Aerospace Engineering

Figure 5: Select the Correct Serial Port

Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

University at Buffalo
Spring 2015

MAE 334: Mechanical and Aerospace Engineering Lab I


Department of Mechanical and Aerospace Engineering

University at Buffalo
Spring 2015

Creating a Sketch
To create a sketch, follow the basic format below; the setup and loop functions are required,
and more complex sketches will have additional functions. There are also many predefined
Arduino functions (see the reference link provided). Commented text shows up as gray, and is
used to document what the sketch is doing. Other reserved words show up as orange (functions
and data/variable types) or blue (constants). Note that the default time unit is milliseconds.

// Sketch Title
// Used to Comment One Line of Code
/*
Used to Create a Block Comment
*/
//Define Global Variables
int RedLEDpin = 13;
void setup()
{
// The setup function is run once when the sketch is uploaded, or if
// the reset button is pressed
// Declare Pins Being Used as Output or Input
pinMode(RedLEDpin, OUTPUT);
}
void loop()
{
// The loop function runs after setup and continues on indefinitely
// until your turn off the board
digitalWrite(RedLEDpin, HIGH);
// Turn on the LED
delay(1000);

// Wait for one second

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

// Turn off the LED

// Wait for one second

Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

MAE 334: Mechanical and Aerospace Engineering Lab I


Department of Mechanical and Aerospace Engineering

University at Buffalo
Spring 2015

Test Sketches
For each test sketch you will be examining how the hardware and software interact to control the
behavior of the system (in this case, a blinking LED). While simple, it is enough to determine if
you have the ability to control a system.
Breadboard Short Course
Each breadboard is a little different in terms of what connections the board has (see
manufacturing specifications for details (and often you can see the connections by looking at the
back of the board. For the bread board provided in the lab, the connections are shown in Figure
6. Connections between components are made on the same row of the breadboard; though the
divider between column e and f on the lab boards separates each row into (effectively) two
different rows (i.e., row 1 a-e and row 1 f-j). The positive and negative rails are joined on each
side, but power must be provided to each side or the two sides must be connected with jumper
wires.

Figure 6: Breadboard Diagram


OneBlinkingLED
Setup the circuit that is shown in Figure 7, paying careful attention to the polarity of the LED legs.
Open the OneBlinkingLED sketch. Check to make sure the correct board and serial port are
selected. Verify the sketch and upload it. If done correctly, your LED will turn on for one second,
then off for one second.

Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

MAE 334: Mechanical and Aerospace Engineering Lab I


Department of Mechanical and Aerospace Engineering

University at Buffalo
Spring 2015

Figure 7: Circuit Setup for the First Two Sketches

OneBlinkingLED Modifications
Modify the sketch so that the LED is lit for 3 seconds and off for 1 second.
OneBlinkingLEDNoDelay
Use the same circuit from the previous exercise shown in Figure 7. Open the sketch named
OneBlinkingLEDNoDelay. Verify the sketch and upload it. Confirm that this sketch has the same
behavior as OneBlinkingLED.
OneBlinkingLEDNoDelay Modifications
Modify the sketch so that the LED is lit for 2 seconds and off for 0.5 seconds.

Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

MAE 334: Mechanical and Aerospace Engineering Lab I


Department of Mechanical and Aerospace Engineering

University at Buffalo
Spring 2015

TwoBlinkingLED
Open the TwoBlinkingLED sketch. Modify the circuit shown in Figure 7 to include the additional
green LED. Verify the sketch and upload it. Note that the pin to be used is already specified in
the sketch. Verify the sketch and upload it. If done correctly, your LED will turn on for one
second, then off for one second.
TwoBlinkingLED Modifications
Modify the sketch so that the green LED is lit while the red LED is off, and vice versa.
PotControlBlinkingLED
Setup the circuit that is shown in Figure 8. Open the PotControlBlinkingLED sketch. Verify the
sketch and upload it. Adjust the potentiometer such that the LED is on/off for roughly 1 second.

Figure 8: Circuit Setup for PotControlBlinking LED

PotControlBlinkingLED Modifications
Modify the sketch so that the LED is off for twice as long as it is on.
Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

You might also like