Serial Monitor Timestamp Accuracy

Hi All,

First time poster and beginner to Arduino here! I've been using a simple code (below) to monitor a breadboard circuit's voltage drops and I'm moving the readings to Excel from the Serial Monitor.

The Serial Monitor provides a timestamp for the readings, but they seem to be wildly inaccurate. It produces identical timestamps for long strings and then jumps. Even looking at them in Excel, the jumps don't appear to be consistent.

I'm monitoring the circuit at 115,200 baud, but the problem seems to happen at all speeds. Is there any way to increase the accuracy of the timestamps? Alternatively, can you calculate the measurement duration based on the baud data transfer rate?

Many thanks in advance!

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

void loop() {
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage , 4);
}

it jumps if the buffer is full. your code prints faster then it can be send to Serial Monitor. if there is no space in Serial buffer, println blocks. try to add Serial.flush(); after the print. flush() will wait until the buffer is empty. it will consistently slow down the readings to the speed of Serial

You could also send a timestamp from the Arduino:

void loop() {
  unsigned long timestamp = micros();
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);

  Serial.print(timestamp);
  Serial.print(',');
  Serial.println(voltage, 4);
  Serial.flush();
}

Juraj:
it jumps if the buffer is full. your code prints faster then it can be send to Serial Monitor. if there is no space in Serial buffer, println blocks. try to add Serial.flush(); after the print. flush() will wait until the buffer is empty. it will consistently slow down the readings to the speed of Serial

Hi Juraj,

Thank you for your help I added

 Serial.flush();

in a new line after println, but the problem still occurs? I’ve added a screenshot of the Serial Monitor for clarity.

johnwasser:
You could also send a timestamp from the Arduino:

void loop() {

unsigned long timestamp = micros();
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);

Serial.print(timestamp);
  Serial.print(’,’);
  Serial.println(voltage, 4);
  Serial.flush();
}

Hi John, thank you for this! I can see it’s produced a string, I presume that it’s the number of microseconds elapsed since the start of monitoring? If so, this is ideal.

smithro:
Hi John, thank you for this! I can see it's produced a string, I presume that it's the number of microseconds elapsed since the start of monitoring? If so, this is ideal.

I thought you might like that.

I'm seeing the same results in the serial monitor, the timestamp tends to increment in intervals of at least 33mS, but fluctuates a bit higher. The code being delayed because of the send buffer being full would not effect the receive interval in the serial monitor, it would just be getting an uninterrupted stream of data as fast as the baud rate could support.