What’s the fastest way…

…to continuously send sensor data over serial? I need to send analog pin readings as a string like this:
“0 analogRead(0) 1 analogRead(1)” etc. Right now, I can’t get the loop to execute faster than every 7 ms, and sometimes there’s a hiccup in the data flow, which makes my application unreliable. I’ve tried increasing the baud rate to 230400, but that doesn’t speed up the routine.

I would really love if I could bring the loop routine down to about 1 ms, so that the jitter would stay under 5 ms. I need the data from all 8 analog pins.

I’m currently using a char buffer and sprintf. And I’m a newbie in programming, so please excuse the clumsy wording.

Can you please post your code; and just in case, don't forget to use code tags for that :wink:

Which board are you using?

What is the receiving end? A PC? Running which OS? Which application?

Two points to be aware off

  1. There is a serial software buffer with 64 bytes; if you print faster than the buffer can be emptied, your code will slow down.
  2. Hiccups can be caused at the receiving end.

Not enough details to give specific advice. My go to answer would be don't send ASCII. Pack the data in a struct and send / receive it as binary. But, need a lot more information in order to flesh that out.

At 115200 you can only transmit 11 characters in 1 ms. How many characters you are transmitting per analog? Multiply this by 8. Each character requires 10 bits to transmit.

Related to Speed up Firmata.

Please explain why you think you need to send the data so fast, it seems unlikley to me.

Which Arduino are you using? sprintf() is not quick to execute.

If you really need the speed you have to slim down the data you sent, sending what you are suggesting has a lot of wasted characters in it, I doubt you really need to send 'analogRead' with every message, if it's always the same there is no point in sending it.

Thanks for the replies. Due to my inexperience, I wasn’t able to convey my goal in a clear way. I actually need to transmit the numbers only. An example of the data flow would be:

0 15 1 230 2 188 3 18 4 66 5 655 6 54 7 2

So, in theory, I would need to send eight 10-bit values (the analog sensors are 10-bit, aren’t they?) and a control bit/byte/whatever. I don’t really know how to go about it, sorry… I’m a musician trying to implement a responsive instrument. I’m using Max/MSP at the other side of the serial communication. I thought I would be able to figure out a way that’s faster than Firmata…

Other post/duplicate LOCKED !
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

So, here's the situation:

I'm communicating between an Arduino Pro Mini 328 and Max/MSP on a Windows 10 PC over USB. My code is:

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

void loop()
{
  char buf[48];
  sprintf(buf, "0 %d 1 %d 2 %d 3 %d 4 %d 5 %d 6 %d 7 %d", analogRead(0), analogRead(1), analogRead(2), analogRead(3), analogRead(4), analogRead(5), analogRead(6), analogRead(7));
  Serial.println(buf);
  delay(7);
}

How to only send a series of 8 10-bit values preceded by a control bit? And how to time it correctly?

Your example is 41 characters which is 410 bits. At 115200 bps that will take about 3.6ms. The way you are formatting it 1 analog could require up to 8 characters, which would require a message up to 64 characters or 640 bits. At 115200 bps that will take about 5.5ms.

Actually the analog values are 10 bits. 1023 requires 10 bits.

I'm not familiar with Max/MSP but if you can control how Max/MSP interprets the the message you could possibly transmit it in binary, making it more efficient. However, you would need to "frame" the message so it would be synced up. Otherwise the receiving software might try to start interpreting in the middle of a message.

This will send all 6 values in 12 bytes. The unused bits in the first byte are set to 1's to indicate the first value.

void loop()
{
  int buf[8];
  for (int i=0; i<8; i++)
  {
    buf[i] = analogRead(i);
  }
  
  buf[0] |= 0xFC00; // Set bits 11-15.
  
  Serial.write((char *)buf, sizeof buf);
}

It would be helpful for you to tell us what "Max/MSP" is.

Thanks, this is great! I'll try it!

I think it would be 8 values in 16 bytes...but same concept. If OP wanted to get real fancy OP could pack 8 values in 6 bytes and prefix it with a leading word of 0xFFFF.

Max/MSP is a very versatile visual object-based programming environment for interactive audiovisual applications.

Oops. My mistake. Fixed it to do all 8 analog input pins of an Arduino Nano.

1 Like