UART constraint between ESP32 and Arduino UNO

Hi guys,
For a project I need to communicate ESP32 and arduino UNO over uart.
in this project;

  1. Arduino will continuously send data over UART.

  2. ESP32 will listen to this incoming data from UART2 and transmit it to the computer via UART1.

The picture shows the connection diagram I used for this project.

  • Arduino UNO - ESP32
  • RX-0 --> GPIO17
  • TX-1 --> GPIO16
  • GND --> GND
    (devices used: arduino uno clone, ESP32 dev kit)

code-1 (Arduino UNO)

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.println("Hello Boss");
  delay(1500);
}

code-2 (ESP32)

#define RXp2 16
#define TXp2 17
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial2.begin(9600, SERIAL_8N1, RXp2, TXp2);
}
void loop() {
  Serial.println("Message Received: ");
  Serial.println(Serial2.readString());
}

I made a trial project as in the link. Everything worked fine. But when I want to send data faster than about 1000 ms from arduino, I cannot read this fast incoming data from ESP32. Has anything like this happened before?

link1

...........................................................................
Help us help you.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Post a schematic.

Post an image of your project.

Which Micro Controller are you using?

Is this simulator code?

Please describe the problem better then you just did.

sorry for my mistakes.
This is the first post on the forum.
I will review your suggestions and edit the post as soon as possible.

1 Like

this might be helpful

Serial Input basics.

Are you using level shifters?

have a look at thread communication-between-esp32-cam-and-arduino-uno
it may give you some ideas

I don't think this is the problem. because I tried the same work again with stm32f103c8t6 using esp32 but got the same result.

OK, good luck.

1 Like

what happens if the readString() times out?
call Serial2.available()?

First of all, I'm sorry for my lousy design. I designed this image using powerpoint. because the fritzing app is paid.

I am taking an analog reading using a potentiometer. I'm converting the data here to digital. and I can control the delay by using this digital data in delay in stm32.

The code for stm32f103c8t6 I wrote is briefly as follows (i used stm32CUBEIDE for this)

  HAL_ADC_Start(&hadc1);
  HAL_Delay(10);

 while (1)
  {
	  dataADC=HAL_ADC_GetValue(&hadc1);
	  sprintf(seriData,"%d \r\n",dataADC);

	  HAL_UART_Transmit(&huart2, seriData, sizeof(seriData), 10);

	  HAL_Delay(dataADC);
	  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  }

The code I wrote for esp32 is as follows.(i used arduino IDE)

#include <HardwareSerial.h>

#define RXp2 16
#define TXp2 17

String metin;
int sayi;

HardwareSerial SerialPort(2);

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial2.begin(9600, SERIAL_8N1, RXp2, TXp2);
}
void loop() {
  metin = Serial2.readString();
  digitalWrite(LED_BUILTIN, HIGH);
  if (sayi <= 900) {
    digitalWrite(LED_BUILTIN, LOW);
  }
  Serial.println(metin);
}

As a result, I am reading from COM 4 and COM5 at the same time.

if the delay is greater than 1000 milliseconds; there is no problem. I can read with esp32(COM5)

if the delay is less than 1000 milliseconds;

I don't know how to learn this

you are getting timeouts of 1 second,
e.g. if you call

Serial2.readString()

if nothing is received within a second it will timeout returning empty string
hence you are getting delays of a second in your code
check if there is anything to read, e.g.

if (Serial2.available()) {
     metin = Serial2.readString()
    .... etc
 }
2 Likes

I solved this problem dear friend. thanks for helping. now i will tell you how i solved it. First of all, I should say that I have been trying to solve this problem for about 8-10 days. I tried many purely experimental things to solve this problem. I made a lot of changes in the main libraries of the arduino. I hope I remember unnecessary changes and can undo those changes :smiling_face_with_tear:. now let's get to the solution;

C:\Users..\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5\cores\esp32
we have such a file path. here are the main libraries of arduino. i edited this file "Stream.h"

In a part of this header file, there are codes as seen below.

    Stream():_startMillis(0)
    {
        _timeout = 1000;//here is line 54
    }
    virtual ~Stream() {}

I replaced the 1000 millisecond delay on line 54 with 100 in the header file. and I saved the header file

    Stream():_startMillis(0)
    {
        _timeout = 100;//here is line 54
    }
    virtual ~Stream() {}

this is how i solved this problem. I don't know if there is any other solution.

dear friends thanks for helping again. :gift_heart:

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