The Adafruit LSM303DLHC is a compact and versatile sensor module that combines a triple-axis accelerometer and a triple-axis magnetometer. This sensor is capable of providing precise data on both acceleration and magnetic fields in three dimensions, making it an ideal choice for a wide range of applications including robotics, gaming, personal navigation, and tilt-compensated compasses.
Pin Number | Name | Description |
---|---|---|
1 | VIN | Supply voltage (3.3V) |
2 | 3Vo | 3.3V regulated output |
3 | GND | Ground |
4 | SDA | I2C data line |
5 | SCL | I2C clock line |
6 | DRDY | Data ready output (optional) |
To use the LSM303DLHC with an Arduino, you will need to install the Adafruit LSM303DLHC library. This can be done through the Arduino Library Manager:
After installing the library, you can include it in your sketch and initialize the sensor as follows:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);
void setup(void) {
Serial.begin(9600);
if (!accel.begin()) {
Serial.println("No accelerometer detected");
while (1);
}
if (!mag.begin()) {
Serial.println("No magnetometer detected");
while (1);
}
}
void loop() {
// Add code to read from the sensor and output the results.
}
To read data from the accelerometer and magnetometer, you can use the following code snippet:
sensors_event_t event;
accel.getEvent(&event);
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.println(" m/s^2 ");
mag.getEvent(&event);
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.println(" uT");
setup()
function.Q: Can I use this sensor with a 5V Arduino? A: Yes, but ensure that you connect the VIN pin to a 3.3V output on the Arduino, as the sensor is not 5V tolerant.
Q: How do I calibrate the magnetometer? A: Calibration involves rotating the sensor in several orientations and using the provided library functions to compute offsets. Refer to the library's documentation for detailed instructions.
Q: What is the purpose of the DRDY pin? A: The DRDY (Data Ready) pin is an optional feature that signals when new data is available to read. It can be used to trigger an interrupt in your microcontroller for more efficient data acquisition.
Remember to always consult the sensor's datasheet and the Adafruit LSM303DLHC library documentation for the most detailed and accurate information.