How to transfer UART data quickly?

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  analogReadResolution(16);
  SerialUSB.begin(9600);
}


void loop() {
  int i;
  float voltage;
  int sensorValue;
  unsigned long elsp=millis();
  for (i=0;i<10000;i++)
    {
      // read the input on analog pin 0:
      sensorValue = analogRead(A0);
    }  
  Serial.println(millis()-elsp);
  Serial.println("waiting...");
  delay(10000);
}

Code return 1150ms, it means 9,695 samples per second.

    Serial1.printf("%d:%d:%d:%d:%d:%d", analogRead(A0), analogRead(A1), analogRead(A2), analogRead(A3), analogRead(A4), analogRead(A5));

If Serial bytes are transmitted to the ESP32 by UART and then the ESP32 sends the Serial bytes over Bluetooth, the sampling rate drops to 230 times per second...

Who can tell me how to achieve efficient transmission and avoid lowering sampling frequency as much as possible?

Increase Serial baudrate.
Use more efficient formatting or send binary values.

1 Like

Hello, what format do you recommend for data transmission? I don't know much about this. How about hex format?

Raw bytes is the fastest way to transmit data

Hex is just a convenient way to represent values to make them easier for humans to read

1 Like

@DrDiettrich @UKHeliBob
I did not define the data format, I tried to increase it to 1000000 baud rate, but the packet loss was serious. I changed the baud rate configuration for SERIAL_8N2, SERIAL_8E2

  Serial1.begin(1000000,SERIAL_8E2);

No significant improvement, any tips on keeping baud rates high and not losing packets?

You need to post your entire, actual sketch. Not just a demo program that doesn't demonstrate the problem.

It only sends data once every 10 seconds, I doubt very much that there could be any bottleneck in the data transmission.

@aarg Sorry, the sketch:
ARDUINO CODE:

void setup(){
  analogReadResolution(16);
  SerialUSB.begin(9600);
  Serial1.begin(500000, SERIAL_8E2);
  while (!Serial1);
}
void loop(){
  int i;
  unsigned long elsp = millis();
  for (i = 0; i < 1000; i++){
    Serial1.printf("%d:%d:%d:%d:%d:%d", analogRead(A0), analogRead(A1), analogRead(A2), analogRead(A3), analogRead(A4), analogRead(A5));
    Serial1.println();
  }
  Serial1.printf("XIAO Time: %d \n", millis() - elsp);
  SerialUSB.println(millis() - elsp);
  SerialUSB.println("waiting...");
  delay(5000);
}

ESP CODE:

void setup(){
  Serial.begin(115200);
  Serial1.begin(500000, SERIAL_8E2, 16, 17);
  while (!Serial);
  Serial.println("Set Serial1 ok!");
}
String xiao = "";
void loop(){
  unsigned long elsp = millis();
  if (Serial1.available() > 0){
    if (Serial1.peek() != '\n'){
      xiao += (char)Serial1.read();
    }else{
      Serial1.read();
      Serial.println(xiao);
      xiao = "";
    }
  }
}

I intend to implement ESP Bluetooth to send at least 1000 samples per second with a 6-channel sensor,in practice, however, ESP was poor at receiving sensor data

Sure, and can I have a magnifying glass to read it? Please post normally.

Sending binary instead of formatted ASCII data would increase the speed by 3X or more.

1 Like

Also, if you convert a 16 bit integer to a float and transmit the float, that is also inflating the data.

1 Like

The Serial port baud rate on the ESP needs to be at least as fast as the Serial1 baud rate, otherwise you will be overrunning the Serial1 input buffer while waiting for Serial. You appear to be getting a lot of 5-digit numbers from the analog ports, that can potentially be 36 characters for each set of samples. At 1000 samples per second, that is 36,000 characters, and with 8N2 (11 bits/character), 396,000 bits/second. 115200 baud is only capable of 11,520 characters/second at the standard 8N1.

1 Like

Is this just a test sketch?

If not: What is the reason for reading and transmitting 1000 times 6 analogData as quick as a for()-loop() allows and then wait for 5 sec ?

Yes, that's why I asked for the real one. It's a standard requirement here.

1 Like

You should not send faster than the receiver can process. Use handshake for sensitive data.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.