Skip to content
C library to use a MMA8451 accelerometer on a raspberry pi
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Library to use the MMA8451 accelerometer sensor via I²C on a Raspberry Pi in C.

Distributed under the terms of the MIT license agreement.

Makefile and inspration from this library for another I²C sensor

Code that does read/write in i2c with repeated start correctly is borrowed from this

Somewhat based on the Arduino library here


Build and install the library with the makefile. The makefile will, by default, install everything into /usr/local/{inlcude;lib}

Note to raspbian user: with this configuration, you may need to add the "/usr/local" path to your ldconfig

# echo "/usr/local/lib" > /etc/;
# ldconfig #to make the system know that theses libraries are here


#include <mma8451_pi/mma8451_pi.h>

int main()
  //Initialise the sensor on the 1st i2c-dev file (/dev/i2c-1) at it's default address (0x1D)
  mma8451 sensor = mma8451_initialise(1, MMA8451_DEFAULT_ADDRESS);
  //configure the max range, can be 2G, 4G or 8G
  mma8451_set_range(&sensor, 4); //For a maximum of a 4G acceleartion vector
  //get a measurement
  mma8451_vector3 acceleration = mma8451_get_acceleration_vector(&sensor);
  //There's less overhead on letting the library fill in your own object
  mma8451_vector3 vect;
  mma8451_get_acceleration(&sensor, &vect);
  return 0;

For C++ users, an operator<< overload on std::ostream permit you to easilly print the value of a mma8451_vector3. This is a stuct that exposes 3 x, y and z floats. It contains the acceleration vectors wirtten in G. 1G ~= 9.81 m.s^(-2)

You can’t perform that action at this time.