I Tried MPU6050 Tutorial at this link “https://randomnerdtutorials.com/esp32-mpu-6050-accelerometer-gyroscope-arduino/”
Circuit Diagram is correct. I tried the sensor with other sketches and it is working al right. But for this sketch the Sensor is not detected.
“Adafruit MPU6050 test!
Failed to find MPU6050 chip”
Hi.
What other sketches did you try that were working?
Are you sure you have the correct sensor module?
Regards,
Sara
Dear Madam,
Thank you very much for your prompt response. I have used the correct Sensor and the schematic exactly shown in your tutorial. But your sketch is not working.
The same sensor is working properly with following two sketches.
”
Working MPU6050 Code
Sketch 1-
#include <Wire.h>
#include <WiFi.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
// WiFi network info.
const char *ssid = “Airtel-Hotspot-F044”; // Enter your WiFi Name
const char *pass = “3j0rh70f”; // Enter your WiFi Password
WiFiServer server(80);
void setup(){
Serial.begin(115200);
Wire.begin();
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(“Wrote to IMU”);
Serial.println(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”); // print … till not connected
}
Serial.println(“”);
Serial.println(“WiFi connected”);
Serial.println(“IP address is : “);
Serial.println(WiFi.localIP());
server.begin();
Serial.println(“Server started”);
}
void loop(){
mpu_read();
WiFiClient client = server.available();
if (client)
{
Serial.println(“new client”);
String currentLine = “”; //Storing the incoming data in the string
while (client.connected())
{
if (client.available()) //if there is some client data available
{
char c = client.read(); // read a byte
if (c == ‘\n’) // check for newline character,
{
if (currentLine.length() == 0) //if line is blank it means its the end of the client HTTP request
{
client.print(“<html><title> ESP32 WebServer</title></html>”);
client.print(“<body bgcolor=\”#E6E6FA\”><h1 style=\”text-align: center; color: blue\”> ESP32 WebServer </h1>”);
client.print(“<p style=\”text-align: left; color: red; font-size:150% \”>Accelerometer Values: “);
client.print(“<p style=\”text-align: left; font-size:150% \”>AcX: “);
client.print(AcX);
client.print(“<br/>AcY: “);
client.print(AcY);
client.print(“<br/>AcZ: “);
client.print(AcZ);
client.print(“<p style=\”text-align: left; color: red; font-size:150% \”>Gyroscope Values: “);
client.print(“<p style=\”text-align: left; font-size:150% \”>GyX: “);
client.print(GyX);
client.print(“<br/>GyY: “);
client.print(GyY);
client.print(“<br/>GyZ: “);
client.print(GyZ);
client.print(“</p></body>”);
break; // break out of the while loop:
}
else
{ // if you got a newline, then clear currentLine:
currentLine = “”;
}
}
else if (c != ‘\r’)
{ // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
}
}
void mpu_read(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 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)
//Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print(“Accelerometer Values: \n”);
Serial.print(“AcX: “); Serial.print(AcX); Serial.print(“\nAcY: “); Serial.print(AcY); Serial.print(“\nAcZ: “); Serial.print(AcZ);
//Serial.print(“\nTemperature: ” ); Serial.print(Tmp);
Serial.print(“\nGyroscope Values: \n”);
Serial.print(“GyX: “); Serial.print(GyX); Serial.print(“\nGyY: “); Serial.print(GyY); Serial.print(“\nGyZ: “); Serial.print(GyZ);
Serial.print(“\n”);
delay(3000);
}
=====================================================
Sketch 2 –
#include <Wire.h>
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
void setup() {
Serial.begin(9600);
Wire.begin();
setupMPU();
}
void loop() {
recordAccelRegisters();
recordGyroRegisters();
printData();
delay(100);
}
void setupMPU(){
Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
Wire.write(0x6B); //Accessing the register 6B – Power Management (Sec. 4.28)
Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1B); //Accessing the register 1B – Gyroscope Configuration (Sec. 4.4)
Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1C); //Accessing the register 1C – Acccelerometer Configuration (Sec. 4.5)
Wire.write(0b00000000); //Setting the accel to +/- 2g
Wire.endTransmission();
}
void recordAccelRegisters() {
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x3B); //Starting register for Accel Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B – 40)
while(Wire.available() < 6);
accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processAccelData();
}
void processAccelData(){
gForceX = accelX / 16384.0;
gForceY = accelY / 16384.0;
gForceZ = accelZ / 16384.0;
}
void recordGyroRegisters() {
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x43); //Starting register for Gyro Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 – 48)
while(Wire.available() < 6);
gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processGyroData();
}
void processGyroData() {
rotX = gyroX / 131.0;
rotY = gyroY / 131.0;
rotZ = gyroZ / 131.0;
}
void printData() {
Serial.print(“Gyro (deg)”);
Serial.print(” X=”);
Serial.print(rotX);
Serial.print(” Y=”);
Serial.print(rotY);
Serial.print(” Z=”);
Serial.print(rotZ);
Serial.print(” Accel (g)”);
Serial.print(” X=”);
Serial.print(gForceX);
Serial.print(” Y=”);
Serial.print(gForceY);
Serial.print(” Z=”);
Serial.println(gForceZ);
delay(2000);
}
Hi.
Try passing your sensor I2C address in our codes, in the following line:
if (!mpu.begin()) {
So, in your case, it would be like this:
if (!mpu.begin(0x68)) {
Let me know if this solves the problem.
Regards,
Sara
That’s weird.
I don’t know where the problem is…
How are you connecting the sensor to your board?
Regards,
Sara
I Tried the following code (Basic from Examples with some modifications). It worked but it is giving the same readings for any position of MPU6050 as shown below –
”
Acceleration X: -14.10, Y: -13.41, Z: 16.55 m/s^2
Rotation X: -1.30, Y: -3.27, Z: 0.00 rad/s
Temperature: 36.62 degC
Acceleration X: -14.10, Y: -13.41, Z: 16.55 m/s^2
Rotation X: -1.30, Y: -3.27, Z: 0.00 rad/s
Temperature: 36.62 degC
Acceleration X: -14.10, Y: -13.41, Z: 16.55 m/s^2
Rotation X: -1.30, Y: -3.27, Z: 0.00 rad/s
Temperature: 36.62 degC”