【发布时间】: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