- Sat Feb 12, 2011 5:58 pm
#120116
So I just received the LSM303 Breakout Board with the 3 axis compass and accelerometer. After several failed attempts to get this working I found that I might need to have a level shifter. It seems to lock everything up, no data is outputed.
If anyone has had experience with this breakout board I would would greatly appreciate some help.
I am using an Arduino for prototyping, my current setup is as follows:
3.3V -> VIN
GND -> GND
Analog Pin 5 -> SCL
Analog Pin 4 -> SDA
Here is the code for the Arduino:
If anyone has had experience with this breakout board I would would greatly appreciate some help.
I am using an Arduino for prototyping, my current setup is as follows:
3.3V -> VIN
GND -> GND
Analog Pin 5 -> SCL
Analog Pin 4 -> SDA
Here is the code for the Arduino:
Code: Select all
#include <Wire.h>
//Accelerometer address registers
#define ACCEL_ADDR 0x30 //Accelerometer address, binary 00110000
#define CTRL_REG1_A 0x20 //Enable Accelerometer
#define CTRL_REG4_A 0x23
#define ACCEL_READ 6 //2 bytes for each axis x, y and z
//Magnetometer address registers
#define MAG_ADDR 0x3C //Magnetometer address, binary 00111100
#define MR_REG_M 0x02 //Enable Magnetometer
#define CRA_REG_M 0x00
#define MAG_READ 6 //2 bytes for each axis x, y and z
//Initializes the sensors
void initSensors()
{
//Initializes the accelerometer
writeTo(ACCEL_ADDR, CTRL_REG1_A, 0x27);
writeTo(ACCEL_ADDR, CTRL_REG4_A, 0x40);
//Initializes the magnetometer
writeTo(MAG_ADDR, MR_REG_M, 0x00);
writeTo(MAG_ADDR, CRA_REG_M, 0x14);
}
void setup()
{
Serial.begin(9600);
Wire.begin();
initSensors();
}
void loop()
{
getData();
delay(1);
}
void getData()
{
/**************************************
Accelerometer LSM303DLH I2C
Registers:
x axis MSB = 28, x axis LSB = 29
y axis MSB = 2A, y axis LSB = 2B
z axis MSB = 2C, z axis LSB = 2D
***************************************/
int accelRegAddress = 0x28;
int Ax, Ay, Az;
byte Accel_Data[ACCEL_READ];
//Read the accelerometer data from LSM303DLH
readFrom(ACCEL_ADDR, accelRegAddress, ACCEL_READ, Accel_Data);
Ax = ((Accel_Data[0] << 8) | Accel_Data[1]);
Ay = ((Accel_Data[2] << 8) | Accel_Data[3]);
Az = ((Accel_Data[4] << 8) | Accel_Data[5]);
/**************************************
Magnetometer LSM303DLH I2C
Registers:
x axis MSB = 03, x axis LSB = 04
y axis MSB = 05, y axis LSB = 06
z axis MSB = 07, z axis LSB = 08
***************************************/
int magRegAddress = 0x03;
int Mx, My, Mz;
byte Mag_Data[MAG_READ];
//Read the magnetometer data from LSM303DLH
readFrom(MAG_ADDR, magRegAddress, MAG_READ, Mag_Data);
Mx = ((Mag_Data[0] << 8) | Mag_Data[1]);
My = ((Mag_Data[2] << 8) | Mag_Data[3]);
Mz = ((Mag_Data[4] << 8) | Mag_Data[5]);
//Print the Accelerometer x, y, z values
Serial.print(Ax);
Serial.print(",");
Serial.print(Ay);
Serial.print(",");
Serial.print(Az);
Serial.print(",");
//Print the Magnetometer x, y, z values
Serial.print(Mx);
Serial.print(",");
Serial.print(My);
Serial.print(",");
Serial.print(Mz);
Serial.println();
}
//Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val)
{
Wire.beginTransmission(device); //Start transmission to device
Wire.send(address); //Send register address
Wire.send(val); //Send value to write
Wire.endTransmission(); //End transmission
}
//Reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[])
{
Wire.beginTransmission(device); //Start transmission to device
Wire.send(address); //Sends address to read from
Wire.endTransmission(); //End transmission
Wire.beginTransmission(device); //Start transmission to device
Wire.requestFrom(device, num); //Request bytes from device
int i = 0;
while(Wire.available()) //Device may send less than requested (abnormal)
{
buff[i] = Wire.receive(); //Receive a byte
i++;
}
Wire.endTransmission(); //End transmission
}