Arduino 3D Accelerometer Circuit

Vibrations are an important part of the world around us, from our ears' ability to use them to hear sounds and unwanted noise, to earthquakes which cause low-frequency, destructive, vibrations. Being able to measure them is very useful for many things, including geophysics, and is often done using an accelerometer. You probably won't be surprised to hear that accelerometers measure aceleration, which means they can also be very useful for interacting with games and virtual reality. In fact, the Nintendo Wii game console's controller includes one to measure your game gestures. So, here we make a simple Arduino circuit primarily for measuring vibrations, although in another project we will also use it to control some 3D graphics.

There are many accelerometer modules that we could choose for this project, but we want ours to be able to detect tiny vibrations as well as large accelerations associated with game gestures. Therefore, one device stands out from the crowd: the Freescale MMA7260Q, which has g-select pins that allows us to measure acceleration ranges of 1.5, 2, 4 and 6g. If you're unfamiliar with what we mean by 'g', basically it's the acceleration that an object feels if it falls only under the influence of gravity (on earth, we generally assume 1g to be 9.81 metres per second per second). The Arduino code is designed so that you can get data directly into the serial monitor window, or into a Pocket PC using a specially written NS Basic program.

The circuit diagram is shown in Figure 1. The accelerometer is shown connected to the 3.3V supply, as it won't work with higher voltages (many sensors these days require a maximum of 3.3V, so please check the voltage requirements of any different accelerometer module you may use). The purpose of the LED is to give a visual indication of when measuremens are being made. If you want to communicate with the Arduino using Bluetooth then it should be connected into the Arduino Tx and Rx pins as shown, but you can ignore that if you're getting data using a USB cable into the Arduino serial monitor.

The circuit should be quite simple to construct on a breadboard, expansion shield or even, for long-term use, a piece of veroboard. If you decide to use an accelerometer with a single g-range then you can remove the connections to D10 and D11, as well as the resistors, and change the Arduino code to suit if needs be. Also, please note that the circuit we used had the accelerometer mounted sideways, so you may need to change the code (or your spreadsheet) to make sure that your X/Y/Z values correspond to the X/Y/Z axes. Also, you may find that your circuit needs some calibration to determine g values: in particular the Pocket PC program includes a conversion that may need slight tweeking (and will definitely need changing if you use a different accelerometer module). A simple way to determine how many A2D units correspond to one g is, for each axis, to hold that accelerometer axis vertically upward and downward. One g will then be, approximately, half of the difference between the two readings. You can repeat the proceedure for all three axes if you wish, to account for any slight differences.

The circuit diagram.

Figure 1. The circuit diagram.

Once you have the circuit set up and connected to your computer, you can test it using the serial monitor in the Arduino development software (note the Arduino code is set to use 9600bps, whereas you may want to set a higher serial communication speed to suit your connection method). To output 200 comma separated lines (X, Y, Z) simply send 'e' as a command and maybe tap the circuit gently to get some vibrations while the LED is lit. As the data can be saved to a csv file (I used notepad for that), you can easily import it into Microsoft Excel. As an example, Figure 2 shows the z-axis (vertical) accelerometer values during a light tap with a Pocket PC stylus. As you can see from the graph, the accelerometer does show some noise, but even so we still have a nice graph of what a light tap looks like in terms of acceleration. One thing to remember though is that our circuit will be suscptible to vibrations of its own, so if you use the circuit to detect say a train passing, the amplitudes may not just be those associated with your home moving, but also may include 'resonances' that cause additional amplitudes. Basically our circuit is very simple and there is much more to accurate acceleration measurement, but we can still get some useful vibration information and have some fun learning about accelerometers along the way.

If you're using a Bluetooth module for communications, it's also easy to get the data onto a Pocket PC using the program shown in Figure 3, which is programmed in NS Basic. In that Pocket PC example, the whole circuit was dropped approximately 300mm onto a soft surface. The green line is the vertical acceleration and you can see that the accelerometer was in freefall (i.e. 1g) until it hit the surface, just as we would expect. It has been said that Einstein was partly inspired in his theories of relativity by the thought that a man falling off a roof would not feel the effects of gravity while in freefall, so perhaps it's a shame he didn't build our Arduino circuit to check that out. However, please don't try that as falling off a roof can be very hazardous and at the least probably rather painful. By the way, the files saved by the Pocket PC are stored using comma separated lines of text, so you can copy the files onto a desktop PC to import into your favourite spreadsheet.

A number of aspects of the Pocket PC software need to be mentioned, and may also be important to you when getting data via USB. Firstly, you can choose 'calib' in the 'calib' menu, to set a centre offset for all three channels. This can be very useful as it allows you to measure acceleration relative to a static position. For instance, the z-axis (vertical) will actually have an acceleration of 1g even if there is no movement, entirely due to gravity. A centre offset allows us to subtract that acceleration and so make comparisons between axes easier. Also, you can change the g-select values, which allows you to measure tiny vibrations, or visualise motion (e.g. with it in your pocket try walking, running and climbing stairs, to see the difference), or even measure impacts if you make the circuit rugged enough. To make life easier when doing motion detecting, driving, earthquake measuring and such like, you can also change the duration of the measurement between 'fast' and 'slow', and clicking anywhere on the graph causes a window to appear with information on measured g-values. If you want to monitor over longer periods, say during parachuting, you could change the Arduino code to increase the duration, or even take advantage of the built-in Arduino EEPROM memory to log data. There are many fun and exciting ways to use an accelerometer and so these are just a small example of what you can do.

A graph of vertical acceleration in Microsoft Excel.

Figure 2. An Excel graph.

The Pocket PC program.

Figure 3. Pocket PC software.

The Arduino code can be downloaded by clicking here, and you will find that it contains much more code than is necessary just for this project, as it is shared with code for a compass module and other sensors that will be integrated together in a future project. If you don't have an accelerometer with g-select pins then it should be relatively straight forward to edit the code for your Arduino to suit your needs. The NS Basic Pocket PC software can be downloaded by clicking here. As the Pocket PC software is GPL open source, but the NS Basic file can only be read if you have a paid for copy of their development environment, you can access a text copy by clicking here.

Some example serial communication commands that you can use with this circuit are:

0 - Set +- 1.5g mode, 1 - Set +- 2g mode, 2 - Set +- 4g mode, 3 - Set +- 6g mode, a - Return accelerometer data, c - Calibrate centre values, d - Return SLOW 200 HbLb acc values, e - Return FAST 200 text acc values, f - Return FAST 200 HbLb acc values, l - Load calibration from EEPROM, r - Return raw accelerometer data, s - Save calibration to EEPROM, t - Return calibration data, u - Restore default calibration, and v - Version info.