1

How much time takes to print each sample using Serial.print and analogRead in each iteration?

#include <SoftwareSerial.h>
#define Sensor A0
void setup() {
  Serial.begin(250000);
  pinMode(Sensor,INPUT);
    }
void loop() { 
  Serial.println(analogRead(Sensor));// RUN TIME??
}
  • It depends on how full the buffer is and how big the value being printed is. – Majenko Feb 19 '17 at 14:19
  • @Majenko The sensor output is between 0 and 100 in serial printing. – Mehran Feb 19 '17 at 14:32
1

It is quite complex to work out how "long" it will take to print. Why?

  1. If there is room in the serial buffer then the text just gets placed in that buffer. Takes almost no time at all to print.
  2. You are printing a variable length string. The longer the string the longer it will take to print if the buffer is full.

However:

  • Each character consists of 10 symbols (8 data bits plus 1 start and 1 stop bit).
  • Characters are transmitted at 250000 symbols per second
  • Thus each character takes 1/(250000 / 10), or 40 microseconds to transfer through the actual wire.

So printing the number 100, which also has \r\n appended to it (characters 13 and 10) would need 5 characters. If the buffer is full it will take about 5*0.00004s = 200µs. If the buffer isn't full, it will take a fraction of that to just copy the data into the serial buffer.

0

1000 rounds of analogRead() takes about 100ms.

sustained serial writes will depend mostly on the baud rate picked.

  • Thanks, do you have a document indicating CPU cycles for different operations? – Mehran Feb 19 '17 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.