1

member

Easy Basics: Project 031b ADXL345 (GY-291) three-axis accelerometer module

of Lex C in UNO

Basics: Project 031b

Project name: ADXL345 (GY-291) three-axis accelerometer module

Tags: ADXL345, Arduino, Arduino Uno, ADXL 345, GY-291

Attachments:

calibrationsketch and testsketchi2c and testsketchspi with library1 and driver;

calibrationsketch and example sketch with library2;

examplesketch with library3

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you) :

1.Arduino Uno R3 (you can also use the other version of Arduino)

2. ADXL345 GY-291 module 1pc

3.Arduino IDE ( you can download it from here  )

4.Jumper cables F-M, M-M

5. Breadboard 1 pc

6. Resistor (2 pcs 4.7 KOhm, 1pc 10 KOhm)

7. Logic level converter (optional) 1 pc

Note: If you plan on using the ADXL345 in I2C mode, you do not need to have the logic level converter from the list above.

General

We will learn how to connect ADXL345 (GY-291) to Arduino board, calibrate and use it.

Understanding the ADXL345 (GY-291) module

An accelerometer is used to measure the force generated during the acceleration. The most fundamental is the commonly-known acceleration of gravity which is 1g. By measuring the acceleration caused by gravity, you can calculate the tilt angle of the device to the level surface. Through analyzing the dynamic acceleration, you can tell the way how the device is moving. For example, self-balancing board or hoverboard applies the acceleration sensor and gyroscope for Kalman filter and posture correction.

The ADXL345 is a small, thin, low power, 3-axis MEMS accelerometer with high resolution (13-bit) measurement at up to +/-16 g. Digital output data is formatted as 16-bit two’s complement and is accessible through either an SPI (3- or 4-wire) or I2C digital interface.

The ADXL345 measures both dynamic acceleration resulting from motion or shock and static acceleration, such as gravity, which allows the device to be used as a tilt sensor. The sensor is a polysilicon surface-micromachined structure built on top of a silicon wafer. Polysilicon springs suspend the structure over the surface of the wafer and provide a resistance against acceleration forces. Deflection of the structure is measured using differential capacitors that consist of independent fixed plates and plates attached to the moving mass. Acceleration deflects the beam and unbalances the differential capacitor, resulting in a sensor output whose amplitude is proportional to acceleration. Phase-sensitive demodulation is used to determine the magnitude and polarity of the acceleration. 

The ADXL345 is well suited for mobile device applications. It measures the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (4 mg/LSB) enables measurement of inclination changes less than 1.0°. Several special sensing functions are provided. Activity and inactivity sensing detect the presence or lack of motion and if the acceleration on any axis exceeds a user-set level. Tap sensing detects single and double taps. Free-fall sensing detects if the device is falling. These functions can be mapped to one of two interrupt output pins. An integrated, patent pending 32-level first in, first out (FIFO) buffer can be used to store data to minimize host processor intervention. Low power modes enable intelligent motion-based power management with threshold sensing and active acceleration measurement at extremely low power dissipation. 

FEATURES

  • Ultralow power: as low as 40 μA in measurement mode and 0.1 μA in standby mode at VS = 2.5 V (typical)
  • Power consumption scales automatically with bandwidth 
  • User-selectable resolution (Fixed 10-bit resolution, Full resolution, where resolution increases with g range, up to 13-bit resolution at ±16 g (maintaining 4 mg/LSB scale factor in all g ranges)
  • Embedded, patent pending FIFO technology minimizes host processor load 
  • Tap/double tap detection 
  • Activity/inactivity monitoring 
  • Free-fall detection 
  • Supply voltage range: 2.0 V to 3.6 V 
  • I/O voltage range: 1.7 V to VS 
  • SPI (3- and 4-wire) and I2C digital interfaces
  • Flexible interrupt modes mappable to either interrupt pin 
  • Measurement ranges selectable via serial command
  • Bandwidth selectable via serial command 
  • Wide temperature range (−40°C to +85°C) 
  • 10,000 g shock survival Pb free/RoHS compliant 
  • Small and thin: 3 mm × 5 mm × 1 mm LGA package 

The single and double tap sensing detects when a single, or two simultaneous, acceleration events occur. Activity and inactivity sensing detect the presence or lack of motion. Free-fall sensing compares the acceleration on all axes with the threshold value to know if the device is falling. All thresholds levels that trigger the activity, free-fall, and single tap/double tap events are user-set levels. These functions can also be mapped to one of two interrupt output pins. An integrated, patent pending 32-level first in, first out (FIFO) buffer can be used to store data to minimize host processor intervention.

The ADXL345 is well suited to measure the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (4 mg/LSB) enables measurement of inclination changes less than 1.0°. Furthermore, low power modes enable intelligent motion-based power management with threshold sensing and active acceleration measurement at extremely low power dissipation.

APPLICATIONS

  • Handsets
  • Medical instrumentation
  • Gaming and pointing devices
  • Industrial instrumentation
  • Personal navigation devices
  • Hard disk drive (HDD) protection
  • Fitness equipment

Using an Accelerometer for Inclination Sensing. See more info here.

Specification of the module attached here or here.

Calibration and Programming 

As with all sensors, there is some variation in output between samples of these accelerometers. For non-critical applications such as game controllers, or simple motion or tilt sensors, these variations are not important. But for applications requiring precise measurements, calibration to a reliable reference is a good idea.

The ADXL chips are calibrated at the factory to a level of precision sufficient for most purposes. For critical applications where a higher degree of accuracy is required, you may wish to re-calibrate the sensor yourself.
Calibration does not change the sensor outputs. But it tells you what the sensor output is for a known stable reference force in both directions on each axis. Knowing that, you can calculate the corrected output from a sensor reading.

Acceleration can be measured in units of gravitational force or "G", where 1G represents the gravitational pull at the surface of the earth. Gravity is a relatively stable force and makes a convenient and reliable calibration reference for surface-dwelling earthlings.

To calibrate the sensor to the gravitational reference, you need to determine the sensor output for each axis when it is precisely aligned with the axis of gravitational pull. Laboratory quality calibration uses precision positioning jigs. The method described here is simple and gives surprisingly good results with just a block of wood:

 or 

or you can just use the module itself. See the orientations for calibration with different type of ADXL 345 module below:

Signals and connections of ADXL345 module

There is a 3.3V voltage regulator chip on some modules, so you can power them with 5V or 3.3V. But there are some with 3.3V power connection only. Check specification of the module before connecting it to the Arduino board 5V otherwise you will burn it. 

The accelerometer ADXL345 module can have different pins:

GND - Ground. To be connected to Arduino board GND

Vin (or 5V or +5V) - Power supply. To be connected to Arduino board 5V

CS (Chip Select) - the pin on each device that the master can use to enable and disable specific devices  

VS -  Power supply (not used)

INT1 -Interrupt 1 Output.

INT2 -Interrupt 2 Output.

SDO -Serial Data Output (SPI 4-Wire) / I2C Address Select

SDA / SDI / SDIO - Serial Data (I2C)/Serial Data Input (SPI 4-Wire)/Serial Data Input and Output (SPI 3-Wire). 

SCL (or SCLK)  (Serial Clock) - The clock pulses which synchronize data transmission generated by the master

VCC - power supply, you can connect to Arduino board 5V or 3.3V (see drawings below)

3V3 - for 3.3v microprocessors, connect the pin marked 3V3 to the 3.3v supply. 

Wiring

With the ADXL345, I2C and SPI digital communications are available. In both cases, the ADXL345 operates as a slave device.

Note: A potential problem when hooking up the ADXL345 breakout to an Arduino (or compatible board) is, if you are using a breadboard with loosely connected jumper wires, you risk getting bad data. Make sure your connections are solid, and you should be fine.

1.SPI Communication

In SPI mode, the CS pin is controlled by the bus master. For SPI, either 3- or 4- wire configuration is possible. When using 3-wire SPI, it is recommended that the SDO pin be pulled up to VDD I/O or pulled down to GND via a 10 KOhm resistor. 

Using 3.3V power supply. The following is describing which pins on the Arduino should be connected to the pins on the accelerometer for SPI 4-wire communication:

Arduino Pin -> ADXL345 Pin

GND  GND

3V3  VCC

10  CS

12  SDO

11  SDA

13  SCL

If using a 5V Arduino board, such as the Arduino Uno, you will need to use a logic level converter to protect the ADXL345's 3/3V tolerant pins (as shown in the diagram above). If using a 3.3V Arduino, such as the Arduino Pro or Pro Mini 3.3V/8MHz, logic level conversion is not necessary.

Using 5V power supply. The following is describing which pins on the Arduino should be connected to the pins on the accelerometer for SPI 4-wire communication:

Arduino Pin -> ADXL345 Pin

GND  GND

5V  VCC

10  CS

12  SDO

11  SDA

13  SCL

2.I2C Communication

I2C mode is enabled if the CS pin is tied to high. There is no default mode if the CS pin is left unconnected, so it should always be tied high or driven by an external controller. If other devices are connected to the same I2C bus, the nominal operating voltage level of those other devices cannot exceed VDD I/O by more than 0.3 V. External pull-up resistors are necessary for proper I2C operation. Used in this connection diagram are two 4.7 KOhm resistors.

Using 3.3V power supply. The following is describing which pins on the Arduino should be connected to the pins on the accelerometer for I2C communication:

Arduino Pin -> ADXL345 Pin

GND GND

3V3 VCC

3V3 CS

GND SDO

A4 SDA

A5 SCL

Using 5V power supply. The following is describing which pins on the Arduino should be connected to the pins on the accelerometer for I2C communication.

Arduino Pin -> ADXL345 Pin

GND GND

5V VCC

SDA SDA

SCL SCL

The reference table below shows where Two Wire Interface (TWI) pins are located on different and older Arduino boards.

Board I2C / TWI Pins
Uno, Ethernet A4 (SDA), A5 (SCL)
Mega2560 20 (SDA), 21 (SCL)
Leonardo 2 (SDA), 3 (SCL)
Due 20 (SDA), 21 (SCL), SDA1, SCL1

Step by Step instruction

  1. Assembly the ADXL345 module. These accelerometer boards come with all surface-mount components pre-soldered. The included header strip can be soldered on for convenient use on a breadboard or with 0.1" connectors. However, for applications subject to extreme accelerations, shock or vibration, locking connectors or direct soldering plus strain relief is advised (Cut the strip to length if necessary. It will be easier to solder if you insert it into a breadboard - long pins down. Place the breakout board over the pins. Be sure to solder all pins for reliable electrical contact).
  2. First mount the sensor securely to a block or a box. The size is not important, as long as all the sides are at right angles. The material is not important as long as it is fairly rigid.
    Load the Calibration Sketch:
    Load and run the Calibration sketch. Open the Serial Monitor and wait for the prompt.
    Position the Block:
    Place the block on a firm flat surface such as a sturdy table. Type a character in the Serial Monitor and hit return. The sketch will take a measurement on that axis and print the results.
    Reposition the Block:
    Turn the block so a different side is flat on the table and type another key to measure that axis.
    Repeat:
    Repeat for all six sides of the block to measure the positive and negative aspects of each axis.
    For the sides obstructed by the breakout board and/or wires, press the block up against the bottom of the table while taking the reading.
    Calibration Results:
    Once all six sides have been sampled, the values printed in the Serial Monitor will represent actual measurements for +/- 1G forces on each axis. These values can be used to re-scale readings for better accuracy.
    Mount the sensor securely to a block or a box. The size is not important, as long as all the sides are at right angles. The material is not important as long as it is fairly rigid.
  3. Do wiring.
  4. Open Arduino IDE. 
  5. Plug your Adruino Uno board into your PC and select the correct board and com port
  6. Open up serial monitor and set your baud to 9600 baud
  7. Verify and upload the Calibrationsketch or Calibrationsketch to your Arduino board.
  8. Open the Serial Monitor and wait for the prompt.
  9. Place the block on a firm flat surface such as a sturdy table. Type a character in the Serial Monitor and press Send button. The sketch will take a measurement on that axis and print the results in Serial Monitor.
  10. Turn the block so a different side is flat on the table. Type a character in the Serial Monitor and press Send button. The sketch will take a measurement on that axis and print the results in Serial Monitor.
  11. Repeat that for all six sides of the block to measure the positive and negative aspects of each axis.
    For the sides obstructed by the breakout board and/or wires, press the block up against the bottom of the table while taking the reading.
  12. Once all six sides have been sampled, the values printed in the Serial Monitor will represent actual measurements for +/- 1G forces on each axis. These values can be used to re-scale readings for better accuracy.
  13. You can also check the testsketch / testsketchspi or exampleketch or examplesketch. You can see the readings in Serial Monitor when upload it.

 

Code

1. Adafruit_ADXL345 library.

We attached example and calibration sketches here.

Example sketch:  It works with I2C and SPI configuration 

Calibration sketch: This will be useful whenever you have an application that requires your device to be precision calibrated.The main code will read your accelerometer maximums and minimums.

2. SparkFun ADXL345 library.

We attached example and calibration sketches here.

Example sketch: It works with I2C and SPI communication. The first important selection to make is under the COMMUNICATION SECTION. This is where you will let the library know whether you have setup your hardware for SPI or I2C communication. Make sure to comment out // the line of code you are not using. By default, the code has you utilizing SPI communication.The most complex part of the example code is the void setup() section. This is where you’ll be able to configure your settings and sensing feature thresholds. The comments can help guide you as to what each function does along with recommended ranges to stay within where applicable. More detailed information of the sensing functions and interrupts is here. The sketch will only output free fall detection, inactivity/activity, and single/double tap detection to the Serial Monitor. In order to print the measured accelerometer values to the Serial Monitor, just remember to uncomment the following section:/* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */.

Calibration sketch: This will be useful whenever you have an application that requires your device to be precision calibrated.The main code will read your accelerometer maximums and minimums. With these values we will be able to calculate the offset values and gain factors giving us our new calibrated accelerometer readings. 

3. ADXL345 library

We attached example sketch here.

Example sketch: It works with I2C communication only. ADXL345_STD - standard address if SDO/ALT ADDRESS is HIGH. ADXL345_ALT- alternate address if SDO/ALT ADDRESS is LOW.

Summary

We have learnt how to connect ADXL345 to Arduino board, calibrate and use it.

Libraries:

  • See attachments on the begining of this project description.
  • Used Adafruit_ADXL345 library and Adafruit_Sensor (Adafruit Unified Sensor Driver) in this project. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC.You can read about the library here and the driver here.
  • Used SparkFun ADXL345 library in this project. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC.You can read about the library here.
  • Used ADXL345 library in this project. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC.You can read about this library here.
  • Wire and SPI libraries included in Arduino IDE program.

Sketch:

  • See attachment on the begining of this project description.


Other projects of Lex C
Easy Basics: Project 057a ACR122U NFC Reader of Lex C, Not selected 17-01-2018

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 05-12-2017
Viewed: 3742 times