I2C Wiring
Use this wiring if you want to connect via I2C interface
By default, the i2c address is 0x18. If you add a jumper from SDO to 3Vo the address will change to 0x19
Here is an example using the STEMMA QT connector for solderless wiring
- Connect board VIN (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
- Connect board GND (black wire) to Arduino GND
- Connect board SCL (yellow wire) to Arduino SCL
- Connect board SDA (blue wire) to Arduino SDA
And an example using a breadboard:
- Connect board VIN (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
- Connect board GND (black wire) to Arduino GND
- Connect board SCL (yellow wire) to Arduino SCL
- Connect board SDA (blue wire) to Arduino SDA
SPI Wiring
Since this is a SPI-capable sensor, we can use hardware or 'software' SPI. To make wiring identical on all microcontrollers, we'll begin with 'software' SPI. The following pins should be used:
- Connect Vin to the power supply, 3V or 5V is fine. Use the same voltage that the microcontroller logic is based off of
- Connect GND to common power/data ground
- Connect the SCK pin to Digital #13 but any pin can be used later
- Connect the SDO pin to Digital #12 but any pin can be used later
- Connect the SDI pin to Digital #11 but any pin can be used later
- Connect the CS pin Digital #10 but any pin can be used later
Later on, once we get it working, we can adjust the library to use hardware SPI if you desire, or change the pins to others.
Library Installation
You can install the Adafruit LIS331 library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit LIS331, and select the Adafruit LIS331 library:
Then follow the same process for the Adafruit BusIO library.
Finally follow the same process for the Adafruit Unified Sensor library:
Load Example
Open up File -> Examples -> Adafruit LIS331 -> lis331hh_accel_demo if you are using the LIS331HH
If you are using the H3LIS331 use File -> Examples -> Adafruit LIS331 -> h3lis331_accel_demo
If you are using SPI, change the pin names and comment or uncomment the following lines.
if (! lis.begin_I2C()) { //if (!lis.begin_SPI(LIS331HH_CS)) { //if (!lis.begin_SPI(LIS331HH_CS, LIS331HH_SCK, LIS331HH_MISO, LIS331HH_MOSI)) {
After opening the demo file for the sensor you are using, upload to your Arduino wired up to the sensor. Once you upload the code, you will see the acceleration values for the X, Y, and Z axes being printed when you open the Serial Monitor (Tools->Serial Monitor) at 115200 baud, similar to this:
Acceleration is calculated as how much a velocity (meters per second) changes in a set amount of time, normally 1 second. So you will see acceleration values listed in meters per second per second, or meters per second squared. If space is short you may see m/s^2
Normally, sitting on a table, you'll see X and Y are close to 0 and Z will be about 1 g or ~9.8 m/s^2 because the accelerometer is measuring the force of gravity!
You can also move around the board to measure your movements and also try tilting it to see how the gravity 'force' appears at different axes.
Accelerometer ranges
Accelerometers don't 'naturally' spit out a "meters per second squared" value. Instead, they give you a raw value. In this sensor's case, its a number ranging from -32768 and 32767 (a full 16-bit range). Depending on the sensor range, this number scales between the min and max of the range. E.g. if the accelerometer is set to +-2g then 32767 is +2g of force, and -32768 is -2g. If the range is set to +-16g, then those two number correlate to +16g and -16g respectively. So knowing the range is key to deciphering the data!
You can set, and get the range with:
lis.setRange(LIS331HH_RANGE_6_G); // 6, 12, or 24 G
Not every accelerometer has the same range. If you're using the mega-ranged H3LIS331 you'll use
lis.setRange(H3LIS331_RANGE_100_G); // 100, 200, or 400 G!
// Basic demo for accelerometer readings from Adafruit LIS331HH #include <Wire.h> #include <SPI.h> #include <Adafruit_LIS331HH.h> #include <Adafruit_Sensor.h> // Used for software SPI #define LIS331HH_SCK 13 #define LIS331HH_MISO 12 #define LIS331HH_MOSI 11 // Used for hardware & software SPI #define LIS331HH_CS 10 Adafruit_LIS331HH lis = Adafruit_LIS331HH(); void setup(void) { Serial.begin(115200); while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens Serial.println("LIS331HH test!"); //if (!lis.begin_SPI(LIS331HH_CS)) { // if (!lis.begin_SPI(LIS331HH_CS, LIS331HH_SCK, LIS331HH_MISO, LIS331HH_MOSI)) { if (! lis.begin_I2C()) { // change this to 0x19 for alternative i2c address Serial.println("Couldnt start"); while (1) yield(); } Serial.println("LIS331HH found!"); lis.setRange(LIS331HH_RANGE_6_G); // 6, 12, or 24 G Serial.print("Range set to: "); switch (lis.getRange()) { case LIS331HH_RANGE_6_G: Serial.println("6 g"); break; case LIS331HH_RANGE_12_G: Serial.println("12 g"); break; case LIS331HH_RANGE_24_G: Serial.println("24 g"); break; } // lis.setDataRate(LIS331_DATARATE_50_HZ); Serial.print("Data rate set to: "); switch (lis.getDataRate()) { case LIS331_DATARATE_POWERDOWN: Serial.println("Powered Down"); break; case LIS331_DATARATE_50_HZ: Serial.println("50 Hz"); break; case LIS331_DATARATE_100_HZ: Serial.println("100 Hz"); break; case LIS331_DATARATE_400_HZ: Serial.println("400 Hz"); break; case LIS331_DATARATE_1000_HZ: Serial.println("1000 Hz"); break; case LIS331_DATARATE_LOWPOWER_0_5_HZ: Serial.println("0.5 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_1_HZ: Serial.println("1 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_2_HZ: Serial.println("2 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_5_HZ: Serial.println("5 Hz Low Power"); break; case LIS331_DATARATE_LOWPOWER_10_HZ: Serial.println("10 Hz Low Power"); break; } } void loop() { /* Get a new sensor event, normalized */ sensors_event_t event; lis.getEvent(&event); /* Display the results (acceleration is measured in m/s^2) */ Serial.print("\t\tX: "); Serial.print(event.acceleration.x); Serial.print(" \tY: "); Serial.print(event.acceleration.y); Serial.print(" \tZ: "); Serial.print(event.acceleration.z); Serial.println(" m/s^2 "); Serial.println(); delay(1000); }
Text editor powered by tinymce.