logo elektroda
logo elektroda
X
logo elektroda

ESP12-E with ADXL345 I2C: Incorrect Axis Values on Lolin NodeMCU V3

mat695 333 2
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18275703
    mat695
    Level 6  
    Welcome,

    Maybe you can find some spuds :) .

    The following program uploaded to anything with an ATMEL works fine and shows correct +/-2 measurements from the accelerometer. When I upload the same to the Lolin wifi esp8266 nodemcu v3 (ESP12-E) the result is completely different. There are no negative values and some axes jump up to a value of 255. where is the difference?

    #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(115200); // 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);
    delay(10);
    }
  • ADVERTISEMENT
  • #2 18278216
    khoam
    Level 42  
    What voltage do you supply to the ADXL345 module when you connect it to the NodeMCU?
  • #3 18279490
    mat695
    Level 6  
    3V but 5V can also be supplied to this circuit.
    Problem solved, had to convert 8bit data to 16to using the following.
    X_out = (int16_t( Wire.read() | Wire.read() << 8)) / 256.0;
ADVERTISEMENT