Latency of Serial.println() with Arduino UNO

As a part of my current experiment, I’m running following piece of code:

   if (digitalRead(pinDeviceReadReady) == HIGH) {
    Serial.println("DATA READY");
    spiSend(0x20); // command "read request"
    readBuffer.deviceId = spiReceive();
    Serial.println(readBuffer.deviceId);
    readBuffer.wireId = spiReceive();
    Serial.println(readBuffer.wireId);
    readBuffer.length = spiReceive();
    Serial.println(readBuffer.length);
    for (int i = 0; i <= readBuffer.length; ++i) {
       readBuffer.data[i] = spiReceive();
       Serial.println(readBuffer.data[i]);
    }
  }

where

inline void spiSend(uint8_t data)
{
  // wait for device ready to write
  while (digitalRead(pinDeviceWriteReady) == LOW);
  SPI.transfer(data);
}

inline uint8_t spiReceive()
{
  while (digitalRead(pinDeviceReadReady) == LOW);
  uint8_t data = SPI.transfer(0);
  return data;
}

The picture below shows the “pinDeviceReadReady (DATA_READY)” indicator value (red) and SPI clock (blue).  There are significant delays after DATA READY indicator is up until the UNO executes SPI transfer.

data_ready_and_SPI_clock_with_serial

This is due to Serial.println() debug prints inserted between SPI transfers.  I’ve not read the Serial.println() implementation, but the delay looks to be ranging from 100 to 150 micro seconds.

The large delay disappeared when I removed the Serial.println() lines.

data_ready_and_SPI_clock_without_serial

However, you still see delay when you close up a little more.

data_ready_and_SPI_clock_closeup

This is possibly caused by digitalRead() for the indicator or invoking SPI.transfer().  I don’t have to remove this delay for this experiment, but may need to do so later.  The delay is about 3 micro seconds.

2 thoughts on “Latency of Serial.println() with Arduino UNO

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.