You are on page 1of 10

Product Manual

LSM303DLH-BB

Version 1.0

Phi Robotics Research Pvt. Ltd.


www.phi-robotics.com
Table of Contents
1 Introduction ................................................................................................................................................... 2
2 Board Features ............................................................................................................................................... 2
3 Specifications ................................................................................................................................................. 2
1.1 PCB Details ............................................................................................................................................ 2
4 Hardware Connections ................................................................................................................................... 3
5 Pseudo Code ................................................................................................................................................... 4
1.2 Register Addresses and Configuration Parameters .............................................................................. 4
1.3 Accessing Registers and Configuring Device ......................................................................................... 5
1.4 Reading Accelerometer and Magnetometer Values.............................................................................. 7
6 References ..................................................................................................................................................... 8
1 Introduction
LSM303DLH is a sensor that combines a digital 3-axis accelerometer and 3-axis magnetometer into a single
package that is ideal for making tilt-compensated compass. It has a linear acceleration full-scale of ±2 g / ±4 g
/ ±8 g and a magnetic field full-scale of ±1.3 / ±1.9 / ±2.5 / ±4.0 / ±4.7 / ±5.6 / ±8.1 gauss, both fully selectable by
the user. The LSM303DLH includes I2C serial interface that supports standard mode and fast mode. The
internal self-test capability allows the user to check the functioning of the whole module in the final
application. The system can be configured to generate an interrupt signal by inertial wakeup/free-fall events,
as well as by the position of the device itself. Thresholds and timing of interrupt generators are
programmable on the fly by the end user. Magnetic and accelerometer parts can be enabled or put in power-
down mode separately.

2 Board Features
 3 magnetic field channel and 3 acceleration channel
 On board 1.8V regulator
 I2C interface
 Embedded self-test
 16-bit output
 6D orientation detection
 2 independent programmable interrupt generator for free fall and motion detection

3 Specifications
 Sensitivity range (configurable):
Accelerometer: ±2, ±4, or ±8 gauss
Magnetometer: ±1.3, ±1.9, ±2.5, ±4.0, ±4.7, ±5.6, or ±8.1 gauss
 LSM303DHL package: LGA-28
 Input voltage: 3.3V

1.1 PCB Details


 PCB size: 50.8 mm x 28.1 mm
 PCB type: FR4
 Solder mask: Black
 Board thickness: 1.6mm
 Surface finish: Immersion Gold
4 Hardware Connections

Figure 1 - LSM303DLH-BB Top View

LSM303DLH-BB board can be connected directly using 10 pin connector to the microcontroller. Since the
board has I2C interface, SDA and SCL lines are provided on the connector. I2C is employed to write the data
into the registers whose content can also be read back.

The board also has a 1.8V voltage regulator hence external power regulator is not required. SB1 and SB2 are
solder jumpers to pull the SDA and SCL lines high. JP1 jumper gives two I2C addresses depending upon
whether it is logic high or logic low. If JP1 is logic high (by default) then read and write are 0x33h and 0x32h
and if it logic low then read and write are 0x31h and 0x30h respectively. Figure 2 shows the pin layout for J1
header.
Figure 2 - LSM303DLH-BB pin layout

5 Pseudo Code
1.2 Register Addresses and Configuration Parameters
// LSM303 Accelerometer I2C slave address
LSM_ACC_SLAVE_ADDR = 0x30
// LSM303 Magnetometer I2C slave address
LSM_MAG_SLAVE_ADDR = 0x3C

// LSM303 register addresses


LSM_REG_MAG_CRA = 0x00 // magnetometer control reg A
LSM_REG_MAG_CRB = 0x01 // mag control reg B
LSM_REG_MAG_MR = 0x02 // mag mode control reg
LSM_REG_MAG_OUT_X_H = 0x03 // mag x axis data higher byte reg
LSM_REG_MAG_OUT_X_L = 0x04 // mag x axis data lower byte reg
LSM_REG_MAG_OUT_Y_H = 0x05 // mag y axis data higher byte reg
LSM_REG_MAG_OUT_Y_L = 0x06 // mag y axis data lower byte reg
LSM_REG_MAG_OUT_Z_H = 0x07 // mag z axis data higher byte reg
LSM_REG_MAG_OUT_Z_L = 0x08 // mag z axis data lower byte reg
LSM_REG_MAG_STATUS = 0x09 // mag status reg
LSM_REG_ACC_CR1 = 0x20 // accelerometer control reg 1
LSM_REG_ACC_CR2 = 0x21 // acc control reg 2
LSM_REG_ACC_CR3 = 0x22 // acc control reg 3
LSM_REG_ACC_CR4 = 0x23 // acc control reg 4
LSM_REG_ACC_REF = 0x26 // acc high pass filter reference value
LSM_REG_ACC_STATUS = 0x27 // acc status reg
LSM_REG_ACC_OUT_X_L = 0x28 // acc x axis data lower byte reg
LSM_REG_ACC_OUT_X_H = 0x29 // acc x axis data higher byte reg
LSM_REG_ACC_OUT_Y_L = 0x2A // acc y axis data lower byte reg
LSM_REG_ACC_OUT_Y_H = 0x2B // acc y axis data higher byte reg
LSM_REG_ACC_OUT_Z_L = 0x2C // acc z axis data lower byte reg
LSM_REG_ACC_OUT_Z_H = 0x2D // acc z axis data higher byte reg
LSM_REG_ACC_INT1_CFG = 0x30 // acc INT1 configuration reg
LSM_REG_ACC_INT1_THS = 0x32 // acc INT1 threshold value
LSM_REG_ACC_INT1_DUR = 0x33 // acc duration to validate INT1
LSM_REG_ACC_INT2_CFG = 0x34 // accelerometer INT2 configuration reg
LSM_REG_ACC_INT2_THS = 0x36 // accelerometer INT2 threshold value
LSM_REG_ACC_INT2_DUR = 0x37 // accelerometer duration to validate INT2

// LSM303 configuration parameters


// magnetometer scaling factor
LSM_MAG_SCALLING_FACTOR = 0.9478
// accelerometer scaling factor
LSM_ACC_SCALLING_FACTOR = 1.0
// data output rate = 15Hz, default magnetic sensor operating mode
LSM_MAG_CRA_REG_VAL = 0x10
// gain for X and Y axis = 230 LSB/Gauss, gain for Z axis = 205LSB/Gauss
LSM_MAG_CRB_REG_VAL = 0xE0
// continuous-conversion mode
LSM_MAG_MR_REG_VAL = 0x00
// output data rate =50Hz, power down mode, X,Y,Z axis enabled
LSM_ACC_CR1_REG_VAL = 0x17
// high-pass filter enabled for INT1, INT2; filtered data selection
LSM_ACC_CR2_REG_VAL = 0x1C
// active low interrupts,
// latching interrupt request on INT1_SRC, INT2_SRC reg
LSM_ACC_CR3_REG_VAL = 0xA4
// accelerometer range selection = ±2g, little endian data,
// continuous update block data
LSM_ACC_CR4_REG_VAL = 0x00
// enabling INT1 generation on X,Y,Z high and low event
LSM_ACC_INT1_CFG_REG_VAL = 0x3F
// enabling INT2 generation on X,Y,Z high and low event
LSM_ACC_INT2_CFG_REG_VAL = 0x3F

1.3 Accessing Registers and Configuring Device


uint8_t lsmMagRegRead(uint8_t regAddr)
{
uint8_t data;
// I2C start signal
i2cStart();
// send LSM303 magnetometer I2C slave address, R/W bit set as 0
i2cWriteByte(LSM_MAG_SLAVE_ADDR);
// send register address to read
i2cWriteByte(regAddr);
// I2C repeated start signal
i2cStart();
// send LSM303 magnetometer I2C address, R/W bit set as 1
i2cWriteByte(LSM_MAG_SLAVE_ADDR | 0x01);
// read a byte from I2C
data = i2cReadByte();
// I2C stop signal
i2cStop();
return data;
}

void lsmMagRegWrite( uint8_t regAddr, uint8_t data)


{
i2cStart();
// send LSM303 magnetometer I2C slave address, R/W bit set as 0
i2cWriteByte(LSM_MAG_SLAVE_ADDR);
// send register address to write
i2cWriteByte(regAddr);
i2cWriteByte(data);
i2cStop();
}
i2cStop();
}

uint8_t lsmAccRegRead(uint8_t regAddr)


{
uint8_t data;
i2cStart();
// send LSM303 accelerometer I2C slave address, R/W bit set as 0
i2cWriteByte(LSM_ACC_SLAVE_ADDR);
i2cWriteByte(regAddr);
i2cStart();
// send LSM303 accelerometer I2C address, R/W bit set as 1
i2cWriteByte(LSM_ACC_SLAVE_ADDR | 0x01);
data = i2cReadByte();
i2cStop();
return data;
}

void lsmAccRegWrite(uint8_t regAddr, uint8_t data)


{
i2cStart();
// send LSM303 accelerometer I2C slave address, R/W bit set as 0
i2cWriteByte(LSM_ACC_SLAVE_ADDR);
i2cWriteByte(regAddr);
i2cWriteByte(data);
i2cStop();
}

void lsmInit()
{
// set data output rate and measurement configuration
lsmMagRegWrite(LSM_REG_MAG_CRA, LSM_MAG_CRA_REG_VAL);
// set device gain
lsmMagRegWrite(LSM_REG_MAG_CRA, LSM_MAG_CRB_REG_VAL);
// select operating mode of the device
lsmMagRegWrite(LSM_REG_MAG_MR, LSM_MAG_MR_REG_VAL);
// set output data rate and power mode selection
lsmAccRegWrite(LSM_REG_ACC_CR1, LSM_ACC_CR1_REG_VAL);
// filter settings
lsmAccRegWrite(LSM_REG_ACC_CR2, LSM_ACC_CR2_REG_VAL );
// configure interrupt
lsmAccRegWrite(LSM_REG_ACC_CR3, LSM_ACC_CR3_REG_VAL );
// range selection
lsmAccRegWrite(LSM_REG_ACC_CR4, LSM_ACC_CR4_REG_VAL );
// configure INT1 source
lsmAccRegWrite(LSM_REG_ACC_INT1_CFG, LSM_ACC_INT1_CFG_REG_VAL);
// set threshold value to validate INT1
lsmAccRegWrite(LSM_REG_ACC_INT1_THS, 0x02);
// set duration to validate INT1
lsmAccRegWrite(LSM_REG_ACC_INT1_DUR, 0x20);
// configure INT2 source
lsmAccRegWrite(LSM_REG_ACC_INT2_CFG,LSM_ACC_INT2_CFG_REG_VAL);
// set threshold value to validate INT2
lsmAccRegWrite(LSM_REG_ACC_INT2_THS, 0x02);
// set duration to validate INT2
lsmAccRegWrite(LSM_REG_ACC_INT2_DUR, 0x20);
// select reference for high pass filter
lsmAccRegWrite(LSM_REG_ACC_REF, 0x01);
}
1.4 Reading Accelerometer and Magnetometer Values
int16_t lsmMagGetRawValue(uint8_t axis)
{
uint8_t data[2];
int16_t mag_val;
// wait for data ready bit in status reg to set
do{
data[0] = lsmMagRegRead(LSM_REG_MAG_STATUS)
}while( !(data[0] & 0x01) );

switch(axis)
{
X_AXIS:
data[0] = lsmMagRegRead(LSM_REG_MAG_OUT_X_H);
data[1] = lsmMagRegRead(LSM_REG_MAG_OUT_X_L);
mag_val = (data[0] << 8) | data[1];
break;

Y_AXIS;
data[0] = lsmMagRegRead(LSM_REG_MAG_OUT_Y_H);
data[1] = lsmMagRegRead(LSM_REG_MAG_OUT_Y_L);
mag_val = (data[0] << 8) | data[1];
break;

Z_AXIS:
data[0] = lsmMagRegRead(LSM_REG_MAG_OUT_Z_H);
data[1] = lsmMagRegRead(LSM_REG_MAG_OUT_Z_L);
mag_val = (data[0] << 8) | data[1];
break;
}
return mag_val;
}

int32_t lsmGetHeadingAngle(void)
{
uint16_t comp[2];
int32_t angle;
// read x axis component
comp[0] = lsmMagGetRawValue(X_AXIS);
// read y axis component
comp[1] = lsmMagGetRawValue(Y_AXIS);

// calculate heading angle


angle = ( ( (atan2(comp[0]/comp[1])) * 180 ) / PI ) + 180;

return angle;
}

int32_t lsmGetMagneticFieldStrength(uint8_t axis)


{
int16_t raw_data;
int32_t field_strength;

raw_data = lsmMagGetRawValue(axis);

// calculate field strength by multiplying by scaling factor


field_strength = raw_data * LSM_MAG_SCALLING_FACTOR;

return field_strength;
}
int32_t lsmGetAcceleration(uint8_t axis)
{
uint8_t data[2];
int32_t acc_val;

// wait for data ready bit in status reg to set


do{
data[0] = lsmAccRegRead(LSM_REG_ACC_STATUS)
}while( !(data[0] & 0x0f) );

switch(axis)
{
X_AXIS:
data[0] = lsmAccRegRead(LSM_REG_ACC_OUT_X_H);
data[1] = lsmAccRegRead(LSM_REG_ACC_OUT_X_L);
acc_val = (data[0] << 8) | data[1];
break;

X_AXIS:
data[0] = lsmAccRegRead(LSM_REG_ACC_OUT_X_H);
data[1] = lsmAccRegRead(LSM_REG_ACC_OUT_X_L);
acc_val = (data[0] << 8) | data[1];
break;

X_AXIS:
data[0] = lsmAccRegRead(LSM_REG_ACC_OUT_X_H);
data[1] = lsmAccRegRead(LSM_REG_ACC_OUT_X_L);
acc_val = (data[0] << 8) | data[1];
break;
}

// multiply by scaling factor


acc_val = acc_val * LSM_ACC_SCALLING_FACTOR

return acc_val;
}

6 References
LSM303DHL datasheet: http://www.st.com/web/en/resource/technical/document/datasheet/CD00260288.pdf

MCP1826S Datasheet: http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en531455


Phi Robotics Research Pvt. Ltd.
www.phi-robotics.com

You might also like