The Flora LSM303 Compass+Accel is a compact and versatile sensor module that integrates both a 3-axis accelerometer and a 3-axis magnetometer. This sensor is designed for motion detection, orientation tracking, and magnetic field measurement, making it ideal for wearable technology, navigation systems, and gesture recognition applications.
Pin Number | Name | Description |
---|---|---|
1 | SCL | I2C clock line for communication with microcontrollers |
2 | SDA | I2C data line for communication with microcontrollers |
3 | VCC | Power supply (3.3V) |
4 | GND | Ground connection |
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
/* Assign a unique ID to the sensors */
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);
Serial.println("LSM303 Accelerometer + Magnetometer Test");
/* Initialize the sensors */
if (!accel.begin() || !mag.begin()) {
Serial.println("Failed to initialize the sensor(s)");
while (1);
}
}
void loop(void) {
/* Get a new sensor event */
sensors_event_t event;
/* Read the accelerometer */
accel.getEvent(&event);
Serial.print("Accel 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");
/* Read the magnetometer */
mag.getEvent(&event);
Serial.print("Mag 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");
/* Delay before the next reading */
delay(500);
}
Q: Can I use the Flora LSM303 with a 5V microcontroller? A: Yes, but ensure that the data lines are level-shifted to 3.3V to prevent damage to the sensor.
Q: How do I calibrate the magnetometer? A: Calibration typically involves rotating the sensor in several axes and using software to compute offsets. Refer to the sensor's library documentation for specific instructions.
Q: What is the default I2C address of the LSM303? A: The default I2C address for the accelerometer is 0x32 and for the magnetometer is 0x3C, but check the datasheet as addresses may vary.