Notifications
Clear all

how to display and adjust the sampling rate in the code a display in the serial monitor

18 Posts
6 Users
1 Likes
1,861 Views
(@abdul)
Member
Joined: 1 year ago
Posts: 14
Topic starter  

From the code of MPU6050 basic reading, can anyone help with editing or suggesting how I can display the sampling rate or show the number of samples per second? I connected the MPU6050 breakout board with ESP32 wroom d1

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup(void) {
  Serial.begin(2000000);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
 /* mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }*/

 /* mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }*/

  //Serial.println("");
  //delay(100);
}

void loop() {

  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  /* Print out the values */
 // Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(",");
  Serial.print(a.acceleration.y);
  Serial.print(",");
  Serial.println(a.acceleration.z);
  //Serial.println(" m/s^2");

  

  //Serial.println("");
  //delay(100);
}

   
Quote
(@abdul)
Member
Joined: 1 year ago
Posts: 14
Topic starter  

I tried to copy and paste the serial monitor data to excel, but still no way.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@harri_son Add code to the main loop to keep count of number of samples, perhaps per loop and total? Also keep track of time for one loop probably using millis() or even micros() again per loop and total. Simple math then print. If wanting to export to excel, remember to add commas so excel can understand the data easily.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
 

@harri_son I wrote my data to the serial monitor in comma delimited format and then copied and pasted into MS Word.  I saved as a .txt file.  To import into Excel I select Get External Data from Text which is under the Data tab and then open the txt file you saved.  Just follow the prompts and select comma (default in tab) when importing into Excel.


   
ReplyQuote
(@abdul)
Member
Joined: 1 year ago
Posts: 14
Topic starter  

Posted by: @zander

@harri_son Add code to the main loop to keep count of number of samples, perhaps per loop and total? Also keep track of time for one loop probably using millis() or even micros() again per loop and total. Simple math then print. If wanting to export to excel, remember to add commas so excel can understand the data easily.

Can you help with the code to count the number of samples per loop and total.

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@harri_son I will be happy to when I fix my own problem. Since it's a xmas thing for my wife and she loves xmas, it is priority 1 2 and 3.

Meanwhile, the math is simple, samples/time, time is trivial and samples is ? I don't see anywhere in your code when I look quickly, you might know better than me.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@harri_son

can anyone help with editing or suggesting how I can display the sampling rate or show the number of samples per second?

https://forum.arduino.cc/t/understand-the-sampling-rate-of-the-mpu-6050/502129


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@harri_son 

Aare you really sure you want to set te baud rate at 2 million ? It may be a valid setting (I don't use ESPs) but it may be too fast for the monitor to keep up.

You only seem to be taking one sample per loop. I can't duplicate your situation because I don't have the equipment nor libraries, but, assuming that you want to report the time per loop and count of loops per second (where loop and sample are equivalent). I'm assuming that you want the sample estimates every second since there doesn't seem to be any "polite" way to end the sketch.

Declare the following at the top of the sketch...

  unsigned long wasMillis = millis(), elapsedMillis = 0;
  float loopCount = 0;

 

And in your loop() ...

  //
  //      Handle sample rates
  //
  elapsedMillis = millis() - wasMillis;       // Calc elapsed time
  loopCount += 1;                             // Count loop passage
  if (elapsedMillis >= 1000) {                // If one second has elapsed
    Serial.print("Average time per sample = ");
    Serial.print( loopCount / elapsedMillis, 5 );
    Serial.print("ms for ");
    Serial.print(loopCount);
    wasMillis = millis();                   // Reset for next group
    loopCount = 0.0;
  }

 

 

I'm sorry but I can't test this and barely hope it compiles on your equipment. Hope it helps.

 

 

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@robotbuilder That code is weird. Maybe old, maybe no library.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@harri_son What other languages are you versed in, and how many years have you been working as a programmer? That way we might be able to couch our answer in a way and language that you will understand.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

Posted by: @zander

@robotbuilder That code is weird. Maybe old, maybe no library.

Ok. It just had the millis() so I posted it probably should not have.

I have the sensor but haven't played with it yet.

https://dronebotworkshop.com/mpu-6050-level/

 

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@harri_son The sample rate is the loop rate, you take one sample per loop. If you want to know the time between samples, take a time at the bottom of the loop and subtract from the prev time. declare prev with an initializer of millis();

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@harri_son int cntLoop = cntLoop + 1;

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@e-danil)
Member
Joined: 3 years ago
Posts: 18
 

I guess the baud rate of the serial monitor is your sample rate.

I am very unsure about this 

This post was modified 1 year ago by E.Danil

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@e-danil Baud rate in the IDE serial monitor has to match the Serial.begin(BaudRate); in your sketch otherwise the data you are sending to the Serial Monitor will be garbled. It has NOTHING to do with your sample rate, it is only to set the speed of communications between the Serial.println(" Hello World "); and your screen.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Page 1 / 2