MPU6050 accelerometer without serial output
Posted: Tue Apr 26, 2022 8:10 pm
Hi,
I'm doing a test on ESP32 with MPU6050 accelerometer for brake light (LED) but i could not get accel reading from output. The sketch below could
compile and the setup part is ok with output ( Setup complete ). It then just hang there. I've check for loose connection which is ok (see attached pic). Since the vcc, scl and sda pin are all in 3.3v , i didn't use any level shifter. i've added 3k ohms pullup resistor each to scl and sda line.
Any help to solve this problem is much appreciated. Thanks.
==================================================================================================
#include <Wire.h>
const int MPU_ADDR = 0x68; // equals to 0b1101000
int AcX, AcY, AcZ;
float gForceX, gForceY, gForceZ;
const float g = 16384.0;
void setup() {
Serial.begin(115200);
Wire.begin((int)21, (int)22, (uint32_t)100000);
Wire.beginTransmission( MPU_ADDR );
Wire.write( 0x6B ); // PWR_MGMT_1 register
Wire.write( 0 ); // set to zero (wakes up the MPU−6050)
Wire.endTransmission( true );
Serial.println( "Setup complete" );
}
void loop() {
Wire.beginTransmission( MPU_ADDR );
Wire.write( 0x3B ); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission( true ); // true to Wire.endTransmission() >>> ESP32 send stop command and release I2C lines
Wire.beginTransmission( MPU_ADDR );
Wire.requestFrom( (int)MPU_ADDR , (int)6 , (int)true ); // request a total of 6 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
gForceX = AcX / g;
gForceY = AcY / g;
gForceZ = AcZ / g;
Serial.print( gForceX ); Serial.print( " , " );
Serial.print( gForceY ); Serial.print( " , " );
Serial.print( gForceZ ); Serial.println( " , " );
}
I'm doing a test on ESP32 with MPU6050 accelerometer for brake light (LED) but i could not get accel reading from output. The sketch below could
compile and the setup part is ok with output ( Setup complete ). It then just hang there. I've check for loose connection which is ok (see attached pic). Since the vcc, scl and sda pin are all in 3.3v , i didn't use any level shifter. i've added 3k ohms pullup resistor each to scl and sda line.
Any help to solve this problem is much appreciated. Thanks.
==================================================================================================
#include <Wire.h>
const int MPU_ADDR = 0x68; // equals to 0b1101000
int AcX, AcY, AcZ;
float gForceX, gForceY, gForceZ;
const float g = 16384.0;
void setup() {
Serial.begin(115200);
Wire.begin((int)21, (int)22, (uint32_t)100000);
Wire.beginTransmission( MPU_ADDR );
Wire.write( 0x6B ); // PWR_MGMT_1 register
Wire.write( 0 ); // set to zero (wakes up the MPU−6050)
Wire.endTransmission( true );
Serial.println( "Setup complete" );
}
void loop() {
Wire.beginTransmission( MPU_ADDR );
Wire.write( 0x3B ); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission( true ); // true to Wire.endTransmission() >>> ESP32 send stop command and release I2C lines
Wire.beginTransmission( MPU_ADDR );
Wire.requestFrom( (int)MPU_ADDR , (int)6 , (int)true ); // request a total of 6 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
gForceX = AcX / g;
gForceY = AcY / g;
gForceZ = AcZ / g;
Serial.print( gForceX ); Serial.print( " , " );
Serial.print( gForceY ); Serial.print( " , " );
Serial.print( gForceZ ); Serial.println( " , " );
}