The LSM303C is a compact 6 Degrees of Freedom (6DOF) Inertial Measurement Unit (IMU) that combines a 3-axis accelerometer and a 3-axis magnetometer. This sensor is capable of providing real-time information about linear acceleration and magnetic field strength along three perpendicular axes. It is widely used in applications such as robotics, navigation, gesture recognition, and motion tracking.
Pin Number | Pin Name | Description |
---|---|---|
1 | VDD | Power supply voltage (1.9V to 3.6V) |
2 | GND | Ground |
3 | SDA/SDI | I2C data line / SPI serial data input |
4 | SCL/SCLK | I2C clock line / SPI serial clock |
5 | SA0/SDO | I2C address selection / SPI data output |
6 | CS | SPI chip select (active low) |
7 | DRDY | Data ready (active high) |
8 | INT | Interrupt output |
#include <Wire.h>
#include <LSM303C.h>
LSM303C imu;
void setup() {
Wire.begin(); // Initialize I2C
Serial.begin(9600); // Start serial communication at 9600 baud
if (!imu.begin()) {
Serial.println("Failed to initialize LSM303C");
while (1);
}
}
void loop() {
imu.read(); // Read the accelerometer and magnetometer values
// Print the acceleration values
Serial.print("Accel X: "); Serial.print(imu.ax); Serial.print(" ");
Serial.print("Accel Y: "); Serial.print(imu.ay); Serial.print(" ");
Serial.print("Accel Z: "); Serial.println(imu.az);
// Print the magnetometer values
Serial.print("Mag X: "); Serial.print(imu.mx); Serial.print(" ");
Serial.print("Mag Y: "); Serial.print(imu.my); Serial.print(" ");
Serial.print("Mag Z: "); Serial.println(imu.mz);
delay(100); // Delay for readability
}
Note: This example assumes the use of a library (LSM303C.h
) that abstracts the complexity of interfacing with the sensor. Make sure to install the library before compiling the code.
Wire
library's beginTransmission()
and endTransmission()
functions to check for successful communication over I2C.Q: Can the LSM303C be used with a 5V microcontroller like the Arduino UNO? A: Yes, but ensure that the voltage levels on the I2C/SPI lines are shifted down to be within the sensor's operating range.
Q: How can I change the measurement range of the accelerometer or magnetometer? A: This can typically be done through the sensor's registers. Refer to the LSM303C datasheet and use the appropriate library functions to configure the ranges.
Q: What is the default I2C address of the LSM303C? A: The default I2C address depends on the voltage level applied to the SA0/SDO pin. Check the datasheet for the specific addresses.
Q: How do I interpret the raw data from the sensor? A: The raw data needs to be converted to meaningful units. The library used in the example code often provides functions to do this conversion.