Sensitivity of MPU6050 and communication between Arduino and Processing

Hello, I am trying to create a serial communication between Arduino and Processing for the MPU6050 acceleration sensor. When I run the code in Processing and my UI shows up, I see that the MPU6050 is collecting data even though I haven't even moved it yet and continues to collect it to a point where it cannot collect data anymore. I believe the problem is in the sensitivity of the sensor: I have tried to modify it in the mapping function, however I'm not sure this is the way to go about it. Does anybody know how to solve the issue? Below is the code I used:

#include "MPU6050.h"
int sampleRate = 1000; //samples per second
int sampleInterval = 1000000/sampleRate; //Inverse of SampleRate

long timer = micros(); //timer
MPU6050 imu;
int16_t ax, ay, az;//Defining the RGB LED pins

const int redPin   = 5;
const int greenPin = 6;
const int bluePin  = 9;
//int ledOn = 0; //to control the LED.
int dF = 30;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
 // pinMode(LED_BUILTIN,OUTPUT);
     
//Setting the RGB LED pins as outputs
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

//Turn off the RGB LED by default
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);

  if (!imu.begin(AFS_2G, GFS_250DPS)) {
    Serial.println("MPU6050 is online...");
  }
  else {
    Serial.println("Failed to init MPU6050");
    while (true)
      ;
  }
  timer = micros();
}

void setColor(int redPin, int greenPin, int bluePin, int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

void loop()
{
  if (micros() - timer >= sampleInterval) { //Timer: send sensor data in every 10ms
    timer = micros();
    getDataFromProcessing(); //Receive before sending out the signals
    Serial.flush(); //Flush the serial buffer
    if (imu.getAcc3Counts(&ax, &ay, &az)) {
      sendDataToProcessing('A', map(ax, -32768/dF, 32767/dF, 100, 400));
      sendDataToProcessing('B', map(ay, -32768/dF, 32767/dF, 100, 400));
      sendDataToProcessing('C', map(az, -32768/dF, 32767/dF, 100, 400));
      //sendDataToProcessing('D', map(analogRead(A0), 0, 1023, 100, 500));
    }
  }
  analogWrite(redPin,   0);
  analogWrite(greenPin, 0);
  analogWrite(bluePin,  0);
  delay(1000);
}

void sendDataToProcessing(char symbol, int data) {
  Serial.print(symbol);  // symbol prefix of data type
  Serial.println(data);  // the integer data with a carriage return
}

void getDataFromProcessing() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == 'a') { //when an 'a' charactor is received.
      //ledOn = 1;
      //digitalWrite(LED_BUILTIN, ledOn); //turn on the built in LED on Arduino Uno
      setColor(5,6,9,0,255,0);
    }
    if (inChar == 'b') { //when an 'b' charactor is received.
      //ledOn = 0;
      //digitalWrite(LED_BUILTIN, 0); //turn on the built in LED on Arduino Uno
      setColor(5,6,9,255,0,0);
    }
        if (inChar == 'c') { //when an 'b' charactor is received.
      //ledOn = 0;
      //digitalWrite(LED_BUILTIN, 0); //turn on the built in LED on Arduino Uno
      setColor(5,6,9,0,0,255);
    }
  }
}

" I see that the MPU6050 is collecting data even though I haven't even moved it yet"

Why do you think this is an error?

1 Like

Which library are you using? I tried to find that method but failed.

As @zwieblum indicates, and as I see your code, it looks like the only thing driving whether you send chars and data TO Processing is a timer.

  if (micros() - timer >= sampleInterval) { //Timer: send sensor data in every 10ms
    timer = micros();
    getDataFromProcessing(); //Receive before sending out the signals
    Serial.flush(); //Flush the serial buffer
    if (imu.getAcc3Counts(&ax, &ay, &az)) {
      sendDataToProcessing('A', map(ax, -32768/dF, 32767/dF, 100, 400));
      sendDataToProcessing('B', map(ay, -32768/dF, 32767/dF, 100, 400));
      sendDataToProcessing('C', map(az, -32768/dF, 32767/dF, 100, 400));
      //sendDataToProcessing('D', map(analogRead(A0), 0, 1023, 100, 500));
    }
  }

If you want to only send data if something changed like you moved the device, then don't you want to store the last readings and compare them to the current readings as a condition immediately inside your timing conditional?

Couple of questions:
Are you sure Serial.flush(); //Flush the serial buffer is necessary? It never has been when I have communicated between Processing and Arduino.
Question 2: are you sure you want

I think you'll find if (Serial.available > 0){} at the top of void loop() could be one thing to try if things seem stuck in that part of the code.

Also, can you post your Processing code?

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