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

Arduino and BME environmental sensor example

In this article we will connect a BME680 sensor to an Arduino

BME680 is an integrated environmental sensor developed specifically for mobile applications and wearables where size and low power consumption are key requirements. Expanding Bosch Sensortec’s existing family of environmental sensors, the BME680 integrates for the first time high-linearity and high-accuracy gas, pressure, humidity and temperature sensors. The gas sensor within the BME680 can detect a broad range of gases to measure air quality for personal well being.

Gases that can be detected by the BME680 include Volatile Organic Compounds (VOC) from paints (such as formaldehyde), lacquers, paint strippers, cleaning supplies, furnishings, office equipment, glues, adhesives and alcohol.

Parameter Technical data
Package dimensions 8-Pin LGA with metal
3.0 x 3.0 x 0.93 mm³
Operation range (full accuracy) Pressure: 300…1100 hPa
Humidity 0…100%
Temperature: -40…85°C
Supply voltage VDDIO
Supply voltage VDD
1.2 … 3.6 V
1.71 … 3.6 V
Interface I²C and SPI
Average current consumption
(1Hz data refresh rate)
2.1 µA at 1 Hz humidity and temperature
3.1 µA at 1 Hz pressure and temperature
3.7 µA at 1 Hz humidity, pressure and temperature 0.09‒12 mA for p/h/T/gas depending on operation mode
Average current consumption in sleep mode 0.15 μA
Gas sensor
Response time (τ 33-63%)
Sensor-to-sensor deviation
Power consumption
Output data processing
< 1 s (for new sensors)
+/- 15% +/- 15
< 0.1 mA in ultra-low power mode
direct output of IAQ: Index for Air Quality
Humidity sensor
Response time (τ0-63%)
Accuracy tolerance
Hysteresis
8 s
± 3 % relative humidity
≤ 1.5 % relative humidity
Pressure sensor
RMS Noise
Sensitivity Error
Temperature coefficient offset
0.12 Pa (equiv. to 1.7 cm)
± 0.25 % (equiv. to 1 m at 400 m height change)
±1.3 Pa/K (equiv. to ±10.9 cm at 1°C temperature change)

 

Parts List

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
BME680 BME680 Sensor Module Temperature and Humidity Air Pressure Air Quality IAQ MCU680 Module
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic

We use the I2C connection for the sensor

arduino and bme680

arduino and bme680

 

Code

You will need to import the adafruit sensor and bme680 libraries – you can add these using the library manager

My particular sensor used address 0x76, the default is 0x77 so you may have to change this line from if (!bme.begin(0x76)) to if (!bme.begin())

 

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
 
 
#define SEALEVELPRESSURE_HPA (1013.25)
 
Adafruit_BME680 bme; // I2C
 
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println(F("BME680 test"));
 
  if (!bme.begin(0x76)) 
  {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }
 
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms
}
 
void loop() 
{
  if (! bme.performReading()) 
  {
    Serial.println("Failed to perform reading :(");
    return;
  }
  Serial.print("Temperature = ");
  Serial.print(bme.temperature);
  Serial.println(" *C");
 
  Serial.print("Pressure = ");
  Serial.print(bme.pressure / 100.0);
  Serial.println(" hPa");
 
  Serial.print("Humidity = ");
  Serial.print(bme.humidity);
  Serial.println(" %");
 
  Serial.print("Gas = ");
  Serial.print(bme.gas_resistance / 1000.0);
  Serial.println(" KOhms");
 
  Serial.print("Approx. Altitude = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");
 
  Serial.println();
  delay(2000);
}

 

Output

Open the serial monitor and you will see something like this

BME680 test
Temperature = 21.52 *C
Pressure = 971.46 hPa
Humidity = 42.24 %
Gas = 0.00 KOhms
Approx. Altitude = 353.89 m

Temperature = 22.38 *C
Pressure = 971.44 hPa
Humidity = 42.71 %
Gas = 144.27 KOhms
Approx. Altitude = 354.06 m

Temperature = 24.83 *C
Pressure = 971.46 hPa
Humidity = 43.88 %
Gas = 139.47 KOhms
Approx. Altitude = 353.72 m

Share

Arduino and MCP3424 A/D converter example

Arduino and MCP3424 A/D converter example, lets look at the device.

The MCP3424 is a four channel low-noise, high accuracy delta-sigma A/D converter with differential inputs and up to 18 bits of resolution. The on-board precision 2.048V reference voltage enables an input range of ±2.048V differentially. The device uses a two-wire I2C™ compatible serial interface and operates from a single power supply ranging from 2.7V to 5.5V.

The MCP3424 device performs conversions at rates of 3.75, 15, 60 or 240 samples per second depending on user controllable configuration bit settings using the two-wire I2C™ compatible serial interface. The I2C™ address is user configurable with two address selection pins. This device has an onboard programmable gain amplifier (PGA). User can select the PGA gain of x1, x2, x4, or x8 before the analog-to-digital conversion takes place. This allows the MCP3424 device to convert a smaller input signal with high resolution. The device has two conversion modes: (a) Continuous mode and (b) One-Shot mode.

In One-Shot mode, the device enters a low current standby mode automatically after one conversion. This reduces current consumption greatly during idle periods. The MCP3424 device can be used for various high accuracy analog-to-digital data conversion applications where ease of use, low power consumption and small footprint are major considerations.

Features
    • 18-bit resolution
    • 4-channel differential input operation
    • Differential input operation
    • On-board voltage reference with 15 ppm/°C drift
    • On-board PGA, gains of 1, 2, 4, 8
    • Programmable data rate options
      • 3.75 SPS (18 bits)
      • 15 SPS (16 bits)
      • 60 SPS (14 bits)
      • 240 SPS (12 bits)
    • INL 10 ppm of FSR max
    • Low current consumption, 135 µA at 3V
    • One-shot or continuous conversion options
    • Supports I2C™ serial interface with user configurable addresses
    • Extended temperature range: -40°C to +125°C

 

Parts List

Part Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
MCP3424 module MCP3424 Digital I2C ADC-4 Channel Conversion Module
Connecting cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Code

You need to install the following library – https://github.com/bersch/MCP3424

#include <Wire.h>
#include <MCP3424.h>
 
MCP3424 adc(PIN_FLOAT, PIN_FLOAT);
 
void setup() 
{
    Serial.begin(9600);
    Wire.begin();
    adc.generalCall(GC_RESET);
    adc.creg[CH1].bits = { GAINx1, SR18B, CONTINUOUS, CH1, 1 };
}
 
double value;
static char * errmsg[] = {"", "underflow", "overflow", "i2c", "in progress", "timeout"};
 
void loop() 
{
    ConvStatus err = adc.read(CH1, value);
    if (err == R_OK) 
      Serial.println(value, DEC); 
    else 
    {
      Serial.print("conversion error: ");
      Serial.println(errmsg[err]);
    }
    asm volatile ("nop");
}

Output

Open the serial monitor – the low reading was ground and the higher reading was 3.3v

2.0479843616
2.0479843616
2.0479843616
0.0402031278
0.0402031278
0.0401875019
0.0401875019
0.0401875019
0.0401875019
0.0401875019
0.0401875019
0.0401718759
0.0401406240
1.9360313415
2.0479843616
2.0479843616

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