【问题标题】:ADXL345 with ESP32 I2C garbage value issueADXL345 与 ESP32 I2C 垃圾值问题
【发布时间】:2021-06-14 11:44:30
【问题描述】:

根据Tutorial 后面的教程,我已使用 I2C 接口将我的 ESP32 与 ADXL345 连接起来

但是,当我将传感器平放在桌子上运行代码时,我的输出不应该接近X = 0.04, Y = 0.04, Z = 9.81 m/s^2. 但是,这是我得到的输出:

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

此外,当我移动传感器时,X 和 Y 输出不会改变。

这是我正在使用的代码:

#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);
}

更新: 这是进行建议更改后对输出的更新:

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

这是更新后的代码:

#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);
}

当我改变 ADXL345 传感器的方向时,Z 轴的值似乎没有改变。我们如何解决这个问题?

【问题讨论】:

  • 您好 Aryaman,我们需要查看您的代码。请将其发布为Minimal, Reproducible Example
  • 使用的代码是直接从教程网站复制过来的。它是 AdaFruit 库中提供的最基本的传感器测试代码。但是,如果您仍然需要我很乐意提供的代码。
  • 通常的要求是直接在本站发布您的代码,因为外部资源可能会消失;或者您可能已经更改了原始示例。
  • 我已经添加了正在使用的代码。

标签: arduino accelerometer i2c esp32


【解决方案1】:

首先,您的代码和输出不匹配。其次,在您的代码中,您正在从寄存器中读取一个 2 字节有符号整数值并将其分配给浮点类型的变量。这将导致垃圾。使用正确类型的变量(在本例中为 int16_t)。加速度计的data sheet 指定数据格式(“Register 0x32 to Register 0x37—DATAX0, DATAX1, DATAY0, DATAY1, DATAZ0, DATAZ1 (Read Only)”部分)

【讨论】:

  • 是的,我已经在输出中进行了更改。我已经对您建议的变量类型进行了更改。并对我收到的输出进行了更新。但是,Z 轴的值似乎没有显着变化。这可能是什么原因?
  • 好的。在寻找变化时如何移动传感器?例如。把它翻过来是一个很好的测试,因为 X 和 Y 轴应该保持原样,Z 应该变成负数,对吧?
  • 是的,我们将它翻转到负 Z 方向。即使那样,价值观似乎也没有改变。 X 和 Y 的值不应该是 0 和 Z 在 +1 和 -1 之间变化吗?
  • 我们真的被困在这个问题上,非常感谢@Tarmo 的帮助。
  • 很遗憾,我无能为力。仔细阅读数据表和您的代码,确保您所做的每一个假设都成立(例如,您选择了备用 I2C 地址0x53 - 您确定吗)。
猜你喜欢
  • 2021-09-06
  • 1970-01-01
  • 2011-06-25
  • 2014-10-09
  • 1970-01-01
  • 2011-02-23
  • 2011-06-03
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多