ME 462 - Lab 1 - Basic Atom 28 and PMD

advertisement
ME 445 – Lab 1 Part 1: Introduction to Arduino and Basic I/O
Arduino
This lab will introduce you to the Arduino microprocessor platform, which will be the foundation of all
the labs we do this semester. Your goal for part 2 of this lab is to create a game with your Arduino
and some of the simple components we have around the lab. While you are working on this lab, be
thinking about how you will design your game. This lab will recommend some tutorials from the
Arduino website. Make sure that you become comfortable with all of the functions described below as we
will use them throughout the semester. Try to use as many of the functions described below in your game.
Note: Arduino may ask if you want to update to the latest version of the software, which we won’t
need to do.
Arduino links
home http://www.arduino.cc/en/
getting started http://www.arduino.cc/en/Guide/HomePage
different boards http://store.arduino.cc/category/11
download software http://www.arduino.cc/en/Main/Software
examples http://www.arduino.cc/en/Tutorial/HomePage
language reference http://www.arduino.cc/en/Reference/HomePage
standalone CPU http://arduino.cc/en/Tutorial/ArduinoToBreadboard
schematic http://arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf
EAGLE 6.0 files http://arduino.cc/en/uploads/Main/arduino_Uno_Rev3-02-TH.zip
Arduino UNO specs http://arduino.cc/en/Main/ArduinoBoardUno
Microcontroller
ATmega328
Operating Voltage
5V
Input Voltage (recommended)
7-12V
Input Voltage (limits)
6-20V
Digital I/O Pins
14 (of which 6 provide PWM output)
Analog Input Pins
6
DC Current per I/O Pin
40 mA
DC Current for 3.3V Pin
50 mA
Flash Memory
32 KB (ATmega328) of which 0.5 KB used by bootloader
SRAM
2 KB (ATmega328)
EEPROM
1 KB (ATmega328)
Clock Speed
16 MHz
Cost
$25 to $30
Common guidelines for all labs
1) Do not hot wire any circuits – i.e., disconnect power from all circuits (especially the Arduino) when
making any connections.
2) Keep wires short and logically placed to make debugging much easier.
3) Build your circuit in small, easily tested modules. Test these modules using your DVM. Digital
inputs from the Arduino can be simulated with 0 V or 5 V from your protoboard.
4) Make the connection to the Arduino LAST, after you have manually debugged as much of your circuit
as possible.
5) Be organized and methodical. Do not plug and pray. It will save considerable time later tracing
wires, and will prevent fights with your lab partner.
6) Use an extractor to remove all IC's, not a screw driver.
7) Preserve parts for future generations. Do not cut resistor leads or bend LED legs.
8) Every program line should have a comment
page 1 of 3
ME 445 – Lab 1 Part 1: Introduction to Arduino and Basic I/O
9) Write reports that are self-contained, provide sufficient explanation so that anyone can duplicate your
work, and are complete enough so that you will be able to refer to them two years from now.
1) The serial monitor
One of the biggest challenges in microcontroller programming is figuring out what is happening inside
your code. Fortunately, the Arduino serial monitor makes this task much easier. It allows you to print out
numbers or messages from your code. Click the magnifying glass in the upper right hand corner of the
Arduino window to activate the Serial monitor. There are two ways to print out serial information.
Serial.print() prints out whatever you place inside the parentheses while Serial.println() does the same
thing, but then moves to the next line in the serial monitor. Many of the examples you will work through
will use the serial monitor. Make sure you understand how to use the serial monitor before you finish this
lab.
Note: You will need to reopen the serial monitor every time you upload a new program to your Arduino.
2) Digital Inputs and Outpts
Digital inputs and outputs only have two possible states which are known by a number of names, but most
commonly as high (nominally +5V for the Arduino) and low (ground on the Arduino).
Digital inputs can be used to read the state of devices that only have two possible states. The most
common example of this type of a device is a mechanical switch, although you will see later in the class
that many other devices can be read in a similar way.
3) Interfacing a switch
Follow the example at http://www.arduino.cc/en/Tutorial/InputPullupSerial to learn about digital inputs
and interfacing momentary switches.
4) Lighting an LED
Digital outputs can be used to turn circuits on and off, such as those containing lights or motors. Note that
the pins on the Arduino can only supply low currents (see Arduino Uno specs above) so they cannot be
used to control motors directly. We will discuss motor control later this semester. For now, we will
control a small light, known as an LED.
Follow the example at http://arduino.cc/en/Tutorial/Blink to learn about lighting the LED built into the
Arduino
5) Hardware PWM
The Arduino can provide hardware PWM output on pins 3, 5, 6, 9, 10, and 11 with the
analogWrite() command. See the tutorial on Arduino’s website for more information on PWM:
http://arduino.cc/en/Tutorial/PWM.
Follow the example at http://arduino.cc/en/Tutorial/Fade. Note that analogWrite() accepts
integers from 0 to 255, not 0 to 100 percent as you might think at first.
6) RC servo
Connect an RC servo to the Arduino as shown in Knob at http://arduino.cc/en/Tutorial/Knob to learn how
to control a simple servo. Also run Sweep http://arduino.cc/en/Tutorial/Sweep .
7) Analog input
page 2 of 3
ME 445 – Lab 1 Part 1: Introduction to Arduino and Basic I/O
Connect a potentiometer to the Arduino as shown in AnalogReadSerial at
http://arduino.cc/en/Tutorial/AnalogReadSerial and check if all analog inputs work. Note that
analogRead() values range from 0 to 1023.
8) Measuring Time
The Arduino has two built-in functions for measuring time: millis() gives you the amount of time elapsed
since the program began in milliseconds and micros(), which does the same thing, but provides the result
in microseconds.
Write a piece of code that measures your reaction time by lighting up an LED and measuring the amount
of time in milliseconds it takes you to press a momentary switch after the LED illuminates.
9) PMD 1208
The PMD-1208 is a low-cost data acquisition system you will use throughout the semester to measure
voltages with MATLAB. See the guide at the end of this lab to learn more about its use. Experiment with
changing the sampling rate and the number of samples collected. Build a simple circuit with a switch and
a pull-up resistor so that you can measure five volts when the switch is not pressed and zero volts when
the switch is pressed. Practice making a well-formatted MATLAB plot (see the TA if you have any
questions about how to do that).
Note: You may need to change the line:
ai = analoginput( 'mcc', 1 );
to the code below:
ai = analoginput( 'mcc', 0 );
Report
1) Describe each of the following Arduino functions in your own words. Serial.print(), Serial.println(),
digitalRead(),digitalWrite(), pinMode(), and analogWrite().
2) If you have two variables in your code (call them var1 and var2) write the lines of code you would
need to print a single line in the serial monitor that displayed both values separated by a space.
3) Roughly sketch what the output would look like if you were to write the following commands in
Arduino: analogWrite(9,50), analogWrite(9,128). What would be the mean voltage of the pwm signal
in each case?
4) Why do you need to activate the internal pull-up resistor to interface a switch?
5) Describe how a potentiometer can be used to create an analog voltage you can read with the Arduino.
Make sure to talk about what is happening inside the potentiometer as you adjust the knob.
6) What is the fastest you can sample with analogRead()?
7) What is the fastest you can sample with the PMD1208?
8) Which has a better voltage resolution, the Arduino or the PMD1208 set to the range -10 V to 10 V?
page 3 of 3
Download