Im working in a project based on MPU6050 GY-521 board connected to my PCB (running with a atmega328p). The code was developed in Arduino.
The PCB is the second version. The first one works fine with the same accelerometer.
But with the second version I´m facing problems because doesnt works and I cant understand the cause.
I did the following test:
1) Run the code in a arduino nano board connected to the GY-521 MPU6050 board. Works fine 2) Use other PCB to dismiss possible problem while soldering process. Doesnt woks 3) Probe with other GY-521 MPU6050 board. Doesnt works
Finally I decided to cut some parts of the code trying to understand what part of this crash. My final test put a led to blink while the rest part of the code ran.
With this code the led blink fine, but the commented lines are critical to MPU6050 setup and operation
If someone can help to understand what happen to my PCB, I´ll be grateful
Thanks in advance
Mod edit: code tags
The PCB is the second version. The first one works fine with the same accelerometer.
But with the second version I´m facing problems because doesnt works and I cant understand the cause.
I did the following test:
1) Run the code in a arduino nano board connected to the GY-521 MPU6050 board. Works fine 2) Use other PCB to dismiss possible problem while soldering process. Doesnt woks 3) Probe with other GY-521 MPU6050 board. Doesnt works
Finally I decided to cut some parts of the code trying to understand what part of this crash. My final test put a led to blink while the rest part of the code ran.
C:
void setup(){Serial.begin(115200);Wire.begin();#if ARDUINO >= 157Wire.setClock(400000UL);#else
TWBR =((F_CPU /400000UL)-16)/2;#endif
i2cData[0]=7;// Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
i2cData[1]=0x00;// Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
i2cData[2]=0x00;// Set Gyro Full Scale Range to ±250deg/s
i2cData[3]=0x00;// Set Accelerometer Full Scale Range to ±2gwhile(i2cWrite(0x19, i2cData,4,false));// Write to all four registers at once
//line commented in the last test//while (i2cWrite(0x6B, 0x01, true));
//line commented in the last test//while (i2cRead(0x75, i2cData, 1));if(i2cData[0]!=0x68){// Read "WHO_AM_I" registerSerial.print(F("Error reading sensor"));while(1);}
delay(100);// Wait for sensor to stabilize
//line commented in the last test//while (i2cRead(0x3B, i2cData, 6));
accX =(int16_t)((i2cData[0]<<8)| i2cData[1]);
accY =(int16_t)((i2cData[2]<<8)| i2cData[3]);
accZ =(int16_t)((i2cData[4]<<8)| i2cData[5]);
// It is then converted from radians to degrees#ifdef RESTRICT_PITCH // Eq. 25 and 26double roll = atan2(accY, accZ)* RAD_TO_DEG;double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ))* RAD_TO_DEG;#else// Eq. 28 and 29double roll = atan(accY / sqrt(accX * accX + accZ * accZ))* RAD_TO_DEG;double pitch = atan2(-accX, accZ)* RAD_TO_DEG;#endif
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
}
void loop(){/* Update all the values *///line commented in the last test//while (i2cRead(0x3B, i2cData, 14));
accX =(int16_t)((i2cData[0]<<8)| i2cData[1]);
accY =(int16_t)((i2cData[2]<<8)| i2cData[3]);
accZ =(int16_t)((i2cData[4]<<8)| i2cData[5]);
tempRaw =(int16_t)((i2cData[6]<<8)| i2cData[7]);
gyroX =(int16_t)((i2cData[8]<<8)| i2cData[9]);
gyroY =(int16_t)((i2cData[10]<<8)| i2cData[11]);
gyroZ =(int16_t)((i2cData[12]<<8)| i2cData[13]);;
double dt =(double)(micros()- timer)/1000000;// Calculate delta time
timer = micros();
// It is then converted from radians to degrees#ifdef RESTRICT_PITCH // Eq. 25 and 26double roll = atan2(accY, accZ)* RAD_TO_DEG;double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ))* RAD_TO_DEG;#else// Eq. 28 and 29double roll = atan(accY / sqrt(accX * accX + accZ * accZ))* RAD_TO_DEG;double pitch = atan2(-accX, accZ)* RAD_TO_DEG;#endif
double gyroXrate = gyroX /131.0;// Convert to deg/sdouble gyroYrate = gyroY /131.0;// Convert to deg/sdouble gyroZrate = gyroZ /131.0;// Convert to deg/s
#ifdef RESTRICT_PITCH// This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degreesif((roll <-90&& kalAngleX >90)||(roll >90&& kalAngleX <-90)){
kalmanX.setAngle(roll);
compAngleX = roll;
kalAngleX = roll;
gyroXangle = roll;}else
kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt);// Calculate the angle using a Kalman filter
if(abs(kalAngleX)>90)
gyroYrate =-gyroYrate;// Invert rate, so it fits the restriced accelerometer reading
kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);#else// This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degreesif((pitch <-90&& kalAngleY >90)||(pitch >90&& kalAngleY <-90)){
kalmanY.setAngle(pitch);
compAngleY = pitch;
kalAngleY = pitch;
gyroYangle = pitch;}else
kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);// Calculate the angle using a Kalman filter
if(abs(kalAngleY)>90)
gyroXrate =-gyroXrate;// Invert rate, so it fits the restriced accelerometer reading
kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt);// Calculate the angle using a Kalman filter#endif
gyroXangle += gyroXrate * dt;// Calculate gyro angle without any filter
gyroYangle += gyroYrate * dt;// gyroXangle += kalmanX.getRate() * dt; // Calculate gyro angle using the unbiased rate// gyroYangle += kalmanY.getRate() * dt;
compAngleX =0.93*(compAngleX + gyroXrate * dt)+0.07* roll;// Calculate the angle using a Complimentary filter
compAngleY =0.93*(compAngleY + gyroYrate * dt)+0.07* pitch;
// Reset the gyro angle when it has drifted too muchif(gyroXangle <-180|| gyroXangle >180)
gyroXangle = kalAngleX;if(gyroYangle <-180|| gyroYangle >180)
gyroYangle = kalAngleY;
// leer el valor analogo que entra// filtrarlo// determinar si se enciende el led
sensorValue = analogRead(sensorPin);
filBattery = myFilterBattery.getFilteredValue(sensorValue);if(filBattery <750)// hacer la prueba con este nuevo valor{
digitalWrite(3, HIGH);}else{
digitalWrite(3, LOW);}
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
delay(500);}
With this code the led blink fine, but the commented lines are critical to MPU6050 setup and operation
If someone can help to understand what happen to my PCB, I´ll be grateful
Thanks in advance
Mod edit: code tags
Last edited by a moderator: