Does serial plotter slow down the code ?
compared to the speed of a processor any output to the Serial Monitor or other similar serial devices is slow.
Increasing the serial baud rate helps (assuming the plotter can accept data and display it at that baudrate) or switching to a faster communication technology, e.g. send data over WiFi or Ethernet to be plotted
Example
I am doing some testing using serial monitor/ plotter and LCD,
should I comment serial monitor/ plotter when testing is done and use only LCD ?
try it - see if it speeds up the system
keep in mind that LCDs are slow compared to the processor and could well be slower than the serial IO
overall it depends where you data is comming from (e.g. sesnors which take milliseconds or more to acquire), what processing is being carried out on the data, etc etc
a multithreaded system could have data being aquired in one thead while being displayed in another
you can measure how much it slows down by
assigning actual value of function milli() at the beginning and at the end
and then calculate the difference.
And then you run this code with serial printing and without serial printing and then you will see the how much time is needed for the serial printing.
The standard-baudrate of older code
Serial.begin(9600);
is indeed slow.
change it to
Serial.begin(115200);
and you can do tests if you go even above 115200 what your microcontroller and computer can handle. Often you can go up to
Serial.begin(1000000);
make sure that you have adapt the baudrate in the serial monitor / serialplotter to the same value.Otherwise you will only nothing or strange characters
If you take a sketch such as the example sketch "AnalogReadSerial":
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
Because there is a delay(1) in loop, it will take a minimum of 500ms to plot the 500 points .
Here I am putting in a 10Hz (period 100ms) signal from a function generator.
The time taken to do the analogRead() is about 3.5µs so can be considered negligible compared with the 1ms delay.
This means that you would expect to see about 5 complete cycles of the signal if the loop wasn't been slowed down by the printing. But in fact you see almost 25 full cycles (=2.5s):
If you change the baud rate from 9600Bd to 115200Bd then you get a lot closer to the 5 cycles minimum:
.
possibly, but it depends on how fast you want your code to run and if serial transmission becomes the bottleneck.
it takes ~1msec to transmit 1 char at 9600 bits-per-second. of course increasing the serial bit-rate will reduce this time
Serial will buffer some # of bytes (64??) without delaying the code. but if buffer is filled, an attempt to print() won't return until the buffer empties enough to put the new print data into the buffer.
this is the same for just sending text to the serial monitor
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.