Grove - 6-Axis Accelerometer&Compass V2.0

Från Karlskrona Makerspace Wiki
Version från den 29 oktober 2018 kl. 23.20 av Christian (Diskussion | bidrag)
(skillnad) ← Äldre version | Nuvarande version (skillnad) | Nyare version → (skillnad)
Hoppa till: navigering, sök
Grove - 6-Axis Accelerometer&Compass V2.0

En 3-axlig accelerometer i kombination med en 3-axlig magnetisk sensor.


  • Arduino
  • Raspberry Pi


  • I2C

Bibliotek (Arduino) på Github



hardware & software comment

I2C mode:
1, solder the jumper "I2C EN" and the jumper of ADDR to 0x1E
2, use Lsm303d.initI2C() function to initialize the Grove by I2C

SPI mode:

1, break the jumper "I2C_EN" and the jumper ADDR to any side
2, define a pin as chip select for SPI protocol.
3, use Lsm303d.initSPI(SPI_CS) function to initialize the Grove by SPI
SPI.h sets these for us in arduino
const int SDI = 11;
const int SDO = 12;
const int SCL = 13;

#include <LSM303D.h>
#include <Wire.h>
#include <SPI.h>

/* Global variables */
int accel[3];  // raw acceleration values store
int mag[3];  // raw magnetometer values store
float realAccel[3];  // calculated acceleration values 
float heading, titleHeading;

#define SPI_CS 10

void setup()
	char rtn = 0;
    Serial.begin(9600);  // Serial is used for debugging
    Serial.println("\r\npower on");
    rtn = Lsm303d.initI2C();
    //rtn = Lsm303d.initSPI(SPI_CS);
    if(rtn != 0)  // Initialize the LSM303, using a SCALE full-scale range
		Serial.println("\r\nLSM303D is not found");
		Serial.println("\r\nLSM303D is found");

void loop()
	//getLSM303_accel(accel);  // get the acceleration values and store them in the accel array
	while(!Lsm303d.isMagReady());// wait for the magnetometer readings to be ready
	Lsm303d.getMag(mag);  // get the magnetometer values, store them in mag
	for (int i=0; i<3; i++)
		realAccel[i] = accel[i] / pow(2, 15) * ACCELE_SCALE;  // calculate real acceleration values, in units of g
	heading = Lsm303d.getHeading(mag);
	titleHeading = Lsm303d.getTiltHeading(mag, realAccel);
	delay(200);  // delay for serial readability

void printValues()
	Serial.println("Acceleration of X,Y,Z is");
	for (int i=0; i<3; i++)
	/* print both the level, and tilt-compensated headings below to compare */
	Serial.println("The clockwise angle between the magnetic north and x-axis: ");
	Serial.print(heading, 3); // this only works if the sensor is level
	Serial.println(" degrees");
	Serial.print("The clockwise angle between the magnetic north and the projection");
	Serial.println(" of the positive x-axis in the horizontal plane: ");
	Serial.print(titleHeading, 3);  // see how awesome tilt compensation is?!
	Serial.println(" degrees");

Raspberry Pi

import lsm303d


    while True:

        # Get accelerometer values

        # Wait for compass to get ready
        while True:
            if acc_mag.isMagReady():

        # Read the heading
        heading= acc_mag.getHeading()

        print("Acceleration of X,Y,Z is %.3fg, %.3fg, %.3fg" %(acc[0],acc[1],acc[2]))
        print("Heading %.3f degrees\n" %(heading))

except IOError:
    print("Unable to read from accelerometer, check the sensor and try again")

Mer information