Is this sampling rate?

Hello all. An Arduino newbie here.
I uploaded a simple Accelerometer sketch to my Nano.

In the pic below, you can see the serial output that was transfered to Excel.
Column A shows the number of elapsed micro seconds since the sketch began.

Column G shows the difference between the timestamps.
G1 = A2 - A1
G2 = A3 - A2
etc etc.

  1. Is it okay to call the G1, G2 ,G3 etc values the 'sampling rate' of my sketch?
  2. If it is not the sampling rate, then is there a specific term for it?
  3. If the G1, G2 ,G3 etc values are indeed the sampling rates, then why are they increasing? Should a microcontroller's sampling rate be steady?
  4. Also as you can see in the 'G' column, the number is going towards 20800 and staying there for a relatively longer time. Why is 20800 there for a relatively longer time than 968 or 972 or 1020 etc?
  5. As you can see, the G column value increases as you go down. But, if you look closely at G3 with a value of 1020, the next value at G4 goes down to 1016. Why did this abrupt thing happen?

Thank you

#include "ADXL335.h"

ADXL335 accelerometer;
unsigned long startMicros; // Variable to store the start time in microseconds


void setup()
{
	Serial.begin(9600);
	accelerometer.begin();
  startMicros = micros(); // Record the start time
}
void loop()
{
  unsigned long elapsedTime = micros() - startMicros; // Calculate elapsed time
	int x,y,z;
	accelerometer.getXYZ(&x,&y,&z);
 Serial.print(elapsedTime);
	Serial.print(" ");
	Serial.print(x);
	Serial.print(" ");
	Serial.print(y);
	Serial.print(" ");
	Serial.println(z);
	
  
}

Problem 1
Transmission way too slow. Try 115200
Problem 2
Sending way too much data as well; at 115200, you can send 11 characters per millisecond, without your outbound buffer filling up
Problem 3
Seven print calls to get your data sent, way too much overhead.
Fix1, then 2, then reassess.

Problem 4 - a rate would be events per period of time.
What you're showing is event time, relative to start of program. If it were constant, you could compute a rate
rate = # events/ period of time.

Or, you could compute a sample interval, the average delta from one sample to the next. Your rate would then be 1/sample interval.

YMMV.

As noted above, the period of "loop" is mostly limited by the rate at which serial data can be sent. Given that the period increases about 20 fold from the first loop to when it is Serial buffer limited, you'll need at least a 20x increase in baud rate to keep pace.

i.e.
Serial.begin(230400) ;
or higher.

More detailed explaination:

Serial.print() formats the data to be output and copies it in a relatively small buffer of RAM and directs the serial hardware to output the first character if the buffer was initially empty. Under the covers, when the serial hardware has completed transmitting a character, it generates an interrupt to a service routine that takes the next character from the serial buffer and copies it to the serial hardware to be transmitted.

If your sketch is putting serial data into the buffer faster than it can be output, Serial.print() will detect that there is not sufficient room in the buffer to copy the output string and will block program execution until sufficient room becomes available. In your example, there appears to be room in the buffer during the first four loops, and then the buffer is (nearly?) full and the loop period becomes much longer.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.