The Adafruit H3LIS331 Triple-Axis Accelerometer is a versatile and powerful sensor capable of measuring acceleration along three axes: X, Y, and Z. This low-power, high-performance module is based on the H3LIS331DL IC from STMicroelectronics and is designed for a wide array of applications, from motion detection to dynamic orientation tracking. Its wide measurement range and adjustable sensitivity make it an excellent choice for projects ranging from robotics to sports equipment.
Pin Number | Pin Name | Description |
---|---|---|
1 | VDD | Power supply (2.16V to 3.6V) |
2 | GND | Ground |
3 | SCL | I2C Serial Clock Line |
4 | SDA | I2C Serial Data Line |
5 | INT1 | Interrupt 1 (configurable) |
6 | INT2 | Interrupt 2 (configurable) |
7 | CS | Chip Select for SPI (not used in I2C mode) |
8 | SA0 | I2C Least Significant Bit of the device address |
#include <Wire.h>
// Adafruit H3LIS331 I2C address (assuming SA0 is connected to GND)
#define ACCEL_I2C_ADDR 0x18
void setup() {
Wire.begin(); // Initialize I2C
Serial.begin(9600); // Start serial communication at 9600 baud
// Configure accelerometer settings
writeAccelRegister(0x20, 0x27); // Normal power mode, 50Hz data rate
writeAccelRegister(0x23, 0x00); // Continuous update, LSB at lower address, ±100g
}
void loop() {
int16_t x, y, z;
// Read acceleration data
x = readAccel(0x28);
y = readAccel(0x2A);
z = readAccel(0x2C);
// Print the acceleration values
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.println(z);
delay(100); // Wait for 100 milliseconds
}
void writeAccelRegister(byte reg, byte value) {
// Write a value to a register on the accelerometer
Wire.beginTransmission(ACCEL_I2C_ADDR);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
int16_t readAccel(byte reg) {
// Read a 16-bit value from a register on the accelerometer
Wire.beginTransmission(ACCEL_I2C_ADDR);
Wire.write(reg | 0x80); // Set auto increment bit
Wire.endTransmission(false);
Wire.requestFrom(ACCEL_I2C_ADDR, 2, true);
byte lowByte = Wire.read();
byte highByte = Wire.read();
return (int16_t)(highByte << 8 | lowByte);
}
Q: Can the accelerometer be used with a 5V microcontroller like the Arduino UNO? A: Yes, but ensure that the VDD pin is connected to a voltage within the specified range, and use level shifters if necessary for the I2C lines.
Q: How can I change the measurement range? A: The measurement range can be changed by writing to the CTRL_REG4 (0x23) register. Refer to the datasheet for the appropriate settings.
Q: What is the default I2C address? A: The default I2C address is 0x18 when SA0 is connected to GND, and 0x19 when SA0 is connected to VDD.