SparkFun Qwiic 3-Axis Accelerometer (ADXL313) Hookup Guide

Pages
Favorited Favorite 3

Arduino Library

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

We've written a library to easily get setup and take readings from the SparkFun 3-Axis Digital Accelerometer Breakout - ADXL313 (Qwiic). However, before we jump into getting data from the sensor, let's take a closer look at the available functions in the library. You can install this library through the Arduino Library Manager. Search for SparkFun ADXL313 Arduino Library and you should be able to install the latest version. If you prefer manually downloading the libraries from the GitHub repository, you can grab them here:

Let's get started by looking at the functions that set up the ADXL313:

Class

In the global scope, construct your sensor object (such as mySensor) without arguments.

ADXL313 mySensor;

Object Parameters and setup()

Rather than passing a bunch of data to the constructor, configuration is accomplished by setting the values of the ADXL313 type in the setup() function. They are exposed by being public: so use the myName.aVariable = someValue; syntax.

  • Initialize Sensor
    • .begin() or .begin(i2caddr, Wire) - Initializes the sensor with basic settings and returns false if sensor is not detected.
      • .isConnected() - Returns true if I2C device ack's
    • .beginSPI(CS_pin) - Initializes the sensor with basic settings via SPI and returns false if sensor is not detected.
    • .checkPartId() - Returns true if device's part ID register is correct.
    • .dataReady() - Checks the dataReady bit
    • .updateIntSourceStatuses()
    • .standby() - Clears the measure bit, putting decive in standby mode, ready for configuration
    • .measureModeOn() - Sets the measure bit, putting decive in measure mode, ready for reading data
    • .softReset() - Soft reset clears all settings, and puts it in standby mode
    • .printAllRegister() - Print Register Values to Serial Output.
      • Can be used to Manually Check the Current Configuration of Device
  • Reading Acceleration
    • .readAccel() - Reads acceleration data from ADXL313 into three class variables: x, y, and z.
  • Sensor Configuration
    • Range Setting
      • .setRange(range)
        • 0.5g, 1g, 2g, or 4g
      • .getRange()
    • Autosleep Bit
      • .autosleepOn() - Sets the autosleep bit
        • Note, prior to calling this, you will need to set THRESH_INACT and TIME_INACT.
      • .autosleepOff() - Clears the autosleep bit
    • Self-Test Bit
      • .setSelfTestBit(selfTestBit)
        • 1 - Self-Test Applied. Electrostatic Force exerted on the sensor causing a shift in the output data.
        • 0 - Self-Test Disabled.
      • .getSelfTestBit()
    • SPI Bit State
      • .setSpiBit(spiBit)
        • 1 - Puts Device in 3-wire Mode
        • 0 - Puts Device in 4-wire SPI Mode
      • .getSpiBit()
    • INT_INVERT Bit State
      • .setInterruptLevelBit(interruptLevelBit)
        • 0 - Sets the Interrupts to Active HIGH
        • 1 - Sets the Interrupts to Active LOW
      • .getInterruptLevelBit()
    • FULL_RES Bit State
      • .setFullResBit(fullResBit)
        • 1 - Device is in Full Resolution Mode: Output Resolution Increase with G Range set by the Range Bits to Maintain a 4mg/LSB Scale Factor
        • 0 - Device is in 10-bit Mode: Range Bits Determine Maximum G Range and Scale Factor
      • .getFullResBit()
    • JUSTIFY Bit State
      • .setJustifyBit(justifyBit)
        • 1 - Selects the Left Justified Mode
        • 0 - Selects Right Justified Mode with Sign Extension
      • .getJustifyBit()
    • Gain -Gains for each axis in g's/count
      • .setAxisGains(_gains)
      • .getAxisGains(_gains)
    • OFSX, OFSY, and OFSZ Bytes - User Offset Adjustments in Twos Complement Format. Scale Factor of 15.6mg/LSB.
      • .setAxisOffset(x, y, z)
      • .getAxisOffset(x, y, z)
    • THRESH_ACT Register - Holds the Threshold Value for Detecting Activity.
      • .setActivityThreshold(activityThreshold)
        • Data Format is Unsigned, so the Magnitude of the Activity Event is compared with the Value is Compared with the Value in the THRESH_ACT Register.
        • The Scale Factor is 62.5mg/LSB.
        • Value of 0 may Result in Undesirable Behavior if the Activity Interrupt Enabled.
        • It Accepts a Maximum Value of 255.
      • .getActivityThreshold() - Gets the THRESH_ACT byte
    • THRESH_INACT Register - Holds the Threshold Value for Detecting Inactivity.
      • .setInactivityThreshold(inactivityThreshold)
        • The Data Format is Unsigned, so the Magnitude of the Inactivity Event is compared with the value in the THRESH_INACT Register.
        • Scale Factor is 62.5mg/LSB.
        • Value of 0 May Result in Undesirable Behavior if the Inactivity Interrupt Enabled.
        • It Accepts a Maximum Value of 255.
      • .getInactivityThreshold()
    • TIME_INACT Register
      • .setTimeInactivity(timeInactivity)
        • Contains an Unsigned Time Value Representing the Amount of Time that Acceleration must be Less Than the Value in the THRESH_INACT Register for Inactivity to be Declared.
        • Uses Filtered Output Data unlike other Interrupt Functions
        • Scale Factor is 1sec/LSB.
        • Value Must Be Between 0 and 255.
      • .getTimeInactivity()
  • Activity Bits
    • Enabled
      • .isActivityXEnabled()
      • .isActivityYEnabled()
      • .isActivityZEnabled()
      • .isInactivityXEnabled()
      • .isInactivityYEnabled()
      • .isInactivityZEnabled()
    • State
      • .setActivityX(state)
      • .setActivityY(state)
      • .setActivityZ(state)
      • .setActivityXYZ(stateX, stateY, stateZ)
      • .setInactivityX(state)
      • .setInactivityY(state)
      • .setInactivityZ(state)
      • .setInactivityXYZ(stateX, stateY, stateZ)
    • Active
      • .isActivityAc()
      • .isInactivityAc()
      • .setActivityAc(state)
      • .setInactivityAc(state)
  • Low Power Bit
    • .isLowPower()
    • .lowPowerOn()
    • .lowPowerOff()
  • Rate Bits
    • .getRate()
    • .setRate(rate)
  • Bandwidth
    • .setBandwidth(bw)
    • .getBandwidth()
  • Trigger Check - Check if Action was Triggered in Interrupts
    • .triggered(interrupts, mask)
    • .getInterruptSource()
    • .getInterruptSource(interruptBit)
    • .getInterruptMapping(interruptBit)
  • Interrupt Mapping - Set the Mapping of an Interrupt to pin1 or pin2
    • .setInterruptMapping(interruptBit, interruptPin)
    • .isInterruptEnabled(interruptBit)
    • .setInterrupt(interruptBit, state)
    • .ActivityINT(status)
    • .InactivityINT(status)
    • .DataReadyINT(status)
    • .WatermarkINT(status)
    • .OverrunINT(status)
  • FIFO Mode Setting
    • .getFifoMode()
    • .setFifoMode(mode)
    • .getFifoSamplesThreshhold()
    • .setFifoSamplesThreshhold(samples)
    • .getFifoEntriesAmount()
    • .clearFifo()