# Trouble reading accelerometer

**URL:** <https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551>\
**Category:** General Guidance\
**Created:** [October 23, 2024, 7:41pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551 "2024-10-23T19:41:21Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![scadriantruthwatcher](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@scadriantruthwatcher](https://forum.arduino.cc/u/scadriantruthwatcher)\
**Post date:** [October 23, 2024, 7:41pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/1 "2024-10-23T19:41:21Z")

</div>

I flagged this as "hardware help" — this could be a software issue, but I suspect not.

I'm trying to read an ADXL345 accelerometer over 3-wire SPI, but I keep getting "-1, -1, -1" despite lots of motion. I've tried a couple of example sketches and run into the same issue. I need SPI communication for the speed (otherwise I'd use the I2C quick connector). What's going on? Thanks in advance! I'm using a Seeed Studio ESP32-C3. Details:

ADXL SDA to XIAO D18 (MOSI)  
ADXL SCL to XIAO D8 (SCK)  
ADXL CS to XIAO D1 (I've tried the GPIO: "3", label: "D1", and "1" in the code)  
Powering the ADXL from 3v3 (lists 3-5v range)

Code:

```arduino
#include <SparkFun_ADXL345.h> // Include the ADXL345 library

// Initialize the ADXL345 object for SPI communication
ADXL345 adxl = ADXL345(9); // Use GPIO 9 as the CS pin (modify this if needed)

void setup() {
  Serial.begin(9600); // Initialize serial communication at 9600 baud

  adxl.powerOn(); // Power on the ADXL345 sensor

  adxl.setRangeSetting(2); // Set accelerometer range to ±2g
  adxl.setSpiBit(1); // Set to 3-wire SPI mode
}

void loop() {
  int x, y, z; // Variables to store the XYZ values

  adxl.readAccel(&x, &y, &z); // Read the XYZ values from the accelerometer

  // Output the values to the serial monitor

  Serial.print(x);

  Serial.print(y);

  Serial.println(z);

  delay(100); // Add a short delay for readability
}

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [October 23, 2024, 7:47pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/2 "2024-10-23T19:47:24Z")

</div>

Sorry, don't know the device, but you declared x, y, z as integers; the range is set to +/- 2g; does that mean you get -2, -1, 0, 1, 2 as results, or does it return some number like milli-g?

---

<div class="post-metadata">

**Author:** ![scadriantruthwatcher](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@scadriantruthwatcher](https://forum.arduino.cc/u/scadriantruthwatcher)\
**Post date:** [October 23, 2024, 9:02pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/3 "2024-10-23T21:02:40Z")

</div>

It sets the sensitivity — this is from Sparkfun's demo:

```arduino
adxl.setRangeSetting(2); // Give the range settings
// Accepted values are 2g, 4g, 8g or 16g
// Higher Values = Wider Measurement Range
// Lower Values = Greater Sensitivity

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [October 23, 2024, 9:26pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/4 "2024-10-23T21:26:42Z")

</div>

out of ideas. Good luck!

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [October 23, 2024, 9:30pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/5 "2024-10-23T21:30:21Z")

</div>

Hi, @scadriantruthwatcher  
Welcome to the forum.

Have you Googled;

> esp32-c adxl345

> **[ADXL345 Accelerometer Interfacing with ESP32 | ESP32](https://www.electronicwings.com/esp32/adxl345-accelerometer-interfacing-with-esp32)**
>
> In this guide, we will learn how to Interface the ADXL345 Accelerometer with ESP32 and show the output result on a serial window and Web Server using ESP32 and Arduino IDE.

Thanks.. Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![scadriantruthwatcher](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@scadriantruthwatcher](https://forum.arduino.cc/u/scadriantruthwatcher)\
**Post date:** [October 24, 2024, 3:45pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/6 "2024-10-24T15:45:43Z")

</div>

Hi @TomGeorge, thank you for the welcome 😃  
I have looked at that tutorial, and my wiring matches what it suggests for 3-wire SPI. However, with that sample code I don't see anything on the serial monitor — not even the "There was a problem detecting the ADXL345 ... check your connections".

My first thought was that there was an ESP32 ↔ laptop communication problem, but Arduino's ReadAnalogVoltage.ino example works and sends an accurate voltage to the monitor 😕

---

<div class="post-metadata">

**Author:** ![maikarg](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maikarg/32/1092406_2.png) [@maikarg](https://forum.arduino.cc/u/maikarg)\
**Post date:** [October 24, 2024, 4:27pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/7 "2024-10-24T16:27:25Z")

</div>

Are you trying this example?

> <https://github.com/sparkfun/SparkFun_ADXL345_Arduino_Library/blob/master/examples/SparkFun_ADXL345_Example/SparkFun_ADXL345_Example.ino>

```arduino
  adxl.setActivityXYZ(1, 0, 0); // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)

```

This call seems important.

Also, why SPI? i2c doesnt work?

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [October 24, 2024, 4:30pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/8 "2024-10-24T16:30:23Z")

</div>

> [@scadriantruthwatcher](#):
>
> I need SPI communication for the speed (otherwise I'd use the I2C quick connector)

So...

---

<div class="post-metadata">

**Author:** ![scadriantruthwatcher](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@scadriantruthwatcher](https://forum.arduino.cc/u/scadriantruthwatcher)\
**Post date:** [October 24, 2024, 5:23pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/9 "2024-10-24T17:23:43Z")

</div>

Yes, that is the one I started from. It constantly returns a loop of

```arduino
***ACTIVITY***
 ***DOUBLE TAP***
 ***TAP***
 ***FREE FALL***
 ***INACTIVITY***

```

. When I comment out the code for those detections, and uncomment the code for listing coordinates, I get the "-1, -1, -1" 🤔

As @camsysca mentioned, I need SPI for speed — the ADXL345 can only sample up to 800Hz over I2c, and I need at least 1000Hz. It can do 3,200Hz via SPI.

---

<div class="post-metadata">

**Author:** ![maikarg](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maikarg/32/1092406_2.png) [@maikarg](https://forum.arduino.cc/u/maikarg)\
**Post date:** [October 24, 2024, 5:53pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/10 "2024-10-24T17:53:41Z")

</div>

But you are not getting anything at all.

I think it would be smart to try i2c. Just to check if it works.

Also, you have no pin definition in your code. google how to know which pin is witch for SPI.

```arduino
  #include <SPI.h>
  #include <SD.h>
  #define SD_CS 32 // SD Chip Select (SS)
  #define SD_CLK 33 // SD Clock (SCK)
  #define SD_MOSI 25 // SD Master Out Slave In (MOSI)
  #define SD_MISO 26 // SD Master In Slave Out (MISO)
  SPIClass hspi = SPIClass(HSPI);

// blablabla loop code

  pinMode(SD_CS, OUTPUT); // SD SS.
  hspi.begin(SD_CLK, SD_MISO, SD_MOSI, SD_CS); // Test hspi or vspi.
  SD.begin(SD_CS, hspi, SPEED)

```

I use that for sd card.

---

<div class="post-metadata">

**Author:** ![scadriantruthwatcher](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@scadriantruthwatcher](https://forum.arduino.cc/u/scadriantruthwatcher)\
**Post date:** [October 24, 2024, 8:16pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/11 "2024-10-24T20:16:56Z")

</div>

I tried previously with I2C, and got the same results.

I'm confused — why do I need to define the pins? None of the example codes for this sensor define them, and MOSI and SCK are fixed on the board (GPIO 10 and 8, respectively as I have it wired). Image attached. The CS pin should work with any output, and I specify GPIO 3 in line 4

 ![pin_map-2](https://europe1.discourse-cdn.com/arduino/original/4X/9/f/a/9fa42dd09bf74c653c3dd833c37978bb0895d6f9.png)

---

<div class="post-metadata">

**Author:** ![maikarg](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maikarg/32/1092406_2.png) [@maikarg](https://forum.arduino.cc/u/maikarg)\
**Post date:** [October 24, 2024, 8:54pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/12 "2024-10-24T20:54:31Z")

</div>

In electronics 'assuming' wont provide any solution.

In the example, I believe they use a esp32 wroover, but you are using a c3. They are not totally compatible.

You are sure your connection is ok, you are sure your pin out definition for the spi bus is ok, but your code doesnt work properly. because you assume many things.

I cant see why you are not even trying to define pins (or try other buses/connections), it will save time.

For the C3, something like this might work:

```arduino
#include <SPI.h>

#define SD_CS GPIO_NUM_21 // SD Chip Select (SS)
#define SD_CLK GPIO_NUM_20 // SD Clock (SCK)
#define SD_MOSI GPIO_NUM_10 // SD Master Out Slave In (MOSI)
#define SD_MISO GPIO_NUM_9 // SD Master In Slave Out (MISO)*/

// void setup 
static SPIClass spi(FSPI);
pinMode(SD_CS, OUTPUT); // SD SS.

// call setup_SPI to init SPI bus.
if (setup_SPI(spiValue)) { // spiValue is your SPI speed in Hz.
        spiStatus = true;
        break; // Leave SPI config attemp. Place this into a loop for retries.
      } 

// setup function for the SPI bus
bool setup_SPI(int i) {
  bool success = false;
  Serial.print("Init SD | .");
  for (int attempt = 0; attempt < maxAttempts; attempt++) {
    SD.end();
    SPI.end();
    spi.begin(SD_CLK, SD_MISO, SD_MOSI, SD_CS);
    SdSpiConfig sdConfig(SD_CS, DEDICATED_SPI, SD_SCK_MHZ(i), &spi);
    if (SD.begin(sdConfig)) {
      Serial.println("SPI CLK = " + String(i) + " MHz SUCCESS!!! ");
      success = true;
      break;
    }
    Serial.println("Retry SPI and SD...");
    delay(100);
  }
  if (!success) return false;
  return true;
}

```

You can also try a sketch for others SPI peripherals and check if they work as expected.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2025, 8:54pm UTC](https://forum.arduino.cc/t/trouble-reading-accelerometer/1314551/13 "2025-04-22T20:54:59Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
