I am using a MPU6050 Accelerometer/Gyro breakout (GY-521) to retrieve data and send it to a java program. I have tried several ways, but I couldn't find a way to send the float values calculated from the Arduino over Serial. Could you please let me know a good way of serial communication with Arduino and a Java Program? Thanks in advance.


Thank you for your comments and replies. What I have tried is the I2Cdev library code. I could work with it without any issue. Code is in this link. I also could run the processing code to get a visual output. But the problem I have i that I'm not able to send the accelerometer and gyro data to a Java/C# program and process it from there. I tried sending an array of FIFO bytes from the Arduino and convert it from the C# code and processing code, but it didn't give me correct results. Now what i did was, just to send a string of values and receive it from the C# program. But I don't know whether it's good method. I would be glad if you could help me in this. Thanks again.


        // display quaternion values in InvenSense Teapot demo format:
        teapotPacket[2] = fifoBuffer[0];
        teapotPacket[3] = fifoBuffer[1];
        teapotPacket[4] = fifoBuffer[4];
        teapotPacket[5] = fifoBuffer[5];
        teapotPacket[6] = fifoBuffer[8];
        teapotPacket[7] = fifoBuffer[9];
        teapotPacket[8] = fifoBuffer[12];
        teapotPacket[9] = fifoBuffer[13];
        Serial.write(teapotPacket, 14); //Serial.write(buf, len)
        teapotPacket[11]++; // packetCount, loops at 0xFF on purpose

    #ifdef OUTPUT_ACCEL_STR_RAW        
        mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

        String accelGyroRaw = "STX,";
        accelGyroRaw += ax;
        accelGyroRaw += ',';
        accelGyroRaw += ay;
        accelGyroRaw += ',';
        accelGyroRaw += az;
        accelGyroRaw += ',';
        accelGyroRaw += gx;
        accelGyroRaw += ',';
        accelGyroRaw += gy;
        accelGyroRaw += ',';
        accelGyroRaw += gz; 
        accelGyroRaw += ',';
        accelGyroRaw += "ETX";



C# Code

class Program
    static SerialPort port = new SerialPort("COM7", 115200);
    //static int serialCount = 0;
    //static int aligned = 0;
    //static char[] teapotPacket = new char[14];
    //static float[] q = new float[4];

    static void Main(string[] args)

        Console.WriteLine("****** Serial Console Read ********");
        port.DataReceived += port_DataReceived;


    static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)

        string s = port.ReadLine();
        string[] accelGyroVals = s.Split(',');
        string[] accl = new string[3];
        string[] gyro = new string[3];

        if (accelGyroVals[0] == "STX" && accelGyroVals[accelGyroVals.Length - 1] == "ETX\r")
            accl[0] = accelGyroVals[1];
            accl[1] = accelGyroVals[2];
            accl[2] = accelGyroVals[3];

            gyro[0] = accelGyroVals[4];
            gyro[1] = accelGyroVals[5];
            gyro[2] = accelGyroVals[6];

            Console.WriteLine("a : " + accl[0] + " , " + accl[1] + " , " + accl[2] + "\t g : " + gyro[0] + " , " + gyro[1] + " , " + gyro[2]);


    static void CurrentDomain_ProcessExit(object sender, EventArgs e)
        if (port.IsOpen)

Sorry for this for lengthy code and if my question is unclear. It's my first time to work with an IMU. Thank you.

  • What have you tried so far? Is your problem related to Arduino side or Java side? Can you post some code you have tried until now?
    – jfpoilpret
    Jul 28, 2014 at 5:02
  • Java and C# are similar, but not the same, so you need to be clear about which you are actually using. In C# you can use == to compare String contents, but in Java that operator will only compare object instances and you must use the .equals() method to compare contents of Strings. Given the non-capitalization of "string" it appears your posted code is actually C#, but the "==" will subtly fail if you try to make trivial changes to build and run it as java. Jul 28, 2014 at 14:23
  • Chirs, Thanks a lot for your comments. You are correct. But what I am actually stuck in is the sending packets from serial to a java program / C# program and use the data packets in some meaningful manner. Anyway thanks for the help Chris.
    – kinath_ru
    Jul 28, 2014 at 14:51
  • 1
    If your question was about a previous attempt to communicate floats in binary, that can indeed be tricky as the binary encoding of floating point values can easily be different between platforms/languages. You'd either need to transmit a standard form supported by both, or for simplicity sake use a textual encoding. The latter might have 3x overhead, but has the huge advantage of making your data easily monitorable with a terminal program during debug. Jul 28, 2014 at 16:19

1 Answer 1


You may find RXTX java library helpful.

You can format the data (float as calculated at Arduino side) in any mannner that you desire. RXTX library will send the data. It will then up to the Java program (non Arduino side, like PC, etc.) how to interpret the data and extract the values.


Hope this helps

  • Thanks a lot for your answer John. I'll give it a try. Thanks a lot.
    – kinath_ru
    Jul 28, 2014 at 14:49
  • The RXTX is very standard. In fact, the Arduino IDE (on PC side) itself is written in Java, and it can communicate to the serial port (COMxx: port via USB on PC side) via the RXTX Java library (to the Arduino MCU hardware board via the USB power/download cable).
    – EEd
    Jul 28, 2014 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.