Arduino Accelerometer Tutorial: Adding Input to Your Project

Published By

If you’re a fan of WWII airplanes and other machinery of that era, eventually you’ll stumble onto the subject of gyroscopes. The M-7 gyroscope was a marvel of modern technology at the time, but it took up a lot of space. These gyroscopes were about the size of a small internal combustion engine.

Over 70 years later, these devices have undergone incredible miniaturization. You can find a gyroscope, accelerometer, and even a temperature sensor built into a single chip – called an inertial measurement unit (IMU) – that is even smaller than your fingernail. One very popular accelerometer is the MPU-6050 from InvenSense, which you can buy, along with a breakout board and all the components you’ll need to use it, for about a dollar.

This chip uses microelectromechanical systems (MEMS) technology to vary internal capacitance values based on external forces. While its inner workings are fascinating, the chip takes in all the sensing details. Feed the values it produces into your Arduino board or any other computing system that you choose. Conveniently, the chip puts out this data in an I²C format, allowing it to use two wires and an interrupt pin to sense:

  • Acceleration data
  • Gyroscopic data
  • Temperature data 

How to Use an Accelerometer with Arduino

 

Image by Jeremy S. Cook

For this exercise, we’ll be using a GY-521 module that includes an MPU-6050 chip, piping data to an Arduino Uno. A wide variety of dev boards and computing systems are compatible with the I²C standard, so you should be able to apply these concepts in many situations, even if the specific pins and libraries you use are different.

For this experiment, we’ll first hook up the GY-521 board based on the following connections:

  • VCC: +5V
  • GND: GND
  • SCL: A5
  • SDA: A4
  • XDA: Not connected
  • XCL: Not connected
  • AD0: GND
  • INT: DIO 2

We also connected one 4.7kohm resistor between SCL and ground, and another between SDA and ground. You can find examples that don’t use an external resistor, so this may not be necessary for every application.

MPU-6050 Arduino Raw Data

Here are the basic steps to obtain the raw data:

  1. Once you’ve connected everything, load the “Short Example Sketch” found here on your Arduino board.
  2. Open your serial monitor and set it to 9600 baud.

You’ll then see the following values:

  • AcX: accelerometer reading in the X direction
  • AcY: accelerometer reading in the Y direction
  • AcZ: accelerometer reading in the Z direction
  • Tmp: temperature reading
  • GyX: gyroscope reading about the X-axis
  • GyY: gyroscope reading about the Y-axis
  • GyZ: gyroscope reading about the Z-axis

We’ve illustrated these axes and their orientation in the sketch below. Each value should respond when the device moves. When resting with the writing on the chip facing up, the Z-axis will read as positive, indicating the direction of gravity. If the X or Y axes face upwards, they will produce similar readings. The curves about each axis will indicate the roll values.

A note about roll direction: it follows the “right-hand rule” that we encounter in physics. If your right thumb points in the same direction as an axis, your fingers will curve in the positive rotational direction.