Home CodeArduino ADXL313 accelerometer and Raspberry Pi Pico Arduino example

ADXL313 accelerometer and Raspberry Pi Pico Arduino example

by rp2040guy71

In this article we connect the ever popular ADXL313 accelerometer to a raspberry Pi Pico and we will then display readings via the serial monitor window. We are using the Arduino IDE.

Lets look at the sensor first


The ADXL313 is a small, thin, low power, 3-axis accelerometer with high resolution (13-bit) measurement up to ±4g. Digital output data is formatted as 16-bit twos complement and is accessible through either a serial port interface (SPI) (3-wire or 4-wire) or I2C digital interface.

  • Ultralow power (scales automatically with data rate)
    • As low as 30 μA in measurement mode (VS = 3.3 V)
    • As low as 0.1 μA in standby mode (VS = 3.3 V)
  • Low noise performance
    • 150 μg/√Hz typical for X- and Y-axes
    • 250 μg/√Hz typical for the Z-axis
  • Embedded, patent pending FIFO technology minimizes host processor load
  • User-selectable resolution
    • Fixed 10-bit resolution for any g range
    • Fixed 1024 LSB/g sensitivity for any g range
    • Resolution scales from 10-bit at ±0.5 g to 13-bit at ±4 g
  • Built-in motion detection functions for activity/inactivity monitoring
  • Supply and I/O voltage range: 2.0 V to 3.6 V
  • SPI (3-wire and 4-wire) and I2C digital interfaces
  • Flexible interrupt modes mappable to two interrupt pins
  • Measurement range selectable via serial command
  • Bandwidth selectable via serial command
  • Wide temperature range (−40°C to +105°C)
  • 10,000 g shock survival

Parts Required

Name Link
Raspberry Pi Pico
Connecting cables



Here is a layout in fritzing to show this

Code Example

I used the Sparkfun ADXL313 library and  this is the default example which worked, there are several examples to experiment with

The library needs updated to work with the Raspberry Pi Pico – this issue is detailed here – https://github.com/sparkfun/SparkFun_ADXL313_Arduino_Library/issues/5

Look for the following in SparkFunADXL313.cpp

//Initializes the sensor with basic settings via SPI
//Returns false if sensor is not detected
boolean ADXL313::beginSPI(uint8_t CS_pin, SPIClass &spiPort)
    _CS = CS_pin;
    _spiPort = &spiPort;
    I2C = false;
//_spiPort->begin(_CS); // throws a compile error, no such function
//_spiPort->setDataMode(SPI_MODE3); // old way of doing SPI settings, now you must pass in an SPISettings object
_spiPort->beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE3));
    pinMode(_CS, OUTPUT);
    digitalWrite(_CS, HIGH);

    // since we can't simply check for an "ACK" like in I2C,
    // we will check PART ID, to verify that it's there and working
  	if (checkPartId() == false) // Check for sensor Part ID
    	return (false);

  	return (true); //We're all setup!

And the basic example

#include <Wire.h>
#include "SparkFunADXL313.h" //Click here to get the library: http://librarymanager/All#SparkFun_ADXL313
ADXL313 myAdxl;

void setup()
  Serial.println("Example 1 - Reading values from ADXL313");


  if (myAdxl.begin() == false) //Begin communication over I2C
    Serial.println("The sensor did not respond. Please check wiring.");
    while(1); //Freeze
  Serial.print("Sensor is connected properly.");
  myAdxl.measureModeOn(); // wakes up the sensor from standby and puts it into measurement mode

void loop()
  if(myAdxl.dataReady()) // check data ready interrupt, note, this clears all other int bits in INT_SOURCE reg
    myAdxl.readAccel(); // read all 3 axis, they are stored in class variables: myAdxl.x, myAdxl.y and myAdxl.z
    Serial.print("x: ");
    Serial.print("\ty: ");
    Serial.print("\tz: ");
    Serial.println("Waiting for dataReady.");



x: 10 y: -21 z: -512
x: 95 y: 57 z: -512
x: 60 y: -145 z: -512
x: -256 y: -210 z: -512
x: -512 y: 424 z: 511
x: -512 y: -512 z: 250
x: -164 y: 159 z: -512
x: -17 y: -219 z: -512




You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More