I2C Wiring
Use this wiring if you want to connect via I2C interface
By default, the I2C address is 0x6A. If you add a jumper from DO to 3.3V the address will change to 0x6B
- 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
The final results should resemble the illustration above, showing an Adafruit Metro development board.
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 DO pin to Digital #12 but any pin can be used later
- Connect the SDA 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 LSM6DS Library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit LSM6DS, and select the Adafruit LSM6DS 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 LSM6DS -> adafruit_lsm6ds33_test and upload to your Arduino wired up to the sensor.
Depending on whether you are using I2C or SPI, change the pin names and comment or uncomment the following lines.
if (!lsm6ds33.begin_I2C()) { // if (!lsm6ds33.begin_SPI(LSM_CS)) { // if (!lsm6ds33.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
Once you upload the code and open the Serial Monitor (Tools->Serial Monitor) at 115200 baud, you will see the current configuration printed, followed by the accelerometer, gyro, and temperature measurements. You should see something similar to this:
Give the sensor a wiggle or a spin and watch how the measurements change!
// Basic demo for accelerometer/gyro readings from Adafruit LSM6DS33 #include <Adafruit_LSM6DS33.h> // For SPI mode, we need a CS pin #define LSM_CS 10 // For software-SPI mode we need SCK/MOSI/MISO pins #define LSM_SCK 13 #define LSM_MISO 12 #define LSM_MOSI 11 Adafruit_LSM6DS33 lsm6ds33; void setup(void) { Serial.begin(115200); while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens Serial.println("Adafruit LSM6DS33 test!"); if (!lsm6ds33.begin_I2C()) { // if (!lsm6ds33.begin_SPI(LSM_CS)) { // if (!lsm6ds33.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) { Serial.println("Failed to find LSM6DS33 chip"); while (1) { delay(10); } } Serial.println("LSM6DS33 Found!"); // lsm6ds33.setAccelRange(LSM6DS_ACCEL_RANGE_2_G); Serial.print("Accelerometer range set to: "); switch (lsm6ds33.getAccelRange()) { case LSM6DS_ACCEL_RANGE_2_G: Serial.println("+-2G"); break; case LSM6DS_ACCEL_RANGE_4_G: Serial.println("+-4G"); break; case LSM6DS_ACCEL_RANGE_8_G: Serial.println("+-8G"); break; case LSM6DS_ACCEL_RANGE_16_G: Serial.println("+-16G"); break; } // lsm6ds33.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS); Serial.print("Gyro range set to: "); switch (lsm6ds33.getGyroRange()) { case LSM6DS_GYRO_RANGE_125_DPS: Serial.println("125 degrees/s"); break; case LSM6DS_GYRO_RANGE_250_DPS: Serial.println("250 degrees/s"); break; case LSM6DS_GYRO_RANGE_500_DPS: Serial.println("500 degrees/s"); break; case LSM6DS_GYRO_RANGE_1000_DPS: Serial.println("1000 degrees/s"); break; case LSM6DS_GYRO_RANGE_2000_DPS: Serial.println("2000 degrees/s"); break; case ISM330DHCX_GYRO_RANGE_4000_DPS: break; // unsupported range for the DS33 } // lsm6ds33.setAccelDataRate(LSM6DS_RATE_12_5_HZ); Serial.print("Accelerometer data rate set to: "); switch (lsm6ds33.getAccelDataRate()) { case LSM6DS_RATE_SHUTDOWN: Serial.println("0 Hz"); break; case LSM6DS_RATE_12_5_HZ: Serial.println("12.5 Hz"); break; case LSM6DS_RATE_26_HZ: Serial.println("26 Hz"); break; case LSM6DS_RATE_52_HZ: Serial.println("52 Hz"); break; case LSM6DS_RATE_104_HZ: Serial.println("104 Hz"); break; case LSM6DS_RATE_208_HZ: Serial.println("208 Hz"); break; case LSM6DS_RATE_416_HZ: Serial.println("416 Hz"); break; case LSM6DS_RATE_833_HZ: Serial.println("833 Hz"); break; case LSM6DS_RATE_1_66K_HZ: Serial.println("1.66 KHz"); break; case LSM6DS_RATE_3_33K_HZ: Serial.println("3.33 KHz"); break; case LSM6DS_RATE_6_66K_HZ: Serial.println("6.66 KHz"); break; } // lsm6ds33.setGyroDataRate(LSM6DS_RATE_12_5_HZ); Serial.print("Gyro data rate set to: "); switch (lsm6ds33.getGyroDataRate()) { case LSM6DS_RATE_SHUTDOWN: Serial.println("0 Hz"); break; case LSM6DS_RATE_12_5_HZ: Serial.println("12.5 Hz"); break; case LSM6DS_RATE_26_HZ: Serial.println("26 Hz"); break; case LSM6DS_RATE_52_HZ: Serial.println("52 Hz"); break; case LSM6DS_RATE_104_HZ: Serial.println("104 Hz"); break; case LSM6DS_RATE_208_HZ: Serial.println("208 Hz"); break; case LSM6DS_RATE_416_HZ: Serial.println("416 Hz"); break; case LSM6DS_RATE_833_HZ: Serial.println("833 Hz"); break; case LSM6DS_RATE_1_66K_HZ: Serial.println("1.66 KHz"); break; case LSM6DS_RATE_3_33K_HZ: Serial.println("3.33 KHz"); break; case LSM6DS_RATE_6_66K_HZ: Serial.println("6.66 KHz"); break; } lsm6ds33.configInt1(false, false, true); // accelerometer DRDY on INT1 lsm6ds33.configInt2(false, true, false); // gyro DRDY on INT2 } void loop() { // /* Get a new normalized sensor event */ sensors_event_t accel; sensors_event_t gyro; sensors_event_t temp; lsm6ds33.getEvent(&accel, &gyro, &temp); Serial.print("\t\tTemperature "); Serial.print(temp.temperature); Serial.println(" deg C"); /* Display the results (acceleration is measured in m/s^2) */ Serial.print("\t\tAccel X: "); Serial.print(accel.acceleration.x); Serial.print(" \tY: "); Serial.print(accel.acceleration.y); Serial.print(" \tZ: "); Serial.print(accel.acceleration.z); Serial.println(" m/s^2 "); /* Display the results (rotation is measured in rad/s) */ Serial.print("\t\tGyro X: "); Serial.print(gyro.gyro.x); Serial.print(" \tY: "); Serial.print(gyro.gyro.y); Serial.print(" \tZ: "); Serial.print(gyro.gyro.z); Serial.println(" radians/s "); Serial.println(); delay(100); // // serial plotter friendly format // Serial.print(temp.temperature); // Serial.print(","); // Serial.print(accel.acceleration.x); // Serial.print(","); Serial.print(accel.acceleration.y); // Serial.print(","); Serial.print(accel.acceleration.z); // Serial.print(","); // Serial.print(gyro.gyro.x); // Serial.print(","); Serial.print(gyro.gyro.y); // Serial.print(","); Serial.print(gyro.gyro.z); // Serial.println(); // delayMicroseconds(10000); }
Text editor powered by tinymce.