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.

It will help you get the best out of the forum in the future.

  • Your OS and version can be valuable information, please include it along with extra security you are using.
  • Always list the version of the IDE you are using and the board version if applicable.
  • Use quote or add error messages as an attachment NOT a picture.
  • How to insert an image into your post. ( Thanks @sterretje )
  • Add your sketch where applicable but please use CODE TAGS ( </> )
  • Add a SCHEMATIC were needed even if it is hand drawn
  • Add working links to any specific hardware as needed (NOT links to similar items)
  • Remember that the people trying to help cannot see your problem so give as much information as you can

COMMON ISSUES

  • Ensure you have FULLY inserted the USB cables.
  • Check you have a COMMON GROUND where required. ( Thanks @Perry)
  • Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.
  • Try other computers where possible.
  • Try other USB leads where possible.
  • You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI
  • There may be a problem with the board check or remove your wiring first.
  • Remove any items connected to pins 0 and 1.

COMPUTER RELATED

  • Close any other serial programs before opening the IDE.
  • Ensure you turn off any additional security / antivirus just to test.
  • There may be a problem with the PC try RESTARTING it.
  • You may be selecting the wrong COM port.
  • Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.
  • Clear your browsers CACHE.
  • Close the IDE before using any other serial programs.
  • Preferably install IDE’s as ADMINISTRATOR or your OS equivalent

ARDUINO SPECIFIC BOARDS

  • CH340/341 based clones do not report useful information to the “get board info” button.
  • NANO (Old Types) some require you to use the OLD BOOTLOADER option.
  • NANO (ALL Types) See the specific sections lower in the forum.
  • NANO (NEW Types) Install your board CORE’s.
  • Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.
  • Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

  • On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini
  • On Linux ~/ArduinoCreateAgent-1.1/config.ini
  • On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.
Language problem ?
Try a language closer to your native language:

Thanks to all those who helped and added to this list.

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