ADXL345 与 ESP32 I2C 垃圾值问题 [英] ADXL345 with ESP32 I2C garbage value issue

查看:25
本文介绍了ADXL345 与 ESP32 I2C 垃圾值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据 教程

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

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

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

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

这是更新的代码:

#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 轴的值似乎没有改变.我们如何解决这个问题?

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?

推荐答案

首先,您的代码和输出不匹配.其次,在您的代码中,您正在从寄存器中读取一个 2 字节有符号整数值并将其分配给一个 float 类型的变量.这将导致垃圾.使用正确类型的变量(本例中为 int16_t).加速度计的数据表指定了数据格式(部分寄存器 0x​​32 到寄存器 0x​​37—DATAX0、DATAX1、DATAY0、DATAY1、DATAZ0、DATAZ1(只读)")

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)")

这篇关于ADXL345 与 ESP32 I2C 垃圾值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆