Categories

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 MPU6500 6-axis Motion Tracking device

The MPU-6500 is the company’s second-generation 6-axis Motion Tracking device for smartphones, tablets, wearable sensors, and other consumer markets.

The MPU-6500, delivered in a 3 mm x 3 mm x 0.9 mm QFN package, addresses the market requirements for high-performance applications such as pedestrian navigation, context-aware advertising, and other location-based services.

The device also supports the specifications for emerging wearable sensor applications, such as remote health monitoring, sports and fitness tracking, and other consumer applications.

Features

Digital-output of 6-axis MotionFusion data.
9-axis fused data from Motion Processing Library
Tri-Axis angular rate sensor (gyro) with a sensitivity up to 131 LSBs/dps and a full-scale range of ±250, ±500, ±1000, and ±2000dps
Tri-Axis accelerometer with a programmable full scale range of ±2g, ±4g, ±8g and ±16g
Reduced settling effects and sensor drift by elimination of board-level cross-axis alignment errors between accelerometers and gyroscopes
Digital Motion Processing™ (DMP™) engine offloads complex MotionFusion, sensor timing synchronization and gesture detection
MotionApps™ Platform support for Android, Linux, and Windows
Embedded algorithms for run-time bias and compass calibration
Digital-output temperature sensor
Digital input on FSYNC pin to support video Electronic Image Stabilization
Programmable interrupt supports gesture recognition, panning, zooming, scrolling, tap detection, and shake detection
VDD Supply voltage range of 1.71V–3.6V
Gyro operating current: 3.2mA (full power, gyro at all rates)
Gyro + Accel operating current: 3.4mA (full power, gyro at all rates, accel at 1kHz sample rate)
Accel low power mode operating currents: 18.65µA at 31.25Hz
Full Chip Idle Mode Supply Current: 6µA
400kHz Fast Mode I²C or up to 20MHz SPI serial host interfaces
10,000g shock tolerant

 

Layout

 

arduino and mpu-6500

arduino and mpu-6500

 

Code

I found a library here that seemed to work ok – http://www.uctronics.com/download/U3635_MPU6500.zip

 

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"
 
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"
 
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
 
int16_t ax, ay, az;
int16_t gx, gy, gz;
 
#define LED_PIN 13
bool blinkState = false;
 
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin();
 
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(38400);
 
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
 
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
 
// configure Arduino LED for
pinMode(LED_PIN, OUTPUT);
}
 
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
 
// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);
 
// display tab-separated accel/gyro x/y/z values
Serial.print("a/g:\t");
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
 
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}

 

Output

Open the serial monitor

a/g: -280 -10732 -7336 32767 18392 -32768
a/g: 1620 -9008 -5564 32767 16477 -32768
a/g: 4368 -10656 -4060 32767 15867 -32768
a/g: 7724 -9952 -832 32767 14362 -32768
a/g: 6512 -6872 -340 32767 19106 -32768
a/g: 13252 -12072 3184 32767 20260 -32768
a/g: 15236 -12164 4784 32767 16474 -32768
a/g: 13152 -12644 2924 32767 10330 -32768
a/g: 16376 -11592 4708 31548 6686 -32768
a/g: 15676 -10160 4612 28194 3577 -32768
a/g: 15320 -10376 4480 25826 63 -32768
a/g: 17024 -9380 5472 25643 -483 -32768
a/g: 12064 -8728 4524 25102 -2927 -32768
a/g: 13240 -7600 5172 21817 -4007 -32768

 

Link

https://www.invensense.com/wp-content/uploads/2015/02/MPU-6500-Datasheet2.pdf

GY-6500 MPU-6500 6DOF six-axis accelerometer 6-axis attitude gyro sensor module SPI Interface MPU6500

Share

Arduino and LM75 temperature sensor example

The LM75 temperature sensor includes a delta-sigma analog-to-digital converter, and a digital overtemperature detector. The host can query the LM75 through its I²C interface to read temperature at any time. The open-drain overtemperature output (OS) sinks current when the programmable temperature limit is exceeded.

The OS output operates in either of two modes, comparator or interrupt. The host controls the temperature at which the alarm is asserted (TOS) and the hysteresis temperature below which the alarm condition is not valid (THYST). Also, the LM75’s TOS and THYST registers can be read by the host.

The address of the LM75 is set with three pins to allow multiple devices to work on the same bus. Power-up is in comparator mode, with defaults of TOS = +80°C and THYST = +75°C. The 3.0V to 5.5V supply voltage range, low supply current, and I²C interface make the LM75 ideal for many applications in thermal management and protection.

Key Features

SO (SOP) and µMAX® (µSOP) Packages
I²C Bus Interface
Separate Open-Drain OS Output Operates as Interrupt or Comparator/Thermostat Input
Register Readback Capability
Power-Up Defaults Permit Stand-Alone Operation as a Thermostat
3.0V to 5.5V Supply Voltage
Low Operating Supply Current 250µA (typ), 1mA (max)
4µA (typ) Shutdown Mode Minimizes Power Consumption
Up to Eight LM75s Can Be Connected to a Single Bus

 

Layout

I2C device you can power it from 3.3 or 5v

 

 

Code

I used this library – https://github.com/jlz3008/lm75

 

#define VERSION "1.1"
 
#include <inttypes.h>
#include <Wire.h>
 
#include <lm75.h>
 
TempI2C_LM75 termo = TempI2C_LM75(0x48,TempI2C_LM75::nine_bits);
 
 
void setup()
{
Serial.begin(9600);
Serial.println("Start");
Serial.print("Actual temp ");
Serial.print(termo.getTemp());
Serial.println(" oC");
delay(2000);
}
 
void loop()
{
Serial.print(termo.getTemp());
Serial.println(" oC");
delay(5000);
}

 

 

Links

LM75 temperature sensor high speed I2C interface high precision development board module

Share

Arduino and GY-21P module example

The GY-21P is an interesting module in that it combines a BMP280 sensor and an SI7021 sensor. The on-board BMP280+SI7021 sensor measures atmospheric pressure from 30kPa to 110kPa as well as relative humidity and temperature.

BMP280
Pressure range: 300-1100 hPa (9000 meters above sea level at -500m)
Relative accuracy (at 950 – 1050 hPa at 25 ° C): ± 0.12 hPa, equiv. to ± 1 m
Absolute accuracy (at (950 – 1050 hPa, 0 – +40 ° C): ± 0.12 hPa, equiv. To ± 1 m
Mains voltage: 1.8V – 3.6V
Power consumption: 2.7µA at 1Hz readout rate
Temperature range: -40 to + 85 ° C

SI7021
HVAC/R
Thermostats/humidistats
Respiratory therapy
White goods
Indoor weather stations
Micro-environments/data centers
Automotive climate control and defogging
Asset and goods tracking
Mobile phones and tablets
Size: 1.3*1cm/0.51*0.39″

Features:

Operation Voltage: 3.3V
I2C & SPI Communications Interface
Temp Range: -40C to 85C
Humidity Range: 0 – 100% RH, =-3% from 20-80%
Pressure Range: 30,000Pa to 110,000Pa, relative accuracy of 12Pa, absolute accuracy of 100Pa
Altitude Range: 0 to 30,000 ft (9.2 km), relative accuracy of 3.3 ft (1 m) at sea level, 6.6 (2 m) at 30,000 ft.

Code

I use a variety of Adafruit libraries, took the default examples and made the following out of them

https://github.com/adafruit/Adafruit_Sensor

https://github.com/adafruit/Adafruit_BMP280_Library

https://github.com/adafruit/Adafruit_Si7021

 

 

include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_Si7021.h"
 
Adafruit_BMP280 bme; // I2C
Adafruit_Si7021 sensor = Adafruit_Si7021();
 
void setup() 
{
Serial.begin(9600);
Serial.println("BMP280 and SI7021 (GY-21p) test");
 
if (!bme.begin()) 
{ 
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
 
if (!sensor.begin()) 
{
Serial.println("Did not find Si7021 sensor!");
while (true);
}
}
 
void loop() 
{
Serial.println("BMP280 results");
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure());
Serial.println(" Pa");
Serial.print("Approx altitude = ");
Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
Serial.println(" m");
Serial.println();
 
Serial.println("SI7021 results");
Serial.print("Humidity: "); 
Serial.println(sensor.readHumidity(), 2);
Serial.print("Temperature: "); 
Serial.println(sensor.readTemperature(), 2);
Serial.println();
delay(2000);
}

 

Output

OPen the serial monitor

BMP280 results
Temperature = 27.26 *C
Pressure = 99042.63 Pa
Approx altitude = 191.78 m

SI7021 results
Humidity: 31.04
Temperature: 27.30

BMP280 results
Temperature = 28.43 *C
Pressure = 99044.82 Pa
Approx altitude = 191.60 m

SI7021 results
Humidity: 32.59
Temperature: 28.56

BMP280 results
Temperature = 28.17 *C
Pressure = 99037.41 Pa
Approx altitude = 192.22 m

SI7021 results
Humidity: 32.88
Temperature: 28.46

You can see the temperature output is close between the sensors

 

Links

Atmospheric Humidity Temperature Sensor Breakout Barometric Pressure BMP280 SI7021 for Arduino

Share