0
votes

I have connected my ESP32 with ADXL345 using I2C interface according to the tutorial followed from Tutorial

However, when I run the code with the sensor flat on the table, shouldn't I be getting an output close to X = 0.04, Y = 0.04, Z = 9.81 m/s^2. However, this is the output I am getting:

22:26:29.569 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.604 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.639 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.672 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.705 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.739 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.772 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.806 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.873 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.908 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.942 -> Xa= 254.00   Ya= 254.00   Za= 2.00
22:26:29.975 -> Xa= 254.00   Ya= 254.00   Za= 2.00

Moreover, the X and Y output do not change when I move the sensor.

Here is the code I am using :

#include <Wire.h>  // Wire library - used for I2C communication

int ADXL345 = 0x53; // The ADXL345 sensor I2C address

float X_out, Y_out, Z_out;  // Outputs

void setup() {
  Serial.begin(9600); // Initiate serial communication for printing the 
                         results on the Serial monitor
  Wire.begin(); // Initiate the Wire library
  // Set ADXL345 in measuring mode
  Wire.beginTransmission(ADXL345); // Start communicating with the device 
  Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
  // Enable measurement
  Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable 
  Wire.endTransmission();
  delay(10);
}

void loop() {
  // === Read acceleromter data === //
  Wire.beginTransmission(ADXL345);
  Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
  X_out = X_out/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
  Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
  Y_out = Y_out/256;
  Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
  Z_out = Z_out/256;

  Serial.print("Xa= ");
  Serial.print(X_out);
  Serial.print("   Ya= ");
  Serial.print(Y_out);
  Serial.print("   Za= ");
  Serial.println(Z_out);
}

UPDATES: Here is an Update to the output after making the suggested changes:

22:14:43.271 -> Xa= -2   Ya= -2   Za= 1
22:14:43.271 -> Xa= -2   Ya= -2   Za= 1
22:14:43.304 -> Xa= -2   Ya= -2   Za= 1
22:14:43.337 -> Xa= -2   Ya= -2   Za= 1
22:14:43.370 -> Xa= -2   Ya= -2   Za= 1
22:14:43.403 -> Xa= -2   Ya= -2   Za= 1
22:14:43.403 -> Xa= -2   Ya= -2   Za= 1
22:14:43.437 -> Xa= -2   Ya= -2   Za= 1
22:14:43.471 -> Xa= -2   Ya= -2   Za= 1

Here is the updated code:

#include <Wire.h>  // Wire library - used for I2C communication

int ADXL345 = 0x53; // The ADXL345 sensor I2C address

int16_t X_out, Y_out, Z_out;  // Outputs

void setup() {
  Serial.begin(9600); // Initiate serial communication for printing the 
                       //  results on the Serial monitor
  Wire.begin(); // Initiate the Wire library
  // Set ADXL345 in measuring mode
  Wire.beginTransmission(ADXL345); // Start communicating with the device 
  Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
  // Enable measurement
  Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable 
  Wire.endTransmission();
  delay(10);
}

void loop() {
  // === Read acceleromter data === //
  Wire.beginTransmission(ADXL345);
  Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom((uint16_t)ADXL345,(uint8_t) 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
  X_out = X_out/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
  Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
  Y_out = Y_out/256;
  Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
  Z_out = Z_out/256;

  Serial.print("Xa= ");
  Serial.print(X_out);
  Serial.print("   Ya= ");
  Serial.print(Y_out);
  Serial.print("   Za= ");
  Serial.println(Z_out);
}

The values of the Z axis does not seem to be changing when I change the orientation of the ADXL345 sensor. How could we solve this prolem?

1
Hi Aryaman, we need to see your code. Please post it as a Minimal, Reproducible Example.Tarmo
The code in use was directly copied from the tutorial website. It is the most basic sensortest code available from AdaFruit library. However if you still require the code I would be happy to provide.Aryaman Patel
The usual requirement is to post your code on this site directly, as external resources can disappear; or you might have changed the original sample.Tarmo
I have added the code in use.Aryaman Patel

1 Answers

0
votes

Firstly, your code and output don't match. Secondly, in your code you're reading a 2-byte signed integer value from a register and assigning it to a variable of type float. This will result in garbage. Use the correct type of variable (int16_t in this case). The data sheet for the accelerometer specifies the data format (section "Register 0x32 to Register 0x37—DATAX0, DATAX1, DATAY0, DATAY1, DATAZ0, DATAZ1 (Read Only)")