Professional Documents
Culture Documents
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.
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.
University at Buffalo
Spring 2015
University at Buffalo
Spring 2015
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
University at Buffalo
Spring 2015
Figure 4: Select the Correct Board Type (Arduino Mega or Mega 2560)
University at Buffalo
Spring 2015
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);
digitalWrite(RedLEDpin, LOW);
delay(1000);
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.
University at Buffalo
Spring 2015
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.
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.
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