weird spikes in mpu 6050 data

Hi,

I am trying to get acceleration data from a mpu 6050 and an esp32 board but I am getting extremely high data spikes at seemingly random intervals.(even when the sensor isn't moving)

is there a way to filter out these spikes?

any help is appreciated

(image attached)

This could be a hardware or software problem, but no circuit and no code to work with....

However if the root cause can't be figured out and eliminated, the median filter is what you
need I think: Median filter - Wikipedia

Thanks for the reply,

I have been able to diagnose the problem and have been able to solve it.
The culprit seems to be a wrongly placed delay and the following piece of code:

display.clearDisplay();
display.setCursor(0, 0);
display.println(“Accelerometer”);
display.setCursor(0, 15);
display.println(“Sending data”);
display.display();

But I do not understand why this causes an issue(I am new at Arduino and coding).

Could you explain this?

(I have marked the issue causing pieces of the code)

/*
Adapted from MOPU6050_DMP6 by Jeff Rowberg for testing purposes. Find the source
 at: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
 */

#include <WiFi.h> 
#include "PubSubClient.h"
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include <Adafruit_SSD1306.h>

MPU6050 mpu;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32);

bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector

// Add your MQTT Broker IP address, for example with Mosquitto Broker:
//const char* mqtt_server = "192.168.0.192";
const char* mqtt_server = "test.mosquitto.org";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

const char *ssid =  "SSID";                                    // replace with your wifi ssid and wpa2 key
const char *pass =  "wpa2 key";

// ================================================================
// ===               INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
  mpuInterrupt = true;
}

void setup() {
  Serial.begin(115200);
  delay(10);
 
  Wire.begin(5,4);
  Wire.write(0x1A);                    // write to address 26 of the register
  Wire.write(0x04);                    // options here are 0x00 which is off, and 0x01, 0x02, 0x03, 0x04, 0x05, 0x06
  Wire.endTransmission(true);    // 0x06 being the highest filter setting
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
  Serial.println(F("SSD1306 allocation failed"));
  for (;;); // Don't proceed, loop forever
  }
  
  display.display();
  delay(500); // Pause for 2 seconds
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setRotation(0);
 
  
  setup_wifi();
  client.setServer(mqtt_server, 1883);


  
  mpu.initialize();
  devStatus = mpu.dmpInitialize();
  
    mpu.setXGyroOffset(20);// offset for my chip
    mpu.setYGyroOffset(-34);// offset for my chip
    mpu.setZGyroOffset(13);// offset for my chip
    mpu.setXAccelOffset(-1833);// offset for my chip
    mpu.setYAccelOffset(1370); // offset for my chip
    mpu.setZAccelOffset(1235); // offset for my chip

  if (devStatus == 0) {
    Serial.println(F("Enabling DMP..."));
    mpu.setDMPEnabled(true);

    Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
    attachInterrupt(0, dmpDataReady, RISING);
    mpuIntStatus = mpu.getIntStatus();

    Serial.println(F("DMP ready! Waiting for first interrupt..."));
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("Waiting for interrupt");
    display.display();
    dmpReady = true;

    packetSize = mpu.dmpGetFIFOPacketSize();
  } 
  else {
    Serial.print(F("DMP Initialization failed (code "));
    Serial.print(devStatus);
    Serial.println(F(")"));
  }
}

void setup_wifi() {
    Serial.println("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) 
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("WIFI connected");
    display.display();
    delay(2000);
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266ClientVindi")) {
      Serial.println("connected");
      display.clearDisplay();
      display.setCursor(0,15);
      display.println("MQTT connected");
      display.display();
      delay(2000);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");// Wait 5 seconds before retrying
      display.clearDisplay();
      display.setCursor(0,15);
      display.println("MQTT connection failed");
      display.display();
      delay(5000);
    }
  }
}



void loop() {
  if (!dmpReady) return;
  while (!mpuInterrupt && fifoCount < packetSize) {
  }

  mpuInterrupt = false;
  mpuIntStatus = mpu.getIntStatus();

  fifoCount = mpu.getFIFOCount();

  if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));

  } 
  else if (mpuIntStatus & 0x02) {
    while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
    mpu.getFIFOBytes(fifoBuffer, packetSize);
    fifoCount -= packetSize;

    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetAccel(&aa, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
    mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
  }

  Serial.print(aaWorld.x); //acceleration rotated into the world reference frame
  Serial.print("\t");
  Serial.print(aaWorld.y);
  Serial.print("\t");
  Serial.println(aaWorld.z);
  delay(25); // This seemed to cause the issue <---------------

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
  long now = millis();
  if (now - lastMsg > 250) {
    lastMsg = now;
     
    client.publish("esp32/XAcc", String(aaWorld.x).c_str(), true);
    client.publish("esp32/YAcc", String(aaWorld.y).c_str(), true);
    client.publish("esp32/ZAcc", String(aaWorld.z).c_str(), true);

    display.clearDisplay(); // This seemed to cause the issue <---------------
    display.setCursor(0, 0);  
    display.println("Accelerometer"); 
    display.setCursor(0, 15); 
    display.println("Sending data"); 
    display.display(); // This seemed to cause the issue <---------------
   
  }
}

Sounds like interference - no circuit to go on though.

A complete guesses: WiFi packet blasting the 6050? 6050 sharing bus with display?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.