No stable recieving of Data from two MPU 6050

edited February 2017 in Arduino


i'm interested in displaying the roll and pitch angles in processing. My Arduino code is from

According to that I defined the output of the arduino code and in the serial monitor everything looks fine :). I deleted the line OUTPUT_SERIAL.print("pr:"); so that just a 0 or 1 is send for the identification of the mpu, then TAB Angle1 for current mpu, TAB Angle2 for current mpu:

 OUTPUT_SERIAL.print(mpu); OUTPUT_SERIAL.print("\t");
 OUTPUT_SERIAL.print(ypr[1] * 180 / M_PI);
 OUTPUT_SERIAL.println(ypr[2] * 180 / M_PI);

(Interrupt pins not used as recommended by the way)

The Code for Processing is below . I just want to print the values in the console first, sometimes the real values are printed sometimes just zeros appear. Is the Problem the Character which needs to send to start the DMPs or something else (Transmitting led of the arduino not blinking in the case of zeros)?

import processing.serial.*;
Serial myPort;  // Create object from Serial class
String inString;     // Data received from the serial port
int interval = 0;
int     lf = 10; 

float imu0[] = new float[2];
float imu1[] = new float[2];

void setup()

  size(800, 600);
  String portName = "COM3";
  myPort = new Serial(this, portName, 115200);

void draw()
{ background(25);

 if (millis() - interval > 1000) {
        // resend single character to trigger DMP init/start
        // in case the MPU is halted/reset while applet is running
        interval = millis();}



void serialEvent(Serial p) {
  inString = myPort.readString();

  try {
    // Parse the data
    String[] dataStrings = split(inString, '\t');
    if (dataStrings.length == 3) {
      if (dataStrings[0].equals("0")) {
        for (int i = 0; i < dataStrings.length - 1; i++) {
          imu0[i] = float(dataStrings[i+1]);
      } else if (dataStrings[0].equals("1")) {
        for (int i = 0; i < dataStrings.length - 1; i++) {
          imu1[i] = float(dataStrings[i+1]);
      } else {
  } catch (Exception e) {
    println("Caught Exception");


If you don't know but have a stable example of recieving data from two mpu 6050 that would be also a big help Thank you


  • Answer ✓

    In your first code block in your previous post, you have two lines in a single line (Line 1) which is confusing for people reading your post. Everybody will miss the second part of the line...

    In Line 18, after you send 'r', you should update the interval variable there.

    Related to line 24, you send the 'r' command (I assume it is a broadcast requesting info from all devices) and then you are checking the values right away. One strategy is to send 'r' and then use a boolean value that is set to true when new data arrives at serialEvent(). This boolean then triggers the printout of the data in draw

    (if boolean_new_data_arrived==true){
      boolean_new_data_arrived=false;   //Reset flag to prep for next received value

    Notice that for clarity, you should do instead

    println("IMU0="+ imu0[0] + "\t" + imu0[1]);
    println("IMU1="+ imu1[0] + "\t" + imu1[1]);

    If you implement these changes, you will see that boolean flag will be updated when it receives data from at least one device but it will print both devices. This could be an inconvenience and something to think about but not a deal breaker at this point.

    Lastly in line 44, you are not checking for null cases. Try if(inString!=null){ }.

    Not sure if this will solve your problem.


  • Thank you so far, i will try it the things you mentioned.

    The character needs to be send to start the motion processing in the mpus. One in the beginning and in intervalls after that worked fine for one mpu.

    I also noticed that adding a delay in the processing sketch improves the stability, works now in 80 % im starting the programm

  • edited February 2017

    After updating the interval variable in Line 19 no problems occur until now Thank you for the hint :)

    Feel free to use @all

Sign In or Register to comment.