Learn to use I2C Accelerometer

I2C Accelerometer Sample

We’ll connect an I2C accelerometer to your Raspberry Pi 2/MinnowBoard Max/DragonBoard and create a simple app to read data from it. We’ll walk you through step-by-step, so no background knowledge of I2C is needed. However, if you’re curious, SparkFun provides a great tutorial on I2C.

This is a headed sample. To better understand what headed mode is and how to configure your device to be headed, follow the instructions here.learn to use i2c accelerometer

Load the project in Visual Studio

You can find the source code for this sample by downloading a zip of all of our samples here and navigating to the `samples-develop\Accelerometer`. Make a copy of the folder on your disk and open the project from Visual Studio.

Connect the I2C Accelerometer to your device

You’ll need a few components:

Raspberry Pi 2

If you have a Raspberry Pi 2, we need to hook up power, ground, and the I2C lines to the accelerometer. Those familiar with I2C know that normally pull-up resistors need to be installed. However, the Raspberry Pi 2 already has pull-up resistors on its I2C pins, so we don’t need to add any additional external pull-ups here. See the Raspberry Pi 2 pin mapping page for more details on the RPi2 IO pins.

Note: Make sure to power off the RPi2 when connecting your circuit. This is good practice to reduce the chance of an accidental short circuit during construction.

The ADXL345 breakout board has 8 IO pins, connect them as follows:

  1. GND: Connect to ground on the RPi2 (Pin 6)
  2. VCC: Connect to 3.3V on the RPi2 (Pin 1)
  3. CS: Connect to 3.3V (The ADXL345 actually supports both SPI and I2C protocols. To select I2C, we keep this pin tied to 3.3V. The datasheet contains much more information about the pin functions)
  4. INT1: Leave unconnected, we’re not using this pin
  5. INT2: Leave unconnected, we’re not using this pin
  6. SDO: Connect to ground (In I2C mode, this pin is used to select the device address. You can attach two ADXL345 to the same I2C bus if you connect this pin to 3.3V on the second device. See the datasheet for more details)
  7. SDA: Connect to SDA on the RPi2 (Pin 3). This is the data line for the I2C bus.
  8. SCL: Connect to SCL on the RPi2 (Pin 5). This is the clock line for the I2C bus.

schematic learn to use i2c accelerometer

MinnowBoard Max

If you have a MinnowBoard Max, we need to hook up power, ground, and the I2C lines to the accelerometer. Those familiar with I2C know that normally pull-up resistors need to be installed. However, the MBM already has 10K pull-up resistors on its IO pins, so we don’t need to add any additional external pull-ups here. See the MBM pin mapping page for more details on the MBM IO pins.

For more detail: Learn to use I2C Accelerometer

Scroll to Top
Read previous post:
pi0rover a smart rover with the pi zero
Pi0Rover: A Smart Rover with the Pi Zero

Story This tutorial demonstrates how to build a Linux drone with the Raspberry Pi Zero. The drone uses a real-time...

Close