Configure an ADXL345 Accelerometer

The accel-adxl345 movement sensor model supports the Analog Devices ADXL345 digital accelerometer. This three axis accelerometer supplies linear acceleration data, supporting the LinearAcceleration method.

If you are using a Viam Rover, this is the accelerometer on it.

Navigate to the Config tab of your machine’s page in the Viam app. Click on the Components subtab and click Create component. Select the movement-sensor type, then select the accel-adxl345 model. Enter a name for your movement sensor and click Create.

Creation of an `accel-adxl345` movement sensor in the Viam app config builder.

Copy and paste the following attribute template into your movement sensor’s Attributes box. Then remove and fill in the attributes as applicable to your movement sensor, according to the table below.

{
  "i2c_bus": "<your-i2c-bus-index>",
  "use_alternate_i2c_address": <boolean>,
  "board": "<your-board-name>",
  "tap": {
    "accelerometer_pin": <int>,
    "interrupt_pin": "<your-digital-interrupt-name-on-board>",
    "exclude_x": <boolean>,
    "exclude_y": <boolean>,
    "exclude_z": <boolean>,
    "threshold": <float>,
    "dur_us": <float>
  },
  "free_fall": {
    "accelerometer_pin": <int>,
    "interrupt_pin": "<your-digital-interrupt-name-on-board>",
    "threshold": <float>,
    "time_ms": <float>
  }
}
{
  "i2c_bus": "2"
}
{
  "components": [
    {
      "name": "<your-sensor-name>",
      "model": "accel-adxl345",
      "type": "movement_sensor",
      "namespace": "rdk",
      "attributes": {
        "board": "<your-board-name>",
        "i2c_bus": "<your-i2c-bus-index-on-board>",
        "use_alternate_i2c_address": <boolean>,
        "tap": {
          "accelerometer_pin": <int>,
          "interrupt_pin": "<your-digital-interrupt-name-on-board>",
          "exclude_x": <boolean>,
          "exclude_y": <boolean>,
          "exclude_z": <boolean>,
          "threshold": <float>,
          "dur_us": <float>
        },
        "free_fall": {
          "accelerometer_pin": <int>,
          "interrupt_pin": "<your-digital-interrupt-name-on-board>",
          "threshold": <float>,
          "time_ms": <float>
        }
      },
      "depends_on": []
    }
  ]
}
{
  "components": [
    {
      "name": "my-adxl",
      "model": "accel-adxl345",
      "type": "movement_sensor",
      "namespace": "rdk",
      "attributes": {
        "i2c_bus": "2",
        "use_alternate_i2c_address": false
      }
    }
  ]
}

Attributes

NameTypeInclusionDescription
i2c_busstringRequiredThe index of the I2C bus on the board your device is connected to. Often a number.
Example: “2”
use_alternate_i2c_addressboolOptionalDepends on whether you wire SDO low (leaving the default address of 0x53) or high (making the address 0x1D). If high, set true. If low, set false or omit the attribute.
Default: false
boardstringOptionalThe name of the board to which the device is wired. Only needed if you’ve configured any interrupt functionality.
tapobjectOptionalHolds the configuration values necessary to use the tap detection interrupt on the ADXL345. See Tap attributes.
free_fallobjectOptionalHolds the configuration values necessary to use the free-fall detection interrupt on the ADXL345. See Freefall attributes.

Tap attributes

Inside the tap object, you can include the following attributes:

NameTypeInclusionDescription
accelerometer_pinintRequiredOn the accelerometer you can choose to send the interrupts to int1 or int2. Specify this by setting this config value to 1 or 2.
interrupt_pinstringRequiredThe name of the digital interrupt you configured for the pin on the board wired to the accelerometer_pin.
exclude_xboolOptionalTap detection defaults to all three axes. Exclude the x axis by setting this to true.
Default: false
exclude_yboolOptionalTap detection defaults to all three axes. Exclude the y axis by setting this to true.
Default: false
exclude_zboolOptionalTap detection defaults to all three axes. Exclude the z axis by setting this to true.
Default: false
thresholdfloatOptionalThe magnitude of the threshold value for tap interrupt (in milligrams, between 0 and 15,937).
Default: 3000
dur_usfloatOptionalUnsigned time value representing maximum time that an event must be above the threshold to qualify as a tap event (in microseconds, between 0 and 159,375).
Default: 10000

Freefall attributes

Inside the freefall object, you can include the following attributes:

NameTypeInclusionDefault ValueDescription
accelerometer_pinintRequiredOn the accelerometer you can choose to send the interrupts to int1 or int2. Specify this by setting this config value to 1 or 2.
interrupt_pinstringRequiredThe name of the digital interrupt you configured for the pin on the board wired to the accelerometer_pin.
thresholdfloatOptionalThe acceleration on each axis is compared with this value to determine if a free-fall event occurred (in milligrams, between 0 and 15,937).
Default: 437.5
time_msfloatOptionalUnsigned time value representing the minimum time that the value of all axes must be less than threshold to generate a free-fall interrupt (in milliseconds, between 0 and 1,275).
Default: 160

Test the movement sensor

After you configure your movement sensor, navigate to the Control tab and select the dedicated movement sensor dropdown panel. This panel presents the data collected by the movement sensor.

The movement sensor component in the control tab


Have questions, or want to meet other people working on robots? Join our Community Discord.

If you notice any issues with the documentation, feel free to file an issue or edit this file.