Reading MPU6050 from Processing

Hi, I’m quite new to Raspberry pi and I’m trying to read a MPU6050 sensor from a Processing program running on it.

I already have a C++ demo example working and accessing the MPU6050 info (through the I2C port by which it is connected to the PI), but I need to access it from Processing for further elaboration and display. My main difficulty is how to access the MPU.

Please, could anybody help me with that? A working demo program would be most helpful to me.

Thanks and regards,

This reference may or may not be helpful; it contains a Processing sketch. We can help with basic code to connect to serial but there is quite a bit of data being sent from one of these gyros: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/Graph/Graph.pde

Basic Serial Code for Processing:

import processing.serial.*;

Serial myPort;    // The serial port
String inStr;    // Input string from serial port
int lf = 10;    // ASCII linefeed
float x, y, z;

void setup() {
  surface.setVisible(false); // Output is in console at bottom of editor
  // List all the available serial ports:
  printArray(Serial.list());
  // Enter device number for your system
  myPort = new Serial(this, Serial.list()[4], 9600);
  myPort.bufferUntil(lf);
}

void draw() {
}

void serialEvent( Serial myPort) {
  //'\n' (line feed) is delimiter indicating end of packet
  inStr = myPort.readStringUntil('\n');
  //make sure our data isn't empty before continuing
  if (inStr != null) {
    //trim whitespace and formatting characters (like carriage return and/or line feed)
    inStr = trim(inStr);  //Data is sent as a string
    println("inStr = ", inStr);    
    String[] myData = split(inStr, ','); // String is split into array at comma
    //printArray(myData);   
    x = Float.valueOf(myData[0]); // Array elements are converted back to integers
    println("x = ", x);
    y = Float.valueOf(myData[1]);
    println("y = ", y);
    z = Float.valueOf(myData[2]);
    println("z = ", z);    
  }
}

Arduino code for testing purposes:

int randNumber[3];

void setup() {
  Serial.begin(9600);
}

void loop() {
  // print a random number from 0 to 299
  randNumber[0] = random(300);
  Serial.print(randNumber[0]);
  Serial.print(','); // Comma separated values
  // print a random number from 10 to 19
  randNumber[1] = random(10, 20);
  Serial.print(randNumber[1]); 
  Serial.print(',');
  // print a random number from 500 to 899
  randNumber[2] = random(500,900);
  Serial.println(randNumber[2]);// line feed terminated
  delay(50);
}

Thanks for replying!

When I printed the list just appeared 2 ports, which don’t seem to be the I2C port.

This is the code I’m trying to run:

void setup()
{
 size(1000,400);
 smooth();
 printArray(Serial.list());
 port = new Serial(this, Serial.list()[0], 9600);
 print(port.available());
}

with this output

[0] "/dev/serial1"
[1] "/dev/ttyAMA0"
RuntimeException: Error opening serial port /dev/serial1: Port busy
RuntimeException: Error opening serial port /dev/serial1: Port busy
RuntimeException: Error opening serial port /dev/serial1: Port busy

But with the first execution just print a 0 on port.available()

Thanks!

The problem here is that Processing is normally not set up for talking to hardware, unless you use a third party library. So one way is to get the data sent into Processing from your Pi through a serial port. As discussed so far.

Yes that is what you will get. You will never get Processing to drive an I2C bus. What you should do is to use one of those serial ports and send it data from the Pi to Processing using it. So what you read from thee I2C port is sent into Processing. You have to setup these ports at both ends, before you can transfer data.

1 Like