36
1 Introduction to the Arduino-BOT Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Introduction to the Arduino-BOT

  • Upload
    shika

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering. Introduction to the Arduino-BOT. Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering. References : 1) Arduino -BOT Lectures #1-5 - http://faculty.tcc.edu/PGordy/Egr120/ - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to the  Arduino-BOT

1

Introduction to the Arduino-BOTArduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 2: Introduction to the  Arduino-BOT

2

References: 1) Arduino-BOT Lectures #1-5 - http://faculty.tcc.edu/PGordy/Egr120/ 2) Robotics with the Board of Education Shield for Arduino web tutorials -

http://learn.parallax.com/ShieldRobot 3) Board of Education Shield for Arduino documentation -

http://www.parallax.com/Portals/0/Downloads/docs/prod/robo/35000-BOEShield-v1.2.pdf 4) Arduino web site (software, microcontrollers, examples, and more) - http://www.arduino.cc/

Much of the information in

this presentation comes from these online tutorials from Parallax

Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 3: Introduction to the  Arduino-BOT

RoboticsWhy study robots? 1) Applications – There are creative uses of robots all around us in

fields such as space exploration, undersea mapping, manufacturing, medicine, navigation, military applications and more.

2) Fun – Most engineering students have a fairly broad exposure to computers, but few have used computers to communicate with external devices. A robot is little more than a specialized computer used to read input devices (sensors) and to control output devices (motors, relays, servos, lights, sirens, etc.) It is challenging and fun to use computers to accomplish tasks and the applications are unlimited.

3) Common usage – A large number of introductory engineering courses around the country include robotics projects. Students are also commonly involved in robotics-based team competitions, such as the ASEE Model Design Competition or the IEEE Autonomous Vehicle Competition.

3Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 4: Introduction to the  Arduino-BOT

ASEE Model Design CompetitionTCC Engineering students have built a number of vehicles for the ASEE Model Design Competition that have been powered by the BASIC Stamp or Arduino microcontrollers..

This TCC vehicle followed a black line on a track by using the BASIC Stamp to read four optical sensors and to steer the vehicle by changing the speed of each rear wheel separately (powered by servos).

Infrared sensors

4Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 5: Introduction to the  Arduino-BOT

This TCC vehicle was designed to climb over barriers on the track instead of following a black line around the barriers. The BASIC Stamp read one optical sensor which counted wheel revolutions. The BASIC Stamp also controlled relays to reverse the tank’s direction at the turn point (after a certain number of wheel revolutions) and stopped the tank at the end of the course.

5Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 6: Introduction to the  Arduino-BOT

This TCC vehicle had to navigate a difficult figure-8 track with sharp hills and valleys. It featured a body that twisted in the middle to maintain traction. The BASIC Stamp was used to read three optical sensors to follow a line and then turned a servo for steering. Additionally, the BASIC Stamp was used to slow the inside rear wheel on the sharpest turns to assist the steering.

6Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 7: Introduction to the  Arduino-BOT

Selecting a robot: The key component to a robot is the computer (or microprocessor or microcontroller) that serves as its “brain.” Some features to consider in a microcontroller are:1) Number of input/output ports2) Programming language3) Complexity (time required for students to learn to use it)4) Cost5) Size6) Power requirements7) Support (such as documentation and sample code that is readily

available online)

7Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 8: Introduction to the  Arduino-BOT

Potential choices: 1) Lego Mindstorm

Easy to use Limited to three inputs/outputs Somewhat oversimplified with all snap-together pieces

2) Handy Board A computer board powered by the 68HC11

microprocessor that can be easily used to power robots.

The 68HC11 is commonly used in junior/senior courses in EE (as part of a microcontrollers course)

Supports many advanced features and has 14 input/outputs

More difficult to program (assembly language or C)

8Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 9: Introduction to the  Arduino-BOT

3) BASIC Stamp BOE-BOT Easy to program (using a version of

BASIC) 16 input/outputs (digital) Easy to use editor Used in EGR 120 for previously

4) Arduino-BOT BOE-BOT above modified to use the

popular Arduino microcontrollers Several Arduino microcontroller

models can be used. Arduino programming language based

on C/C++ which will be used later EGR 125 and EGR 262.

Great online resources Used in EGR 120 currently

9Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 10: Introduction to the  Arduino-BOT

Arduino Microcontrollers Arduino produces several versions of microcontrollers. Three of these have been tested with the Arduino Shield, a development board produced by Parallax which works nicely with the BOE-BOT. These three microcontrollers are:

Arduino UNO R3• Atmega 328

microcontroller• 14 digital I/O pins• 6 analog I/O pins• 16 MHz clock speed• 32 kB flash memory• Used in EGR 120

Arduino Mega 2560 R3• Atmega 2560

microcontroller• 14 digital I/O pins• 6 analog I/O pins• 16 MHz clock speed• 256 kB flash memory

Arduino Duemilanove• Atmega 168

microcontroller• 14 digital I/O pins• 6 analog I/O pins• 16 MHz clock speed• 16 kB flash memory

For more information - www.arduino.cc

10Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 11: Introduction to the  Arduino-BOT

The Board of Education Shield for Arduino The Parallax Board of Education Shield for Arduino allows different Arduino microcontroller boards to be plugged into the bottom on the board, leaving the top of the board free for access to I/O pins, servo ports, LEDs, and a breadboard.

11Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Arduino Shield Arduino UNO Microcontroller plugs into the bottom of the shield

The Arduino Shield with the Arduino UNO is ready to be mounted on a BOEBOT.

Page 12: Introduction to the  Arduino-BOT

The Board of Education (BOE) Shield for Arduino 12Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 13: Introduction to the  Arduino-BOT

Power SwitchThe BOE Shield for Arduino comes with a 3-position power switch that operates as follows:0) Power off1) Power to breadboard, but not to servo ports.2) Power to breadboard and to servo ports.

13Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

LED lit when breadboard is powered

LED lit when servo ports are powered

3-position switch

Note: Position 1 is useful for downloading programs that don’t use the servos or when you don’t want the servos to start turning quite yet.

Page 14: Introduction to the  Arduino-BOT

The Arduino-BOT The Arduino-BOT is a robot with a Board of Education (BOE) Shield mounted on it and the Arduino UNO microcontroller under the shield. We will learn to program the Arduino-BOT in this course.

14Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Arduino UNO

Page 15: Introduction to the  Arduino-BOT

The Arduino-BOT

15Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Free rolling rear wheel (or used as front wheel)

Breadboard on the shield is convenient for connecting sensors or other circuits.

Drive wheels mounted on servos. Steering is accomplished by turning one servo faster than the other.

Chassis has numerous holes for mounting sensors.

Batteries mounted under the chassis power the BOE, servos, and other devices.

Arduino ShieldArduino UNO mounted under the shield

Page 16: Introduction to the  Arduino-BOT

Powering the Arduino-BOT The Arduino can be powered using:1) USB cable (5V input)2) 4 AA Batteries ( ≈ 6V input)3) 5 AA Batteries (≈ 7.5V input) – recommended since specifications for the

Arduino call for a 7-12V input. The Arduino will also run longer using 5 AA batteries.

• If both the USB and (strong) batteries are connected, the Arduino will select the batteries

• Using the USB only is fine for downloading programs, but will give limited voltages for other uses as noted below (results from one Arduino-BOT tested in lab):

16Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Connection USB 4 AA Batteries 5 AA Batteries5 V 4.28 V 4.92 V 4.98 V

3.3 V 3.20 V 3.29 V 3.29 V

Servo Port * 4.28 V 4.92 V 4.98 V

Vin 4.32 V 5.51 V 7.09 V

* Assuming that the jumper on shield is in the 5V position

Page 17: Introduction to the  Arduino-BOT

Powering the Arduino-BOT

Be sure to unplug the battery pack connection at the end of each class or else it will continue to power the Arduino and will drain the batteries.

Power through USB connection

17Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Power through AA battery pack

Page 18: Introduction to the  Arduino-BOT

The Arduino Programming Environment• The Arduino software (Arduino 1.01 or later) is available on lab computers and can

also be downloaded from http://arduino.cc • The software requires no installation and can even be launched from a flash drive.• Open Arduino in the Arduino-1.0.1 folder as indicated below• The file opened is referred to as a “sketch” and will be automatically named using

the date. You can rename it if you wish. The file below has the name sketch_jul06a.ino

18Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 19: Introduction to the  Arduino-BOT

The Arduino Programming EnvironmentThe Arduino software is based on C/C++.Note that the instructions are case-sensitive.C++ is not line-oriented, so a semicolon (;) is needed to indicated the end of an instruction.

19Arduino-BOT Lecture #1 EGR 120 – Introduction to EngineeringNotes:• Verify is used to compile your program

(or sketch). (It checks for errors).• Upload is used to send the program

(sketch) to the Arduino-BOT via the USB cable.

• Arduino programs have two main parts:1. setup – a function to initialize items

or to perform operations one time.2. loop – a function with commands

that will be repeated indefinitely• Serial.begin(baud rate) is used to

establish serial communications between the Arduino-BOT and the computer. Data is transmitted at 9600 baud (bits/second). Note that Serial uses digital pins 0 and 1 so they cannot be used for other inputs/outputs.

• Serial.print(text, values, or variables) is display information from the Arduino-BOT on the display window.

• Select Serial Monitor to open a window to view printed messages from the Arduino-BOT.

Page 20: Introduction to the  Arduino-BOT

More on functions Functions in C/C++ have a particular structure as indicated below for the setup function.

20Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

• Functions execute a series of statements contained within curly braces.• Functions begin with a particular form:

return_type function_name (parameter_list)In the case above, the return_type is void (no output), the function_name is setup, and the parameter_list (typically inputs) is empty.

• Both statements in the body of the setup function are calls to functions in Arduino’s Serial library. We will introduce more useful functions as we continue in this course.

Page 21: Introduction to the  Arduino-BOT

Want more information? For more info on the Arduino programming language, select Help – Reference.

21Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 22: Introduction to the  Arduino-BOT

Online tutorials are available from Parallax at http://learn.parallax.com/ShieldRobot

22Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Tutorial Topics

Page 23: Introduction to the  Arduino-BOT

23Arduino-BOT Lecture #1 EGR 120 – Introduction to EngineeringExample – Printing a message repeatedly The previous program was modified. The Serial.print(“Hello!”) instruction was moved into the loop function. Now it will be written to the output screen repeatedly.

Notes: • The following instruction causes

a delay of 1000 ms or 1 second.delay(1000);

• Hello! is now printed to the display window once per second.

• If we want each Hello! statement printed on a new line, we can used the following command:

• Serial.println(“Hello!”)

Page 24: Introduction to the  Arduino-BOT

24Arduino-BOT Lecture #1 EGR 120 – Introduction to EngineeringControlling Lights (LEDs) with the Arduino-BOT In our first lab we will control lights (LEDs) using the Arduino. First we need to introduce two new components: 1) Resistors 2) Light-emitting diodes (LEDs)

Page 25: Introduction to the  Arduino-BOT

25Arduino-BOT Lecture #1 EGR 120 – Introduction to EngineeringResistor - A resistor is a component that resists the flow of electricity. This flow of electricity is called current. Resistance is measured in ohms, and the sign for the ohm is the Greek letter omega (Ω). There may be a fourth stripe that indicates the resistor’s tolerance. Tolerance is measured in percent, and it tells how far off the part’s true resistance might be from the labeled resistance. The fourth stripe could be gold (5%), silver (10%) or no stripe (20%).

Color Value

Black 0

Brown 1

Red 2

Orange 3

Yellow 4

Green 5

Blue 6

Violet 7

Gray 8

White 9

Example: R = 470

What is the value of R for a resistor with the colors brown-black-orange-silver? R = ________________

Page 26: Introduction to the  Arduino-BOT

26Arduino-BOT Lecture #1 EGR 120 – Introduction to EngineeringResistor Color Code Calculator – A quick online search will yield many resistor color code calculators. An example is shown below. The instructor for this course will typically let you know what value (and color) resistors to use, but it is a good idea to check the values as well.

http://samengstrom.com/24614782/en/read/4_Band_Resistor_Color_Codes

Page 27: Introduction to the  Arduino-BOT

LEDs – A simple device that we can control with the BASIC Stamp is an LED or light-emitting diode. LED’s are simple lights easily controlled using the 5V output from the Arduino. You commonly find LED’s on all sorts of electronic equipment. You computer monitor and keyboard probably uses small LED’s that light to let you know when the power is on.

Polarity – An LED has a positive terminal (anode) and a negative terminal (cathode) so you must be careful to place it in a circuit in the correct direction (correct polarity). You can tell which terminal is which in two ways:• The lead for the anode (+) is typically longer.• There is a flat side to the LED by the cathode (-).

27Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Long lead (+) Flat side (-)

Page 28: Introduction to the  Arduino-BOT

LED Brightness – The brightness of an LED is related to the amount of current passing through it (or the number of electrons passing though it). A device called a resistor is generally placed in series with the LED to limit the amount of current. When 5V is used to light the LED, resistor values from 200 ohms to 470 ohms are commonly used (the LED is brighter with 200 ohms than with 470 ohms). Using no resistor will result in too much current and will often destroy the LED. The resistor is often referred to as a current-limiting resistor. Current limiting resistor

(direction doesn’t matter)

LED cathode (-) connected to negative (–) side of battery

LED anode (+) connected to positive (+) side of battery

28Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 29: Introduction to the  Arduino-BOT

Breadboard – The Arduino-BOT comes with a convenient circuit board called a prototype board or a breadboard where circuits can be easily connected to the Arduino.

breadboard

In addition to giving a handy place to build circuits, the breadboard gives ready access to:

5V, 3.3V, and VinThese connections are like the positive end of a battery (the negative end is at GND). Vin is the battery voltage when using batteries (perhaps 6-7V).Digital Inputs/Outputs 0-14 Digital values should be 0V (LOW) or 5V (HIGH).Analog Inputs 0-5 Analog input values can range from 0V to 5VGround (GND)This is like the negative (-) terminal of a battery. All voltages (3.3V, 5V, etc.) share a common ground.

29Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 30: Introduction to the  Arduino-BOT

Controlling two LEDs with the Arduino – Suppose that we want to use digital outputs 12 and 13 from the Arduino to turn on and off two LEDs. We could wire the circuits on the breadboard as shown below. Be sure that the cathodes (-) of each LED are connected to GND and not the anodes.

30Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

What value resistors are used here? (red-red-brown-gold)R = _____________

Page 31: Introduction to the  Arduino-BOT

Sample program – The program below will blink the LED connected to digital pin 13 once per second.

31Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Notes:• pinMode( ) can be used to specify whether a pin

will be an INPUT or an OUTPUT• Recall that C++ is case-sensitive!• digitalWrite( ) can be used to set an output HIGH

(5V) or LOW (0V).• How could the program be modified to blink both

LEDs at the same time? Alternating?

Page 32: Introduction to the  Arduino-BOT

Variables and loops – Suppose that we wanted to blink an LED a certain number of times? To do this we need to see how to declare variables in C++ and to form loops.

Declaring variables in C++:We will consider three types of variables:int – used to declare integer variables (ranging from -32768 to 32767)float – used to declare floating point variables (values with a decimal point)char – used to declare character variables (single symbols within single quotes)

Examples: int a,b,c; // declare integer variablesa = 25; // assign value to integer variableint d = -21; // declare and assign valuechar c = ‘m’; // declare character variable and assign valuefloat g =-9.81 // declare floating point variable and assign value

32Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 33: Introduction to the  Arduino-BOT

Using variables with the Arduino

33Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 34: Introduction to the  Arduino-BOT

for loop in C++:Several types of looping structures can be used in C++. We will only consider the for loop. The for loop is especially useful when you want to execute instructions in the loop a specific number of times. See the example below.Form: for (initialization; condition; increment) // body of loop

34Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Note:C++ sometimes uses shortcut operators to increment, decrement, or change variables in loops.

i++ means add one to i j-- means to subtract one from jk+=2 means add 2 to k

Page 35: Introduction to the  Arduino-BOT

Blinking an LED a specific number of timesThe program below can be used to blink an LED (on digital pin 12) 10 times.

35Arduino-BOT Lecture #1 EGR 120 – Introduction to Engineering

Page 36: Introduction to the  Arduino-BOT

36Arduino-BOT Lecture #1 EGR 120 – Introduction to EngineeringComments Comments are an important part to computer programs.Comments allow you to explain your code to someone reading or using the code.// is used to indicated a comment in C++Tips for good comments:• Do not explain how C++ works. Explain how you are using the code.

Example: digitalWrite(12, HIGH); // make digital pin 12 HIGH

(Weak comment. Why are you making it HIGH?)

digitalWrite(12, HIGH); // turn on LED (pin 12) (Good comment)

• Explain similar instructions only once.Example:

digitalWrite(12, HIGH); // turn on LED (pin 12)delay(500); // wait 500 ms = 0.5 sdigitalWrite(12, LOW); // turn off LED (pin 12)delay(500); // wait 500 ms = 0.5 s (unnecessary comment - repeated)