Sluggish UART (takes 2 seconds to send/recieve data)

Hello there,
I'm using UART between ESP32 and ARDUINO UNO and I'm trying to communicate as fast as possible between them.

Here's the idea:

ESP32 sends a float requesting a value to UNO.
UNO receives this value, and through a switch...case , returns the value asked (in a float) to ESP32.

However, this is taking 2 seconds to complete, which is A LOT OF TIME. With 38400 baud rate, it should be doing this pretty fast. What's happening?

HERE'S THE ESP32 CODE:

#include <HardwareSerial.h>
  
float send_float;
unsigned long myTimer;
float rec_float;

void setup(){
  Serial2.begin(38400,SERIAL_8N1,22,23);
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop(){

   send_float = 3.00;
   Serial2.print(send_float);
   myTimer = millis();
   while(!Serial2.available()){
    if(millis()-myTimer > 3000){
      Serial.println("TIMEOUT REACHED");
      break;
      }
    }
    rec_float = Serial2.readString().toFloat();
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //blink LED to identify new message
    Serial.println(rec_float); //print received float on MONITOR
    
}

HERE'S ARDUINO UNO CODE:


#define RX 5
#define TX 7

float soilH = 79.00;
float soilT = 18.32;
float airT = 19.29;

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



void loop() {


if(Serial.available()){
  
  float recieved = Serial.parseFloat();

  int my_switch = recieved;

  
  switch(my_switch){
    
    case 1:

    Serial.print(soilH);

    break;
    
    case 2:
    Serial.print(soilT);

    break;
    
    case 3:
    Serial.print(airT);

    break;
    
    default: //command not found

    Serial.print(404);
    break;
    }
  }
}

Any clue?

Thank you in advanced!

Probably time to go see ReadString()’s documentation

Description
Serial.readString() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout()).

The default timeout is 1s

I would suggest to study Serial Input Basics to handle this

I second the Serial Input Basics approach. With a little care, your turnaround time will be only a couple of milliseconds longer than it takes to transmit the characters over the serial line.

The Uno has only one serial port, which is used for program upload and serial monitor communications, so please explain how the MCUs are connected.

Also, explain how you are handling the 3.3V to 5V logic level conversion, without destroying one or the other of the two MCUs.

Thank you so much!

After setting up a Serial.setTimeout(5) on both sides, now the code is running blazing fast!

It is expected to have the answers of the following questions of post #3 @jremington:

Are you using Software UART Port of UNO to connect it with ESP32?
Are you using 3.3V <----->5V level shifters between the Rx/TX lines of the two Arduinos?

If the OP is happy, I'm not interested in the answers.

I'm aware of different voltage levels betweens MCUs.
I'm using a voltage divider (2 x 10k) to step-down the TRANSMITTER voltage from Arduino to ESP32 to no more than 2.5 volts. Arduino accepts the 3.3v logic voltage without problems.

1 Like

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