The last decade has seen more than an order of magnitude drop in the price of
accelerometers, devices capable of measuring physical acceleration (often in more than one direction). History suggests that whenever a useful technology makes a precipitous drop in price, unexpected applications follow, and that’s exactly what has happened in this case.
Starting from zero and summing up acceleration, you can use an accelerometer to find velocity, and from that derive relative position information. By measuring the acceleration due to gravity, one can also determine orientation (technically, inclination)– you can tell which way it’s pointing. Those are pretty useful skills for a chip! And so as bulk prices for tiny chip-scale three-axis accelerometers have begun to approach $5, they have started to appear in all kinds of mass-market applications that you might not have predicted: laptop computers (for hard drive protection), smart phones and cameras (for orientation– e.g., portrait vs. landscape on the iPhone), cameras for image stabilization, and quite visibly in the controllers for Nintendo’s Wii system.
With all that promise, you might think that an accelerometer is a difficult beast to harness. That turns out not to be the case. In this little project we demystify the mighty accelerometer and show you how to get started playing with one. In the spirit of hobbyist electronics we do this the easy way– without designing a PCB or even soldering any surface-mount components.
Note: An updated version of this article is now available here.
Our project consists of two main elements: the accelerometer chip and a microcontroller that will read out the data and display it. Let’s first focus on the accelerometer. We’ll be using the ADXL330, which is a very popular little XYZ accelerometer made by Analog Devices. It’s actually the same chip that you would find as the accelerometer inside the Nintendo Wiimote controller. Purchased one at a time, on its own, this chip costs about $11.50 from Digi-Key, and the price goes down to about $7.25 in large quantity. (If you are Nintendo, the price is even lower.) One of the downsides to new and fancy devices like these is that they tend to come in unfriendly packages. The ADXL330 is only available in a 16-pin LFCSP; that’s a plastic package 4 mm X 4 mm, with pins that can be seen through a good magnifying glass. While it’s hard to work with on it’s own, there actually is a good solution for playing with this: get a breakout board.
This breakout board from SparkFun comes complete with a ADXL330 accelerometer soldered in place. The relevant connections to the chip are broken out into a row of 0.1″ spaced holes (which I have filled in with a six-pin header) and the three sensitivity axes of the chip are clearly labeled with bright markings on the silkscreen layer– a nice touch. The board is Sparkfun SKU#: SEN-00692, $35. Yes, it costs a fair bit more than the bare chip itself, but the price is fair and the convenience factor can’t be beat.
(If cost really is an issue, one potential option is to actually use Nintendo’s buying power to your benefit: disassemble a wii nunchuk controller ($20) to get at the similar accelerometer that lives in it. You could even take apart the Wiimote itself, if you can get a good price on the unit. In any case, getting at the connections to the chip will be much more difficult than just buying a decent breakout board.)
The accelerometer actually has a very simple analog interface. We only really need to connect to five pins on it. First, it wants power. It needs 1.8-3.6 V (and ground), and just to keep our discussion simple, let’s plan on using 3V for everything– either use a single lithium coin cell two alkaline AA cells in series. The chip also has three analog outputs– one for each direction. On these outputs, 1.5 V (really, halfway between the power and ground rails) represents zero acceleration, and deviations from that, either higher or lower, represent higher or lower accelerations.
The chip is sensitive to accelerations of +/- 3 g in each direction. (There is a sixth pin on the breakout board, which is for a self-test feature on the ADXL330 that we will not be using.)
Next, we need a simple microcontroller to read out the analog outputs and process them. We’re using the Atmel ATmega48, a member of the ATmega48/88/168 series of AVR microcontrollers. If you’re new to programming AVR microcontrollers, you have an extra step and some reading to do here. (And, as it turns out, this actually *is* an excellent example of a “first” microcontroller project for anyone.) To get up to speed, please read LadyAda’s tutorial. As is explained in the tutorial, you will need an AVR programmer (e.g., USBtinyISP, $22) and a working installation of the (free) AVR software toolchain.
Now we come to actually building up the hardware. The first step is to build a simple target board for the ATmega48; a board on which the chip can be programmed. As explained in that article, we need a socket for the AVR (28-pin 0.3″ DIP), a 6-pin DIL header, a battery holder (in this case lithium coin cell or 2 X AA), and a piece of prototyping perfboard to build it all on. Besides those, we also have the accelerometer breakout board, of course.
From the battery (left side) we hook the postive end to the indicated pins (+Vcc, +V) of the microcontroller (3 places), the ISP header, and the ADXL330 breakout board. The negative side of the battery is our effective ground, and get wired up to the ground pins of the microcontroller (2 places), the ISP header, and the ADXL330 breakout board.
The four remaining pins on the ISP connector (the 2 x 3 header) also need to be wired up to the matching pins on the microcontroller: MOSI, MISO, SCK and RESET.
We have skipped drawing the wires here to keep the diagram from looking like this. Hopefully, you learned connect-the-dots long before soldering.
Next, wire up the outputs of the ADXL330 board to the ADC inputs of the microcontroller as shown. X output to pin 28, Y to pin 27, Z to pin 26.
Finally, we add some indicators: two LEDs (one red, one blue) for each of the three axes. The big idea is that when there is no acceleration in (say) the X axis direction, both LEDs are off. When it detects acceleration one way, the red LED lights up (and lights up more, the harder the acceleration is) and it lights up blue for acceleration in the opposite direction. (Naturally, the other two axes work the same way.)
To do this, we’re using the pulse width modulation outputs from the three timers (timer 0, timer 1, and timer 2) on the microcontroller. Each timer has two outputs, called “output compare” pins A and B, which go to the two LEDs. The six outputs are called OC0A, OC0B, OC1A, OC1B, OC2A, and OC2B, and are hooked up to the LEDs as indicated in the diagram.
The AVR can directly drive LEDs of either color, without a series resistor, when powered by a lithium coin cell. However, it turns out that the AVR cannot be *programmed* in the circuit if the red LEDs are hooked up as shown but without the series resistors. (That’s because of the difference in LED forward voltage for the two colors.) If you use an alkaline battery to run this circuit, you may wish to put a small resistor (~30 ohms) in series with the blue LEDs as well.
Two minor details, not shown in the diagrams. First, the ADXL330 breakout board is socketed– I cut apart a dip chip socket to make a holder for the breakout board 6-pin header so that it doesn’t have to be permanently soldered to this setup. Secondly, I added a small power switch by the battery holder that lets you switch the circuit on or off easily.
You can download the firmware program (C code) for the AVR here (11 kB .ZIP file). It’s a very simple AVR-GCC program, licensed under the GPL. It reads in three analog
inputs sequentially, and lights up the six display LEDs depending on the values that it reads.
Once you’ve gotten the AVR programmed, it should be ready to go and show you outputs that depend on the acceleration. As you swing it around, even fast, you can see the LEDs responding to motion in the different directions.
If you aren’t wildly swinging the board around, what you’ll see is just the steady-state gravitational acceleration displayed. You might call it a precision tilt sensor, and it can tell you which way is up.
If we tilt our board left or right, such that the X-axis is now pointing slightly up or down (slightly with or slightly against gravity) you can see the X-axis LED pair, which is the on the left, switch from red to blue:
If instead we tilt the board forward and back, such that the Y axis is along or against gravity, you see the same thing for the middle pair of LEDs:
Finally, the Z indicator pair, on the right, is blue until you turn the board upside down– or shake it up and down.
So that’s it: a working “Hello world” for an accelerometer, all the way up to blinking LEDs.
Our C code is intentionally simple, and ready to mod. What can you do with it?
Soon, your little homebrew robot– or maybe gigantic evil death machine– will be able to tell how far it’s been, which way it’s facing, and which way is up. We think that this a useful building block, and we’ll be interested to see what other new things people build with it.
0 komentar:
Post a Comment