Categories

Follow Us

Arduino and LSM6DS3 accelerometer and gyroscope example

The LSM6DS3 is a system-in-package featuring a 3D digital accelerometer and a 3D digital gyroscope performing at 1.25 mA (up to 1.6 kHz ODR) in high-performance mode and enabling always-on low-power features for an optimal motion experience for the consumer.

The LSM6DS3 supports main OS requirements, offering real, virtual and batch sensors with 8 kbyte for dynamic data batching.

ST’s family of MEMS sensor modules leverages the robust and mature manufacturing processes already used for the production of micromachined accelerometers and gyroscopes.

The various sensing elements are manufactured using specialized micromachining processes, while the IC interfaces are developed using CMOS technology that allows the design of a dedicated circuit which is trimmed to better match the characteristics of the sensing element.

The LSM6DS3 has a full-scale acceleration range of ±2/±4/±8/±16 g and an angular rate range of ±125/±250/±500/±1000/±2000 dps.

High robustness to mechanical shock makes the LSM6DS3 the preferred choice of system designers for the creation and manufacturing of reliable products.

The LSM6DS3 is available in a plastic land grid array (LGA) package.

Key Features

Power consumption: 0.9 mA in combo normal mode and 1.25 mA in combo high-performance mode up to 1.6 kHz.
“Always-on” experience with low power consumption for both accelerometer and gyroscope
Smart FIFO up to 8 kbyte based on features set
Compliant with Android K and L
Hard, soft ironing for external magnetic sensor corrections
±2/±4/±8/±16 g full scale
±125/±250/±500/±1000/±2000 dps full scale
Analog supply voltage: 1.71 V to 3.6 V
Independent IOs supply (1.62 V)
Compact footprint, 2.5 mm x 3 mm x 0.83 mm
SPI/I2 C serial interface with main processor data synchronization feature

 

Code

I used the sparkfun library –https://github.com/sparkfun/SparkFun_LSM6DS3_Arduino_Library

This is the minimal example, there are many others

 

#include "SparkFunLSM6DS3.h"
#include "Wire.h"
#include "SPI.h"
 
LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B
 
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(1000); //relax...
Serial.println("Processor came out of reset.\n");
 
//Call .begin() to configure the IMU
myIMU.begin();
 
}
 
 
void loop()
{
//Get all parameters
Serial.print("\nAccelerometer:\n");
Serial.print(" X = ");
Serial.println(myIMU.readFloatAccelX(), 4);
Serial.print(" Y = ");
Serial.println(myIMU.readFloatAccelY(), 4);
Serial.print(" Z = ");
Serial.println(myIMU.readFloatAccelZ(), 4);
 
Serial.print("\nGyroscope:\n");
Serial.print(" X = ");
Serial.println(myIMU.readFloatGyroX(), 4);
Serial.print(" Y = ");
Serial.println(myIMU.readFloatGyroY(), 4);
Serial.print(" Z = ");
Serial.println(myIMU.readFloatGyroZ(), 4);
 
Serial.print("\nThermometer:\n");
Serial.print(" Degrees C = ");
Serial.println(myIMU.readTempC(), 4);
Serial.print(" Degrees F = ");
Serial.println(myIMU.readTempF(), 4);
 
delay(1000);
}

 

 

Output

Open the serial monitor

Accelerometer:
X = -6.5480
Y = 2.1936
Z = -4.5135

Gyroscope:
X = 0.2800
Y = 0.0000
Z = 125.4400

Thermometer:
Degrees C = 25.0000
Degrees F = 77.0000

 

Link

1.71 V to 5 V LSM6DS3 SPI/I2C 3 Axis Accelerometer 3 Axis Gyroscope 6 Axis Inertial Breakout Board Embedded temperature sensor

Share

Arduino and LIS2DH three-axis linear accelerometer example

The LIS2DH is an ultra low-power high performance three-axis linear accelerometer belonging to the “femto” family, with digital I2C/SPI serial interface standard output.

The LIS2DH has dynamically user selectable full scales of ±2g/±4g/±8g/±16g and it is capable of measuring accelerations with output data rates from 1 Hz to 5.3 kHz.

The self-test capability allows the user to check the functioning of the sensor in the final application.

The device may be configured to generate interrupt signals by two independent inertial wake-up/free-fall events as well as by the position of the device itself.

The LIS2DH is available in small thin plastic land grid array package (LGA) and is guaranteed to operate over an extended temperature range from -40 °C to +85 °C.

 

Key Features

Wide supply voltage, 1.71 V to 3.6 V
Independent IOs supply (1.8 V) and supply voltage compatible
Ultra low-power mode consumptiondown to 2 µA
±2g/±4g/±8g/±16g dynamically selectable full-scale
I2 C/SPI digital output interface
2 independent programmable interrupt generators for free-fall and motion detection
6D/4D orientation detection
“Sleep to wake” and “return to sleep” function
Freefall detection
Motion detection
Embedded temperature sensor
Embedded FIFO

Connection

 

LIS2DH Arduino
VCC 5V / 3V3
GND GND
SDA A4(SDA)
SCL A5(SCL)

 

Code

This uses https://github.com/DFRobot/DFRobot_LIS2DH12/archive/master.zip , I had to change the I2C address for my board

 

#include <Wire.h>
#include <DFRobot_LIS2DH12.h>
 
 
DFRobot_LIS2DH12 LIS; //Accelerometer
 
void setup(){
Wire.begin();
Serial.begin(115200);
while(!Serial);
delay(100);
 
// Set measurement range
// Ga: LIS2DH12_RANGE_2GA
// Ga: LIS2DH12_RANGE_4GA
// Ga: LIS2DH12_RANGE_8GA
// Ga: LIS2DH12_RANGE_16GA
while(LIS.init(LIS2DH12_RANGE_16GA) == -1){ //Equipment connection exception or I2C address error
Serial.println("No I2C devices found");
delay(1000);
}
}
 
void loop(){
acceleration();
}
 
/*!
* @brief Print the position result.
*/
void acceleration(void)
{
int16_t x, y, z;
 
delay(100);
LIS.readXYZ(x, y, z);
LIS.mgScale(x, y, z);
Serial.print("Acceleration x: "); //print acceleration
Serial.print(x);
Serial.print(" mg \ty: ");
Serial.print(y);
Serial.print(" mg \tz: ");
Serial.print(z);
Serial.println(" mg");
}

 

 

Output

Open the serial monitor

Acceleration x: 0 mg y: -250 mg z: -375 mg
Acceleration x: 0 mg y: -625 mg z: -375 mg
Acceleration x: -125 mg y: -375 mg z: -500 mg
Acceleration x: -125 mg y: -500 mg z: -375 mg
Acceleration x: -125 mg y: -500 mg z: -375 mg
Acceleration x: 125 mg y: -375 mg z: -375 mg
Acceleration x: 0 mg y: -625 mg z: -375 mg
Acceleration x: 0 mg y: -375 mg z: -625 mg
Acceleration x: 0 mg y: -625 mg z: -375 mg
Acceleration x: -125 mg y: -500 mg z: -500 mg

 

Link

http://www.st.com/resource/en/datasheet/lis2dh.pdf

Share

Arduino and ADXL335 accelerometer example

The ADXL335 is a small, thin, low power, complete 3-axis accelerometer with signal conditioned voltage outputs. The product measures acceleration with a minimum full-scale range of ±3 g. It can measure the static acceleration of gravity in tiltsensing applications, as well as dynamic acceleration resulting from motion, shock, or vibration.

The user selects the bandwidth of the accelerometer using the CX, CY, and CZ capacitors at the XOUT, YOUT, and ZOUT pins. Bandwidths can be selected to suit the application, with a range of 0.5 Hz to 1600 Hz for X and Y axes, and a range of 0.5 Hz to 550 Hz for the Z axis.

Here is a typical module that makes it easy to work with the ADXL335

Features:

Name: ADXL335 module (triaxial accelerometer analog output)
Model: GY-61
Power supply :3-5v
Analog X, Y, Z three-axis output
Schematic

The following layout shows how to wire the ADXL335 module up to an Arduino UNO

 

 

Code

 

const int xpin = A0; // x-axis of the accelerometer
const int ypin = A1; // y-axis
const int zpin = A2; // z-axis
 
void setup()
{
Serial.begin(9600);
}
 
void loop()
{
int x = analogRead(xpin); //read from xpin
delay(1); //
int y = analogRead(ypin); //read from ypin
delay(1); 
int z = analogRead(zpin); //read from zpin
 
float zero_G = 512.0; //ADC is 0~1023 the zero g output equal to Vs/2
float scale = 102.3; //ADXL335330 Sensitivity is 330mv/g
//330 * 1024/3.3/1000
Serial.print(((float)x - 331.5)/65*9.8); //print x value on serial monitor
Serial.print("\t");
Serial.print(((float)y - 329.5)/68.5*9.8); //print y value on serial monitor
Serial.print("\t");
Serial.print(((float)z - 340)/68*9.8); //print z value on serial monitor
Serial.print("\n");
delay(1000); //wait for 1 second 
}

Output

Open the serial monitor and you should see something like this
-3.54 3.51 9.22
-3.39 -5.79 3.31
-4.75 -9.08 6.77
25.40 29.83 -3.31
-1.43 6.22 8.21
-1.58 5.79 8.50
-1.73 5.79 8.50
-1.73 5.65 8.65
Links
http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL335.pdf

The sensor costs under $2

GY-61 ADXL335 Module Triaxial Acceleration Gravity Angle Sensor

Share

Arduino and MMA8451 accelerometer

The NXP® MMA8451Q is a low-power, three-axis capacitive accelerometer with 14 bits of resolution, featuring:

  • Embedded functions with flexible user-programmable options, configurable to two interrupt pins
  • Embedded interrupt functions for overall power savings relieving the host processor from continuously polling data
  • Access to both low-pass filtered data as well as high-pass filtered data, which minimizes the data analysis required for jolt detection and faster transitions
  • Inertial wake-up interrupt signals from any combination of the configurable embedded functions allowing the MMA8451Q to monitor events and remain in a low-power mode during periods of inactivity

Features

  • 1.95 to 3.6-volt supply voltage
  • 1.6 to 3.6-volt interface voltage
  • ±2g/±4g/±8g dynamically selectable full-scale
  • Output data rates (ODR) from 1.56 Hz to 800 Hz
  • 99 μg/√Hz noise
  • 14-bit and 8-bit digital output
  • I²C digital output interface (operates to 2.25 MHz with 4.7 kΩ pull-up)
  • Two programmable interrupt pins for seven interrupt sources
  • Three embedded channels of motion detection
    • Freefall or motion detection: one channel
    • Pulse detection: one channel
    • Jolt detection: one channel
  • Orientation (portrait/landscape) detection with programmable hysteresis
  • Automatic ODR change for auto-wake and return to sleep
  • 32 sample FIFO
  • High pass filter data available per sample and through the FIFO
  • Self-test

Again its easier to buy a module, here is one that I purchased

GY-45-MMA8451

gy-45 bottom

Connection

I used the following connection from the module above to my Arduino

Arduino Connection Module Connection
5v VCC_IN
Gnd Gnd
SDA SDA
SCL SCL

Code

This needs the Adafruit library – https://github.com/adafruit/Adafruit_MMA8451_Library/archive/master.zip

I had to edit the Adafruit header file called Adafruit_MMA8451.h as my device was address 0x1c (28)

#define MMA8451_DEFAULT_ADDRESS                 (0x1C)

#include <Wire.h>
#include "Adafruit_MMA8451.h"
#include <Adafruit_Sensor.h>
 
 
Adafruit_MMA8451 mma = Adafruit_MMA8451();
 
void setup(void) {
  Serial.begin(9600);
 
  Serial.println("Adafruit MMA8451 test!");
 
 
  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  }
  Serial.println("MMA8451 found!");
 
  mma.setRange(MMA8451_RANGE_2_G);
 
  Serial.print("Range = "); Serial.print(2 << mma.getRange());  
  Serial.println("G");
 
}
 
void loop() {
  // Read the 'raw' data in 14-bit counts
  mma.read();
  Serial.print("X:\t"); Serial.print(mma.x); 
  Serial.print("\tY:\t"); Serial.print(mma.y); 
  Serial.print("\tZ:\t"); Serial.print(mma.z); 
  Serial.println();
 
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mma.getEvent(&event);
 
  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  Serial.println("m/s^2 ");
 
  /* Get the orientation of the sensor */
  uint8_t o = mma.getOrientation();
 
  switch (o) {
    case MMA8451_PL_PUF: 
      Serial.println("Portrait Up Front");
      break;
    case MMA8451_PL_PUB: 
      Serial.println("Portrait Up Back");
      break;    
    case MMA8451_PL_PDF: 
      Serial.println("Portrait Down Front");
      break;
    case MMA8451_PL_PDB: 
      Serial.println("Portrait Down Back");
      break;
    case MMA8451_PL_LRF: 
      Serial.println("Landscape Right Front");
      break;
    case MMA8451_PL_LRB: 
      Serial.println("Landscape Right Back");
      break;
    case MMA8451_PL_LLF: 
      Serial.println("Landscape Left Front");
      break;
    case MMA8451_PL_LLB: 
      Serial.println("Landscape Left Back");
      break;
    }
  Serial.println();
  delay(500);
 
}

 

Links
GY-45 MMA8451 Module Digital Triaxial Accelerometer High-precision Inclination Module

Share