Categories

Follow Us

ADXL337 accelerometer Arduino Uno example

In this article we connect an ADXL337 accelerometer to an Arduino Uno

The ADXL337 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 ±3g. 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 ZOUTpins. 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.

 

Parts List

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
ADXL337 3-axis ADXL337 GY-61 Replacement ADXL335 Module Analog Output Accelerometer
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic

arduino and adxl337

arduino and adxl337

 

Code

No libraries required – this is a sparkfun example

// Make sure these two variables are correct for your setup
int scale = 3; // 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
 
 
void setup()
{
  // Initialize serial communication at 115200 baud
  Serial.begin(115200);
}
 
// Read, scale, and print accelerometer data
void loop()
{
  // Get raw accelerometer data for each axis
  int rawX = analogRead(A0);
  int rawY = analogRead(A1);
  int rawZ = analogRead(A2);
 
  // Scale accelerometer ADC readings into common units
  // Scale map depends on if using a 5V or 3.3V microcontroller
  float scaledX, scaledY, scaledZ; // Scaled values for each axis
  scaledX = mapf(rawX, 0, 675, -scale, scale); // 3.3/5 * 1023 =~ 675
  scaledY = mapf(rawY, 0, 675, -scale, scale);
  scaledZ = mapf(rawZ, 0, 675, -scale, scale);
 
  // Print out raw X,Y,Z accelerometer readings
  Serial.print("X: "); Serial.println(rawX);
  Serial.print("Y: "); Serial.println(rawY);
  Serial.print("Z: "); Serial.println(rawZ);
  Serial.println();
 
  // Print out scaled X,Y,Z accelerometer readings
  Serial.print("X: "); Serial.print(scaledX); Serial.println(" g");
  Serial.print("Y: "); Serial.print(scaledY); Serial.println(" g");
  Serial.print("Z: "); Serial.print(scaledZ); Serial.println(" g");
  Serial.println();
 
  delay(2000); // Minimum delay of 2 milliseconds between sensor reads (500 Hz)
}
 
// Same functionality as Arduino's standard map function, except using floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

 

Output

Open the serial monitor and you will see something like this

X: 384
Y: 340
Z: 281

X: 0.41 g
Y: 0.02 g
Z: -0.50 g

X: 410
Y: 345
Z: 345

X: 0.64 g
Y: 0.07 g
Z: 0.07 g

 

Links

https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL337.pdf

Share

LIS3DSH accelerometer sensor and Arduino example

In this example we connect a LIS3DSH sensor to an Arduino. Lets look at the sensor

The LIS3DSH is an ultra-low-power high-performance three-axis linear accelerometer belonging to the “nano” family with an embedded state machine that can be programmed to implement autonomous applications.

The LIS3DSH has dynamically selectable full scales of ±2g/±4g/±6g/±8g/±16g and is capable of measuring accelerations with output data rates from 3.125 Hz to 1.6 kHz. The self-test capability allows the user to check the functioning of the sensor in the final application. The device can be configured to generate interrupt signals activated by user-defined motion patterns.

The LIS3DSH has an integrated first-in, first-out (FIFO) buffer allowing the user to store data in order to limit intervention by the host processor. The LIS3DSH is available in a 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 consumption
  • ±2g/±4g/±6g/±8g/±16g dynamically selectable full scale
  • I2C/SPI digital output interface
  • 16-bit data output
  • Programmable embedded state machines
  • Embedded temperature sensor
  • Embedded self-test
  • Embedded FIFO
  • 10000 g high shock survivability

 

Parts List

Part Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
LIS3DSH module CJMCU- LIS3DSH High-resolution Three-axis Accelerometer Triaxial Accelerometer Module LIS3DH
Connecting Wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Connection

An easy device to connect – remember its 3.3v

Arduino and LIS3DSH

Arduino and LIS3DSH

 

Code

You need to download and install the https://github.com/yazug/LIS3DSH library

#include <Wire.h>
#include <LIS3DSH.h>
 
LIS3DSH accel;
 
void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  accel.enableDefault();
}
 
void loop() 
{
  int16_t x, y, z;
  int8_t temperature;
 
  accel.readAccel(&x, &y, &z);
  accel.readTemperature(&temperature);
 
  Serial.print("Accel ");
  Serial.print("X: ");
  Serial.print(x);
  Serial.print(" Y: ");
  Serial.print(y);
  Serial.print(" Z: ");
  Serial.print(z);
  Serial.print(" T: ");
  Serial.println(temperature);
 
  delay(100);
}

 

Output

Open the serial monitor and you should see something like this – move the module around

Accel X: -16041 Y: 25096 Z: 30902 T: -5
Accel X: -4264 Y: -3219 Z: -4920 T: -5
Accel X: -3665 Y: -24195 Z: -6582 T: -5
Accel X: -2387 Y: 29273 Z: 25093 T: -5
Accel X: -10046 Y: 18720 Z: 15595 T: -5
Accel X: -19708 Y: -2226 Z: 5157 T: -6
Accel X: -13490 Y: -2875 Z: -2190 T: -6
Accel X: -10057 Y: 11695 Z: 22556 T: -6
Accel X: 1924 Y: 27351 Z: 16082 T: -5
Accel X: -4231 Y: 18639 Z: 14947 T: -5
Accel X: -17774 Y: -2564 Z: 6430 T: -5
Accel X: -13527 Y: -7457 Z: 5894 T: -5
Accel X: -20130 Y: -15443 Z: 10049 T: -5
Accel X: 3386 Y: -1488 Z: 17183 T: -5
Accel X: -2989 Y: 15951 Z: 9403 T: -6
Accel X: -15118 Y: 9226 Z: 6249 T: -6

 

Link

 

Share

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