Beginner Concepts: Using An Accelerometer

Accelerometers make for nice user interaction, that’s why every Apple product seems to have one included and the Nintendo Wii is still alive despite its underpowered graphics capabilities. Adding one to your project is pretty simple, just a matter of reading in analog data and interpreting it according to the datasheet. If you’re just starting out, here’s a tutorial on how to interface an accelerometer with a PIC microcontroller. They’re using an ADXL320 which can be acquired on a breakout board for about $30. The schematic and code are simple so even if you don’t intend to build the circuit (or want to use a different uC), this is easy to understand as an academic exercise.

[Thanks Skitchin]

[Photo credit: SparkFun]

32 thoughts on “Beginner Concepts: Using An Accelerometer

  1. As a general note, go buy yourself a wii nunchuck for a few bucks used at gamestop. It will be a lot cheaper and since you’re taking it apart you’ll end up learning a lot more.

  2. This will barely work, and will be useless for anything advanced. The PIC 18F452 analogue port can only reliably read sources with up to 2.5 kΩ impedance, while the ADXL320 has a 32 kΩ impedance. It needs something like an op-amp as a buffer.

    Save yourself a headache and get a digital accelerometer.

  3. Urgh…. utterly useless.

    To further Jeff’s comment if you buy a nunchuck but it online and also get a breakoutboard so you can keep it all together(resell + the connections are a bitch to solder). The nunchuck its self is an I2C device so you can interface it and get it’s 3-axis accelerometer info. dual axis analog stick and 2 buttons… wow it’s a sad day when half a paragraph is more useful than a HAD post.

    Check out ebay/amazon they can be had for £5 (third party versions) and you get a decent amount hacker fodder.

  4. Anyone for an overpriced digital level for the wood shop?

    I got from the posting “Hey, this exists.” So that was nice. A more in depth discussion or example would have been nice, though the math behind the tilt checking was nice.

  5. Adding to Alexander’s comment:
    if more accurate motion data is desired, try getting a Wii MotionPlus in addition to the nunchuk. The MotionPlus has a really nice gyro which can help supplement the readings from the nunchuck

  6. I’d like to know how to get a smooth output out of one, I hooked mine up to a pic, and then sent it over serial to control a cross hair on my computer screen, but it was super jittery, and any sort of smoothing I created in software, only worked at pretty much the point of severe lag. Not sure if I was doing something wrong, or this is a common problem.

  7. @PhillKill, how about taking the average over a period of time? Or a moving average. Or mapping it down from say 10bit to 5bit? These should all smooth out the output somewhat.

  8. What I did was import the adc output into my program, then I’d store those in an array of 5 to 10 in length, I could play with the number to fine tune it, once it was full, I would start averaging the input, with the average of the array values, then take out the oldest, and put in the new average, so it would then fill up with averaged values. Depending on how fast I sampled, along with how big the array was, I got it down to some decent stability, but it was always at the cost of speed, or accuracy, in regards to sample rate, making it skip across the screen. I don’t know if the 3 axis accelerometers all will spit out some wildly varying out put, or what, it just seemed like every few samples, it would say go from around .70 to .75 volts, to like .85 or something then back down, even tho it wasn’t moved. I got it smooth enough for my investigation and play with it, but as far as a controller, like I saw someone had made one to control a video game, it just seemed a bit unpredictable. Maybe a higher quality accelerometer would be better? I didn’t exactly buy something high end or anything I think, or maybe some filters on it before going to the microcontroller. I think it would work fine if you were just datalogging, but for a wii like interface, it seemed lacking.

  9. Gosh, Folks… Constructive criticism can lead to much better results sometimes.

    Frankly, while this is very elementary, I found it rather nice to see something here for any beginning hackers and/or hackers who focus more on a catagory here labeled “Misc. Hacks,” like the non-electronic and non-IC/processor-related ones.

    If HaD stuck enirely to electronics, programming and “high-end hacks,” very few beginners would ever stick around to be able to learn from all you apparently overly educated hackers!

    Thanks, HaD! I’ll be cutting & pasting into a printable document and using this info for some students to work on problem solving and taking it futher. Saves me the time of writing it all myself.

  10. Hack A Day needs much more constructive criticism. I agree with Tod 100% we all have to start somewhere. I cant stand when you have Elitists who apparently know everything and put down people for using starter platforms…example Arduino trolls nuff said.

  11. @PhilKll
    I think the problems you observed with accelerometers are very common.
    The Wii always uses the accelerometer data in such a way that the user does not see the inaccuracies. For example in Mario Cart were it is used to steer, it doesn’t matter whether it has some jitter because the carts always slip a lot and are very inert.
    When it comes to aiming they always use the Infrared camera of the Wii-controller.

    The accelerometer has a lot of jitter because it measures the derivative of the velocity which is in turn the derivative of the position.
    You can test your sensor by placing it on a rigid surface and not touching it in a way. Then it should give you very stable readings. If it does so, the sensor is perfectly fine. This means the jitter it outputs comes directly from the applied acceleration which is by itself jittery.

    I had the exact same problems with my balancing robot. Either the data is too jittery or it has too much of a lag. The only solution that worked for me was to use a Kalman Filter with the data of a rate-gyro and the accelerometer. This gave very accurate and smooth angular data.

  12. @PhilKll low pass filter works, see the datasheet…

    On a side note, accelerometers can probably be used (with a little creative voodoo involving supercooling via peltiers and tilt compensation) to detect high frequency gravitational waves.
    the effect is real and highly controversial as it suggests that gravitational waves not only exist but can cause real effects many light years from the source.

    generating HFGWs can be done using a superconducting disk and a high voltage source, it seems that somehow the sudden microquenches propagate HFGWs at right angles to the source, see ning li’s work…

    that said unless you are looking for them you would never know they were there, as the effect is miniscule. Certainly not enough to knock planes out of the sky or anything Half Lifey.

    :)

  13. whilst its commendable that beginners do stuff with kit, it should be pointed out that there is a battery source involved here and no regulation of the power, no mention of the ADC reference voltage so no idea where thats coming from either.

    These are important factors for beginners to miss and can cause swings in readings from full to low battery.

    I also don’t see a programming interface for getting the code on the chip but I guess that can be googled up somewhere.

  14. Reggie: good point about the ADC reference voltage, it should be noted that if the accelerometer has a built-in regulator like the nice ones from http://www.dimensionengineering.com then your microcontroller should also be run from a fixed regulated voltage, or you’ll start getting wrong readings if the micocontroller is being powered directly from batteries as the batteries voltage slowly drops.

  15. @laube
    If I’m reading you correctly, adding an op-amp integrator will convert the accelerometer’s output to a value proportional to the velocity of the accelerometer. Then connecting that output to a second integrator gives an output proportional to distance traveled.

    @PhilKll
    It could be that a 30 cent part (LM324) fixes your project.

  16. @DB
    yes that’s correct in theory but the problem with the integrator is that you will integrate the offset of the sensor too (which drifts over time due to temperature and other effects and thus cannot be compensated reliably (and even when it’s known you still cant compensate it perfectly))
    This gives you a constant growing error in velocity and a quadratic growing error in position. (because integrate(integrate(offset, t), t) = 1 / 2 * offset * t^2; assuming offset is constant for the integration period)
    The other problem is that you have to subtract the constant acceleration vector caused by gravitation before integrating. (and the orientation of this vector is normally not known either, unless you know the exact angle of the sensor)

    Regarding the thing with the ADC reference:
    The current setup uses the power supply as the ADC reference. Since the output of the accelerometer is ratiometric to supply voltage this technique gives you acceleration values independent of voltage.

  17. yeah, temperature drift is a PITA.

    the best fix so far is to use a LM35 glued face down to the sensor, and underneath the same sensor a power resistor.
    run power resistor from a simple transistorised feedback circuit similar to that used for laser diodes to keep the sensor at a constant 42 degrees (or slightly higher than the maximum system temperature you expect)
    this ensures that the calibration holds as long as the sensor and ADC are run from a known calibrated voltage source (I used a 3V3 LDO regulator)

    the other thing that you have to watch with these sensors is excessive G force, which can damage the delicate sensor bars (held in place with molecule thickness of silicon dioxide)
    I have tested this myself and they can and do break (!) symptoms include lack of change in one plane, random readings and sudden inexplicable “jumps” in one or more planes as debris inside the chip moves around.

    seen this happen on Wiimotes that have been dropped.

    #include “$0.02.h”

  18. As mentioned above, the Wii Nunchuk is a good way to check out accelerometers. Also mentioned is the fact that 99% of the code out there is for the authentic Nintendo Nunchuk and as is won’t work for third party Nunchuks.

    I have a video demonstrating interfacing a PIC to a knock-off Nunchuk and displaying the data on a LCD:

    http://www.youtube.com/watch?v=tdcrdRf9RHI

    I’m in the process of getting a website up to post the code and explain a bit more how it works.

  19. @laube
    We need more op-amps, use one to null out the offset.

    If there’s a CPU in the project, add a DAC to make the offset adjustable. When software detects no motion, update the offset nulling value.

    Now, use the integrator stage, followed by another op-amp to add an offset voltage to make the ADC happy.

    We’ll have to add a switched-cap voltage inverter to provide a negative supply for the op-amp. Might as well add bothersaidpooh’s temperature control while we’re at it.

    This might end up accurately measuring distances up to one inch, but only when it is on a flat and level surface.

  20. AVOID THE ADXL 330. It is now obsolete and has been replaced by the ADXL 335 but there are alot still floating around (esp from CHINA ching chong chang). I tried calling analog devices to get an explanation but haven’t been able to. I had been using the 330 in a high vibration environment and experienced many failures, the 335 is much more robust….

Leave a Reply to DaveCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.