LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department...

17
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 1 EEG 101L INTRODUCTION TO ENGINEERING EXPERIENCE LABORATORY 1: INTRODUCTION TO ARDUINO IDE AND PROGRAMMING DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING UNIVERSITY OF NEVADA, LAS VEGAS 1. FYS KIT COMPONENTS The FYS kit will include all the components necessary to perform the experiments following this section. Please verify that your kit contains all the listed components and make sure to notify any necessary parties if they do not. 1. 01x Arduino UNO https://www.sparkfun.com/products/11021 2. 01x USB A to B Cable https://www.sparkfun.com/products/512 3. 01x Danger Shield https:// www.sparkfun.com/products/10570 4. 01x TB6612FNG Dual Motor Driver Carrier http://www.pololu.com/catalog/product/713 5. 02x 50:1 Micro Metal Gearmotor http://www.pololu.com/catalog/product/1098 6. 01x Sharp Digital Distance Sensor 2-10cm http://www.pololu.com/catalog/product/1134 7. 01x Zumo Chassis Kit (No Motors) http://www.pololu.com/catalog/product/1418 8. 01x QTR-8RC Reflectance Sensor Array http://www.pololu.com/catalog/product/961 9. 20x Jumper Wires Premium 12" M/M https://www.sparkfun.com/products/9388 10. 01x Arduino Prototyping Shield http://tinyurl.com/8qmwpw8 11. 4x Rechargable AA Batteries 12. 1x 9V DC Adapter 13. 1x 9V Battery 14. 1x LED 15. 1x 100 ohm Resistor

Transcript of LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department...

Page 1: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 1

EEG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

LABORATORY 1: INTRODUCTION TO ARDUINO IDE AND PROGRAMMING

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING UNIVERSITY OF NEVADA, LAS VEGAS

1. FYS KIT COMPONENTS

The FYS kit will include all the components necessary to perform the experiments following this

section. Please verify that your kit contains all the listed components and make sure to notify

any necessary parties if they do not.

1. 01x Arduino UNO https://www.sparkfun.com/products/11021

2. 01x USB A to B Cable https://www.sparkfun.com/products/512

3. 01x Danger Shield https:// www.sparkfun.com/products/10570

4. 01x TB6612FNG Dual Motor Driver Carrier http://www.pololu.com/catalog/product/713

5. 02x 50:1 Micro Metal Gearmotor http://www.pololu.com/catalog/product/1098

6. 01x Sharp Digital Distance Sensor 2-10cm http://www.pololu.com/catalog/product/1134

7. 01x Zumo Chassis Kit (No Motors) http://www.pololu.com/catalog/product/1418

8. 01x QTR-8RC Reflectance Sensor Array http://www.pololu.com/catalog/product/961

9. 20x Jumper Wires Premium 12" M/M https://www.sparkfun.com/products/9388

10. 01x Arduino Prototyping Shield http://tinyurl.com/8qmwpw8

11. 4x Rechargable AA Batteries

12. 1x 9V DC Adapter

13. 1x 9V Battery

14. 1x LED

15. 1x 100 ohm Resistor

Page 2: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 2

2. INTRODUCTION TO ARDUINO IDE AND PROGRAMMING

GOAL:

This chapter will familiarize the user with the installation process of the Arduino Uno drivers

and Arduino IDE on a Windows PC. The user will learn how to connect the Arduino board to

the PC, install the necessary drivers, install the IDE, and familiarize themselves with the Arduino

programming language.

OBJECTIVES:

Connect the Arduino Uno to a Windows PC

Install Arduino IDE

Install Arduino Drivers

Familiarize user with Arduino programming language and syntax

OVERVIEW AND REQUIREMENTS:

This experiment only requires a basic knowledge of programming and computer usage.

COMPONENTS:

Page 3: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 3

Host PC

Arduino UNO

USB A-B Cable

Arduino Software [http://arduino.googlecode.com/files/arduino-1.0.1-windows.zip]

SCHEMATICS:

Page 4: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 4

PROCEDURE:

Driver and IDE installation

1. Download the the Arduino software onto the host PC (standalone version)

https://www.arduino.cc/download_handler.php

2. Extract the contents of the Arduino zip file.

3. Connect the Arduino UNO into the PC using the USB cable.

4. Go to StartComputerRight ClickProperties.

5. Open the Device Manager located under Control Panel Home.

Page 5: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 5

6. From the device manager, right click the unknown device and select “Update Driver

Software...”

7. Select “Browse my computer for driver software”.

Page 6: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 6

8. Search for the “drivers” folder from the extracted zip directory.

9. Windows will detect that the drivers in this directory are unsigned and will request that

you formally accept installing them. Select “Install this driver software anyway”.

Page 7: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 7

10. This will complete the installation of the Arduino Uno Drivers. Your device manager

should reflect these changes.

Page 8: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 8

Exploring the Arduino IDE

The Arduino IDE is the software on the host PC that is used for writing programs (referred to as

sketches), as well as for sketch uploading and commnication with arduino devices. It has a built

in text editor, messaging area, text console, and a toolbar based menu with functions and

commands. In order to open the Arduino IDE, run “arduino.exe” found in the extracted folder.

The following is an example of what should appear.

Verify: Checks your code for errors.

Upload: Compiles your code and uploads it to the Arduino I/O board.

Page 9: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 9

New: Creates a new sketch.

Open: Presents a menu of all the sketches in your sketchbook. Clicking one will

open it within the current window.

Save: Saves your sketch.

Serial Monitor: Opens the serial monitor.

Under the Tools menu there are options to select the appropriate Serial port and Board type you

will be programming on. For these experiments we will always be using Tools Board

Arduino Uno and the appropriate Com port listed under device manager for the installed Arduino

Uno. The appropriate Com port can be found through the device manager, which it should say

“Arduino UNO R3 (COMx)”, where x is your Com port.

Programming Basics

The Arduino’s programming language is a hybridized C/C++ language with a customized set of

predetermined functions that directly interface with the Arduino board. Each program created is

called a sketch. After a sketch has been compiled and verified, it is translated to machine

language used by the Arduino Board and uploaded with the use of the Arduino IDE.

Page 10: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 10

Structure

The Arduino programming language has a basic structure consisting of a bare minimum of two

parts. In order for a program to work, it a must to include both the setup() and loop() functions.

The following is the structure for both functions.

void setup()

{

//Setup Contents

}

void loop()

{

//Loop Contents

}

Note: Any text preceeded by “//” is a programmer comment that is ignored by the compiler and

therefore will not be included in program execution.

The instructions in the setup() are performed only once during program execution. It is used for

various tasks such as to initialize the pin modes of Arduino, as well as initialize various

functionality such as Serial. Even if there are no statements contained within the setup fucntion,

it is still necesary to include the setup function in your code.

void setup()

{

pinMode(13, OUTPUT); //Set Pin 13 as an output.

}

The loop() function contains the main body of your program. The loop function will loop

consecutively, using its contents to control and program the Arduino.

void loop() //Constantly Loop.

{

digitalWrite(13, HIGH); //Turn Pin 13 on.

delay(500); //Pause for 500ms.

digitalWrite(13, LOW); //Turn Pin 13 off.

delay(500); //Pause for 500ms.

}

Variables

A variable is a place for storing datum, a single piece of data. Each variable is defined with the

use of a type, name, and value. For example:

int pin = 13;

This creates an integer variable whose name is pin and has a value of 13. With this variable we

can replace all instances where the number 13 is hard coded with the variable pin instead. So in

Page 11: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 11

the previous structure example all instances of “13” can be replaced with “pin” and would still be

functionally correct.

Variables allow a great deal of flexibiltiy in your program. They allow you to change the value

of the pin instead of the value of all instances of 13 if you ever decide to use a different pin. If

you select variable bnames that relate to the contents of the variable such as “pin” or “count”, it

also improves the readability of your code, making it much easier to understand.

Variable Scope

The location of where a variable declared also explicitly defines where it can be used. If a

variable is declared outside of both the setup() and loop() functions, it is considered global and

accessible from anywhere in the program. This means that the variable has a global scope. If a

variable is declared in those respective functions, then they are scoped to those regions.

The scope of a variable is determined through the idea of nesting. Nesting means that a variable

is usable in the area in which it is declared and all areas nested into it. If we take into

consideration varaibles declared withinsetup() and loop(), they are on the same level, so

variables declared in one will not be inherited by the other. If a variable is declared within

setup() or loop(), then it is accessible by that function and only other functions nested within it.

Functions

Functions are blocks of code that can be called and used repeatedly throughout a program.

Within the Arduino programming language there are several built-in functions such as setup(),

loop(), and various other functions. Functions are useful in that they allow you simply call a

function to use its code instead of rewriting it. Rather than constantly repeat a block of code to

handle a specific task, a function may be called to do that task instead.

Below are some examples of basic Arduino functions that are very useful to learn. More built-in

functions will be discussed in later chapters.

The pinMode() function configures an Arduino board’s pin as either an input or an output

pin. The function is passed a pin value (usually stored in a variable) and whether it is an

input or an output. For example:

pinMode(pin, INPUT);

or

pinMode(pin, OUTPUT);

The digitalWrite() function sets the value of a pin to a logical 0 (LOW) or a 1 (HIGH).

This value is typically representative of 5v and GND (0V) respectively. For example, a

logical HIGH is written by:

digitalWrite(pin, HIGH);

Page 12: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 12

The delay() function causes the Arduino board to pause for a set amount of time in

milliseconds before continuing to the next line of code (instruction).

int delaytime = 1000;

delay(delaytime); //Delay for 1000ms or 1s.

Libraries

Libraries are a collection of functions/procedures related to a specific task. There are several

user written Libraries available for the Arduino, such as libraries used to control Servo motors,

LCD’s, and various other peripherals.

To use an existing library in a sketch simply go to the Sketch menu, choose "Import Library",

and pick from the libraries available. This will insert an #include statement at the top of the

sketch for each header (.h) file in the library's folder. These statements make the public functions

and constants defined by the library available to your sketch. They also signal the Arduino

environment to link that library's code with your sketch when it is compiled or uploaded.

Libraries are typically included in “~\Arduino\libraries\”. To add your own library, use Windows

to create a new directory in the libraries directory with the name of your library. The folder

should contain a C or C++ file with your code and a header file with your function and variable

declarations. It will then appear in the Sketch | Import Library menu in the Arduino IDE.

Using Serial Monitor

Communication is enabled by the use of the Serial Library, which is already included and does

not need to be added like Libraries for other tasks.

Information can be passed back and forth between the Arduino IDE on the host PC and Arduino

board by using Serial data transfer. Serial data transfer is the process of transferring data one bit

Page 13: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 13

at a time. In relation to the Arduino, this is used to communicate between the host PC and the

Arduino board..

The Arduino IDE has a serial monitor located in the top right of the menu bar.

By clicking this button, a new window will pop up that monitors serial communication between

the Arduino and PC via the corresponding COM port.

The bottom right of the serial monitor indicates the baud (bits per second) rate in which the serial

data is transferred.

Basic Serial Commands

The Serial.begin() function sets the data rate in bits per second (baud) for serial data

transmission. The available rates for use are 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800,

38400, 57600, or 115200.

Serial.begin(9600);

The Serial.read() function reads incoming serial data.

Serial.read();

Page 14: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 14

The Serial.write() function writes binary data to the serial port. The data is sent as a byte or a

series of bytes.

Serial.write(5); //sends a byte with a value of 5

The Serial.print()/Serial.println() functions print data to the serial port as readable text.

Serial.println additionally prints a carriage return and newline character.

Serial.print("Hello world."); //prints "Hello world."

We are now ready to compile and upload our very first sketch to the Arduino.

1. Open the Arduino IDE.

2. Create and save a new Sketch as “HelloWorld” by going to File Save As...

Page 15: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 15

3. Write the following code into the Arduino IDE, then compile to verify it.

void setup() // Setup, runs once

{

Serial.begin(9600); //Initialize the Serial Library at 9600 Baud

Serial.println("Hello world!"); //Prints “Hellow World! With a line break

}

void loop() // Loop, runs over and over again

{

}

4. Once the code has been verified, upload it using the upload button.

Page 16: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 16

Page 17: LABORATORY 1: INTRODUCTION TO ARDUINO IDE egg 101l introduction to engineering experience department of electrical and computer engineering 2 2. introduction to arduino ide ... ·

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 17

5. Click the Serial Monitor button in order to view your results.

TEST YOUR UNDERSTANDING:

1. Modify the previous code so that “Hello world!” is written once every second to the

serial monitor.

2. Research and decribe all available variable types for the Arduino programming language.

REFERENCES:

http://arduino.cc/en/Guide/Environment