Arduino motion.pdf

(12275 KB) Pobierz
Extracted from:
Arduino
A Quick-Start Guide
This PDF file contains pages extracted from Arduino , published by the Pragmatic
Bookshelf. For more information or to purchase a paperback or PDF copy, please
Note: This extract contains some colored text (particularly in code listing). This
is available only in online versions of the books. The printed versions are black
and white. Pagination might vary between the online and printer versions; the
content is otherwise identical.
Copyright © 2010 The Pragmatic Programmers, LLC.
All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,
without the prior consent of the publisher.
The Pragmatic Bookshelf
Dallas, Texas • Raleigh, North Carolina
 
1114154282.011.png
It’s astonishing how quickly we get used to new technologies. A decade ago,
not many people would have imagined that we would use devices someday
to unobtrusively follow our movements. Today it’s absolutely normal for us
to physically turn our smartphones when we want to change from portrait to
landscape view. Even small children intuitively know how to use motion-
sensing controllers for video game consoles such as Nintendo’s Wii. You can
build your own motion-sensing devices using an Arduino, and in this chapter
you’ll learn how.
We’ll work with one of the most widespread motion-sensing devices: the
accelerometer . Accelerometers detect movement in all directions—they notice
if you move them up, down, forward, backward, to the left, or to the right.
Many popular gadgets such as the iPhone and the Nintendo Wii controllers
contain accelerometers. That’s why accelerometers are cheap.
Both fun and serious projects can benefit from accelerometers. When working
with your computer, you certainly think of projects such as game controllers
or other input control devices. But you can also use them when exercising
or to control a real-life marble maze. You can also use them to measure accel-
eration more or less indirectly, such as in a car.
You will learn how to interpret accelerometer data correctly and how to get
the most accurate results. Then you’ll use an accelerometer to build a motion-
sensing game controller, and you’ll implement a game that uses it.
6.1
What You Need
1.
A half-size breadboard or—even better—an Arduino Prototyping shield
with a tiny breadboard
2.
An ADXL335 accelerometer
3.
A pushbutton
4.
A 10kΩ resistor
5.
Some wires
6.
An Arduino board such as the Uno, Duemilanove, or Diecimila
7.
A USB cable to connect the Arduino to your computer
8.
A 6 pin 0.1" standard header
See Figure 50, All the parts you need in this chapter , on page 6 .
• Click HERE to purchase this book now. discuss
 
6
Figure 50 All the parts you need in this chapter
6.2
Wiring Up the Accelerometer
There are many different accelerometers, differing mainly in the number of
spacial axes they support (usually two or three). We use the ADXL335 from
Analog Devices—it’s easy to use and widely available. 1
In this section, we’ll connect the ADXL335 to the Arduino and create a small
demo program showing the raw data the sensor delivers. At that point, we
will have a quick look at the sensor’s specification and interpret the data.
In Figure 51, An ADXL335 sensor on a breakout board , on page 7 , you see
a breakout board containing an ADXL335 sensor on the right. The sensor is
the small black integrated circuit (IC), and the rest is just a carrier to allow
connections. On the top, you see a 6 pin 0.1" standard header. The sensor
has six connectors labeled GND, Z, Y, X, 3V, and TEST. To use the sensor on
a breadboard, solder the standard header to the connectors. This not only
makes it easier to attach the sensor to a breadboard but also stabilizes the
sensor, so it does not move accidentally. You can see the result on the left
side of the photo (note that the breakout board on the left is not the same as
on the right, but it’s very similar). Don’t worry if you’ve never soldered before.
In Section A1.2, Learning How to Solder , on page ? , you can learn how to
do it.
• Click HERE to purchase this book now. discuss
1114154282.012.png 1114154282.001.png 1114154282.002.png 1114154282.003.png 1114154282.004.png 1114154282.005.png 1114154282.006.png 1114154282.007.png
 
Bringing Your Accelerometer to Life 7
Figure 51 An ADXL335 sensor on a breakout board
You can ignore the connector labeled TEST, and the meaning of the remaining
connectors should be obvious. To power the sensor, connect GND to the
Arduino’s ground pin and 3V to the Arduino’s 3.3 volts power supply. X, Y,
and Z will then deliver acceleration data for the x-, y-, and z-axes.
Like the TMP36 temperature sensor we used in Section 5.4, Increasing Preci-
sion Using a Temperature Sensor , on page ? , the ADXL335 is an analog device:
it delivers results as voltages that have to be converted into acceleration values.
So, the X, Y, and Z connectors have to be connected to three analog pins on
the Arduino. We connect Z to analog pin 0, Y to analog pin 1, and X to analog
pin 2 (see Figure 52, How to connect an ADXL335 sensor to an Arduino , on
page 8 , and double-check the pin labels on the breakout board you’re using!).
Now that we’ve connected the ADXL335 to the Arduino, let’s use it.
6.3
Bringing Your Accelerometer to Life
A pragmatic strategy to get familiar with a new device is to hook it up and
see what data it delivers. The following program reads input values for all
three axes and outputs them to the serial port:
const unsigned int X_AXIS_PIN = 2;
const unsigned int Y_AXIS_PIN = 1;
• Click HERE to purchase this book now. discuss
1114154282.008.png 1114154282.009.png 1114154282.010.png
 
Zgłoś jeśli naruszono regulamin