Making receiving data from arduino faster

I use the arduino to measure voltages and send them over to my computer (sketch is at the end of the post) and save them to a file and process them further with a c# program or a python script using their respective Serial APIs. Unfortunately I can only measure approximately one value per one millisecond which is okay but not perfect. Are there tips to speed up this? I already turned up the the Baud rate to 115200, but beside this I do not know further…

Are there experiences regarding sending binary values instead of strings? I want to ask first because I had that in mind first but had some frustration with due to garbage data in the serial connection and byte order

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


/*
sizeof(int) = 2
sizeof(long) = 4
 */

void loop() {
  //Wait for the computer to send the time to listen in seconds
  if(Serial.available() > 0) {
    int number = Serial.parseInt();
    //Serial.println(anzahl);
    unsigned long endtime = ((unsigned long)number)*1e6;
    unsigned long starttime = micros();
    endzeit = endtime + starttime ;

    unsigned long time= 0;
    signed int value = 0;
    bool notfinished = true;
    while(notfinished ) {
      time = micros();
      value = analogRead(0);
      Serial.print(value);
      Serial.print("\t");
      Serial.print(time);
      Serial.print("\n");
            
      notfinished = time <= endtime;

      if(!notfinished ) {
        Serial.println("end");
      }
    }
  }
}

With 115200 I got about 1000 transmissions per second using an Arduino UNO.
But you can use much higher baud rates than 115200.

I tested your code with 2000000 and got about 1700 transmissions per second.
But this is less than you would expect.

Transmitting characters is always slow.
Omiting the time stamp at 2000000 I got about 4600 transmissions per second.

The Arduinos that use the 32U4 MCU (such as the Micro or Leonardo) always use the full USB speed for communication so they should be significantly faster than an UNO. But you have to use a program on you PC that can work with higher baud rates than Arduino IDE serial monitor.

Going further: you could just send two bytes only for every messurement like:

...
while(notfinished ) {
   time = micros();
   value = analogRead(0);
   byte hbyte = highByte(value);
   byte lbyte = lowByte(value);
   Serial.write(hbyte);
   Serial.write(lbyte);
   notfinished = time <= endtime
   
   if(!notfinished ) {
     Serial.println("end ");
   }
}

Doing so I got about 8300 transmissions per second (Arduino UNO).

OvidiusCicero: Unfortunately I can only measure approximately one value per one millisecond which is okay but not perfect.

Serial.print() is a slow function

If you need faster throughput you may need to send the data in binary format using Serial.write() - but that will make the code on both sides more complex. On the Arduino side you could use a struct to hold the data (structs can hold different types of data) and then use Serial.write() to send the struct as a single message.

I don't know C# but I presume it has the ability to create a similar data structure into which the receiving data can be put and from which the individual items can be extracted. (I use Linux and Python).

...R

    bool notfinished = true;
    while(notfinished ) {
      notfinished = time <= endtime;

      if(!notfinished ) {
        Serial.println("end");
      }

This would be a LOT easier to understand if the variable was called finished, initialized to false. Then, while(!finished) keep reading.

Actually, the whole flag is useless.

time = micros();
while(time <= endtime)
{
   // do something
   time = micros();
}
// The time is up, so you must be finished.

Robin2: Serial.print() is a slow function

Thanks for the tip! As it seems as you say that sending strings makes things slow I will try a binary receiver again (hopefully not runnung in the same problems again).

uxomm: I tested your code with 2000000 and got about 1700 transmissions per second. But this is less than you would expect.

Also thanks for the tip trying higher Baud rates – I did not think of them since I considered them to be standardized. Why can this be done mostly withput problems?

PaulS: Actually, the whole flag is useless.

The flag is a leftover of me making a (maybe not that good) minimal example – the actual break condition depends on some more stuff making it necessary – but thank you for the tip anyway.