Import Data from Arduino Serial Monitor to MS Excel

Hello,

I am using MPU 6050 to read accelerations. I am getting raw values of accelerations in three directions (x,y,z-axes) and angular velocities through gyro( 3 directions). A total of 6 values i am getting in my serial monitor at a speed of 9600 baud.

I am receiving the data of 100 readings per second. I want the data to be collected for the 20 seconds. It would be messy for me everything to copy paste the data of (201006 = 12000) every time into Excel Sheet.

My present code for this (taken from Jeff Rowberg)

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define LED_PIN 13
bool blinkState = false;
void setup() {
** Wire.begin();**
** Serial.begin(9600);**
** Serial.println(“Initializing I2C devices…”);**
** accelgyro.initialize();**
** Serial.println(“Testing device connections…”);**
** Serial.println(accelgyro.testConnection() ? “MPU6050 connection successful” : “MPU6050 connection failed”);**
** pinMode(LED_PIN, OUTPUT);
_
}_
void loop() {
_
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);_
_
Serial.print(“a/g:\t”);_
_
Serial.print(ax); Serial.print("\t");_
_
Serial.print(ay); Serial.print("\t");_
_
Serial.print(az); Serial.print("\t");_
_
Serial.print(gx); Serial.print("\t");_
_
Serial.print(gy); Serial.print("\t");_
_
Serial.println(gz);_
_
blinkState = !blinkState;_
digitalWrite(LED_PIN, blinkState);
_
}**_

All I want is:

  1. I want the program to be terminated after 20 seconds.
  2. I want the data obtained from serial monitor to come into my excel sheet or matlab. ( avoiding the redundant work of copy paste )
  3. Any other suggestions on this regard

Thanking you,

first thing todo is use 115200 baud, is 12x as fast, so it uses less time communicating.

something like this

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"

MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define LED_PIN 13
bool blinkState = false;

unsigned long start = 0;

void setup() 
{
    Wire.begin();

    Serial.begin(115200);
    Serial.println("Initializing I2C devices...");

    accelgyro.initialize();

    Serial.println("Testing device connections...");

    if (accelgyro.testConnection())
    {
      Serial.println("MPU6050 connection failed");
      while(1); // blocking loop;
    }

    pinMode(LED_PIN, OUTPUT);
    start = millis();
}
void loop()
{
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
    
    if (millis() - start == 20000UL) while(1);  // blocking while loop.
}

No, the code is not working. Obviously It wont work because we are terminating the loop before it gives values of accelerations.

** if (accelgyro.testConnection())**
** {**
** Serial.println(“MPU6050 connection failed”);**
** while(1); // blocking loop;**
** }**

So, i deleted the if loop and now. It works after this is deleted and I am getting the loop stopped. I want to know the meaning of this Millis and the code

if (millis() - start == 20000UL) while(1); // blocking while loop.

Thanks a lot for replying to the post.

Cheers,
Rdr

My mistake, forgot a comparison

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"

MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define LED_PIN 13
bool blinkState = false;

unsigned long start = 0;

void setup() 
{
    Wire.begin();

    Serial.begin(115200);
    Serial.println("Initializing I2C devices...");

    accelgyro.initialize();

    Serial.println("Testing device connections...");

    if (accelgyro.testConnection() == false)  /// updated this line 
    {
      Serial.println("MPU6050 connection failed");
      while(1); // blocking loop;
    }

    pinMode(LED_PIN, OUTPUT);
    start = millis();
}
void loop()
{
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
    
    if (millis() - start == 20000UL) while(1);  // blocking while loop.
}

Give it a 2nd chance :wink:

millis() is an internal clock in milliseconds since the Arduino started. By taking a timestamp (start) and comparint the current millis() against it I know if 20 secs (=200000 milliseconds) have passed.
The UL takes care that the compiler knows that the number is an unsigned long.

Hi sir,

Thanks for your reply. It would be great if you could reply to my message .

The problem still persists. The program is not terminating. :confused:

what you mean by: The problem still persists. The program is not terminating. :confused:

Arduino's cannot terminate, they can go in an endless loop.

 if (accelgyro.testConnection() == false)  /// updated this line 
    {
      Serial.println("MPU6050 connection failed");
      while(1); // blocking loop;
    }

this blocks the program when there is no connection

if (millis() - start == 20000UL) while(1);  // blocking while loop.
}

This blocks when 20 seconds have passed.

But it is not blocking the loop =(

All I want is:

  1. I want the program to be terminated after 20 seconds.
  2. I want the data obtained from serial monitor to come into my excel sheet or matlab. ( avoiding the redundant work of copy paste )
  3. Any other suggestions on this regard

You will not get any output any more after 20 seconds, that was your requirement 1.
You need to restart the sketch if you want more.
And yes it does not exit the loop, it just hangs in an endless while.
while(1); means while(true) do nothing as the ; is an empty statement.

For requirement 2, you should google for GoBetwino, that is a PC application that bridges between Arduino and any PC application. It really works good.

Thanks a lot :slight_smile:

rdrprsdm:
2. I want the data obtained from serial monitor to come into my excel sheet or matlab. ( avoiding the redundant work of copy paste )

I use PLX/DAQ. It is a freebie Excel macro that gives a real-time graph or a direct feed into an Excel .csv file.

Also RealTerm writes comma separated .txt files with local timestamp that can be opened in Excel. I don't know what matlab is but it appears that RealTerm does.