LSM303D 3D Compass and Accelerometer Carrier with Voltage Regulator
The LSM303D combines a digital 3-axis accelerometer and 3-axis magnetometer into a single package that is ideal for making a tilt-compensated compass. The six independent readings, whose sensitivities can be set in the ranges of ±2 to ±16 g and ±2 to ±12 gauss, are available through I²C and SPI interfaces. This LSM303 carrier board includes a 3.3 V voltage regulator and integrated level shifters that allows operation from 2.5 to 5.5 V, and the 0.1″ pin spacing makes it easy to use with standard solderless breadboards and 0.1″ perfboards.
This board is a compact (0.4″ × 0.9″) breakout board for ST’s LSM303D e-compass module, a single IC featuring a 3-axis digital linear acceleration sensor and 3-axis digital magnetic sensor; we therefore recommend careful reading of the LSM303D datasheet (1MB pdf) before using this product. The LSM303D is a great IC, but its small package makes it difficult for the typical student or hobbyist to use. It also operates at voltages below 3.6 V, which can make interfacing difficult for microcontrollers operating at 5 V. This carrier board addresses these issues by incorporating additional electronics, including a 3.3 V voltage regulator and level-shifting circuits, while keeping the overall size as compact as possible. The board ships fully populated with its SMD components, including the LSM303D, as shown in the product picture.
Compared to its predecessors, including the LSM303DLM and LSM303DLHC used on our earlier LSM303 compass and accelerometer carrier boards, the LSM303D features a number of improvements, such as a wider maximum magnetic sensing range (up to ±12 gauss). Instead of appearing as two separate devices with distinct addresses like previous sensors, the LSM303D appears as a single unified I²C device, and it also offers an SPI interface for additional flexibility.
This LSM303D carrier is not pin-compatible with our earlier LSM303 compass and accelerometer carriers (the pinout and the orientation of the sensor axes are different). It should still be usable as a replacement with the appropriate wiring changes, but changes in I²C addresses and configuration registers mean that code written to interface with a different chip will need to be modified to work with an LSM303D.
The LSM303D has many configurable options, including dynamically selectable sensitivities for the accelerometer and magnetometer, a choice of output data rates, and two independently-programmable external inertial interrupt pins. The magnetometer and accelerometer can be individually turned on and off to save power. The six independent magnetic and acceleration readings (sometimes called 6DOF) are available through a digital interface, which can be configured to operate in either I²C (TWI) or SPI mode; this data can be used for many applications, including making a tilt-compensated compass that can be used to determine headings regardless of how the board is inclined. (ST provides an application note (1MB pdf) that explains the details of making one, and an Arduino library containing an example application can be found in the Sample Code section below.)
The carrier board includes a low-dropout linear voltage regulator that provides the 3.3 V required by the LSM303D, which allows the sensor to be powered from a 2.5–5.5 V supply. The regulator output is available on the VDD pin and can supply almost 150 mA to external devices. The breakout board also includes a circuit that shifts the I²C clock and data lines to the same logic voltage level as the supplied VIN, making it simple to interface the board with 5 V systems, and the board’s 0.1″ pin spacing makes it easy to use with standard solderless breadboards and 0.1″ perfboards.
For sensor fusion applications, our MinIMU-9 v2 and AltIMU-10 inertial measurement units combine the older LSM303DLHC with an L3GD20 3-axis gyro on a single board, providing nine independent readings that can be used to calculate an absolute orientation. The AltIMU-10 also includes an LPS331AP pressure sensor that can be used to calculate altitude.
- Dimensions: 0.4″ × 0.9″ × 0.1″ (10 × 23 × 3 mm)
- Weight without header pins: 0.6 g (0.02 oz)
- Operating voltage: 2.5 V to 5.5 V
- Supply current: 5 mA
- Output format (I²C/SPI):
- Accelerometer: one 16-bit reading per axis
- Magnetometer: one 16-bit reading per axis
- Sensitivity range (configurable):
- Accelerometer: ±2, ±4, ±6, ±8, or ±16 g
- Magnetometer: ±2, ±4, ±8, or ±12 gauss
A 1×9 strip of 0.1″ header pins and a 1×9 strip of 0.1″ right-angle header pins are included, as shown in the picture below. You can solder the header strip of your choice to the board for use with custom cables orsolderless breadboards, or you can solder wires directly to the board itself for more compact installations.
The board has one mounting hole that works with #2 and M2 screws (not included).
Using the LSM303D
Regardless of the interface being used to communicate with the LSM303D, its VIN pin should be connected to a 2.5 V to 5.5 V source, and GND should be connected to 0 volts. (Alternatively, if you are using the board with a 3.3 V system, you can leave VIN disconnected and bypass the built-in regulator by connecting 3.3 V directly to VDD.)
A minimum of two logic connections are necessary to use the LSM303D in I²C mode (this is the default mode): SCL and SDA. These pins are connected to built-in level-shifters that make them safe to use at voltages over 3.3 V; they should be connected to an I²C bus operating at the same logic level as VIN. The remaining pins are connected to level-shifters on the board and are 5V-tolerant, but our 4-channel bidirectional logic level shifter can be used externally with those pins to achieve the same effect.
To use the LSM303D in SPI mode, four logic connections are typically used: SPC, SDI, SDO, and CS. These should be connected to an SPI bus operating at the same logic level as VIN. The SPI interface operates in 4-wire mode by default, with SDI and SDO on separate pins, but it can be configured to use 3-wire mode so that SDO shares a pin with SDI.
LSM303D 3D compass and accelerometer carrier with voltage regulator, labeled top view.
LSM303D 3D compass and accelerometer carrier with voltage regulator in a breadboard.
|VDD||Regulated 3.3 V output. Almost 150 mA is available to power external components. (If you want to bypass the internal regulator, you can instead use this pin as a 3.3 V input with VIN disconnected.)|
|VIN||This is the main 2.5 V to 5.5 V power supply connection. The SCL/SPC and SDA/SDI level shifters pull the I²C and SPI bus high bits up to this level.|
|GND||The ground (0 V) connection for your power supply. Your I²C or SPI control source must also share a common ground with this board.|
|SDA/SDI/SDO||Level-shifted I²C data line and SPI data in line (also doubles as SDO in 3-wire mode): HIGH is VIN, LOW is 0 V|
|SCL/SPC||Level-shifted I²C/SPI clock line: HIGH is VIN, LOW is 0 V|
|SDO/SA0||SPI data out line in 4-wire mode: HIGH is VDD, LOW is 0 V. This output is not level-shifted. Also used as an input to determine I²C slave address (see below).|
|CS||SPI enable (chip select). Pulled up to VDD to enable I²C communication by default; drive low to begin SPI communication.|
|INT2||Programmable interrupt, a 3.3-V-logic-level output. This output is not level-shifted.|
|INT1||Programmable interrupt, a 3.3-V-logic-level output. This output is not level-shifted.|
The above schematic shows the additional components the carrier board incorporates to make the LSM303D easier to use, including the voltage regulator that allows the board to be powered from a single 2.5–5.5 V supply and the level-shifter circuit that allows for I²C and SPI communication at the same logic voltage level as VIN. This schematic is also available as a downloadable PDF (162k PDF).
With the CS pin in its default state (pulled up to VDD), the LSM303D can be configured and its readings can be queried through the I²C bus. Level shifters on the I²C clock (SCL) and data lines (SDA) enable I²C communication with microcontrollers operating at the same voltage as VIN (2.5–5.5V). A detailed explanation of the protocol can be found in the LSM303D datasheet (1MB pdf), and more detailed information about I²C in general can be found in NXP’s I²C-bus specification (371k pdf).
In I²C mode, the sensor’s 7-bit slave address has its two least significant bits determined by the voltage on the SA0 pin. The carrier board pulls SA0 to VDD through a 4.7 kΩ resistor, making the least significant bits 01 and setting the slave address to 0011101b by default. If the selected slave address happens to conflict with some other device on your I²C bus, or if you want to use two LSM303D sensors on the same bus, you can drive SA0 low to set the least significant bits to 10 (which sets the slave address to 0011110b).
The I²C interface on the LSM303D is compliant with the I²C fast mode (400 kHz) standard. In our tests of the board, we were able to communicate with the chip at clock frequencies up to 400 kHz; higher frequencies might work but were not tested.
To communicate with the LSM303D in SPI mode, the CS pin (which the board pulls to VDD through a 4.7 kΩ resistor) must be driven low before the start of an SPI command and allowed to return high after the end of the command. Level shifters on the SPI clock (SPC) and data in (SDI) lines enable SPI communication with microcontrollers operating at the same voltage as VIN (2.5 V to 5.5 V).
In the default 4-wire mode, the sensor transmits data to the SPI master on a dedicated data out (SDO) line that is level-shifted. If the SPI interface is configured to use 3-wire mode instead, the SDI line doubles as SDO and is driven by the LSM303D when it transmits data to the master. A detailed explanation of the SPI interface on the LSM303D can be found in its datasheet (1MB pdf).
We have written a basic Arduino library for this LSM303 carrier board that makes it easy to interface this sensor with an Arduino or Arduino-compatible board like an A-Star. The library makes it simple to read the raw accelerometer and magnetometer data, and it has a function for computing the tilt-compensated heading for those looking to use this sensor as a tilt-compensated compass.
The datasheet provides all the information you need to use this sensor, but picking out the important details can take some time. Here are some pointers for communicating with and configuring the LSM303D that we hope will get you up and running a little bit faster:
- The magnetometer and accelerometer are in power-down mode by default. You have to turn them on by writing the appropriate values to the CTRL1 and CTRL7 registers.
- You can read or write multiple registers in a single I²C command by asserting the most significant bit of the register address to enable address auto-increment.
- You can enable the same auto-increment feature in SPI mode by asserting the second bit (bit 1, called the MS bit in the datasheet) of an SPI command.
- Compared with previous LSM303-series sensors, the register interface to the magnetometer in the LSM303D is much more consistent with the accelerometer interface, and its accelerometer and magnetometer share a common I²C address instead of acting as two separate slave devices on the same bus.