Increase Serial port USB baud rate

Hi all,
I am working on a vibration measurement project for my graduation project on the university, it will run connecting Arduino over USB to MATLAB/LABVIEW so I need higher baud rates over serial port which I can reach for now.

My idea is working with ADXL345 accelerometer at 3200Hz. So I begin testing with a sinewave example I found on internet and I modified it for my purposes (Later I will use ADXL345).
In the attached program it sends sinusoidal wave data points over Serial.println() command with upcoming format on each point: XxvalueYyvalueZzvalue. Example X25Y15Z2.

But the big issue is I can't reach more than 1300Hz with this method-tested on different serial monitors-. I tried to use Serial.begin() on different baudrates, but I can't reach more than 115200 bps (Tested on two Arduino Mega 2560, one Mega 1280, one Nano V3 and other UNO with same result). I made some test and calculations and using Serial.begin() at higher baudrates, my sample rate is the same. Decreasing Serial.begin() baudrate, decreases my sample rate-It's normal-.

For my purposes 2100 Hz would be perfect due I will use FFT, so my frequency spectrum would be 1050Hz. My purpose is obtaining 1000 or more Hz at the end of the process (as ISO 10816 demands). I estimated I need almost 160000bps for this, so I think 230400bps would be ok (still losing some data points from the accelerometer). Perfect would be 256000bps or more which is translated on 500000bps

How could I increase the baudrate communications from Arduino to the PC? Why is it limited? maybe it's needed to replace the bootloader? Any other suggestion?

Thank you in advance

//code modified for improvement from http://forum.arduino.cc/index.php?topic=8563.0


float wav1[3];//0 frequency, 1 unscaled amplitude, 2 is final amplitude
int average;
const int Pin = 9;
float time;
float percentage;
float templitude;
float offset = 2.5; // default value 2.5 volt as operating range voltage is 0~5V
float minOutputScale = 0.0;
float maxOutputScale = 5.0;
const int resolution = 5; //delay
const float pi = 3.14159;


void setup() {
  wav1[0] = 230; //frequency of the sine wave
  wav1[1] = 2.5; // 0 - 2.5 amplitude (Max amplitude + offset) value must not exceed the "maxOutputScale"
  TCCR1B = TCCR1B & 0b11111000 | 1;//set timer 1B (pin 9) to 31250khz
  pinMode(Pin, OUTPUT);
  Serial.begin(230400);
}

void loop() {


  time = micros()% 1000000;
  percentage = time / 1000000;
  templitude = sin(((percentage) * wav1[0]) * 2 * pi);
  wav1[2] = (templitude * wav1[1]) + offset; //shift the origin of sinewave with offset.
  average = mapf(wav1[2],minOutputScale,maxOutputScale,0,255); 

   String XS=String(average,DEC);
 String YS=String(average,DEC);
 String ZS=String(average,DEC);

 
 
 //String pos_general=String(XS+"X"+YS+"Y"+ZS+"Z");
String pos_general=String("X"+XS+"Y"+YS+"Z"+ZS);


  Serial.println(pos_general);
  delayMicroseconds(resolution);

  }
// function to map float number with integer scale - courtesy of other developers.
long mapf(float x, float in_min, float in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

1.) If speed is key, don't use "S"trings in your Arduino code
2.) Please post your MATLAB code

peko86:
Why is it limited?

Windows problem.
Plenty of people on this forum have run Arduino at 250000 and the like - when talking to another Arduino. Further to reply#1, using "S"trings at all with Arduino is a bad idea, and usually fatal.. I can't imagine it being a requirement for Matlab. If it is, you might be better off sending the data to a terminal for subsequent re-export to Matlab. I don't understand your delay command but delay of any sort means sitting around doing nothing which, with all your time urgency, is probably an equally bad idea. You might need to learn up on interrupts and millis.

Power_Broker:
1.) If speed is key, don't use "S"trings in your Arduino code
2.) Please post your MATLAB code

Hi Power_Broker, and Thank you for your reply.
Regarding point one: you mean not sending strings as X10Y20Z5 for example? What do you recommend me for sending X, Y and Z values faster? Each microsecond here is important. So I think baudrate it's the big problem.
Regarding the Code: I have not still any code on MATLAB or LabVIEW. This is my first step: check the viability of Arduino for this task over serial monitor with time stamp, I copy the data to the clipboard and paste into Excel for row counting for a certain number of seconds (the data rate I obtain). If not: I will try with PIC microcontrollers 16F or 18F series or Raspberry Pi. When I can reach that speed rate over monitor, I will make the program on LabVIEW or MATLAB. The only Code I have now it's on the first post.
I think should be some method for reaching that speed.
Thank you!

Nick_Pyner:
Windows problem.
Plenty of people on this forum have run Arduino at 250000 and the like - when talking to another Arduino. Further to reply#1, using "S"trings at all with Arduino is a bad idea, and usually fatal.. I can't imagine it being a requirement for Matlab. If it is, you might be better off sending the data to a terminal for subsequent re-export to Matlab. I don't understand your delay command but delay of any sort means sitting around doing nothing which, with all your time urgency, is probably an equally bad idea. You might need to learn up on interrupts and millis.

Thank you for your reply Nick_Pyner.
You mean with Linux or another OS would run at higher baudrates?
The delay is used for emulating the ADXL345, I would need a very small delay and equally spaced (not millis: I used micros) between data acquisition vía I2C/SPI and the printing over serial.
Any better suggestion for sending three values over serial continously?
Thank you in avance

peko86:
You mean with Linux or another OS would run at higher baudrates?

No, I just mean it's a Windows problem. Clearly, checking another OS would be worthwhile, but I would have doubts about Linux. This is because there may be a hardware limitation. I have never heard of a Linux motherboard, and running Linux on a Windows motherboard may thus be futile.

Some shots in the dark:

  1. Arduino is not the problem. If getting the data out is more important that seeing it in Matlab, which I strongly suspect is the case, then the window might be open a bit wider. You could send the data to your phone via bluetooth, after configuring an HC-05 at 1382400.

  2. Another option may be WiFi to a phone.

  3. There is nothing particularly I/O intensive about what you are doing and it might be a good idea to move to a NodeMCU, which is faster and has a lot more memory than an Arduino. You don't say how long your project must work for, but you might be able to accumulate the data in local memory, and the dump to MatLab at leisure. This approach may indeed be quite appropriate with an Arduino.

The delay is used for emulating the ADXL345, I would need a very small delay and equally spaced (not millis: I used micros) between data acquisition vía I2C/SPI and the printing over serial.

OK, so that's a hardware issue that I know nothing about, and I didn't note the micros, but there still may be better ways of doing this.

Any better suggestion for sending three values over serial continously?

No. I just send floats and commas, but I don't have a speed problem like you. You might be better off sending chars, but this may incur problems at the other end. Further, Arduino does take time to package data, whatever it is.

peko86:
Hi Power_Broker, and Thank you for your reply.
Regarding point one: you mean not sending strings as X10Y20Z5 for example? What do you recommend me for sending X, Y and Z values faster? Each microsecond here is important.

Yes, exactly.

peko86:
So I think baudrate it's the big problem.

Nobody is saying anything contrary. Just giving you more hints to speed up your code.

peko86:
You mean with Linux or another OS would run at higher baudrates?

Baud of over 115200 is definitely possible with Windows, no need to switch OS's.

peko86:
Regarding the Code: I have not still any code on MATLAB or LabVIEW.

Test like you plan to operate and operate like you test. Try a simple MATLAB script to see how fast you can log info from the Arduino. That will give you a much more accurate benchmark on how fast you can communicate and what bauds are possible.

I regularly use my Uno and Mega at 500,000 baud with applications written in Python on this Linux laptop. I would be very surprised if the same thing is not possible on Windows.

...R

Hi Nick_Pyner,Power_Broker, Robin2...first of all: Thank you all for your replies :smiley: ,

Nick_Pyner:

  1. Arduino is not the problem. If getting the data out is more important that seeing it in Matlab, which I strongly suspect is the case, then the window might be open a bit wider. You could send the data to your phone via bluetooth, after configuring an HC-05 at 1382400.

  2. Another option may be WiFi to a phone.

My idea is making a serial connection between Labview/MATLAB application for its connection and manipulation over a computer. Could be a temporal solution for personal use, so my purpose it's for an University project and I prefer using only two tools: Sensor-hardware and the computer with its software.
Do you think HC-05 will run at that baudrates? In that case, HC-05 connected via bluetooth to the laptop could be an option. Or maybe I find same issue.

Nick_Pyner:
3. There is nothing particularly I/O intensive about what you are doing and it might be a good idea to move to a NodeMCU, which is faster and has a lot more memory than an Arduino.

I've never tried NodeMCU, Which one could be fine?

Nick_Pyner:
You don't say how long your project must work for, but you might be able to accumulate the data in local memory, and the dump to MatLab at leisure.

Working for a few seconds (around 5seconds will be fine)

Power_Broker:
Nobody is saying anything contrary. Just giving you more hints to speed up your code.

Yes, and thank you for your recommendations, so with taking out three characters I can't speed up my code to the needed values, and probably using this method, will make some confusion in the receptor program, I think some kind of separator it's needed. I made some calculations, and always is around 115200bps, example of my last test:
(1310 samples/second)(10,6 mean of characters/sample)(8bits/character)=111088 bps

Nick_Pyner:
Baud of over 115200 is definitely possible with Windows, no need to switch OS's.

If you have any idea how to make it: Let me know how, please. It's quite crazy

Nick_Pyner:
Test like you plan to operate and operate like you test. Try a simple MATLAB script to see how fast you can log info from the Arduino. That will give you a much more accurate benchmark on how fast you can communicate and what bauds are possible.

I agree 100% with you, so I have not clear which one (Labview or MATLAB I will use). My mind in this time ruled in this way: If I can reach that baudrate over the console with a certain margin over the value (not below as now. And If I can get 25% more-will be better), I will use this hardware platform, later according that hardware, I will choose the software.
If I cannot reach my objective over the console (Arduino, Putty...), I am sure won't be suitable on any other application made on MATLAB or LABVIEW. And maybe the solution is to save the data on a SD card via SPI and connected to a very precission clock, and later analyzing it on the computer on previously described programs.

Robin2:
I regularly use my Uno and Mega at 500,000 baud with applications written in Python on this Linux laptop. I would be very surprised if the same thing is not possible on Windows.

...R

Really? I am thinking of using Linux.....Did you made a byte counting similar as I previously explained in this same reply?
Previously I was thinking my computer was running fine on that baudrates too (I tried at 2000000 on the NANO V3), I can see values properlly, but when I made a bit count....I found the same number of bits than on 115200bps. This is why I think it's the real limit- Almost in windows

peko86:
Really? I am thinking of using Linux.....Did you made a byte counting similar as I previously explained in this same reply?

I can't see any reference to byte counting in your earlier Replies in this Thread - but maybe I have missed something. If so, perhaps you would quote it again.

I am all in favour of people using Linux but in this case I doubt very much that Windows is at fault.

...R

Robin2:
I can't see any reference to byte counting in your earlier Replies in this Thread - but maybe I have missed something. If so, perhaps you would quote it again.

I am all in favour of people using Linux but in this case I doubt very much that Windows is at fault.

...R

peko86:
I made some calculations, and always is around 115200bps, example of my last test:
(1310 samples/second)(10,6 mean of characters/sample)(8bits/character)=111088 bps

NOTE: The samples/second and characters/sample were taken from experimental values and calculated over Excel sheet.
Maybe I made I mistake, but I think I should make the baud rate calculations as this. Am I right?
Thank you

peko86:
NOTE: The samples/second and characters/sample were taken from experimental values and calculated over Excel sheet.
Maybe I made I mistake, but I think I should make the baud rate calculations as this. Am I right?
Thank you

Your calculation is fine.

How are you collecting the data? And do you have examples of the raw data for a few different baud rates?

My wild guess is that it is your Matlab stuff that is determining the throughput rather than the baud rate. By the way I have no experience of Matlab.

...R

Robin2:
Your calculation is fine.

How are you collecting the data? And do you have examples of the raw data for a few different baud rates?

My wild guess is that it is your Matlab stuff that is determining the throughput rather than the baud rate. By the way I have no experience of Matlab.

...R

I tried on different serial monitors-terminals, such as Putty, realTerm and Arduino Serial Monitor with same result. But I didn't save the results.

Now I made a different test, on my workshop computer instead my Laptop, with an Arduino Mega 2560 at three different baudrates: 115200, 230400 and 1000000.
I upload RAW data for these baudrates on that Mega 2560(with timestamp-from Arduino IDE monitor). I pasted it onto Excel file on three different pages (one for each baudrate) and its respective baudrate calculations. As you can see, the number of samples did not increased a lot as should be expected, and the baudrate it's quite similar.
I don't know how to upload a file here, so I put a Dropbox link:

https://www.dropbox.com/sh/avkbgu7y213lss0/AAA_CMHEpbLSqpW7OVZwjgfPa?dl=0

In all ways, I got better results on my laptop (It has better resources than this computer). And like there, I can't reach more than 115200bps.

Thank you for your support.

OK, it sounds like I'm right about windows then? but if you had asked me "who the hell fed you that rubbish?" I would have said "Probably Robin2", he being the serial comms eminence grise around here. The limitation you find may be down to the hardware. The UART on the motherboard doesn't run any faster than Bill Gates told them was needed, and no amount of software will fix that.

I prefer using only two tools: Sensor-hardware and the computer

Define: hardware.

The HC-05 has an AT config command for 1382400. The HC-06 is "only" good for 115200.

The NodeMCU is an ESP8266 development board. All ESPs are faster and have more memory than an Arduino - even a Mega. It will also go quite some way to soothe any paranoia you might be suffering as a result of excess hardware.

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwjplteuxqbdAhVOdt4KHR4YBAkQFjAAegQIABAB&url=https%3A%2F%2Ftttapa.github.io%2FESP8266%2FChap01%2520-%2520ESP8266.html&usg=AOvVaw23hIXXZyWgotEobOyFW4fI

Another dev board worth looking at is the Heltec ESP32, which has on-board WiFi and Bluetooth, plus an OLED display(!).

I have little experience with this array stuff, and I just trusted to luck, but it can't be hard to research how much memory is needed to store 5 seconds worth of data at a known width and rate, and it seems that you are already capable of that. You may find the Mega you have is just fine, anyway, I raise all this because I bet there is no real value in feeding the data live to Matlab, or anything else.

Perhaps you are not aware that you can set any program in Windows, Linux, etc. to run at a higher priority than the standard mid-range priority. Increase the priority of your PC program and see if that helps.

Paul

peko86:
Now I made a different test, on my workshop computer instead my Laptop, with an Arduino Mega 2560 at three different baudrates: 115200, 230400 and 1000000.
I upload RAW data for these baudrates on that Mega 2560(with timestamp-from Arduino IDE monitor). I pasted it onto Excel file on three different pages (one for each baudrate) and its respective baudrate calculations.

Never mind Excel.

Post the Mega program and describe what you doing on the PC to collect the data and measure the time taken. And just provide the raw data - for 3 different baud rates there will just be 3 numbers (or maybe 9 if you repeat each test 3 times) so there is no need for spreadsheet.

...R

Nick_Pyner:
OK, it sounds like I'm right about windows then? but if you had asked me "who the hell fed you that rubbish?" I would have said "Probably Robin2",

It's an interesting variation on a compliment. What "rubbish" are you referring to?

...R

That Windows doesn't run faster than 115200

Nick_Pyner:
That Windows doesn't run faster than 115200

I certainly did not suggest that.

I have been suggesting all along that I assume Windows can work at high baud rates just like Linux. In Reply #7 I said

I regularly use my Uno and Mega at 500,000 baud with applications written in Python on this Linux laptop. I would be very surprised if the same thing is not possible on Windows.

...R

I didn't say you did, I said probably, and, if you ever did, it would have been years ago. I also note what you have said lately. For all that, I take that the OP's experience is definitive - apparently not rubbish..